Module: Calabash::Android::MonkeyHelpers

Includes:
EnvironmentHelpers
Included in:
DragHelpers
Defined in:
lib/calabash-android/monkey_helpers.rb

Constant Summary collapse

MAX_RETRIES =
10
@@monkey_port =
nil
@@monkey_pid =
nil

Instance Method Summary collapse

Methods included from EnvironmentHelpers

#xamarin_test_cloud?

Instance Method Details

#adb_commandObject



126
127
128
# File 'lib/calabash-android/monkey_helpers.rb', line 126

def adb_command
    default_device.adb_command
end

#existing_monkey_pidsObject



64
65
66
67
# File 'lib/calabash-android/monkey_helpers.rb', line 64

def existing_monkey_pids
  procs = `#{adb_command} shell ps`
  procs.scan(/.+?\s(?<pid>[0-9]+).+?com.android.commands.monkey\r?\n?/).flatten
end

#get_monkey_portObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/calabash-android/monkey_helpers.rb', line 33

def get_monkey_port
  MAX_RETRIES.times do
    port = rand((1024..65535))
    monkey_starter_thread = Thread.new do
      Thread.current[:output]= `#{adb_command} shell monkey --port #{port}`
    end
    sleep(4)

    output = monkey_starter_thread[:output]
    unless output && output.include?('Error binding to network socket.')
      return port
    end
  end
  raise 'Unable to start monkey on device'
end

#kill_existing_monkey_processesObject



99
100
101
102
103
104
# File 'lib/calabash-android/monkey_helpers.rb', line 99

def kill_existing_monkey_processes
  kill_monkey_processes_on_host
  kill_monkey_processes_on_device
  @@monkey_port = nil
  @@monkey_pid = nil
end

#kill_monkey_processes_on_deviceObject



69
70
71
72
73
74
# File 'lib/calabash-android/monkey_helpers.rb', line 69

def kill_monkey_processes_on_device
  perform_action('send_tcp', @@monkey_port, "quit", true) unless @@monkey_port.nil?
  existing_monkey_pids.each do |pid|
    `#{adb_command} shell kill -9 #{pid}`
  end
end

#kill_monkey_processes_on_hostObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/calabash-android/monkey_helpers.rb', line 76

def kill_monkey_processes_on_host
  unless xamarin_test_cloud?
    monkey_args = "#{adb_command} shell monkey --port"
    if Env.is_windows?
      processes = `WMIC PATH win32_process GET Commandline, processid /FORMAT:CSV`.split(/\r?\n/).flatten
      processes.each do |process|
        components = process.split(',')
        if components.length > 2 && components[1].index(monkey_args) == 0
          `kill -9 #{components[2]}`
        end
      end
    else
      processes = `ps -xww -o pid,user,args`.split("\n").flatten
      processes.each do |process|
        if process.index(monkey_args) == 0
          pid = process.strip().split(' ')[0].to_i
          `kill -9 #{pid}`
        end
      end
    end
  end
end

#monkey_move_from(from_x, from_y, to_x, to_y, args = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/calabash-android/monkey_helpers.rb', line 12

def monkey_move_from(from_x, from_y, to_x, to_y, args={})
  start_monkey
  monkey_touch(:down, from_x, from_y)
  sleep(args.fetch(:hold_time))

  x_delta = to_x - from_x
  y_delta = to_y - from_y
  steps = args.fetch(:steps)

  steps.times do |i|
    move_x = (from_x + ((i+1) * (x_delta.to_f / steps))).to_i
    move_y = (from_y + ((i+1) * (y_delta.to_f / steps))).to_i
    monkey_touch(:move, move_x, move_y)
  end

  sleep(args.fetch(:hang_time))
  monkey_touch(:up, to_x, to_y)

  kill_existing_monkey_processes
end

#monkey_tap(x, y, should_start_monkey = true) ⇒ Object



106
107
108
109
110
111
# File 'lib/calabash-android/monkey_helpers.rb', line 106

def monkey_tap(x, y, should_start_monkey=true)
  start_monkey if should_start_monkey
  monkey_touch(:down, x, y)
  monkey_touch(:up, x, y)
  kill_existing_monkey_processes
end

#monkey_touch(touch_type, x, y) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/calabash-android/monkey_helpers.rb', line 113

def monkey_touch(touch_type, x, y)
  case touch_type
    when :move
      perform_action('send_tcp', @@monkey_port, "touch move #{x} #{y}", true)
    when :down
      perform_action('send_tcp', @@monkey_port, "touch down #{x} #{y}", true)
    when :up
      perform_action('send_tcp', @@monkey_port, "touch up #{x} #{y}", true)
    else
      raise "touch_type #{touch_type} is invalid"
  end
end

#start_monkeyObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/calabash-android/monkey_helpers.rb', line 49

def start_monkey
  kill_existing_monkey_processes
  @@monkey_port   = get_monkey_port
  monkey_timeout  = 10

  options = {
      :timeout => monkey_timeout,
      :timeout_message => "Monkey did not start on #{@@monkey_port} in #{monkey_timeout} seconds"
  }

  wait_for(options) {
    perform_action('send_tcp', @@monkey_port, 'sleep 0', true)
  }
end