2
2
# Copyright 2013 Canonical
4
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Lesser General Public License version 3, as published
6
# by the Free Software Foundation.
5
# under the terms of the GNU Lesser General Public License version 3, as
6
# published by the Free Software Foundation.
8
8
"""Tests for the Ubuntu HTML5 package """
12
import http.server as http
16
16
HTTP_SERVER_PORT = 8383
18
from testtools.matchers import Contains, Equals, GreaterThan
18
from testtools.matchers import Equals, GreaterThan
19
19
from autopilot.matchers import Eventually
20
20
from autopilot.testcase import AutopilotTestCase
21
21
from autopilot.input import Mouse, Touch, Pointer
25
25
# from autopilot.introspection.qt import QtIntrospectionTestMixin
28
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
29
BASE_PATH_FOR_SERVED_APPS = {'rss-reader': "%s/%s" % (os.path.dirname(os.path.realpath(__file__)), '../../../../0.1/examples/apps/rss-reader')}
28
class RequestHandler(http.BaseHTTPRequestHandler):
29
BASE_PATH_FOR_SERVED_APPS = {'rss-reader': "{}/{}".format(
30
os.path.dirname(os.path.realpath(__file__)),
31
'../../../../0.1/examples/apps/rss-reader')}
31
33
def get_served_filename(self, appname, filename):
32
34
if len(filename) == 0 or filename == '/':
33
35
filename = 'autopilot.html'
34
print os.path.join(self.BASE_PATH_FOR_SERVED_APPS[appname], filename)
35
return os.path.join(self.BASE_PATH_FOR_SERVED_APPS[appname], filename)
37
self.BASE_PATH_FOR_SERVED_APPS[appname],
37
40
def serve_file(self, filename):
73
81
class UbuntuHTML5TestCaseBase(AutopilotTestCase):
74
BROWSER_CONTAINER_PATH = "%s/%s" % (os.path.dirname(os.path.realpath(__file__)), '../../tools/qml/webview.qml')
75
INSTALLED_BROWSER_CONTAINER_PATH = '/usr/share/ubuntu-html5-ui-toolkit/tests/tools/qml/webview.qml'
76
arch = subprocess.check_output(
77
["dpkg-architecture", "-qDEB_HOST_MULTIARCH"]).strip()
78
BROWSER_QML_APP_LAUNCHER = "/usr/lib/" + arch + "/qt5/bin/qmlscene"
82
BROWSER_CONTAINER_PATH = "{}/{}".format(
83
os.path.dirname(os.path.realpath(__file__)),
84
'../../tools/qml/webview.qml')
85
INSTALLED_BROWSER_CONTAINER_PATH = \
86
'/usr/share/ubuntu-html5-ui-toolkit/tests/tools/qml/webview.qml'
87
BROWSER_QML_APP_LAUNCHER = "/usr/lib/{}/qt5/bin/qmlscene".format(
88
subprocess.check_output(
89
["dpkg-architecture", "-qDEB_HOST_MULTIARCH"]).strip().decode('utf-8'))
80
91
# TODO: fix version
81
LOCAL_HTML_EXAMPLES_PATH = os.path.abspath("%s/%s" % (os.path.dirname(os.path.realpath(__file__)), '../../../../tests'))
82
INSTALLED_HTML_EXAMPLES_PATH = '/usr/share/ubuntu-html5-ui-toolkit/tests/'
92
LOCAL_HTML_EXAMPLES_PATH = os.path.abspath(
94
os.path.dirname(os.path.realpath(__file__)),
96
INSTALLED_HTML_EXAMPLES_PATH = \
97
'/usr/share/ubuntu-html5-ui-toolkit/tests/'
84
99
APPS_SUBFOLDER_NAME = 'apps'
107
122
self.pointer = Pointer(Touch.create())
109
params = [self.BROWSER_QML_APP_LAUNCHER, self.get_browser_container_path()]
110
if (platform.model() <> 'Desktop'):
111
params.append('--desktop_file_hint=/usr/share/applications/unitywebappsqmllauncher.desktop')
124
params = [self.BROWSER_QML_APP_LAUNCHER,
125
self.get_browser_container_path()]
126
if (platform.model() != 'Desktop'):
128
'--desktop_file_hint=/usr/share/" \
129
+ "applications/unitywebappsqmllauncher.desktop')
113
131
self.app = self.launch_test_application(
117
135
self.webviewContainer = self.get_webviewContainer()
118
self.watcher = self.webviewContainer.watch_signal('resultUpdated(QString)')
136
self.watcher = self.webviewContainer.watch_signal(
137
'resultUpdated(QString)')
119
138
super(UbuntuHTML5TestCaseBase, self).setUp()
121
140
def tearDown(self):
144
163
def assert_url_eventually_loaded(self, url):
145
164
webview = self.get_webview()
146
self.assertThat(webview.loadProgress, Eventually(Equals(100)))
147
self.assertThat(webview.loading, Eventually(Equals(False)))
148
self.assertThat(webview.url, Eventually(Equals(url)))
166
webview.loadProgress,
167
Eventually(Equals(100)))
170
Eventually(Equals(False)))
173
Eventually(Equals(url)))
150
175
def click_dom_node_with_id(self, id):
151
176
webview = self.get_webviewContainer()
152
177
webview.slots.clickElementById(id)
153
self.assertThat(lambda: self.watcher.num_emissions, Eventually(Equals(1)))
179
lambda: self.watcher.num_emissions,
180
Eventually(Equals(1)))
155
182
def click_any_dom_node_by_selector(self, selector):
156
183
webview = self.get_webviewContainer()
157
184
webview.slots.clickAnyElementBySelector(selector)
158
self.assertThat(lambda: self.watcher.num_emissions, Eventually(Equals(1)))
186
lambda: self.watcher.num_emissions,
187
Eventually(Equals(1)))
160
189
def is_dom_node_visible(self, id):
161
190
webview = self.get_webviewContainer()
162
191
prev_emissions = self.watcher.num_emissions
163
192
webview.slots.isNodeWithIdVisible(id)
164
self.assertThat(lambda: self.watcher.num_emissions, Eventually(GreaterThan(prev_emissions)))
165
return json.loads(webview.get_signal_emissions('resultUpdated(QString)')[-1][0])['result']
194
lambda: self.watcher.num_emissions,
195
Eventually(GreaterThan(prev_emissions)))
197
webview.get_signal_emissions(
198
'resultUpdated(QString)')[-1][0])['result']
167
200
def eval_expression_in_page_unsafe(self, expr):
168
201
webview = self.get_webviewContainer()
169
202
prev_emissions = self.watcher.num_emissions
170
webview.slots.evalInPageUnsafe(expr)
171
self.assertThat(lambda: self.watcher.num_emissions, Eventually(GreaterThan(prev_emissions)))
172
return json.loads(webview.get_signal_emissions('resultUpdated(QString)')[-1][0])['result']
203
result = webview.slots.evalInPageUnsafe(expr)
205
lambda: self.watcher.num_emissions,
206
Eventually(GreaterThan(prev_emissions)))
207
return webview.get_signal_emissions('resultUpdated(QString)')[-1][0]
174
209
def get_dom_node_id_attribute(self, id, attribute):
175
210
webview = self.get_webviewContainer()
176
211
prev_emissions = self.watcher.num_emissions
177
212
webview.slots.getAttributeForElementWithId(id, attribute)
178
self.assertThat(lambda: self.watcher.num_emissions, Eventually(GreaterThan(prev_emissions)))
179
return json.loads(webview.get_signal_emissions('resultUpdated(QString)')[-1][0])['result']
214
lambda: self.watcher.num_emissions,
215
Eventually(GreaterThan(prev_emissions)))
217
webview.get_signal_emissions(
218
'resultUpdated(QString)')[-1][0])['result']
181
220
def get_address_bar_action_button(self):
182
221
addressbar = self.get_addressbar()
185
224
def browse_to_url(self, url):
187
226
addressbar = self.get_addressbar()
188
self.assertThat(addressbar.activeFocus, Eventually(Equals(True)))
228
addressbar.activeFocus,
229
Eventually(Equals(True)))
190
231
self.keyboard.type(url, 0.001)
192
233
self.pointer.click_object(self.get_webview())
235
# XXX: very bad, but wont fix
195
button = self.get_address_bar_action_button();
238
button = self.get_address_bar_action_button()
196
239
self.pointer.move_to_object(button)
197
240
self.pointer.press()
241
# XXX: very bad, but wont fix
199
243
self.pointer.release()
201
self.assert_url_eventually_loaded(url);
245
self.assert_url_eventually_loaded(url)
203
247
def browse_to_app(self, appname):
204
appfilepath = os.path.abspath(self.BASE_PATH +
248
appfilepath = os.path.abspath(
206
251
self.APPS_SUBFOLDER_NAME +