~ubuntu-sdk-team/ubuntu-ui-toolkit/orientationManual

« back to all changes in this revision

Viewing changes to tests/autopilot/ubuntuuitoolkit/_custom_proxy_objects/_qquicklistview.py

  • Committer: CI Train Bot
  • Author(s): Leo Arias, Christian Dywan, Nekhelesh Ramananthan, Tim Peeters, Zoltán Balogh
  • Date: 2015-03-27 12:35:52 UTC
  • mfrom: (1000.433.19 last_bugfix_vivid)
  • Revision ID: ci-train-bot@canonical.com-20150327123552-mdwg6mecr05akf0q
The very last bugfix landing for Vivid

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
2
#
3
 
# Copyright (C) 2012, 2013, 2014 Canonical Ltd.
 
3
# Copyright (C) 2012, 2013, 2014, 2015 Canonical Ltd.
4
4
#
5
5
# This program is free software; you can redistribute it and/or modify
6
6
# it under the terms of the GNU Lesser General Public License as published by
15
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
import logging
 
18
import time
18
19
 
19
 
from autopilot import logging as autopilot_logging
 
20
from autopilot import (
 
21
    input,
 
22
    logging as autopilot_logging,
 
23
    platform
 
24
)
20
25
from autopilot.introspection import dbus
21
26
 
22
27
from ubuntuuitoolkit._custom_proxy_objects import _common, _flickable
86
91
        containers = self._get_containers()
87
92
        return self._is_child_visible(child, containers)
88
93
 
89
 
    def _get_first_item(self):
90
 
        """Returns the first item from the ListView."""
91
 
        items = self.get_children_by_type('QQuickItem')[0].get_children()
92
 
        items = sorted(items, key=lambda item: item.globalRect.y)
93
 
        return items[0]
94
 
 
95
94
    @autopilot_logging.log_action(logger.info)
96
95
    def enable_select_mode(self):
