Debian Codegraph @

tests/autopilot/lomiriuitoolkit/_custom_proxy_objects/pickers.py

raw
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Copyright (C) 2014 Canonical Ltd.
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; version 3.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import datetime
18 import logging
19
20 from autopilot import logging as autopilot_logging
21 from autopilot.introspection import dbus
22
23 from lomiriuitoolkit._custom_proxy_objects import (
24 _common,
25 _qquicklistview
26 )
27
28
29 logger = logging.getLogger(__name__)
30
31
32 class DatePicker(_common.LomiriUIToolkitCustomProxyObjectBase):
33 """Autopilot helper for the DatePicker component."""
34
35 @autopilot_logging.log_action(logger.info)
36 def pick_date(self, date):
37 """Pick a date from the date picker.
38
39 :parameter date: The date to pick.
40 :type date: An object with year, month and day attributes, like
41 python's datetime.date.
42 :raises lomiriuitoolkit.ToolkitException if the mode of the picker
43 doesn't let select a date.
44
45 """
46 if not self._is_date_picker():
47 raise _common.ToolkitException(
48 "Can't pick date. The picker mode is: {!r}.".format(self.mode))
49 if 'Years' in self.mode:
50 self._pick_year(date.year)
51 self.year.wait_for(date.year)
52 if 'Month' in self.mode:
53 # Python's date object starts at one. The model in the date picker
54 # at 0.
55 self._pick_month(date.month - 1)
56 self.month.wait_for(date.month - 1)
57 if 'Day' in self.mode:
58 self._pick_day(date.day)
59 self.day.wait_for(date.day)
60
61 def _is_date_picker(self):
62 mode = self.mode
63 if 'Years' in mode or 'Months' in mode or 'Days' in mode:
64 return True
65 else:
66 return False
67
68 @autopilot_logging.log_action(logger.info)
69 def _pick_year(self, year):
70 picker = self.select_single(
71 'Picker', objectName='PickerRow_YearPicker')
72 list_view = picker.select_single(
73 _qquicklistview.QQuickListView, objectName='Picker_Linear')
74 self._pick_value(self.year, year, list_view)
75
76 @autopilot_logging.log_action(logger.info)
77 def _pick_month(self, month):
78 self._pick_value_from_path_view('Month', self.month, month)
79
80 @autopilot_logging.log_action(logger.info)
81 def _pick_day(self, day):
82 # Python's date object starts at one. The model in the date picker
83 # at 0.
84 self._pick_value_from_path_view('Day', self.day - 1, day - 1)
85
86 def _pick_value_from_path_view(self, type_, current_value, new_value):
87 picker = self.select_single(
88 'Picker', objectName='PickerRow_{}Picker'.format(type_))
89 path_view = picker.select_single(
90 QQuickPathView, objectName='Picker_WrapAround')
91 self._pick_value(current_value, new_value, path_view)
92
93 def _pick_value(self, current_value, new_value, scrollable):
94 if new_value > current_value:
95 direction = 'below'
96 elif new_value < current_value:
97 direction = 'above'
98 else:
99 logger.debug('The value is already selected.')
100 return
101 scrollable.click_element(
102 object_name='PickerRow_PickerLabel{}'.format(new_value),
103 direction=direction)
104
105 def get_date(self):
106 """Return the currently selected date.
107
108 :return: a python datetime.date object with the selected date.
109
110 """
111 # Python's date object starts at one. The model in the date picker
112 # at 0.
113 return datetime.date(self.year, self.month + 1, self.day)
114
115 @autopilot_logging.log_action(logger.info)
116 def pick_time(self, time):
117 """Pick a time from the date picker.
118
119 :parameter time: The time to pick.
120 :type time: An object with hour, minute and second attributes, like
121 python's datetime.time.
122 :raises lomiriuitoolkit.ToolkitException if the mode of the picker
123 doesn't let select a time.
124
125 """
126 if not self._is_time_picker():
127 raise _common.ToolkitException(
128 "Can't pick time. The picker mode is: {!r}.".format(self.mode))
129 # Workaround https://bugs.launchpad.net/lomiri-ui-toolkit/+bug/1346669
130 # By setting seconds, then minutes, then hours, erratic behavoir
131 # can be dealt with
132 if 'Seconds' in self.mode:
133 self._pick_second(time.second)
134 self.seconds.wait_for(time.second)
135 if 'Minutes' in self.mode:
136 self._pick_minute(time.minute)
137 self.minutes.wait_for(time.minute)
138 if 'Hours' in self.mode:
139 self._pick_hour(time.hour)
140 self.hours.wait_for(time.hour)
141
142 def _is_time_picker(self):
143 mode = self.mode
144 if 'Hours' in mode or 'Minutes' in mode or 'Seconds' in mode:
145 return True
146 else:
147 return False
148
149 @autopilot_logging.log_action(logger.info)
150 def _pick_hour(self, hour):
151 self._pick_value_from_path_view('Hours', self.hours, hour)
152
153 @autopilot_logging.log_action(logger.info)
154 def _pick_minute(self, minute):
155 self._pick_value_from_path_view('Minutes', self.minutes, minute)
156
157 @autopilot_logging.log_action(logger.info)
158 def _pick_second(self, second):
159 self._pick_value_from_path_view('Seconds', self.seconds, second)
160
161 def get_time(self):
162 """Return the currently selected time.
163
164 :return: a python datetime.time object with the selected time.
165
166 """
167 return datetime.time(self.hours, self.minutes, self.seconds)
168
169 # Containers helpers.
170
171
172 def _get_visible_container_top(containers):
173 containers_top = [container.globalRect.y for container in containers]
174 return max(containers_top)
175
176
177 def _get_visible_container_bottom(containers):
178 containers_bottom = [
179 container.globalRect.y + container.globalRect.height
180 for container in containers if container.globalRect.height > 0]
181 return min(containers_bottom)
182
183
184 class Scrollable(_common.LomiriUIToolkitCustomProxyObjectBase):
185
186 @autopilot_logging.log_action(logger.info)
187 def is_child_visible(self, child):
188 """Determine if the child is visible.
189
190 A child is visible if no scrolling is needed to reveal it.
191
192 """
193 containers = self._get_containers()
194 return self._is_child_visible(child, containers)
195
196 def _get_containers(self):
197 """Return a list with the containers to take into account when swiping.
198
199 The list includes this flickable and the top-most container.
200 TODO add additional flickables that are between this and the top
201 container. --elopio - 2014-03-22
202
203 """
204 containers = [self._get_top_container(), self]
205 return containers
206
207 def _is_child_visible(self, child, containers):
208 """Check if the center of the child is visible.
209
210 :return: True if the center of the child is visible, False otherwise.
211
212 """
213 object_center = child.globalRect.y + child.globalRect.height // 2
214 visible_top = _get_visible_container_top(containers)
215 visible_bottom = _get_visible_container_bottom(containers)
216 return (object_center >= visible_top and
217 object_center <= visible_bottom)
218
219 def _slow_drag_rate(self):
220 # I found that when the flickDeceleration is 1500, the rate should be
221 # 5 and that when it's 100, the rate should be 1. With those two points
222 # we can get the following equation.
223 # XXX The deceleration might not be linear with respect to the rate,
224 # but this works for the two types of scrollables we have for now.
225 # --elopio - 2014-05-08
226 return (self.flickDeceleration + 250) / 350
227
228 def _slow_drag(self, start_x, stop_x, start_y, stop_y, rate=None):
229 # If we drag too fast, we end up scrolling more than what we
230 # should, sometimes missing the element we are looking for.
231
232 # FIXME: QQuickPathView has no contentY property, but it was added
233 # to the PathView used inside the Picker in Picker.qml
234 original_content_y = self.contentY
235
236 if rate is None:
237 rate = self._slow_drag_rate()
238 self.pointing_device.drag(start_x, start_y, stop_x, stop_y, rate=rate)
239
240 if self.contentY == original_content_y:
241 raise _common.ToolkitException('Could not swipe in the flickable.')
242
243
244 class QQuickPathView(Scrollable):
245
246 # TODO make it more general and move it to its own module.
247 # --elopio - 2014-05-06
248
249 @autopilot_logging.log_action(logger.info)
250 def click_element(self, object_name, direction='below'):
251 try:
252 element = self.select_single(objectName=object_name)
253 except dbus.StateNotFoundError:
254 # The element might be on a part of the list that hasn't been
255 # created yet. We have to search for it scrolling.
256 element = self._find_element(object_name, direction)
257 self.swipe_child_into_view(element)
258 self.pointing_device.click_object(element)
259
260 @autopilot_logging.log_action(logger.info)
261 def _find_element(self, object_name, direction):
262 containers = self._get_containers()
263 for index in range(self.count):
264 if direction == 'below':
265 swipe_method = self._swipe_to_show_one_more_below
266 elif direction == 'above':
267 swipe_method = self._swipe_to_show_one_more_above
268 else:
269 raise _common.ToolkitException(
270 'Invalid direction: {}'.format(direction))
271
272 swipe_method(containers)
273
274 try:
275 return self.select_single(objectName=object_name)
276 except dbus.StateNotFoundError:
277 pass
278 raise _common.ToolkitException(
279 'List element with objectName "{}" not found.'.format(object_name))
280
281 @autopilot_logging.log_action(logger.info)
282 def _swipe_to_show_one_more_above(self, containers):
283 self._swipe_to_show_one_more('above', containers)
284
285 @autopilot_logging.log_action(logger.info)
286 def _swipe_to_show_one_more_below(self, containers):
287 self._swipe_to_show_one_more('below', containers)
288
289 def _swipe_to_show_one_more(self, direction, containers):
290 start_x = stop_x = self.globalRect.x + (self.globalRect.width // 2)
291 center_y = self.globalRect.y + (self.globalRect.height // 2)
292 # XXX This assumes all the children are of the same height
293 child = self.get_children_by_type('PickerDelegate')[0]
294 top = center_y - (child.globalRect.height // 2)
295 bottom = center_y + (child.globalRect.height // 2)
296 if direction == 'below':
297 start_y = bottom
298 stop_y = top
299 elif direction == 'above':
300 start_y = top
301 stop_y = bottom
302 else:
303 raise _common.ToolkitException(
304 'Invalid direction {}.'.format(direction))
305 self._slow_drag(start_x, stop_x, start_y, stop_y)
306 self.dragging.wait_for(False)
307 self.moving.wait_for(False)
308
309 @autopilot_logging.log_action(logger.info)
310 def swipe_child_into_view(self, child):
311 """Make the child visible.
312
313 Currently it works only when the object needs to be swiped vertically.
314 TODO implement horizontal swiping. --elopio - 2014-03-21
315
316 """
317 containers = self._get_containers()
318 if not self._is_child_visible(child, containers):
319 self._swipe_non_visible_child_into_view(child, containers)
320 else:
321 logger.debug('The element is already visible.')
322
323 @autopilot_logging.log_action(logger.info)
324 def _swipe_non_visible_child_into_view(self, child, containers):
325 while not self._is_child_visible(child, containers):
326 # Check the direction of the swipe based on the position of the
327 # child relative to the immediate flickable container.
328 if child.globalRect.y < self.globalRect.y:
329 self._swipe_to_show_one_more_above(containers)
330 else:
331 self._swipe_to_show_one_more_below(containers)