Merge lp:~elopio/ubuntu-ui-toolkit/tabs_emulator into lp:ubuntu-ui-toolkit

Proposed by Leo Arias
Status: Superseded
Proposed branch: lp:~elopio/ubuntu-ui-toolkit/tabs_emulator
Merge into: lp:ubuntu-ui-toolkit
Diff against target: 409 lines (+350/-8)
3 files modified
debian/control (+1/-0)
tests/autopilot/UbuntuUiToolkit/emulators.py (+163/-2)
tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py (+186/-6)
To merge this branch: bzr merge lp:~elopio/ubuntu-ui-toolkit/tabs_emulator
Reviewer Review Type Date Requested Status
Ubuntu SDK team Pending
Review via email: mp+172152@code.launchpad.net

Commit message

Added the Tabs autopilot emulator.

To post a comment you must log in.
Revision history for this message
Nicholas Skaggs (nskaggs) wrote :

Looks nice Leo! Thanks again.

582. By Leo Arias

Moar tests and wait for the animation to finish.

583. By Leo Arias

Fixed pep8.

Unmerged revisions

583. By Leo Arias

Fixed pep8.

582. By Leo Arias

Moar tests and wait for the animation to finish.

581. By Leo Arias

Fixed pep8.

580. By Leo Arias

Added switch_to_previous_tab and throw an error when switching to an index out of range.

579. By Leo Arias

Added the switch_to_tab_by_index method.

578. By Leo Arias

Added a shortcut to switch a tab from the main view.

577. By Leo Arias

Removed the unused pointer from the tabs emulator.

576. By Leo Arias

Throw an error when trying to switch to a tab when there are no tabs.

575. By Leo Arias

