17
17
"""Ubuntu UI Toolkit autopilot tests."""
21
from tempfile import mktemp
24
from autopilot.input import Mouse, Touch, Pointer
22
from autopilot.input import Pointer
25
23
from autopilot.matchers import Eventually
26
from autopilot.platform import model
27
24
from testtools.matchers import Is, Not, Equals
28
from autopilot.testcase import AutopilotTestCase
30
from ubuntuuitoolkit import emulators
33
def get_module_include_path():
27
from ubuntuuitoolkit import base, emulators
30
_DESKTOP_FILE_CONTENTS = ("""[Desktop Entry]
39
def _write_test_desktop_file():
40
desktop_file_dir = get_local_desktop_file_directory()
41
if not os.path.exists(desktop_file_dir):
42
os.makedirs(desktop_file_dir)
43
desktop_file = tempfile.NamedTemporaryFile(
44
suffix='.desktop', dir=desktop_file_dir, delete=False)
45
desktop_file.write(_DESKTOP_FILE_CONTENTS)
47
return desktop_file.name
50
def get_local_desktop_file_directory():
51
return os.path.join(os.environ['HOME'], '.local', 'share', 'applications')
54
def _get_module_include_path():
55
return os.path.join(get_path_to_source_root(), 'modules')
58
def get_path_to_source_root():
34
59
return os.path.abspath(
36
os.path.dirname(__file__),
45
def get_input_device_scenarios():
46
"""Return the scenarios with the right input device for the platform."""
47
if model() == 'Desktop':
48
scenarios = [('with mouse', dict(input_device_class=Mouse))]
50
scenarios = [('with touch', dict(input_device_class=Touch))]
54
class UbuntuUiToolkitTestCase(AutopilotTestCase):
55
"""Common test case class for SDK tests."""
57
scenarios = get_input_device_scenarios()
60
self.pointing_device = Pointer(self.input_device_class.create())
61
super(UbuntuUiToolkitTestCase, self).setUp()
62
self.launch_test_qml()
64
def launch_test_qml(self):
65
# If the test class has defined a 'test_qml' class attribute then we
66
# write it to disk and launch it inside the Qml Viewer. If not, then we
67
# silently do nothing (presumably the test has something else planned).
68
arch = subprocess.check_output(
69
["dpkg-architecture", "-qDEB_HOST_MULTIARCH"]).strip()
70
if hasattr(self, 'test_qml') and isinstance(self.test_qml, basestring):
71
qml_path = mktemp(suffix='.qml')
72
open(qml_path, 'w').write(self.test_qml)
73
self.addCleanup(remove, qml_path)
75
self.app = self.launch_test_application(
76
"/usr/lib/" + arch + "/qt5/bin/qmlscene",
77
"-I" + get_module_include_path(),
79
emulator_base=emulators.UbuntuUIToolkitEmulatorBase,
82
if (hasattr(self, 'test_qml_file') and
83
isinstance(self.test_qml_file, basestring)):
84
qml_path = self.test_qml_file
85
self.app = self.launch_test_application(
86
"/usr/lib/" + arch + "/qt5/bin/qmlscene",
87
"-I" + get_module_include_path(),
89
emulator_base=emulators.UbuntuUIToolkitEmulatorBase,
93
self.main_view.visible, Eventually(Equals(True)))
61
os.path.dirname(__file__), '..', '..', '..', '..'))
64
class QMLStringAppTestCase(base.UbuntuUIToolkitAppTestCase):
65
"""Base test case for self tests that define the QML on an string."""
69
import Ubuntu.Components 0.1
78
super(QMLStringAppTestCase, self).setUp()
79
self.pointing_device = Pointer(self.input_device_class.create())
80
self.launch_application()
82
def launch_application(self):
83
arch = subprocess.check_output(
84
["dpkg-architecture", "-qDEB_HOST_MULTIARCH"]).strip()
85
qml_file_path = self._write_test_qml_file()
86
self.addCleanup(os.remove, qml_file_path)
87
desktop_file_path = _write_test_desktop_file()
88
self.addCleanup(os.remove, desktop_file_path)
89
self.app = self.launch_test_application(
90
'/usr/lib/' + arch + '/qt5/bin/qmlscene',
91
'-I' + _get_module_include_path(),
93
'--desktop_file_hint={0}'.format(desktop_file_path),
94
emulator_base=emulators.UbuntuUIToolkitEmulatorBase,
98
self.main_view.visible, Eventually(Equals(True)))
100
def _write_test_qml_file(self):
101
qml_file = tempfile.NamedTemporaryFile(suffix='.qml', delete=False)
102
qml_file.write(self.test_qml)
108
return self.app.select_single(emulators.MainView)
111
class QMLFileAppTestCase(base.UbuntuUIToolkitAppTestCase):
112
"""Base test case for self tests that launch a QML file."""
114
test_qml_file_path = '/path/to/file.qml'
115
desktop_file_path = None
118
super(QMLFileAppTestCase, self).setUp()
119
self.pointing_device = Pointer(self.input_device_class.create())
120
self.launch_application()
122
def launch_application(self):
123
arch = subprocess.check_output(
124
["dpkg-architecture", "-qDEB_HOST_MULTIARCH"]).strip()
125
desktop_file_path = self._get_desktop_file_path()
126
self.app = self.launch_test_application(
127
'/usr/lib/' + arch + '/qt5/bin/qmlscene',
128
"-I" + _get_module_include_path(),
129
self.test_qml_file_path,
130
'--desktop_file_hint={0}'.format(desktop_file_path),
131
emulator_base=emulators.UbuntuUIToolkitEmulatorBase,
135
self.main_view.visible, Eventually(Equals(True)))
137
def _get_desktop_file_path(self):
138
if self.desktop_file_path is None:
139
desktop_file_path = _write_test_desktop_file()
140
self.addCleanup(os.remove, desktop_file_path)
141
return desktop_file_path
143
self.desktop_file_path
96
146
def main_view(self):