~canonical-platform-qa/autopilot/overlay

« back to all changes in this revision

Viewing changes to autopilot/tests/unit/test_input.py

Fixed the pressed finger move on touch.
Moved the last position handling to the touch backend.
Moved the animated move from drag to move. Fixes: https://bugs.launchpad.net/bugs/1266601.

Approved by PS Jenkins bot, Christopher Lee, Richard Huddie.

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
3
# Autopilot Functional Test Tool
4
 
# Copyright (C) 2013, 2014 Canonical
 
4
# Copyright (C) 2013, 2014, 2015 Canonical
5
5
#
6
6
# This program is free software: you can redistribute it and/or modify
7
7
# it under the terms of the GNU General Public License as published by
661
661
        self.assertFalse(touch.pressed)
662
662
 
663
663
 
664
 
class UInputTouchTestCase(TestCase):
 
664
class UInputTouchBaseTestCase(TestCase):
 
665
 
 
666
    def setUp(self):
 
667
        super(UInputTouchBaseTestCase, self).setUp()
 
668
        # Mock the sleeps so we don't have to spend time actually sleeping.
 
669
        self.addCleanup(utilities.sleep.disable_mock)
 
670
        utilities.sleep.enable_mock()
 
671
 
 
672
    def get_touch_with_mocked_backend(self):
 
673
        touch = _uinput.Touch(device_class=Mock)
 
674
        touch._device.mock_add_spec(_uinput._UInputTouchDevice)
 
675
        return touch
 
676
 
 
677
 
 
678
class UInputTouchFingerCoordinatesTestCase(
 
679
        testscenarios.TestWithScenarios, UInputTouchBaseTestCase):
 
680
 
 
681
    TEST_X_DESTINATION = 10
 
682
    TEST_Y_DESTINATION = 10
 
683
 
 
684
    scenarios = [
 
685
        ('tap', {
 
686
            'method': 'tap',
 
687
            'args': (TEST_X_DESTINATION, TEST_Y_DESTINATION)
 
688
        }),
 
689
        ('press', {
 
690
            'method': 'press',
 
691
            'args': (TEST_X_DESTINATION, TEST_Y_DESTINATION)
 
692
        }),
 
693
        ('move', {
 
694
            'method': 'move',
 
695
            'args': (TEST_X_DESTINATION, TEST_Y_DESTINATION)
 
696
        }),
 
697
        ('drag', {
 
698
            'method': 'drag',
 
699
            'args': (0, 0, TEST_X_DESTINATION, TEST_Y_DESTINATION)
 
700
        })
 
701
    ]
 
702
 
 
703
    def call_scenario_method(self, object_, method, *args):
 
704
        getattr(object_, method)(*self.args)
 
705
 
 
706
    def test_method_must_update_finger_coordinates(self):
 
707
        touch = self.get_touch_with_mocked_backend()
 
708
 
 
709
        self.call_scenario_method(touch, self.method, *self.args)
 
710
 
 
711
        self.assertEqual(touch.x, self.TEST_X_DESTINATION)
 
712
        self.assertEqual(touch.y, self.TEST_Y_DESTINATION)
 
713
 
 
714
 
 
715
class UInputTouchTestCase(UInputTouchBaseTestCase):
665
716
    """Test UInput Touch helper for autopilot tests."""
666
717
 
667
 
    def setUp(self):
668
 
        super(UInputTouchTestCase, self).setUp()
669
 
        # Mock the sleeps so we don't have to spend time actually sleeping.
670
 
        self.addCleanup(utilities.sleep.disable_mock)
671
 
        utilities.sleep.enable_mock()
 
718
    def test_initial_coordinates_must_be_zero(self):
 
719
        touch = self.get_touch_with_mocked_backend()
672
720
 
673
 
    def get_touch_with_mocked_backend(self):
674
 
        touch = _uinput.Touch(device_class=Mock)
675
 
        touch._device.mock_add_spec(_uinput._UInputTouchDevice)
676
 
        return touch
 
721
        self.assertEqual(touch.x, 0)
 
722
        self.assertEqual(touch.y, 0)
677
723
 
678
724
    def test_tap_must_put_finger_down_then_sleep_and_then_put_finger_up(self):