97
96
        """Default implementation to enable select mode. Performs a long tap
107
106
        except dbus.StateNotFoundError:
108
107
            raise _common.ToolkitException(
109
108
                'ListView delegate is not a ListItem or not in selectMode')
 
109
 
 
110
    def _get_first_item(self):
 
111
        """Returns the first item from the ListView."""
 
112
        items = self.get_children_by_type('QQuickItem')[0].get_children()
 
113
        items = sorted(items, key=lambda item: item.globalRect.y)
 
114
        return items[0]
 
115
 
 
116
    @autopilot_logging.log_action(logger.info)
 
117
    def drag_item(self, from_index, to_index):
 
118
        if platform.model() != 'Desktop':
 
119
            raise NotImplementedError(
 
120
                'Drag does not work on the phone because of bug #1266601')
 
121
 
 
122
        self._enable_drag_mode()
 
123
 
 
124
        both_items_visible = (
 
125
            self._is_drag_panel_visible(from_index) and
 
126
            self._is_drag_panel_visible(to_index))
 
127
        if both_items_visible:
 
128
            self._drag_both_items_visible(from_index, to_index)
 
129
        else:
 
130
            self._drag_item_with_pagination(from_index, to_index)
 
131
        # wait 1 second till all animations complete
 
132
        time.sleep(1)
 
133
 
 
134
    @autopilot_logging.log_action(logger.debug)
 
135
    def _enable_drag_mode(self):
 
136
        self.swipe_to_top()
 
137
        first_item = self._get_first_item()
 
138
        self.pointing_device.click_object(first_item, press_duration=2)
 
139
        self._get_drag_panel(wait=True)
 
140
 
 
141
    def _is_drag_panel_visible(self, index):
 
142
        try:
 
143
            drag_panel = self._get_drag_panel(index)
 
144
        except:
 
145
            return False
 
146
        else:
 
147
            return self.is_child_visible(drag_panel)
 
148
 
 
149
    def _get_drag_panel(self, index=0, wait=False):
 
150
        select_method = self.wait_select_single if wait else self.select_single
 
151
        try:
 
152
            return select_method(
 
153
                'QQuickItem', objectName='drag_panel{}'.format(index))
 
154
        except ValueError:
 
155
            # While we are dragging, each panel item is duplicated.
 
156
            # A ValueError will be raised in this scenario. We can take
 
157
            # any of the returned elements, as they have the same position.
 
158
            return self.select_many(
 
159
                'QQuickItem', objectName='drag_panel{}'.format(index))[0]
 
160
 
 
161
    def _drag_both_items_visible(self, from_index, to_index):
 
162
        from_drag_handler = self._get_drag_panel(from_index)
 
163
        to_drag_handler = self._get_drag_panel(to_index)
 
164
        start_x, start_y = input.get_center_point(from_drag_handler)
 
165
        stop_x, stop_y = input.get_center_point(to_drag_handler)
 
166
        self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
 
167
 
 
168
    def _drag_item_with_pagination(self, from_index, to_index):
 
169
        # the from_index might be invisible
 
170
        from_item = self._find_element('listitem{}'.format(from_index))
 
171
        from_item.swipe_into_view()
 
172
        from_drag_panel = self._get_drag_panel(from_index)
 
173
        containers = self._get_containers()
 
174
        if from_index < to_index:
 
175
            self._drag_downwards(from_drag_panel, to_index, containers)
 
176
        else:
 
177
            self._drag_upwards(from_drag_panel, to_index, containers)
 
178
 
 
179
    def _drag_downwards(self, drag_panel, to_index, containers):
 
180
        visible_bottom = _flickable._get_visible_container_bottom(
 
181
            containers)
 
182
        start_x, start_y = input.get_center_point(drag_panel)
 
183
 
 
184
        self.pointing_device.move(start_x, start_y)
 
185
        self.pointing_device.press()
 
186
        stop_x = start_x
 
187
        self.pointing_device.move(stop_x, visible_bottom)
 
188
        to_drag_panel = self._get_drag_panel(to_index, wait=True)
 
189
        while not self.is_child_visible(to_drag_panel):
 
190
            pass
 
191
        # stop moving
 
192
        h = to_drag_panel.height / 2
 
193
        self.pointing_device.move(stop_x, self.pointing_device.y - h)
 
194
        # move under the item with the to_index
 
195
        to_drag_panel = self._get_drag_panel(to_index - 1)
 
196
        stop_y = (
 
197
            to_drag_panel.globalRect.y +
 
198
            to_drag_panel.globalRect.height)
 
199
        self.pointing_device.move(stop_x, stop_y)
 
200
        self.pointing_device.release()
 
201
 
 
202
    def _drag_upwards(self, handler, to_index, containers):
 
203
        visible_top = _flickable._get_visible_container_top(
 
204
            containers)
 
205
        start_x, start_y = input.get_center_point(handler)
 
206
 
 
207
        self.pointing_device.move(start_x, start_y)
 
208
        self.pointing_device.press()
 
209
        stop_x = start_x
 
210
        # Header alters topMargin, therefore drag only till that edge
 
211
        self.pointing_device.move(stop_x, visible_top + self.topMargin)
 
212
        to_drag_panel = self._get_drag_panel(to_index, wait=True)
 
213
        while not self.is_child_visible(to_drag_panel):
 
214
            pass
 
215
        # stop moving
 
216
        h = to_drag_panel.height / 2
 
217
        self.pointing_device.move(stop_x, self.pointing_device.y + h)
 
218
        # move after the item with the to_index
 
219
        to_drag_panel = self._get_drag_panel(to_index)
 
220
        stop_y = (
 
221
            to_drag_panel.globalRect.y +
 
222
            to_drag_panel.globalRect.height)
 
223
        self.pointing_device.move(stop_x, stop_y)
 
224
        self.pointing_device.release()