/usr/lib/python3/dist-packages/ubuntuuitoolkit/tests/__init__.py is in ubuntu-ui-toolkit-autopilot 0.1.46+14.04.20140408.1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Copyright (C) 2012, 2013 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Ubuntu UI Toolkit autopilot tests."""
import os
import tempfile
from autopilot.input import Pointer
from autopilot.matchers import Eventually
from testtools.matchers import Is, Not, Equals
from ubuntuuitoolkit import base, emulators, fixture_setup
_DESKTOP_FILE_CONTENTS = ("""[Desktop Entry]
Type=Application
Exec=Not important
Path=Not important
Name=Test app
Icon=Not important
""")
def _write_test_desktop_file():
desktop_file_dir = get_local_desktop_file_directory()
if not os.path.exists(desktop_file_dir):
os.makedirs(desktop_file_dir)
desktop_file = tempfile.NamedTemporaryFile(
suffix='.desktop', dir=desktop_file_dir, delete=False)
desktop_file.write(_DESKTOP_FILE_CONTENTS.encode('utf-8'))
desktop_file.close()
return desktop_file.name
def get_local_desktop_file_directory():
return os.path.join(os.environ['HOME'], '.local', 'share', 'applications')
def _get_module_include_path():
return os.path.join(get_path_to_source_root(), 'modules')
def get_path_to_source_root():
return os.path.abspath(
os.path.join(
os.path.dirname(__file__), '..', '..', '..', '..'))
class QMLStringAppTestCase(base.UbuntuUIToolkitAppTestCase):
"""Base test case for self tests that define the QML on an string."""
test_qml = ("""
import QtQuick 2.0
import Ubuntu.Components 0.1
MainView {
width: units.gu(48)
height: units.gu(60)
}
""")
def setUp(self):
super(QMLStringAppTestCase, self).setUp()
self.pointing_device = Pointer(self.input_device_class.create())
self.launch_application()
def launch_application(self):
fake_application = fixture_setup.FakeApplication(
qml_file_contents=self.test_qml)
self.useFixture(fake_application)
self.app = self.launch_test_application(
base.get_qmlscene_launch_command(),
'-I' + _get_module_include_path(),
fake_application.qml_file_path,
'--desktop_file_hint={0}'.format(
fake_application.desktop_file_path),
emulator_base=emulators.UbuntuUIToolkitEmulatorBase,
app_type='qt')
self.assertThat(
self.main_view.visible, Eventually(Equals(True)))
@property
def main_view(self):
return self.app.select_single(emulators.MainView)
class FlickDirection:
"""Enum for flick or scroll direction."""
UP, DOWN, LEFT, RIGHT = range(0, 4)
class QMLFileAppTestCase(base.UbuntuUIToolkitAppTestCase):
"""Base test case for self tests that launch a QML file."""
test_qml_file_path = '/path/to/file.qml'
desktop_file_path = None
def setUp(self):
super(QMLFileAppTestCase, self).setUp()
self.pointing_device = Pointer(self.input_device_class.create())
self.launch_application()
def launch_application(self):
desktop_file_path = self._get_desktop_file_path()
self.app = self.launch_test_application(
base.get_qmlscene_launch_command(),
"-I" + _get_module_include_path(),
self.test_qml_file_path,
'--desktop_file_hint={0}'.format(desktop_file_path),
emulator_base=emulators.UbuntuUIToolkitEmulatorBase,
app_type='qt')
self.assertThat(
self.main_view.visible, Eventually(Equals(True)))
def _get_desktop_file_path(self):
if self.desktop_file_path is None:
desktop_file_path = _write_test_desktop_file()
self.addCleanup(os.remove, desktop_file_path)
return desktop_file_path
else:
self.desktop_file_path
@property
def main_view(self):
return self.app.select_single(emulators.MainView)
def checkListItem(self, itemText):
item = self.getListItem(itemText)
self.assertThat(item, Not(Is(None)))
def getListItem(self, itemText):
# XXX We shouldn't access the elements by text, because that's likely
# to change often and might be translated. We should always use the
# objectName instead. --elopio - 2013-06-26216
return self.main_view.select_single("Standard", text=itemText)
def getWidgetLoaderAndListView(self):
contentLoader = self.main_view.select_single(
"QQuickLoader", objectName="contentLoader")
listView = self.main_view.select_single(
"QQuickListView", objectName="widgetList")
self.assertThat(listView, Not(Is(None)))
self.assertThat(listView.visible, Eventually(Equals(True)))
return (contentLoader, listView)
def loadItem(self, item):
self.selectItem(item)
contentLoader = self.main_view.select_single(
"QQuickLoader", objectName="contentLoader")
self.assertThat(contentLoader.progress, Eventually(Equals(1.0)))
loadedPage = self.getListItem(item)
self.assertThat(loadedPage, Not(Is(None)))
#loadedPage is not a page, it is the list item which goes in
#background when the item is selected, which changes the visible
#property of item in list itself to False. So followin check
#fails on Nexus 4. Commenting it for now.
#self.assertThat(loadedPage.visible, Eventually(Equals(True)))
def drag(self, itemText, itemTextTo):
item = self.getListItem(itemText)
itemTo = self.getListItem(itemTextTo)
self.pointing_device.move_to_object(item)
self.pointing_device.press()
self.pointing_device.move_to_object(itemTo)
self.pointing_device.release()
def reveal_item_by_flick(self, item, flickable, direction):
x1, y1, w1, h1 = item.globalRect
x2, y2, w2, h2 = flickable.globalRect
if direction is FlickDirection.UP:
while y1 + h1 > y2 + h2:
self.flick(flickable, direction)
x1, y1, w1, h1 = item.globalRect
elif direction is FlickDirection.DOWN:
while y1 < y2:
self.flick(flickable, direction)
x1, y1, w1, h1 = item.globalRect
def flick(self, flickable, direction, delta=40):
"""This funcito flicks the page from middle to the given direction."""
x, y, w, h = flickable.globalRect
if direction == FlickDirection.UP:
self.pointing_device.drag(x + w / 2, y + h / 2, x + w / 2,
y + h / 2 - delta)
flickable.flicking.wait_for(False)
elif direction == FlickDirection.DOWN:
self.pointing_device.drag(x + w / 2, y + h / 2, x + w / 2,
y + h / 2 + delta)
flickable.flicking.wait_for(False)
else:
raise ValueError("Invalid direction or not implementd yet")
def selectItem(self, itemText):
item = self.getListItem(itemText)
x1, y1, w1, h1 = item.globalRect
x2, y2, w2, h2 = self.main_view.globalRect
orientationHelper = self.getOrientationHelper()
rot = orientationHelper.rotation
scrollTo = h2 / 2 - (y1 - h2 - h1)
if rot == 0.0 and y1 > h2:
self.pointing_device.drag(w2 / 2, h2 / 2, w2 / 2, scrollTo)
self.assertThat(item.selected, Eventually(Equals(False)))
self.pointing_device.move_to_object(item)
self.pointing_device.click()
self.assertThat(item.selected, Eventually(Equals(True)))
def getOrientationHelper(self):
orientationHelper = self.main_view.select_many(
"OrientationHelper")[0]
self.assertThat(orientationHelper, Not(Is(None)))
return orientationHelper
def checkPageHeader(self, pageTitle):
orientationHelper = self.getOrientationHelper()
header = orientationHelper.select_many("Header", title=pageTitle)[0]
self.assertThat(header, Not(Is(None)))
self.assertThat(header.visible, Eventually(Equals(True)))
return header
def getObject(self, objectName):
obj = self.app.select_single(objectName=objectName)
self.assertThat(obj, Not(Is(None)))
return obj
def tap(self, objectName):
obj = self.getObject(objectName)
self.pointing_device.move_to_object(obj)
self.pointing_device.click()
def mousePress(self, objectName):
obj = self.getObject(objectName)
self.pointing_device.move_to_object(obj)
self.pointing_device.press()
def mouseRelease(self):
self.pointing_device.release()
def type_string(self, string):
self.keyboard.type(string)
def type_key(self, key):
self.keyboard.key(key)
def tap_clearButton(self, objectName):
textField = self.getObject(objectName)
self.assertIsNotNone(textField)
self.pointing_device.click_object(textField)
self.assertThat(textField.focus, Eventually(Equals(True)))
self.assertThat(textField.hasClearButton, Eventually(Equals(True)))
btn = textField.select_single("AbstractButton")
self.assertIsNotNone(btn)
self.assertThat(btn.visible, Eventually(Equals(True)))
self.pointing_device.click_object(btn)
self.assertThat(btn.pressed, Eventually(Equals(False)))
|