679
725
        expected_calls = [
720
766
        touch.move(10, 10)
721
767
        self.assertEqual(expected_calls, touch._device.mock_calls)
722
768
 
723
 
    def test_drag_must_call_finger_down_move_and_up(self):
724
 
        expected_calls = [
725
 
            call.finger_down(0, 0),
726
 
            call.finger_move(10, 10),
727
 
            call.finger_up()
728
 
        ]
729
 
 
730
 
        touch = self.get_touch_with_mocked_backend()
731
 
        touch.drag(0, 0, 10, 10)
732
 
        self.assertEqual(expected_calls, touch._device.mock_calls)
733
 
 
734
 
    def test_drag_must_move_with_specified_rate(self):
735
 
        expected_calls = [
736
 
            call.finger_down(0, 0),
 
769
    def test_move_must_move_with_specified_rate(self):
 
770
        expected_calls = [
737
771
            call.finger_move(5, 5),
738
772
            call.finger_move(10, 10),
739
773
            call.finger_move(15, 15),
740
 
            call.finger_up()]
 
774
        ]
741
775
 
742
776
        touch = self.get_touch_with_mocked_backend()
743
 
        touch.drag(0, 0, 15, 15, rate=5)
 
777
        touch.move(15, 15, rate=5)
744
778
 
745
779
        self.assertEqual(
746
780
            expected_calls, touch._device.mock_calls)
747
781
 
748
 
    def test_drag_without_rate_must_use_default(self):
 
782
    def test_move_without_rate_must_use_default(self):
749
783
        expected_calls = [
750
 
            call.finger_down(0, 0),
751
784
            call.finger_move(10, 10),
752
785
            call.finger_move(20, 20),
753
 
            call.finger_up()]
 
786
        ]
754
787
 
755
788
        touch = self.get_touch_with_mocked_backend()
756
 
        touch.drag(0, 0, 20, 20)
 
789
        touch.move(20, 20)
757
790
 
758
791
        self.assertEqual(
759
792
            expected_calls, touch._device.mock_calls)
760
793
 
761
 
    def test_drag_to_same_place_must_not_move(self):
 
794
    def test_move_to_same_place_must_not_move(self):
 
795
        expected_calls = []
 
796
 
 
797
        touch = self.get_touch_with_mocked_backend()
 
798
        touch.move(0, 0)
 
799
        self.assertEqual(expected_calls, touch._device.mock_calls)
 
800
 
 
801
    def test_drag_must_call_finger_down_move_and_up(self):
762
802
        expected_calls = [
763
803
            call.finger_down(0, 0),
 
804
            call.finger_move(10, 10),
764
805
            call.finger_up()
765
806
        ]
766
807
 
767
808
        touch = self.get_touch_with_mocked_backend()
768
 
        touch.drag(0, 0, 0, 0)
 
809
        touch.drag(0, 0, 10, 10)
769
810
        self.assertEqual(expected_calls, touch._device.mock_calls)
770
811
 
771
812
    def test_tap_without_press_duration_must_sleep_default_time(self):
848
889
        self.assertFalse(finger2.pressed)
849
890
 
850
891
 
851
 
class DragUInputTouchTestCase(testscenarios.TestWithScenarios, TestCase):
 
892
class MoveWithAnimationUInputTouchTestCase(
 
893
        testscenarios.TestWithScenarios, TestCase):
852
894
 
