~ubuntu-branches/ubuntu/saucy/autopilot/saucy-proposed

« back to all changes in this revision

Viewing changes to autopilot/tests/functional/test_input_stack.py

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2013-06-07 13:33:46 UTC
  • mfrom: (57.1.1 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130607133346-42zvbl1h2k1v54ac
Tags: 1.3daily13.06.05-0ubuntu2
autopilot-touch only suggests python-ubuntu-platform-api for now.
It's not in distro and we need that requirement to be fulfilled to
have unity 7, 100 scopes and the touch stack to distro.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
#
 
3
# Autopilot Functional Test Tool
 
4
# Copyright (C) 2012-2013 Canonical
 
5
#
 
6
# This program is free software: you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation, either version 3 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
 
 
21
import json
 
22
import os
 
23
from tempfile import mktemp
 
24
from testtools import TestCase
 
25
from testtools.matchers import IsInstance, Equals
 
26
from unittest import SkipTest
 
27
 
 
28
from autopilot.testcase import AutopilotTestCase, multiply_scenarios
 
29
from autopilot.input import Keyboard, Pointer, Touch
 
30
from autopilot.input._common import get_center_point
 
31
from autopilot.matchers import Eventually
 
32
from autopilot.utilities import on_test_started
 
33
 
 
34
 
 
35
class InputStackKeyboardBase(AutopilotTestCase):
 
36
 
 
37
    scenarios = [
 
38
        ('X11', dict(backend='X11')),
 
39
        ('UInput', dict(backend='UInput')),
 
40
        ]
 
41
 
 
42
    def setUp(self):
 
43
        super(InputStackKeyboardBase, self).setUp()
 
44
        if self.backend == 'UInput' and not (
 
45
            os.access('/dev/autopilot-uinput', os.W_OK) or
 
46
            os.access('/dev/uinput', os.W_OK)):
 
47
            raise SkipTest("UInput backend currently requires write access to /dev/autopilot-uinput or /dev/uinput")
 
48
 
 
49
 
 
50
class InputStackKeyboardCreationTests(InputStackKeyboardBase):
 
51
 
 
52
 
 
53
    def test_can_create_backend(self):
 
54
        keyboard = Keyboard.create(self.backend)
 
55
        self.assertThat(keyboard, IsInstance(Keyboard))
 
56
 
 
57
 
 
58
class InputStackKeyboardTypingTests(InputStackKeyboardBase):
 
59
 
 
60
    scenarios = multiply_scenarios(
 
61
        InputStackKeyboardBase.scenarios,
 
62
        [
 
63
            ('lower_alpha', dict(input='abcdefghijklmnopqrstuvwxyz')),
 
64
            ('upper_alpha', dict(input='ABCDEFGHIJKLMNOPQRSTUVWXYZ')),
 
65
            ('numeric', dict(input='0123456789')),
 
66
            ('punctuation', dict(input='`~!@#$%^&*()_-+={}[]|\\:;"\'<>,.?/'))
 
67
        ]
 
68
        )
 
69
 
 
70
    def start_mock_app(self):
 
71
        window_spec_file = mktemp(suffix='.json')
 
72
        window_spec = { "Contents": "TextEdit" }
 
73
        json.dump(
 
74
            window_spec,
 
75
            open(window_spec_file, 'w')
 
76
            )
 
77
        self.addCleanup(os.remove, window_spec_file)
 
78
 
 
79
        return self.launch_test_application('window-mocker', window_spec_file)
 
80
 
 
81
    def pick_app_launcher(self, app_path):
 
82
        # force Qt app introspection:
 
83
        from autopilot.introspection.qt import QtApplicationLauncher
 
84
        return QtApplicationLauncher()
 
85
 
 
86
    def test_some_text(self):
 
87
        app_proxy = self.start_mock_app()
 
88
        text_edit = app_proxy.select_single('QTextEdit')
 
89
 
 
90
        # make sure the text edit has keyboard focus:
 
91
        self.mouse.click_object(text_edit)
 
92
 
 
93
        # create keyboard and type the text.
 
94
        keyboard = Keyboard.create(self.backend)
 
95
        keyboard.type(self.input, 0.01)
 
96
 
 
97
        self.assertThat(text_edit.plainText, Eventually(Equals(self.input)))
 
98
 
 
99
 
 
100
class TouchTests(AutopilotTestCase):
 
101
 
 
102
    def setUp(self):
 
103
        super(TouchTests, self).setUp()
 
104
        self.device = Touch.create()
 
105
 
 
106
        self.app = self.start_mock_app()
 
107
        self.widget = self.app.select_single('MouseTestWidget')
 
108
        self.button_status = self.app.select_single('QLabel', objectName='button_status')
 
109
 
 
110
    def start_mock_app(self):
 
111
        window_spec_file = mktemp(suffix='.json')
 
112
        window_spec = { "Contents": "MouseTest" }
 
113
        json.dump(
 
114
            window_spec,
 
115
            open(window_spec_file, 'w')
 
116
            )
 
117
        self.addCleanup(os.remove, window_spec_file)
 
118
 
 
119
        return self.launch_test_application('window-mocker', window_spec_file, app_type='qt')
 
120
 
 
121
    def test_tap(self):
 
122
        x,y = get_center_point(self.widget)
 
123
        self.device.tap(x,y)
 
124
 
 
125
        self.assertThat(self.button_status.text, Eventually(Equals("Touch Release")))
 
126
 
 
127
    def test_press_and_release(self):
 
128
        x,y = get_center_point(self.widget)
 
129
        self.device.press(x, y)
 
130
 
 
131
        self.assertThat(self.button_status.text, Eventually(Equals("Touch Press")))
 
132
 
 
133
        self.device.release()
 
134
        self.assertThat(self.button_status.text, Eventually(Equals("Touch Release")))
 
135
 
 
136
 
 
137
class PointerWrapperTests(AutopilotTestCase):
 
138
 
 
139
    def test_can_move_touch_wrapper(self):
 
140
        device = Pointer(Touch.create())
 
141
        device.move(34, 56)
 
142
 
 
143
        self.assertThat(device._x, Equals(34))
 
144
        self.assertThat(device._y, Equals(56))
 
145
 
 
146
 
 
147
class InputStackCleanupTests(TestCase):
 
148
 
 
149
    def test_cleanup_called(self):
 
150
        """Derived classes cleanup method must be called when interface cleanup
 
151
        method is called.
 
152
 
 
153
        """
 
154
 
 
155
        class FakeKeyboard(Keyboard):
 
156
 
 
157
            cleanup_called = False
 
158
 
 
159
            @classmethod
 
160
            def on_test_end(cls, test_instance):
 
161
                FakeKeyboard.cleanup_called = True
 
162
 
 
163
 
 
164
        class FakeTestCase(TestCase):
 
165
 
 
166
            def test_foo(self):
 
167
                on_test_started(self)
 
168
 
 
169
                kbd = FakeKeyboard()
 
170
 
 
171
        FakeTestCase("test_foo").run()
 
172
 
 
173
        self.assertThat(FakeKeyboard.cleanup_called, Equals(True))