1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
3
# Autopilot Functional Test Tool
4
# Copyright (C) 2012-2013 Canonical
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.
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.
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/>.
23
from tempfile import mktemp
24
from testtools import TestCase
25
from testtools.matchers import IsInstance, Equals
26
from unittest import SkipTest
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
35
class InputStackKeyboardBase(AutopilotTestCase):
38
('X11', dict(backend='X11')),
39
('UInput', dict(backend='UInput')),
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")
50
class InputStackKeyboardCreationTests(InputStackKeyboardBase):
53
def test_can_create_backend(self):
54
keyboard = Keyboard.create(self.backend)
55
self.assertThat(keyboard, IsInstance(Keyboard))
58
class InputStackKeyboardTypingTests(InputStackKeyboardBase):
60
scenarios = multiply_scenarios(
61
InputStackKeyboardBase.scenarios,
63
('lower_alpha', dict(input='abcdefghijklmnopqrstuvwxyz')),
64
('upper_alpha', dict(input='ABCDEFGHIJKLMNOPQRSTUVWXYZ')),
65
('numeric', dict(input='0123456789')),
66
('punctuation', dict(input='`~!@#$%^&*()_-+={}[]|\\:;"\'<>,.?/'))
70
def start_mock_app(self):
71
window_spec_file = mktemp(suffix='.json')
72
window_spec = { "Contents": "TextEdit" }
75
open(window_spec_file, 'w')
77
self.addCleanup(os.remove, window_spec_file)
79
return self.launch_test_application('window-mocker', window_spec_file)
81
def pick_app_launcher(self, app_path):
82
# force Qt app introspection:
83
from autopilot.introspection.qt import QtApplicationLauncher
84
return QtApplicationLauncher()
86
def test_some_text(self):
87
app_proxy = self.start_mock_app()
88
text_edit = app_proxy.select_single('QTextEdit')
90
# make sure the text edit has keyboard focus:
91
self.mouse.click_object(text_edit)
93
# create keyboard and type the text.
94
keyboard = Keyboard.create(self.backend)
95
keyboard.type(self.input, 0.01)
97
self.assertThat(text_edit.plainText, Eventually(Equals(self.input)))
100
class TouchTests(AutopilotTestCase):
103
super(TouchTests, self).setUp()
104
self.device = Touch.create()
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')
110
def start_mock_app(self):
111
window_spec_file = mktemp(suffix='.json')
112
window_spec = { "Contents": "MouseTest" }
115
open(window_spec_file, 'w')
117
self.addCleanup(os.remove, window_spec_file)
119
return self.launch_test_application('window-mocker', window_spec_file, app_type='qt')
122
x,y = get_center_point(self.widget)
125
self.assertThat(self.button_status.text, Eventually(Equals("Touch Release")))
127
def test_press_and_release(self):
128
x,y = get_center_point(self.widget)
129
self.device.press(x, y)
131
self.assertThat(self.button_status.text, Eventually(Equals("Touch Press")))
133
self.device.release()
134
self.assertThat(self.button_status.text, Eventually(Equals("Touch Release")))
137
class PointerWrapperTests(AutopilotTestCase):
139
def test_can_move_touch_wrapper(self):
140
device = Pointer(Touch.create())
143
self.assertThat(device._x, Equals(34))
144
self.assertThat(device._y, Equals(56))
147
class InputStackCleanupTests(TestCase):
149
def test_cleanup_called(self):
150
"""Derived classes cleanup method must be called when interface cleanup
155
class FakeKeyboard(Keyboard):
157
cleanup_called = False
160
def on_test_end(cls, test_instance):
161
FakeKeyboard.cleanup_called = True
164
class FakeTestCase(TestCase):
167
on_test_started(self)
171
FakeTestCase("test_foo").run()
173
self.assertThat(FakeKeyboard.cleanup_called, Equals(True))