~abreu-alexandre/ubuntu-html5-theme/rtm-backport-html5-ui-fixes

« back to all changes in this revision

Viewing changes to tests/autopilot/ubuntu_html5_ui_toolkit/tests/__init__.py

  • Committer: Alexandre Abreu
  • Date: 2014-12-05 21:41:18 UTC
  • Revision ID: alexandre.abreu@canonical.com-20141205214118-3czfr3ijmzb43tda
Fix oxide support

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
# Copyright 2013 Canonical
3
3
#
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.
7
7
 
8
8
"""Tests for the Ubuntu HTML5 package """
9
9
 
10
10
import os
11
11
import json
12
 
import BaseHTTPServer
 
12
import http.server as http
13
13
import threading
14
14
import subprocess
15
15
 
16
16
HTTP_SERVER_PORT = 8383
17
17
 
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
26
26
 
27
27
 
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')}
30
32
 
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)
 
36
        return os.path.join(
 
37
            self.BASE_PATH_FOR_SERVED_APPS[appname],
 
38
            filename)
36
39
 
37
40
    def serve_file(self, filename):
38
41
        import mimetypes
51
54
        if self.path.startswith('/rss-reader'):
52
55
            filename = self.path[len('/rss-reader'):]
53
56
            self.send_response(200)
54
 
            self.serve_file(self.get_served_filename('rss-reader', filename))
 
57
            self.serve_file(
 
58
                self.get_served_filename(
 
59
                    'rss-reader',
 
60
                    filename))
55
61
        else:
56
62
            self.send_error(404)
57
63
 
59
65
class UbuntuHTML5HTTPServer(threading.Thread):
60
66
    def __init__(self, port):
61
67
        super(UbuntuHTML5HTTPServer, self).__init__()
62
 
        self.server = BaseHTTPServer.HTTPServer(("", port), RequestHandler)
 
68
        self.server = http.BaseHTTPServer.HTTPServer(
 
69
            ("", port),
 
70
            RequestHandler)
63
71
        self.server.allow_reuse_address = True
64
72
 
65
73
    def run(self):
71
79
 
72
80
 
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'))
79
90
 
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(
 
93
        "{}/{}".format(
 
94
            os.path.dirname(os.path.realpath(__file__)),
 
95
            '../../../../tests'))
 
96
    INSTALLED_HTML_EXAMPLES_PATH = \
 
97
        '/usr/share/ubuntu-html5-ui-toolkit/tests/'
83
98
 
84
99
    APPS_SUBFOLDER_NAME = 'apps'
85
100
 
106
121
        else:
107
122
            self.pointer = Pointer(Touch.create())
108
123
 
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'):
 
127
            params.append(
 
128
                '--desktop_file_hint=/usr/share/" \
 
129
                + "applications/unitywebappsqmllauncher.desktop')
112
130
 
113
131
        self.app = self.launch_test_application(
114
132
            *params,
115
133
            app_type='qt')
116
134
 
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()
120
139
 
121
140
    def tearDown(self):
143
162
 
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)))
 
165
        self.assertThat(
 
166
            webview.loadProgress,
 
167
            Eventually(Equals(100)))
 
168
        self.assertThat(
 
169
            webview.loading,
 
170
            Eventually(Equals(False)))
 
171
        self.assertThat(
 
172
            webview.url,
 
173
            Eventually(Equals(url)))
149
174
 
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)))
 
178
        self.assertThat(
 
179
            lambda: self.watcher.num_emissions,
 
180
            Eventually(Equals(1)))
154
181
 
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)))
 
185
        self.assertThat(
 
186
            lambda: self.watcher.num_emissions,
 
187
            Eventually(Equals(1)))
159
188
 
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']
 
193
        self.assertThat(
 
194
            lambda: self.watcher.num_emissions,
 
195
            Eventually(GreaterThan(prev_emissions)))
 
196
        return json.loads(
 
197
            webview.get_signal_emissions(
 
198
                'resultUpdated(QString)')[-1][0])['result']
166
199
 
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)
 
204
        self.assertThat(
 
205
            lambda: self.watcher.num_emissions,
 
206
            Eventually(GreaterThan(prev_emissions)))
 
207
        return webview.get_signal_emissions('resultUpdated(QString)')[-1][0]
173
208
 
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']
 
213
        self.assertThat(
 
214
            lambda: self.watcher.num_emissions,
 
215
            Eventually(GreaterThan(prev_emissions)))
 
216
        return json.loads(
 
217
            webview.get_signal_emissions(
 
218
                'resultUpdated(QString)')[-1][0])['result']
180
219
 
181
220
    def get_address_bar_action_button(self):
182
221
        addressbar = self.get_addressbar()
185
224
    def browse_to_url(self, url):
186
225
        import time
187
226
        addressbar = self.get_addressbar()
188
 
        self.assertThat(addressbar.activeFocus, Eventually(Equals(True)))
 
227
        self.assertThat(
 
228
            addressbar.activeFocus,
 
229
            Eventually(Equals(True)))
189
230
 
190
231
        self.keyboard.type(url, 0.001)
191
232
 
192
233
        self.pointer.click_object(self.get_webview())
 
234
 
 
235
        # XXX: very bad, but wont fix
193
236
        time.sleep(1)
194
237
 
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
198
242
        time.sleep(1)
199
243
        self.pointer.release()
200
244
 
201
 
        self.assert_url_eventually_loaded(url);
 
245
        self.assert_url_eventually_loaded(url)
202
246
 
203
247
    def browse_to_app(self, appname):
204
 
        appfilepath = os.path.abspath(self.BASE_PATH +
 
248
        appfilepath = os.path.abspath(
 
249
            self.BASE_PATH +
205
250
            '/data/html/' +
206
251
            self.APPS_SUBFOLDER_NAME +
207
252
            '/' +
213
258
        self.browse_to_url(APP_HTML_PATH)
214
259
 
215
260
    def browse_to_test_html(self, html_filename):
216
 
        self.browse_to_url(self.create_file_url_from(os.path.abspath(self.BASE_PATH + '/data/html/' + html_filename)))
 
261
        self.browse_to_url(
 
262
            self.create_file_url_from(
 
263
                os.path.abspath(
 
264
                    '{}/data/html/{}'.format(
 
265
                        self.BASE_PATH,
 
266
                        html_filename))))
217
267
 
218
268
 
219
269
class UbuntuThemeWithHttpServerTestCaseBase(UbuntuHTML5TestCaseBase):