537
535
self.pointing_device.click_object(element)
539
537
def _swipe_element_into_view(self, objectName):
540
element = self.select_single(objectName=objectName)
541
x, y, width, height = self.globalRect
542
start_x = x + (width / 2)
543
start_y = y + (height / 2)
538
element = self._select_element(objectName)
545
while not self._is_element_fully_visible(objectName):
540
while not self._is_element_clickable(objectName):
547
541
if element.globalRect.y < self.globalRect.y:
548
stop_y = start_y + element.implicitHeight
550
stop_y = start_y - element.implicitHeight
552
if platform.model() == 'Desktop':
553
# The drag on the desktop is done two fast, so we are left at
554
# the bottom or at the top of the list, sometimes missing the
555
# element we are looking for.
556
# TODO: use the slow drag once it's implemented:
557
# https://bugs.launchpad.net/autopilot/+bug/1257055
558
# --elopio - 2014-01-09
559
self.pointing_device.move(start_x, start_y)
560
self.pointing_device.press()
561
self.pointing_device.move(stop_x, stop_y)
563
self.pointing_device.release()
565
self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
567
def _is_element_fully_visible(self, objectName):
542
self._show_more_elements_above()
544
self._show_more_elements_below()
546
def _select_element(self, object_name):
548
return self.select_single(objectName=object_name)
549
except dbus.StateNotFoundError:
550
# If the list is big, elements will only be created when we scroll
552
self._scroll_to_top()
553
while not self.atYEnd:
554
self._show_more_elements_below()
556
return self.select_single(objectName=object_name)
557
except dbus.StateNotFoundError:
559
raise ToolkitEmulatorException(
560
'List element with objectName "{}" not found.'.format(
563
@autopilot_logging.log_action(logger.info)
564
def _scroll_to_top(self):
565
x, y, width, height = self.globalRect
566
while not self.atYBeginning:
567
self._show_more_elements_above()
569
@autopilot_logging.log_action(logger.info)
570
def _show_more_elements_below(self):
572
raise ToolkitEmulatorException('There are no more elements below.')
574
self._show_more_elements('below')
576
@autopilot_logging.log_action(logger.info)
577
def _show_more_elements_above(self):
578
if self.atYBeginning:
579
raise ToolkitEmulatorException('There are no more elements above.')
581
self._show_more_elements('above')
583
def _show_more_elements(self, direction):
584
x, y, width, height = self.globalRect
585
start_x = stop_x = x + (width // 2)
586
# Start and stop just a little under the top of the list.
588
bottom = y + height - 5
589
if direction == 'below':
592
elif direction == 'above':
596
raise ToolkitEmulatorException(
597
'Invalid direction {}.'.format(direction))
598
self._slow_drag(start_x, stop_x, start_y, stop_y)
599
self.dragging.wait_for(False)
600
self.moving.wait_for(False)
602
def _slow_drag(self, start_x, stop_x, start_y, stop_y):
603
# If we drag too fast, we end up scrolling more than what we
604
# should, sometimes missing the element we are looking for.
605
self.pointing_device.drag(start_x, start_y, stop_x, stop_y, rate=5)
607
def _is_element_clickable(self, objectName):
608
"""Return True if the center of the element is visible."""
568
609
element = self.select_single(objectName=objectName)
569
return (element.globalRect.y >= self.globalRect.y and
570
element.globalRect.y + element.globalRect.height <=
571
self.globalRect.y + self.globalRect.height)
610
element_center = element.globalRect.y + element.globalRect.height // 2
611
return (element_center >= self.globalRect.y and
612
element_center <= self.globalRect.y + self.globalRect.height)
574
615
class Empty(UbuntuUIToolkitEmulatorBase):