Module: Cukunity::Android::Process

Extended by:
Utils, Utils
Defined in:
lib/cukunity/drivers/android/process.rb

Class Method Summary collapse

Methods included from Utils

adb, launchable_activity_name, monkey, package_name, shell

Methods included from Utils

check_timeout, merge_options, restrict_options, to_options, wait_connectivity

Class Method Details

.foregroundObject



41
42
43
44
45
# File 'lib/cukunity/drivers/android/process.rb', line 41

def self.foreground
  shell('dumpsys activity').match(/TaskRecord\{[^\n\}]+\s+([^\s\}]+)}/) do |m|
    return m[1]
  end
end

.foreground?(path) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/cukunity/drivers/android/process.rb', line 47

def self.foreground?(path)
  fg = foreground
  if fg
    fg.downcase  == package_name(path).downcase
  else
    false
  end
end

.launch(path, extras = {}, max_time = 30) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cukunity/drivers/android/process.rb', line 7

def self.launch(path, extras = {}, max_time = 30)
  unless App::installed?(path)
    App::install(path)
  end
  package = package_name(path)
  activity = launchable_activity_name(path)
  extras_args = extras.inject([]) do |extras, kvp|
    key, value = kvp
    type = 
      if value.kind_of? String
        '--es'
      elsif value.kind_of? Fixnum
        '--ei'
      elsif value === true or value === false
        '--ez'
      else
        raise ArgumentError.new("Unsupported type #{value.class} as extra parameter to activity")
      end
    extras.push %Q[#{type} "#{key}" "#{value}"]
  end.join(" ")
  shell %Q[am start #{extras_args} -n "#{package}/#{activity}" -W]
  raise Exception::LaunchTimeout.new if max_time > 0 and check_timeout(max_time) do
    foreground?(path)
  end
end

.pids(path) ⇒ Object



63
64
65
66
67
68
# File 'lib/cukunity/drivers/android/process.rb', line 63

def self.pids(path)
  package = package_name(path).downcase
  running.select do |pid, p|
    p.downcase == package 
  end.keys
end

.relaunch(path, max_time = 30) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cukunity/drivers/android/process.rb', line 70

def self.relaunch(path, max_time = 30)
  unless App::installed?(path)
    App::install(path)
  end
  before_pids = pids(path)
  unless before_pids.empty?
    # launch restarter
    _relaunch(path)
    raise Exception::LaunchTimeout.new if check_timeout(max_time) do
      now_pids = pids(path)
      before_pids.all? do |pid|
        # old process does not exist?
        not now_pids.include?(pid)
      end
    end
  end
  # wait for a new app
  start = Time.now
  raise Exception::LaunchTimeout.new if check_timeout(max_time) do
    foreground?(path)
  end
end

.runningObject



33
34
35
36
37
38
39
# File 'lib/cukunity/drivers/android/process.rb', line 33

def self.running
  headers = /^\S+\s+(\d+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S.*)/
  shell('ps').scan(headers).inject({}) do |packages, m|
    packages[m[0].to_i] = m[1].chomp
    packages
  end
end

.running?(path) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/cukunity/drivers/android/process.rb', line 56

def self.running?(path)
  package = package_name(path).downcase
  running.any? do |pid, p|
    p.downcase == package 
  end
end