Added a Tabs emulator and methods to get the current tab and switch to the next tab.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/control'
2--- debian/control 2013-06-27 02:42:26 +0000
3+++ debian/control 2013-06-28 21:01:28 +0000
4@@ -105,6 +105,7 @@
5 libqt5test5,
6 libqt5widgets5,
7 python-autopilot (>= 1.3),
8+ python-mock,
9 ubuntu-ui-toolkit-examples (>= ${source:Version}),
10 Description: Test package for Ubuntu UI Toolkit
11 Autopilot tests for the ubuntu-ui-toolkit package
12
13=== modified file 'tests/autopilot/UbuntuUiToolkit/emulators.py'
14--- tests/autopilot/UbuntuUiToolkit/emulators.py 2013-06-27 02:31:46 +0000
15+++ tests/autopilot/UbuntuUiToolkit/emulators.py 2013-06-28 21:01:28 +0000
16@@ -14,9 +14,27 @@
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20+from autopilot import input, platform
21 from autopilot.introspection import dbus
22
23
24+_NO_TABS_ERROR = 'The MainView has no Tabs.'
25+
26+
27+def get_pointing_device():
28+ """Get the pointing device depending on the platform.
29+
30+ If the platform is `Desktop`, the pointing device will be a `Mouse`.
31+ If not, the pointing device will be `Touch`.
32+
33+ """
34+ if platform.model == 'Desktop':
35+ input_device_class = input.Mouse
36+ else:
37+ input_device_class = input.Touch
38+ return input.Pointer(input_device_class.create())
39+
40+
41 class UbuntuUIToolkitEmulatorBase(dbus.CustomEmulatorBase):
42 """A base class for all the Ubuntu UI Toolkit emulators."""
43
44@@ -24,6 +42,149 @@
45 class MainView(UbuntuUIToolkitEmulatorBase):
46 """MainView Autopilot emulator."""
47
48+ def __init__(self, *args):
49+ super(MainView, self).__init__(*args)
50+ self.pointing_device = get_pointing_device()
51+
52 def get_header(self):
53- """Get the Header emulator of the MainView."""
54- return self.select_single('Header')
55+ """Return the Header emulator of the MainView."""
56+ return self.select_single(Header)
57+
58+ def get_toolbar(self):
59+ """Return the Toolbar emulator of the MainView."""
60+ return self.select_single(Toolbar)
61+
62+ def open_toolbar(self):
63+ """Open the toolbar if it's not already opened.
64+
65+ :return: The toolbar.
66+
67+ """
68+ toolbar = self.get_toolbar()
69+ if not toolbar.opened:
70+ self._drag_to_open_toolbar()
71+ toolbar.opened.wait_for(True)
72+
73+ return toolbar
74+
75+ def _drag_to_open_toolbar(self):
76+ x, y, _, _ = self.globalRect
77+ line_x = x + self.width * 0.50
78+ start_y = y + self.height - 1
79+ stop_y = y + self.height * 0.95
80+
81+ self.pointing_device.drag(line_x, start_y, line_x, stop_y)
82+
83+ def close_toolbar(self):
84+ """Close the toolbar if it's opened."""
85+ toolbar = self.get_toolbar()
86+ if toolbar.opened:
87+ self._drag_to_close_toolbar()
88+ toolbar.opened.wait_for(False)
89+
90+ def _drag_to_close_toolbar(self):
91+ x, y, _, _ = self.globalRect
92+ line_x = x + self.width * 0.50
93+ start_y = y + self.height * 0.95
94+ stop_y = y + self.height - 1
95+
96+ self.pointing_device.drag(line_x, start_y, line_x, stop_y)
97+
98+ def get_tabs(self):
99+ """Return the Tabs emulator of the MainView."""
100+ tabs = self.select_single(Tabs)
101+ assert tabs is not None, _NO_TABS_ERROR
102+ return tabs
103+
104+ def switch_to_next_tab(self):
105+ """Open the next tab.
106+
107+ :return: the newly opened tab.
108+
109+ """
110+ self.get_header().switch_to_next_tab()
111+ return self.get_tabs().get_current_tab()
112+
113+ def switch_to_tab_by_index(self, index):
114+ """Open a tab.
115+
116+ :parameter index: The index of the tab to open.
117+ :return: The newly opened tab.
118+
119+ """
120+ tabs = self.get_tabs()
121+ if index >= tabs.get_number_of_tabs():
122+ raise IndexError('Tab index out of range.')
123+ while not tabs.selectedTabIndex == index:
124+ self.switch_to_next_tab()
125+ return tabs.get_current_tab()
126+
127+ def switch_to_previous_tab(self):
128+ """Open the previous tab.
129+
130+ :return: the newly opened tab.
131+
132+ """
133+ tabs = self.get_tabs()
134+ if tabs.selectedTabIndex == 0:
135+ previous_tab_index = tabs.get_number_of_tabs() - 1
136+ else:
137+ previous_tab_index = tabs.selectedTabIndex - 1
138+ return self.switch_to_tab_by_index(previous_tab_index)
139+
140+
141+class Header(UbuntuUIToolkitEmulatorBase):
142+ """Header Autopilot emulator."""
143+
144+ def __init__(self, *args):
145+ super(Header, self).__init__(*args)
146+ self.pointing_device = get_pointing_device()
147+
148+ def switch_to_next_tab(self):
149+ """Open the next tab."""
150+ tab_bar = self.select_single('NewTabBar')
151+ assert tab_bar is not None, _NO_TABS_ERROR
152+ tab_bar_x, tab_bar_y, _, _ = tab_bar.globalRect
153+ line_y = tab_bar_y + tab_bar.height * 0.5
154+ current_tab_x = tab_bar_x + tab_bar.width * 0.35
155+ next_tab_x = tab_bar_x + tab_bar.width * 0.65
156+
157+ self.pointing_device.move(current_tab_x, line_y)
158+ self.pointing_device.click()
159+ self.pointing_device.move(next_tab_x, line_y)
160+ self.pointing_device.click()
161+
162+
163+class Toolbar(UbuntuUIToolkitEmulatorBase):
164+ """Toolbar Autopilot emulator."""
165+
166+ def __init__(self, *args):
167+ super(Toolbar, self).__init__(*args)
168+ self.pointing_device = get_pointing_device()
169+
170+ def click_button(self, object_name):
171+ """Click a button of the toolbar.
172+
173+ :param object_name: The QML objectName property of the button.
174+
175+ """
176+ button = self._get_button(object_name)
177+ not_found_error = 'Button with objectName "{0}" not found.'.format(
178+ object_name)
179+ assert button is not None, not_found_error
180+ self.pointing_device.click_object(button)
181+
182+ def _get_button(self, object_name):
183+ return self.select_single('ActionItem', objectName=object_name)
184+
185+
186+class Tabs(UbuntuUIToolkitEmulatorBase):
187+ """Tabs Autopilot emulator."""
188+
189+ def get_current_tab(self):
190+ """Return the currently selected tab."""
191+ return self.select_many('Tab')[self.selectedTabIndex]
192+
193+ def get_number_of_tabs(self):
194+ """Return the number of tabs."""
195+ return len(self.select_many('Tab'))
196
197=== modified file 'tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py'
198--- tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py 2013-06-27 02:29:35 +0000
199+++ tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py 2013-06-28 21:01:28 +0000
200@@ -14,6 +14,7 @@
201 # You should have received a copy of the GNU Lesser General Public License
202 # along with this program. If not, see <http://www.gnu.org/licenses/>.
203
204+import mock
205
206 from UbuntuUiToolkit import tests
207 from UbuntuUiToolkit import emulators
208@@ -28,16 +29,195 @@
209 MainView {
210 width: units.gu(48)
211 height: units.gu(60)
212-
213- Page {
214- title: "Test title"
215- }
216 }
217 """)
218
219 def test_main_view_custom_emulator(self):
220 self.assertIsInstance(self.main_view, emulators.MainView)
221
222- def test_get_header(self):
223- header = self.main_view.get_header()
224+ def test_get_header_without_header(self):
225+ header = self.main_view.get_header()
226+ self.assertFalse(header.visible)
227+
228+ def test_toolbar_custom_emulator(self):
229+ toolbar = self.main_view.get_toolbar()
230+ self.assertIsInstance(toolbar, emulators.Toolbar)
231+ self.assertFalse(toolbar.opened)
232+
233+ def test_get_tabs_without_tabs(self):
234+ error = self.assertRaises(
235+ AssertionError, self.main_view.get_tabs)
236+ self.assertEqual(
237+ error.message, 'The MainView has no Tabs.')
238+
239+ def test_switch_to_next_tab_without_tabs(self):
240+ header = self.main_view.get_header()
241+ error = self.assertRaises(
242+ AssertionError, header.switch_to_next_tab)
243+ self.assertEqual(
244+ error.message, 'The MainView has no Tabs.')
245+
246+
247+class PageTestCase(tests.UbuntuUiToolkitTestCase):
248+
249+ test_qml = ("""
250+import QtQuick 2.0
251+import Ubuntu.Components 0.1
252+
253+MainView {
254+ width: units.gu(48)
255+ height: units.gu(60)
256+
257+ Page {
258+ title: "Test title"
259+ }
260+}
261+""")
262+
263+ def test_header_custom_emulator(self):
264+ header = self.main_view.get_header()
265+ self.assertIsInstance(header, emulators.Header)
266+ self.assertTrue(header.visible)
267 self.assertEqual(header.title, "Test title")
268+
269+
270+class ToolbarTestCase(tests.UbuntuUiToolkitTestCase):
271+
272+ test_qml = ("""
273+import QtQuick 2.0
274+import Ubuntu.Components 0.1
275+
276+MainView {
277+ width: units.gu(50)
278+ height: units.gu(50)
279+
280+ Page {
281+
282+ Label {
283+ id: "label"
284+ objectName: "clicked_label"
285+ anchors.centerIn: parent
286+ text: "Button not clicked."
287+ }
288+
289+ tools: ToolbarItems {
290+ ToolbarButton {
291+ objectName: "buttonName"
292+ action: Action {
293+ text: "buttonText"
294+ onTriggered: label.text = "Button clicked."
295+ }
296+ }
297+ locked: false
298+ opened: false
299+ }
300+ }
301+}
302+""")
303+
304+ def test_open_toolbar(self):
305+ toolbar = self.main_view.open_toolbar()
306+ self.assertTrue(toolbar.opened)
307+
308+ def test_opened_toolbar_is_not_opened_again(self):
309+ toolbar = self.main_view.open_toolbar()
310+ with mock.patch.object(
311+ self.main_view.pointing_device, 'drag') as mock_drag:
312+ self.main_view.open_toolbar()
313+
314+ self.assertFalse(mock_drag.called)
315+ self.assertTrue(toolbar.opened)
316+
317+ def test_close_toolbar(self):
318+ toolbar = self.main_view.open_toolbar()
319+ self.main_view.close_toolbar()
320+ self.assertFalse(toolbar.opened)
321+
322+ def test_closed_toolbar_is_not_closed_again(self):
323+ with mock.patch.object(
324+ self.main_view.pointing_device, 'drag') as mock_drag:
325+ self.main_view.close_toolbar()
326+
327+ self.assertFalse(mock_drag.called)
328+ self.assertFalse(self.main_view.get_toolbar().opened)
329+
330+ def test_click_toolbar_button(self):
331+ label = self.app.select_single('Label', objectName='clicked_label')
332+ self.assertNotEqual(label.text, 'Button clicked.')
333+ toolbar = self.main_view.open_toolbar()
334+ toolbar.click_button('buttonName')
335+ self.assertEqual(label.text, 'Button clicked.')
336+
337+ def test_click_unexisting_button(self):
338+ toolbar = self.main_view.open_toolbar()
339+ error = self.assertRaises(
340+ AssertionError, toolbar.click_button, 'unexisting')
341+ self.assertEqual(
342+ error.message, 'Button with objectName "unexisting" not found.')
343+
344+
345+class TabsTestCase(tests.UbuntuUiToolkitTestCase):
346+
347+ test_qml = ("""
348+import QtQuick 2.0
349+import Ubuntu.Components 0.1
350+import Ubuntu.Components.ListItems 0.1 as ListItem
351+
352+MainView {
353+ width: units.gu(48)
354+ height: units.gu(60)
355+
356+ Tabs {
357+ id: tabs
358+ Tab {
359+ objectName: "tab1"
360+ title: "Test tab 1"
361+ }
362+ Tab {
363+ objectName: "tab2"
364+ title: "Test tab 2"
365+ }
366+ }
367+}
368+""")
369+
370+ def test_tabs_custom_emulator(self):
371+ self.assertIsInstance(self.main_view.get_tabs(), emulators.Tabs)
372+
373+ def test_get_current_tab(self):
374+ tabs = self.main_view.get_tabs()
375+ self.assertEqual(tabs.get_current_tab().title, 'Test tab 1')
376+
377+ def test_switch_to_next_tab_from_header(self):
378+ header = self.main_view.get_header()
379+ header.switch_to_next_tab()
380+ current_tab = self.main_view.get_tabs().get_current_tab()
381+ self.assertEqual(current_tab.title, 'Test tab 2')
382+
383+ def test_switch_to_next_tab_from_main_view(self):
384+ current_tab = self.main_view.switch_to_next_tab()
385+ self.assertEqual(current_tab.title, 'Test tab 2')
386+
387+ def test_switch_to_tab_by_index(self):
388+ current_tab = self.main_view.switch_to_tab_by_index(1)
389+ self.assertEqual(current_tab.title, 'Test tab 2')
390+ current_tab = self.main_view.switch_to_tab_by_index(0)
391+ self.assertEqual(current_tab.title, 'Test tab 1')
392+
393+ def test_get_number_of_tabs(self):
394+ tabs = self.main_view.get_tabs()
395+ self.assertEqual(tabs.get_number_of_tabs(), 2)
396+
397+ def test_swith_to_tab_by_index_out_of_range(self):
398+ error = self.assertRaises(
399+ IndexError, self.main_view.switch_to_tab_by_index, 2)
400+ self.assertEqual(error.message, 'Tab index out of range.')
401+
402+ def test_switch_to_previous_tab_from_first(self):
403+ current_tab = self.main_view.switch_to_previous_tab()
404+ self.assertEqual(current_tab.title, 'Test tab 2')
405+
406+ def test_switch_to_previous_tab_not_from_first(self):
407+ self.main_view.switch_to_next_tab()
408+ current_tab = self.main_view.switch_to_previous_tab()
409+ self.assertEqual(current_tab.title, 'Test tab 1')

Subscribers

People subscribed via source and target branches

to status/vote changes: