~zsombi/ubuntu-ui-toolkit/mainview-bloop

« back to all changes in this revision

Viewing changes to tests/autopilot/ubuntuuitoolkit/emulators.py

  • Committer: Zsombor Egri
  • Date: 2014-03-10 10:42:42 UTC
  • mfrom: (933.1.35 ubuntu-ui-toolkit)
  • Revision ID: zsombor.egri@canonical.com-20140310104242-u4cnc90n8i4gee7z
trunk merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
import logging
18
 
import time
19
18
from distutils import version
20
19
 
21
20
import autopilot
373
372
        for button in buttons:
374
373
            if button.buttonIndex == index:
375
374
                return button
376
 
        else:
377
 
            raise ToolkitEmulatorException(
378
 
                'There is no tab button with index {0}.'.format(index))
 
375
        raise ToolkitEmulatorException(
 
376
            'There is no tab button with index {0}.'.format(index))
379
377
 
380
378
 
381
379
class ActionSelectionPopover(UbuntuUIToolkitEmulatorBase):
527
525
    def click_element(self, objectName):
528
526
        """Click an element from the list.
529
527
 
530
 
        It swipes the element into view if it's not fully visible.
 
528
        It swipes the element into view if it's center is not visible.
531
529
 
532
530
        :parameter objectName: The objectName property of the element to click.
533
531
 
537
535
        self.pointing_device.click_object(element)
538
536
 
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)
544
539
 
545
 
        while not self._is_element_fully_visible(objectName):
546
 
            stop_x = start_x
 
540
        while not self._is_element_clickable(objectName):
547
541
            if element.globalRect.y < self.globalRect.y:
548
 
                stop_y = start_y + element.implicitHeight
549
 
            else:
550
 
                stop_y = start_y - element.implicitHeight
551
 
 
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)
562
 
                time.sleep(0.3)
563
 
                self.pointing_device.release()
564
 
            else:
565
 
                self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
566
 
 
567
 
    def _is_element_fully_visible(self, objectName):
 
542
                self._show_more_elements_above()
 
543
            else:
 
544
                self._show_more_elements_below()
 
545
 
 
546
    def _select_element(self, object_name):
 
547
        try:
 
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
 
551
            # them into view.
 
552
            self._scroll_to_top()
 
553
            while not self.atYEnd:
 
554
                self._show_more_elements_below()
 
555
                try:
 
556
                    return self.select_single(objectName=object_name)
 
557
                except dbus.StateNotFoundError:
 
558
                    pass
 
559
            raise ToolkitEmulatorException(
 
560
                'List element with objectName "{}" not found.'.format(
 
561
                    object_name))
 
562
 
 
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()
 
568
 
 
569
    @autopilot_logging.log_action(logger.info)
 
570
    def _show_more_elements_below(self):
 
571
        if self.atYEnd:
 
572
            raise ToolkitEmulatorException('There are no more elements below.')
 
573
        else:
 
574
            self._show_more_elements('below')
 
575
 
 
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.')
 
580
        else:
 
581
            self._show_more_elements('above')
 
582
 
 
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.
 
587
        top = y + 5
 
588
        bottom = y + height - 5
 
589
        if direction == 'below':
 
590
            start_y = bottom
 
591
            stop_y = top
 
592
        elif direction == 'above':
 
593
            start_y = top
 
594
            stop_y = bottom
 
595
        else:
 
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)
 
601
 
 
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)
 
606
 
 
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)
572
613
 
573
614
 
574
615
class Empty(UbuntuUIToolkitEmulatorBase):
587
628
    @autopilot_logging.log_action(logger.info)
588
629
    def swipe_to_delete(self, direction='right'):
589
630
        """Swipe the item in a specific direction."""
590
 
        if (self.removable):
 
631
        if self.removable:
591
632
            self._drag_pointing_device_to_delete(direction)
592
633
            if self.confirmRemoval:
593
634
                self.waitingConfirmationForRemoval.wait_for(True)
599
640
 
600
641
    def _drag_pointing_device_to_delete(self, direction):
601
642
        x, y, w, h = self.globalRect
602
 
        tx = x + (w / 8)
603
 
        ty = y + (h / 2)
 
643
        tx = x + (w // 8)
 
644
        ty = y + (h // 2)
604
645
 
605
 
        if (direction == 'right'):
 
646
        if direction == 'right':
606
647
            self.pointing_device.drag(tx, ty, w, ty)
607
 
        elif (direction == 'left'):
 
648
        elif direction == 'left':
608
649
            self.pointing_device.drag(w - (w*0.1), ty, x, ty)
609
650
        else:
610
651
            raise ToolkitEmulatorException(
622
663
    @autopilot_logging.log_action(logger.info)
623
664
    def confirm_removal(self):
624
665
        """Comfirm item removal if this was already swiped."""
625
 
        if (self.waitingConfirmationForRemoval):
 
666
        if self.waitingConfirmationForRemoval:
626
667
            deleteButton = self._get_confirm_button()
627
668
            self.pointing_device.click_object(deleteButton)
628
669
            self._wait_until_deleted()