853
895
    scenarios = [
854
 
        ('drag to top', dict(
 
896
        ('move to top', dict(
855
897
            start_x=50, start_y=50, stop_x=50, stop_y=30,
856
898
            expected_moves=[call.finger_move(50, 40),
857
899
                            call.finger_move(50, 30)])),
858
 
        ('drag to bottom', dict(
 
900
        ('move to bottom', dict(
859
901
            start_x=50, start_y=50, stop_x=50, stop_y=70,
860
902
            expected_moves=[call.finger_move(50, 60),
861
903
                            call.finger_move(50, 70)])),
862
 
        ('drag to left', dict(
 
904
        ('move to left', dict(
863
905
            start_x=50, start_y=50, stop_x=30, stop_y=50,
864
906
            expected_moves=[call.finger_move(40, 50),
865
907
                            call.finger_move(30, 50)])),
866
 
        ('drag to right', dict(
 
908
        ('move to right', dict(
867
909
            start_x=50, start_y=50, stop_x=70, stop_y=50,
868
910
            expected_moves=[call.finger_move(60, 50),
869
911
                            call.finger_move(70, 50)])),
870
912
 
871
 
        ('drag to top-left', dict(
 
913
        ('move to top-left', dict(
872
914
            start_x=50, start_y=50, stop_x=30, stop_y=30,
873
915
            expected_moves=[call.finger_move(40, 40),
874
916
                            call.finger_move(30, 30)])),
875
 
        ('drag to top-right', dict(
 
917
        ('move to top-right', dict(
876
918
            start_x=50, start_y=50, stop_x=70, stop_y=30,
877
919
            expected_moves=[call.finger_move(60, 40),
878
920
                            call.finger_move(70, 30)])),
879
 
        ('drag to bottom-left', dict(
 
921
        ('move to bottom-left', dict(
880
922
            start_x=50, start_y=50, stop_x=30, stop_y=70,
881
923
            expected_moves=[call.finger_move(40, 60),
882
924
                            call.finger_move(30, 70)])),
883
 
        ('drag to bottom-right', dict(
 
925
        ('move to bottom-right', dict(
884
926
            start_x=50, start_y=50, stop_x=70, stop_y=70,
885
927
            expected_moves=[call.finger_move(60, 60),
886
928
                            call.finger_move(70, 70)])),
887
929
 
888
 
        ('drag less than rate', dict(
 
930
        ('move less than rate', dict(
889
931
            start_x=50, start_y=50, stop_x=55, stop_y=55,
890
932
            expected_moves=[call.finger_move(55, 55)])),
891
933
 
892
 
        ('drag with last move less than rate', dict(
 
934
        ('move with last move less than rate', dict(
893
935
            start_x=50, start_y=50, stop_x=65, stop_y=65,
894
936
            expected_moves=[call.finger_move(60, 60),
895
937
                            call.finger_move(65, 65)])),
896
938
    ]
897
939
 
898
940
    def setUp(self):
899
 
        super(DragUInputTouchTestCase, self).setUp()
 
941
        super(MoveWithAnimationUInputTouchTestCase, self).setUp()
900
942
        # Mock the sleeps so we don't have to spend time actually sleeping.
901
943
        self.addCleanup(utilities.sleep.disable_mock)
902
944
        utilities.sleep.enable_mock()
910
952
    def test_drag_moves(self):
911
953
        touch = self.get_touch_with_mocked_backend()
912
954
 
913
 
        touch.drag(
914
 
            self.start_x, self.start_y, self.stop_x, self.stop_y)
 
955
        touch.press(self.start_x, self.start_y)
 
956
        touch.move(self.stop_x, self.stop_y)
915
957
 
916
 
        # We don't check the finger down and finger up. They are already
917
 
        # tested.
918
 
        expected_calls = [ANY] + self.expected_moves + [ANY]
 
958
        expected_calls = (
 
959
            [call.finger_down(self.start_x, self.start_y)] +
 
960
            self.expected_moves)
919
961
        self.assertEqual(
920
962
            expected_calls, touch._device.mock_calls)
921
963
 
929
971
        pointer = autopilot.input.Pointer(touch)
930
972
        return pointer
931
973
 
 
974
    def test_initial_coordinates_must_be_zero(self):
 
975
        pointer = self.get_pointer_with_touch_backend_with_mock_device()
 
976
 
 
977
        self.assertEqual(pointer.x, 0)
 
978
        self.assertEqual(pointer.y, 0)
 
979
 
 
980
    def test_drag_must_call_move_with_animation(self):
 
981
        test_rate = 2
 
982
        test_time_between_events = 1
 
983
        test_destination_x = 20
 
984
        test_destination_y = 20
 
985
 
 
986
        pointer = self.get_pointer_with_touch_backend_with_mock_device()
 
987
        with patch.object(pointer._device, 'move') as mock_move:
 
988
            pointer.drag(
 
989
                0, 0,
 
990
                test_destination_x, test_destination_y,
 
991
                rate=test_rate, time_between_events=test_time_between_events)
 
992
 
 
993
        mock_move.assert_called_once_with(
 
994
            test_destination_x, test_destination_y,
 
995
            animate=True,
 
996
            rate=test_rate, time_between_events=test_time_between_events)
 
997
 
932
998
    def test_drag_with_rate(self):
933
999
        pointer = self.get_pointer_with_touch_backend_with_mock_device()
934
1000
        with patch.object(pointer._device, 'drag') as mock_drag:
968
1034
 
969
1035
        mock_tap.assert_called_once_with(
970
1036
            0, 0, press_duration=10, time_between_events=0.1)
 
1037
 
 
1038
    def test_not_pressed_move_must_not_move_pointing_figer(self):
 
1039
        """Test for moving the finger when it is not pressed.
 
1040
 
 
1041
        The move method on the pointer class must update the finger coordinates
 
1042
        but it must not execute a move on the device.
 
1043
 
 
1044
        """
 
1045
        test_x_destination = 20
 
1046
        test_y_destination = 20
 
1047
        pointer = self.get_pointer_with_touch_backend_with_mock_device()
 
1048
 
 
1049
        pointer.move(10, 10)
 
1050
        pointer._device._device.pressed = False
 
1051
 
 
1052
        with patch.object(pointer._device._device, 'finger_move') as mock_move:
 
1053
            pointer.move(test_x_destination, test_y_destination)
 
1054
 
 
1055
        self.assertFalse(mock_move.called)
 
1056
        self.assertEqual(pointer.x, test_x_destination)
 
1057
        self.assertEqual(pointer.y, test_y_destination)
 
1058
 
 
1059
    def test_pressed_move_must_move_pointing_finger(self):
 
1060
        test_x_destination = 20
 
1061
        test_y_destination = 20
 
1062
 
 
1063
        pointer = self.get_pointer_with_touch_backend_with_mock_device()
 
1064
 
 
1065
        pointer.move(10, 10)
 
1066
        pointer._device._device.pressed = True
 
1067
 
 
1068
        with patch.object(pointer._device._device, 'finger_move') as mock_move:
 
1069
            pointer.move(test_x_destination, test_y_destination)
 
1070
 
 
1071
        mock_move.assert_called_once_with(20, 20)
 
1072
        self.assertEqual(pointer.x, test_x_destination)
 
1073
        self.assertEqual(pointer.y, test_y_destination)