-
Notifications
You must be signed in to change notification settings - Fork 369
/
device_agent.rb
485 lines (392 loc) · 13.8 KB
/
device_agent.rb
1
2
3
4
5
6
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# @!visibility private
module Calabash
module Cucumber
module Automator
require "calabash-cucumber/automator/automator"
# @!visibility private
class DeviceAgent < Calabash::Cucumber::Automator::Automator
require "run_loop"
require "calabash-cucumber/map"
require "calabash-cucumber/query_helpers"
include Calabash::Cucumber::QueryHelpers
require "calabash-cucumber/status_bar_helpers"
include Calabash::Cucumber::StatusBarHelpers
require "calabash-cucumber/rotation_helpers"
include Calabash::Cucumber::RotationHelpers
require "calabash-cucumber/environment_helpers"
include Calabash::Cucumber::EnvironmentHelpers
require "calabash-cucumber/automator/coordinates"
# @!visibility private
def self.expect_valid_args(args)
if args.nil?
raise ArgumentError, "Expected args to be a non-nil Array"
end
if !args.is_a?(Array)
raise ArgumentError, %Q[Expected args to be an Array, found:
args = #{args}
]
end
if args.count != 1
raise(ArgumentError,
%Q[Expected args to be an Array with one element, found:
args = #{args}
])
end
if !args[0].is_a?(RunLoop::DeviceAgent::Client)
raise(ArgumentError, %Q[
Expected first element of args to be a RunLoop::DeviceAgent::Client instance, found:
args[0] = #{args[0]}])
end
true
end
attr_reader :client
# @!visibility private
def initialize(*args)
DeviceAgent.expect_valid_args(args)
@client = args[0]
end
# @!visibility private
def name
:device_agent
end
# @!visibility private
def stop
client.send(:shutdown)
end
# @!visibility private
def running?
client.send(:running?)
end
# @!visibility private
def session_delete
client.send(:session_delete)
end
# @!visibility private
def touch(options)
hash = query_for_coordinates(options)
client.perform_coordinate_gesture("touch",
hash[:coordinates][:x],
hash[:coordinates][:y])
[hash[:view]]
end
# @!visibility private
def double_tap(options)
hash = query_for_coordinates(options)
client.perform_coordinate_gesture("double_tap",
hash[:coordinates][:x],
hash[:coordinates][:y])
[hash[:view]]
end
# @!visibility private
def two_finger_tap(options)
hash = query_for_coordinates(options)
client.perform_coordinate_gesture("two_finger_tap",
hash[:coordinates][:x],
hash[:coordinates][:y])
[hash[:view]]
end
# @!visibility private
def touch_hold(options)
hash = query_for_coordinates(options)
duration = options[:duration] || 3
client.perform_coordinate_gesture("touch",
hash[:coordinates][:x],
hash[:coordinates][:y],
{:duration => duration})
[hash[:view]]
end
# @!visibility private
def swipe(options)
dupped_options = options.dup
if dupped_options[:query].nil?
element = element_for_device_screen
from_point = point_from(element, options)
else
hash = query_for_coordinates(dupped_options)
from_point = hash[:coordinates]
element = hash[:view]
end
# DeviceAgent does not understand the :force. Does anyone?
force = dupped_options[:force]
case force
when :strong
duration = 0.2
when :normal
duration = 0.4
when :light
duration = 0.7
else
# Caller is responsible for validating the :force option.
duration = 0.5
end
gesture_options = {
:duration => duration
}
direction = dupped_options[:direction]
to_point = Coordinates.end_point_for_swipe(direction, element, force)
client.pan_between_coordinates(from_point, to_point, gesture_options)
[element]
end
# @!visibility private
def pinch(in_out, options)
dupped_options = options.dup
if dupped_options[:query].nil?
element = element_for_device_screen
coordinates = point_from(element, options)
else
hash = query_for_coordinates(dupped_options)
element = hash[:view]
coordinates = hash[:coordinates]
end
in_out = in_out.to_s
duration = dupped_options[:duration]
amount = dupped_options[:amount]
gesture_options = {
:pinch_direction => in_out,
:amount => amount,
:duration => duration
}
client.perform_coordinate_gesture("pinch",
coordinates[:x],
coordinates[:y],
gesture_options)
[element]
end
# @!visibility private
def pan(from_query, to_query, options)
dupped_options = options.dup
dupped_options[:query] = from_query
from_hash = query_for_coordinates(dupped_options)
from_point = from_hash[:coordinates]
dupped_options[:query] = to_query
to_hash = query_for_coordinates(dupped_options)
to_point = to_hash[:coordinates]
gesture_options = {
:duration => dupped_options[:duration]
}
client.pan_between_coordinates(from_point, to_point,
gesture_options)
[from_hash[:view], to_hash[:view]]
end
# @!visibility private
def pan_coordinates(from_point, to_point, options)
gesture_options = {
:duration => options[:duration]
}
client.pan_between_coordinates(from_point, to_point,
gesture_options)
[first_element_for_query("*")]
end
# @!visibility private
def flick(options)
gesture_options = {
duration: 0.2
}
delta = options[:delta]
# The UIA deltas are too small.
scaled_delta = {
:x => delta[:x] * 2.0,
:y => delta[:y] * 2.0
}
hash = query_for_coordinates(options)
view = hash[:view]
start_point = point_from(view)
end_point = point_from(view, {:offset => scaled_delta})
client.pan_between_coordinates(start_point,
end_point,
gesture_options)
[view]
end
# @!visibility private
def enter_text_with_keyboard(string, options={})
client.enter_text_without_keyboard_check(string)
end
# @!visibility private
def enter_char_with_keyboard(char)
client.enter_text_without_keyboard_check(char)
end
# @!visibility private
def char_for_keyboard_action(action_key)
SPECIAL_ACTION_CHARS[action_key]
end
# @!visibility private
def tap_keyboard_action_key
mark = mark_for_return_key_of_first_responder
if mark
begin
# The underlying query for coordinates always expects results.
value = client.touch({type: "Button", marked: mark})
return value
rescue RuntimeError => _
RunLoop.log_debug("Cannot find mark '#{mark}' with query; will send a newline")
end
else
RunLoop.log_debug("Cannot find keyboard return key type; sending a newline")
end
code = char_for_keyboard_action("Return")
client.enter_text_without_keyboard_check(code)
end
# @!visibility private
def tap_keyboard_delete_key
client.touch({marked: "delete"})
end
# @!visibility private
def fast_enter_text(text)
client.enter_text_without_keyboard_check(text)
end
# @!visibility private
#
# Stable across different keyboard languages.
def dismiss_ipad_keyboard
client.touch({marked: "Hide keyboard"})
end
# @!visibility private
def rotate(direction)
# Caller is responsible for normalizing and verifying direction.
current_orientation = status_bar_orientation.to_sym
key = orientation_key(direction, current_orientation)
position = orientation_for_key(key)
rotate_home_button_to(position)
end
# @!visibility private
def rotate_home_button_to(position)
# Caller is responsible for normalizing and verifying position.
client.rotate_home_button_to(position)
status_bar_orientation.to_sym
end
private
# @!visibility private
#
# Calls #point_from which applies any :offset supplied in the options.
def query_for_coordinates(options)
uiquery = options[:query]
if uiquery.nil?
offset = options[:offset]
if offset && offset[:x] && offset[:y]
{
:coordinates => offset,
:view => offset
}
else
raise ArgumentError, %Q[
If query is nil, there must be a valid offset in the options.
Expected: options[:offset] = {:x => NUMERIC, :y => NUMERIC}
Actual: options[:offset] = #{offset ? offset : "nil"}
]
end
else
first_element = first_element_for_query(uiquery)
if first_element.nil?
msg = %Q[
Could not find any views with query:
#{uiquery}
Make sure your query returns at least one view.
]
Calabash::Cucumber::Map.new.screenshot_and_raise(msg)
else
{
:coordinates => point_from(first_element, options),
:view => first_element
}
end
end
end
# @!visibility private
def first_element_for_query(uiquery)
if uiquery.nil?
raise ArgumentError, "Query cannot be nil"
end
# Will raise if response "outcome" is not SUCCESS
results = Calabash::Cucumber::Map.raw_map(uiquery, :query)["results"]
if results.empty?
nil
else
results[0]
end
end
# @!visibility private
def element_for_device_screen
screen_dimensions = device.screen_dimensions
scale = screen_dimensions[:scale]
height = (screen_dimensions[:height]/scale).to_i
center_y = (height/2)
width = (screen_dimensions[:width]/scale).to_i
center_x = (width/2)
{
"screen" => true,
"rect" => {
"height" => height,
"width" => width,
"center_x" => center_x,
"center_y" => center_y
}
}
end
# @!visibility private
#
# Don't change the double quotes.
SPECIAL_ACTION_CHARS = {
"Delete" => "\b",
"Return" => "\n"
}.freeze
# @!visibility private
#
# Keys are from the UIReturnKeyType enum.
#
# The values are localization independent identifiers - these are
# stable across localizations and keyboard languages. The exception is
# Continue which is not stable.
RETURN_KEY_TYPE = {
0 => "Return",
1 => "Go",
2 => "Google",
# Needs special physical device vs simulator handling.
3 => "Join",
4 => "Next",
5 => "Route",
6 => "Search",
7 => "Send",
8 => "Yahoo",
9 => "Done",
10 => "Emergency call",
# https://xamarin.atlassian.net/browse/TCFW-344
# Localized!!! Apple bug.
11 => "Continue"
}.freeze
# @!visibility private
def mark_for_return_key_type(number)
# https://xamarin.atlassian.net/browse/TCFW-361
value = RETURN_KEY_TYPE[number]
if value == "Join" && !simulator?
"Join:"
else
value
end
end
# @!visibility private
def return_key_type_of_first_responder
query = "* isFirstResponder:1"
raw = Calabash::Cucumber::Map.raw_map(query, :query, :returnKeyType)
elements = raw["results"]
return nil if elements.count == 0
return_key_type = elements[0]
# first responder did not respond to :text selector
if return_key_type == "*****"
RunLoop.log_debug("First responder does not respond to :returnKeyType")
return nil
end
if return_key_type.nil?
RunLoop.log_debug("First responder has nil :returnKeyType")
return nil
end
return_key_type
end
# @!visibility private
def mark_for_return_key_of_first_responder
number = return_key_type_of_first_responder
mark_for_return_key_type(number)
end
end
end
end
end