| 1 |
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2012, 2013, 2014, 2015 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 logging |
| 18 |
import time |
| 19 |
|
| 20 |
from autopilot import ( |
| 21 |
input, |
| 22 |
logging as autopilot_logging, |
| 23 |
platform |
| 24 |
) |
| 25 |
from autopilot.introspection import dbus |
| 26 |
|
| 27 |
from lomiriuitoolkit._custom_proxy_objects import _common, _flickable |
| 28 |
|
| 29 |
logger = logging.getLogger(__name__) |
| 30 |
|
| 31 |
|
| 32 |
class QQuickListView(_flickable.QQuickFlickable): |
| 33 |
|
| 34 |
@autopilot_logging.log_action(logger.info) |
| 35 |
def click_element(self, object_name, direction=None): |
| 36 |
"""Click an element from the list. |
| 37 |
|
| 38 |
It swipes the element into view if it's center is not visible. |
| 39 |
|
| 40 |
:parameter object_name: The objectName property of the element to |
| 41 |
click. |
| 42 |
:parameter direction: The direction where the element is, it can be |
| 43 |
either 'above' or 'below'. Default value is None, which means we |
| 44 |
don't know where the object is and we will need to search the full |
| 45 |
list. |
| 46 |
|
| 47 |
""" |
| 48 |
try: |
| 49 |
element = self.select_single(objectName=object_name) |
| 50 |
except dbus.StateNotFoundError: |
| 51 |
# The element might be on a part of the list that hasn't been |
| 52 |
# created yet. We have to search for it scrolling the entire list. |
| 53 |
element = self._find_element(object_name, direction) |
| 54 |
self.swipe_child_into_view(element) |
| 55 |
self.pointing_device.click_object(element) |
| 56 |
return element |
| 57 |
|
| 58 |
@autopilot_logging.log_action(logger.info) |
| 59 |
def _find_element(self, object_name, direction=None): |
| 60 |
if direction is None: |
| 61 |
# We don't know where the object is so we start looking for it from |
| 62 |
# the top or left, depending on the ListView orientation. |
| 63 |
if self.orientation == 2: |
| 64 |
# 2 == ListView.Vertical |
| 65 |
self.swipe_to_top() |
| 66 |
direction = 'below' |
| 67 |
else: |
| 68 |
# orientation == 1 == ListView.Horizontal |
| 69 |
self.swipe_to_leftmost() |
| 70 |
direction = 'right' |
| 71 |
|
| 72 |
if direction == 'below': |
| 73 |
def fail_condition(): |
| 74 |
return self.atYEnd |
| 75 |
swipe_method = self.swipe_to_show_more_below |
| 76 |
elif direction == 'above': |
| 77 |
def fail_condition(): |
| 78 |
return self.atYBeginning |
| 79 |
swipe_method = self.swipe_to_show_more_above |
| 80 |
elif direction == 'left': |
| 81 |
def fail_condition(): |
| 82 |
return self.atXBeginning |
| 83 |
swipe_method = self.swipe_to_show_more_left |
| 84 |
elif direction == 'right': |
| 85 |
def fail_condition(): |
| 86 |
return self.atXEnd |
| 87 |
swipe_method = self.swipe_to_show_more_right |
| 88 |
else: |
| 89 |
raise _common.ToolkitException( |
| 90 |
'Invalid direction: {}'.format(direction)) |
| 91 |
|
| 92 |
containers = self._get_containers() |
| 93 |
while not fail_condition(): |
| 94 |
try: |
| 95 |
return self.select_single(objectName=object_name) |
| 96 |
except dbus.StateNotFoundError: |
| 97 |
swipe_method(containers) |
| 98 |
else: |
| 99 |
# We have reached the start or the end of the list. |
| 100 |
try: |
| 101 |
return self.select_single(objectName=object_name) |
| 102 |
except dbus.StateNotFoundError: |
| 103 |
raise _common.ToolkitException( |
| 104 |
'List element with objectName "{}" not found.'.format( |
| 105 |
object_name)) |
| 106 |
|
| 107 |
def _is_element_cached(self, object_name): |
| 108 |
"""Return the object with the given object name if the element |
| 109 |
is cached, or None if the element is not cached. |
| 110 |
""" |
| 111 |
try: |
| 112 |
child = self.select_single(objectName=object_name) |
| 113 |
except dbus.StateNotFoundError: |
| 114 |
return None |
| 115 |
|
| 116 |
return child |
| 117 |
|
| 118 |
def _is_element_visible(self, object_name): |
| 119 |
"""Return True if the center of element with the given object name |
| 120 |
is visible and False otherwise. |
| 121 |
""" |
| 122 |
child = self._is_element_cached(object_name) |
| 123 |
if not child: |
| 124 |
return False |
| 125 |
|
| 126 |
containers = self._get_containers() |
| 127 |
return self._is_child_visible(child, containers) |
| 128 |
|
| 129 |
@autopilot_logging.log_action(logger.info) |
| 130 |
def enable_select_mode(self): |
| 131 |
"""Default implementation to enable select mode. Performs a long tap |
| 132 |
over the first list item in the ListView. The delegates must be |
| 133 |
the new ListItem components. |
| 134 |
""" |
| 135 |
self.swipe_to_top() |
| 136 |
first_item = self._get_first_item() |
| 137 |
self.pointing_device.click_object(first_item, press_duration=2) |
| 138 |
try: |
| 139 |
self.wait_select_single('QQuickItem', |
| 140 |
objectName='selection_panel0') |
| 141 |
except dbus.StateNotFoundError: |
| 142 |
raise _common.ToolkitException( |
| 143 |
'ListView delegate is not a ListItem or not in selectMode') |
| 144 |
|
| 145 |
def _get_first_item(self): |
| 146 |
"""Returns the first item from the ListView.""" |
| 147 |
items = self.get_children_by_type('QQuickItem')[0].get_children() |
| 148 |
items = sorted(items, key=lambda item: item.globalRect.y) |
| 149 |
return items[0] |
| 150 |
|
| 151 |
@autopilot_logging.log_action(logger.info) |
| 152 |
def drag_item(self, from_index, to_index): |
| 153 |
if platform.model() != 'Desktop': |
| 154 |
raise NotImplementedError( |
| 155 |
'Drag does not work on the phone because of bug #1266601') |
| 156 |
|
| 157 |
self._enable_drag_mode() |
| 158 |
|
| 159 |
both_items_visible = ( |
| 160 |
self._is_drag_panel_visible(from_index) and |
| 161 |
self._is_drag_panel_visible(to_index)) |
| 162 |
if both_items_visible: |
| 163 |
self._drag_both_items_visible(from_index, to_index) |
| 164 |
else: |
| 165 |
self._drag_item_with_pagination(from_index, to_index) |
| 166 |
# wait 1 second till all animations complete |
| 167 |
time.sleep(1) |
| 168 |
|
| 169 |
@autopilot_logging.log_action(logger.debug) |
| 170 |
def _enable_drag_mode(self): |
| 171 |
self.swipe_to_top() |
| 172 |
first_item = self._get_first_item() |
| 173 |
self.pointing_device.click_object(first_item, press_duration=2) |
| 174 |
self._get_drag_panel(wait=True) |
| 175 |
|
| 176 |
def _is_drag_panel_visible(self, index): |
| 177 |
try: |
| 178 |
drag_panel = self._get_drag_panel(index) |
| 179 |
except: |
| 180 |
return False |
| 181 |
else: |
| 182 |
return self.is_child_visible(drag_panel) |
| 183 |
|
| 184 |
def _get_drag_panel(self, index=0, wait=False): |
| 185 |
select_method = self.wait_select_single if wait else self.select_single |
| 186 |
try: |
| 187 |
return select_method( |
| 188 |
'QQuickItem', objectName='drag_panel{}'.format(index)) |
| 189 |
except ValueError: |
| 190 |
# While we are dragging, each panel item is duplicated. |
| 191 |
# A ValueError will be raised in this scenario. We can take |
| 192 |
# any of the returned elements, as they have the same position. |
| 193 |
return self.select_many( |
| 194 |
'QQuickItem', objectName='drag_panel{}'.format(index))[0] |
| 195 |
|
| 196 |
def _drag_both_items_visible(self, from_index, to_index): |
| 197 |
from_drag_handler = self._get_drag_panel(from_index) |
| 198 |
to_drag_handler = self._get_drag_panel(to_index) |
| 199 |
start_x, start_y = input.get_center_point(from_drag_handler) |
| 200 |
stop_x, stop_y = input.get_center_point(to_drag_handler) |
| 201 |
self.pointing_device.drag(start_x, start_y, stop_x, stop_y) |
| 202 |
|
| 203 |
def _drag_item_with_pagination(self, from_index, to_index): |
| 204 |
# the from_index might be invisible |
| 205 |
from_item = self._find_element('listitem{}'.format(from_index)) |
| 206 |
from_item.swipe_into_view() |
| 207 |
from_drag_panel = self._get_drag_panel(from_index) |
| 208 |
containers = self._get_containers() |
| 209 |
if from_index < to_index: |
| 210 |
self._drag_downwards(from_drag_panel, to_index, containers) |
| 211 |
else: |
| 212 |
self._drag_upwards(from_drag_panel, to_index, containers) |
| 213 |
|
| 214 |
def _drag_downwards(self, drag_panel, to_index, containers): |
| 215 |
visible_bottom = _flickable._get_visible_container_bottom( |
| 216 |
containers) |
| 217 |
start_x, start_y = input.get_center_point(drag_panel) |
| 218 |
|
| 219 |
self.pointing_device.move(start_x, start_y) |
| 220 |
self.pointing_device.press() |
| 221 |
stop_x = start_x |
| 222 |
self.pointing_device.move(stop_x, visible_bottom) |
| 223 |
to_drag_panel = self._get_drag_panel(to_index, wait=True) |
| 224 |
while not self.is_child_visible(to_drag_panel): |
| 225 |
pass |
| 226 |
# stop moving |
| 227 |
h = to_drag_panel.height / 2 |
| 228 |
self.pointing_device.move(stop_x, self.pointing_device.y - h) |
| 229 |
# move under the item with the to_index |
| 230 |
to_drag_panel = self._get_drag_panel(to_index - 1) |
| 231 |
stop_y = ( |
| 232 |
to_drag_panel.globalRect.y + |
| 233 |
to_drag_panel.globalRect.height) |
| 234 |
self.pointing_device.move(stop_x, stop_y) |
| 235 |
self.pointing_device.release() |
| 236 |
|
| 237 |
def _drag_upwards(self, handler, to_index, containers): |
| 238 |
visible_top = _flickable._get_visible_container_top( |
| 239 |
containers) |
| 240 |
start_x, start_y = input.get_center_point(handler) |
| 241 |
|
| 242 |
self.pointing_device.move(start_x, start_y) |
| 243 |
self.pointing_device.press() |
| 244 |
stop_x = start_x |
| 245 |
# Header alters topMargin, therefore drag only till that edge |
| 246 |
self.pointing_device.move(stop_x, visible_top + self.topMargin) |
| 247 |
to_drag_panel = self._get_drag_panel(to_index, wait=True) |
| 248 |
while not self.is_child_visible(to_drag_panel): |
| 249 |
pass |
| 250 |
# stop moving |
| 251 |
h = to_drag_panel.height / 2 |
| 252 |
self.pointing_device.move(stop_x, self.pointing_device.y + h) |
| 253 |
# move after the item with the to_index |
| 254 |
to_drag_panel = self._get_drag_panel(to_index) |
| 255 |
stop_y = ( |
| 256 |
to_drag_panel.globalRect.y + |
| 257 |
to_drag_panel.globalRect.height) |
| 258 |
self.pointing_device.move(stop_x, stop_y) |
| 259 |
self.pointing_device.release() |