-
Notifications
You must be signed in to change notification settings - Fork 369
/
flick.rb
240 lines (193 loc) · 5.51 KB
/
flick.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
module TestApp
module Flick
def flick_delta_from_radians(radians, element, distance=:normal)
case distance
when :normal
scale_radius_by = 0.333
when :small
scale_radius_by = 0.25
when :long
scale_radius_by = 0.5
else
raise ArgumentError, %Q[
Expected distance: #{distance} to be one of: [:normal, :small, :long]
]
end
element_width = element["rect"]["width"]
element_height = element["rect"]["height"]
x_center = element["rect"]["center_x"]
y_center = element["rect"]["center_y"]
radius = ([element_width, element_height].min) * scale_radius_by
target_x = x_center + (radius * Math.cos(radians))
target_y = y_center + (radius * Math.sin(radians))
{
x: (x_center - target_x).to_i.to_f,
y: (y_center - target_y).to_i.to_f
}
end
def flick_delta_from_degrees(degrees, element)
radians = degrees * Math::PI / 180.0
flick_delta_from_radians(radians, element)
end
def flick_delta_from_direction(direction, element)
case direction
when :left
degrees = 0
when :up_and_left
degrees = 45
when :up
degrees = 90
when :up_and_right
degrees = 135
when :right
degrees = 180
when :down_and_right
degrees = 225
when :down
degrees = 270
when :down_and_left
degrees = 315
else
raise ArgumentError, %Q[
Expected direction '#{direction}' to be one of:
:left, :up_and_left, :up, :right, :down_and_right, :down, :down_and_left
]
end
flick_delta_from_degrees(degrees, element)
end
def flick_to(direction, container_view_query, view_query, times, sleep=1.0)
return if !query(view_query).empty?
found = false
times.times do
element = wait_for_view(container_view_query)
delta = flick_delta_from_direction(direction, element)
flick(container_view_query, delta)
sleep(sleep)
wait_for_animations
found = !query(view_query).empty?
break if found
end
if !found
fail(%Q[
Flicked :#{direction} #{times} times on:
#{container_view_query}
but did not see:
#{view_query}
])
end
end
end
end
World(TestApp::Flick)
Given(/^I see the Apple row$/) do
element = status_bar_details
x = element["frame"]["x"] + (element["frame"]["width"]/2.0)
y = element["frame"]["y"] + (element["frame"]["height"]/2.0)
touch(nil, {offset: {x: x, y: y}})
wait_for_animations
wait_for_view("* marked:'apple row'")
end
Then(/^I can flick to the bottom of the Companies table$/) do
scroll_view_query = "* marked:'table page'"
view_query = "* marked:'youtube row'"
flick_to(:up, scroll_view_query, view_query, 2)
end
Then(/^I can flick to the top of the Companies table$/) do
scroll_view_query = "* marked:'table page'"
view_query = "* marked:'amazon row'"
flick_to(:down, scroll_view_query, view_query, 2)
end
Then(/^I center the cayenne box to the middle$/) do
query = "UIScrollView marked:'scroll'"
wait_for_view(query)
query(query, :centerContentToBounds)
wait_for_animations
query = "view marked:'cayenne'"
wait_for_view(query)
wait_for_animations
end
Then(/^I flick so I can see the (top|bottom|left|right) of the scroll view$/) do |position|
container_view_query = "* marked:'scroll'"
wait_for_view(container_view_query)
case position
when "top"
direction = :down
box_mark = "purple"
when "bottom"
direction = :up
box_mark = "gray"
when "left"
direction = :right
box_mark = "red"
when "right"
direction = :left
box_mark = "dark red"
else
raise "Can never get here"
end
view_query = "* marked:'#{box_mark}'"
flick_to(direction, container_view_query, view_query, 3)
end
Then(/^I flick to the (top|bottom) (right|left) of the scroll view$/) do |y, x|
container_view_query = "* marked:'scroll'"
wait_for_view(container_view_query)
case y.to_sym
when :top
case x.to_sym
when :left
box_mark = "light blue"
direction = :down_and_right
when :right
box_mark = "blue"
direction = :down_and_left
else
raise "Can never get here"
end
when :bottom
case x.to_sym
when :left
box_mark = "light gray"
direction = :up_and_right
when :right
box_mark = "dark gray"
direction = :up_and_left
else
raise "Can never get here"
end
else
raise "Can never get here"
end
view_query = "* marked:'#{box_mark}'"
flick_to(direction, container_view_query, view_query, 4, 1.0)
end
When(/^I full-screen flick to go back, I see the Pan menu$/) do
wait_for_animations
query = "* marked:'scroll'"
element = wait_for_view(query)
element_width = element["rect"]["width"]
x_center = element["rect"]["center_x"]
delta = {
x: element_width/2.0,
y: 0
}
offset = {
x: -1.0 * x_center,
y: 0
}
flick(query, delta, {offset: offset})
wait_for_animations
if uia_available?
wait_for_view("* marked:'pan page'")
else
begin
wait_for_view("* marked:'pan page'")
rescue Calabash::Cucumber::WaitHelpers::WaitError => e
@device_agent_flick_to_go_back_error = e
end
end
end
But(/^flick to go back does not work with DeviceAgent$/) do
if !uia_available?
expect(@device_agent_flick_to_go_back_error).to be_truthy
end
end