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
=== modified file 'debian/control'
--- debian/control 2013-06-27 02:42:26 +0000
+++ debian/control 2013-06-28 21:01:28 +0000
@@ -105,6 +105,7 @@
105 libqt5test5,105 libqt5test5,
106 libqt5widgets5,106 libqt5widgets5,
107 python-autopilot (>= 1.3),107 python-autopilot (>= 1.3),
108 python-mock,
108 ubuntu-ui-toolkit-examples (>= ${source:Version}),109 ubuntu-ui-toolkit-examples (>= ${source:Version}),
109Description: Test package for Ubuntu UI Toolkit110Description: Test package for Ubuntu UI Toolkit
110 Autopilot tests for the ubuntu-ui-toolkit package111 Autopilot tests for the ubuntu-ui-toolkit package
111112
=== modified file 'tests/autopilot/UbuntuUiToolkit/emulators.py'
--- tests/autopilot/UbuntuUiToolkit/emulators.py 2013-06-27 02:31:46 +0000
+++ tests/autopilot/UbuntuUiToolkit/emulators.py 2013-06-28 21:01:28 +0000
@@ -14,9 +14,27 @@
14# You should have received a copy of the GNU Lesser General Public License14# You should have received a copy of the GNU Lesser General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17from autopilot import input, platform
17from autopilot.introspection import dbus18from autopilot.introspection import dbus
1819
1920
21_NO_TABS_ERROR = 'The MainView has no Tabs.'
22
23
24def get_pointing_device():
25 """Get the pointing device depending on the platform.
26
27 If the platform is `Desktop`, the pointing device will be a `Mouse`.
28 If not, the pointing device will be `Touch`.
29
30 """
31 if platform.model == 'Desktop':
32 input_device_class = input.Mouse
33 else:
34 input_device_class = input.Touch
35 return input.Pointer(input_device_class.create())
36
37
20class UbuntuUIToolkitEmulatorBase(dbus.CustomEmulatorBase):38class UbuntuUIToolkitEmulatorBase(dbus.CustomEmulatorBase):
21 """A base class for all the Ubuntu UI Toolkit emulators."""39 """A base class for all the Ubuntu UI Toolkit emulators."""
2240
@@ -24,6 +42,149 @@
24class MainView(UbuntuUIToolkitEmulatorBase):42class MainView(UbuntuUIToolkitEmulatorBase):
25 """MainView Autopilot emulator."""43 """MainView Autopilot emulator."""
2644
45 def __init__(self, *args):
46 super(MainView, self).__init__(*args)
47 self.pointing_device = get_pointing_device()
48
27 def get_header(self):49 def get_header(self):
28 """Get the Header emulator of the MainView."""50 """Return the Header emulator of the MainView."""
29 return self.select_single('Header')51 return self.select_single(Header)
52
53 def get_toolbar(self):
54 """Return the Toolbar emulator of the MainView."""
55 return self.select_single(Toolbar)
56
57 def open_toolbar(self):
58 """Open the toolbar if it's not already opened.
59
60 :return: The toolbar.
61
62 """
63 toolbar = self.get_toolbar()
64 if not toolbar.opened:
65 self._drag_to_open_toolbar()
66 toolbar.opened.wait_for(True)
67
68 return toolbar
69
70 def _drag_to_open_toolbar(self):
71 x, y, _, _ = self.globalRect
72 line_x = x + self.width * 0.50
73 start_y = y + self.height - 1
74 stop_y = y + self.height * 0.95
75
76 self.pointing_device.drag(line_x, start_y, line_x, stop_y)
77
78 def close_toolbar(self):
79 """Close the toolbar if it's opened."""
80 toolbar = self.get_toolbar()
81 if toolbar.opened:
82 self._drag_to_close_toolbar()
83 toolbar.opened.wait_for(False)
84
85 def _drag_to_close_toolbar(self):
86 x, y, _, _ = self.globalRect
87 line_x = x + self.width * 0.50
88 start_y = y + self.height * 0.95
89 stop_y = y + self.height - 1
90
91 self.pointing_device.drag(line_x, start_y, line_x, stop_y)
92
93 def get_tabs(self):
94 """Return the Tabs emulator of the MainView."""
95 tabs = self.select_single(Tabs)
96 assert tabs is not None, _NO_TABS_ERROR
97 return tabs
98
99 def switch_to_next_tab(self):
100 """Open the next tab.
101
102 :return: the newly opened tab.
103
104 """
105 self.get_header().switch_to_next_tab()
106 return self.get_tabs().get_current_tab()
107
108 def switch_to_tab_by_index(self, index):
109 """Open a tab.
110
111 :parameter index: The index of the tab to open.
112 :return: The newly opened tab.
113
114 """
115 tabs = self.get_tabs()
116 if index >= tabs.get_number_of_tabs():
117 raise IndexError('Tab index out of range.')
118 while not tabs.selectedTabIndex == index:
119 self.switch_to_next_tab()
120 return tabs.get_current_tab()
121
122 def switch_to_previous_tab(self):
123 """Open the previous tab.
124
125 :return: the newly opened tab.
126
127 """
128 tabs = self.get_tabs()
129 if tabs.selectedTabIndex == 0:
130 previous_tab_index = tabs.get_number_of_tabs() - 1
131 else:
132 previous_tab_index = tabs.selectedTabIndex - 1
133 return self.switch_to_tab_by_index(previous_tab_index)
134
135
136class Header(UbuntuUIToolkitEmulatorBase):
137 """Header Autopilot emulator."""
138
139 def __init__(self, *args):
140 super(Header, self).__init__(*args)
141 self.pointing_device = get_pointing_device()
142
143 def switch_to_next_tab(self):
144 """Open the next tab."""
145 tab_bar = self.select_single('NewTabBar')
146 assert tab_bar is not None, _NO_TABS_ERROR
147 tab_bar_x, tab_bar_y, _, _ = tab_bar.globalRect
148 line_y = tab_bar_y + tab_bar.height * 0.5
149 current_tab_x = tab_bar_x + tab_bar.width * 0.35
150 next_tab_x = tab_bar_x + tab_bar.width * 0.65
151
152 self.pointing_device.move(current_tab_x, line_y)
153 self.pointing_device.click()
154 self.pointing_device.move(next_tab_x, line_y)
155 self.pointing_device.click()
156
157
158class Toolbar(UbuntuUIToolkitEmulatorBase):
159 """Toolbar Autopilot emulator."""
160
161 def __init__(self, *args):
162 super(Toolbar, self).__init__(*args)
163 self.pointing_device = get_pointing_device()
164
165 def click_button(self, object_name):
166 """Click a button of the toolbar.
167
168 :param object_name: The QML objectName property of the button.
169
170 """
171 button = self._get_button(object_name)
172 not_found_error = 'Button with objectName "{0}" not found.'.format(
173 object_name)
174 assert button is not None, not_found_error
175 self.pointing_device.click_object(button)
176
177 def _get_button(self, object_name):
178 return self.select_single('ActionItem', objectName=object_name)
179
180
181class Tabs(UbuntuUIToolkitEmulatorBase):
182 """Tabs Autopilot emulator."""
183
184 def get_current_tab(self):
185 """Return the currently selected tab."""
186 return self.select_many('Tab')[self.selectedTabIndex]
187
188 def get_number_of_tabs(self):
189 """Return the number of tabs."""
190 return len(self.select_many('Tab'))
30191
=== modified file 'tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py'
--- tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py 2013-06-27 02:29:35 +0000
+++ tests/autopilot/UbuntuUiToolkit/tests/test_emulators.py 2013-06-28 21:01:28 +0000
@@ -14,6 +14,7 @@
14# You should have received a copy of the GNU Lesser General Public License14# You should have received a copy of the GNU Lesser General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.15# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
17import mock
1718
18from UbuntuUiToolkit import tests19from UbuntuUiToolkit import tests
19from UbuntuUiToolkit import emulators20from UbuntuUiToolkit import emulators
@@ -28,16 +29,195 @@
28MainView {29MainView {
29 width: units.gu(48)30 width: units.gu(48)
30 height: units.gu(60)31 height: units.gu(60)
31
32 Page {
33 title: "Test title"
34 }
35}32}
36""")33""")
3734
38 def test_main_view_custom_emulator(self):35 def test_main_view_custom_emulator(self):
39 self.assertIsInstance(self.main_view, emulators.MainView)36 self.assertIsInstance(self.main_view, emulators.MainView)
4037
41 def test_get_header(self):38 def test_get_header_without_header(self):
42 header = self.main_view.get_header()39 header = self.main_view.get_header()
40 self.assertFalse(header.visible)
41
42 def test_toolbar_custom_emulator(self):
43 toolbar = self.main_view.get_toolbar()
44 self.assertIsInstance(toolbar, emulators.Toolbar)
45 self.assertFalse(toolbar.opened)
46
47 def test_get_tabs_without_tabs(self):
48 error = self.assertRaises(
49 AssertionError, self.main_view.get_tabs)
50 self.assertEqual(
51 error.message, 'The MainView has no Tabs.')
52
53 def test_switch_to_next_tab_without_tabs(self):
54 header = self.main_view.get_header()
55 error = self.assertRaises(
56 AssertionError, header.switch_to_next_tab)
57 self.assertEqual(
58 error.message, 'The MainView has no Tabs.')
59
60
61class PageTestCase(tests.UbuntuUiToolkitTestCase):
62
63 test_qml = ("""
64import QtQuick 2.0
65import Ubuntu.Components 0.1
66
67MainView {
68 width: units.gu(48)
69 height: units.gu(60)
70
71 Page {
72 title: "Test title"
73 }
74}
75""")
76
77 def test_header_custom_emulator(self):
78 header = self.main_view.get_header()
79 self.assertIsInstance(header, emulators.Header)
80 self.assertTrue(header.visible)
43 self.assertEqual(header.title, "Test title")81 self.assertEqual(header.title, "Test title")
82
83
84class ToolbarTestCase(tests.UbuntuUiToolkitTestCase):
85
86 test_qml = ("""
87import QtQuick 2.0
88import Ubuntu.Components 0.1
89
90MainView {
91 width: units.gu(50)
92 height: units.gu(50)
93
94 Page {
95
96 Label {
97 id: "label"
98 objectName: "clicked_label"
99 anchors.centerIn: parent
100 text: "Button not clicked."
101 }
102
103 tools: ToolbarItems {
104 ToolbarButton {
105 objectName: "buttonName"
106 action: Action {
107 text: "buttonText"
108 onTriggered: label.text = "Button clicked."
109 }
110 }
111 locked: false
112 opened: false
113 }
114 }
115}
116""")
117
118 def test_open_toolbar(self):
119 toolbar = self.main_view.open_toolbar()
120 self.assertTrue(toolbar.opened)
121
122 def test_opened_toolbar_is_not_opened_again(self):
123 toolbar = self.main_view.open_toolbar()
124 with mock.patch.object(
125 self.main_view.pointing_device, 'drag') as mock_drag:
126 self.main_view.open_toolbar()
127
128 self.assertFalse(mock_drag.called)
129 self.assertTrue(toolbar.opened)
130
131 def test_close_toolbar(self):
132 toolbar = self.main_view.open_toolbar()
133 self.main_view.close_toolbar()
134 self.assertFalse(toolbar.opened)
135
136 def test_closed_toolbar_is_not_closed_again(self):
137 with mock.patch.object(
138 self.main_view.pointing_device, 'drag') as mock_drag:
139 self.main_view.close_toolbar()
140
141 self.assertFalse(mock_drag.called)
142 self.assertFalse(self.main_view.get_toolbar().opened)
143
144 def test_click_toolbar_button(self):
145 label = self.app.select_single('Label', objectName='clicked_label')
146 self.assertNotEqual(label.text, 'Button clicked.')
147 toolbar = self.main_view.open_toolbar()
148 toolbar.click_button('buttonName')
149 self.assertEqual(label.text, 'Button clicked.')
150
151 def test_click_unexisting_button(self):
152 toolbar = self.main_view.open_toolbar()
153 error = self.assertRaises(
154 AssertionError, toolbar.click_button, 'unexisting')
155 self.assertEqual(
156 error.message, 'Button with objectName "unexisting" not found.')
157
158
159class TabsTestCase(tests.UbuntuUiToolkitTestCase):
160
161 test_qml = ("""
162import QtQuick 2.0
163import Ubuntu.Components 0.1
164import Ubuntu.Components.ListItems 0.1 as ListItem
165
166MainView {
167 width: units.gu(48)
168 height: units.gu(60)
169
170 Tabs {
171 id: tabs
172 Tab {
173 objectName: "tab1"
174 title: "Test tab 1"
175 }
176 Tab {
177 objectName: "tab2"
178 title: "Test tab 2"
179 }
180 }
181}
182""")
183
184 def test_tabs_custom_emulator(self):
185 self.assertIsInstance(self.main_view.get_tabs(), emulators.Tabs)
186
187 def test_get_current_tab(self):
188 tabs = self.main_view.get_tabs()
189 self.assertEqual(tabs.get_current_tab().title, 'Test tab 1')
190
191 def test_switch_to_next_tab_from_header(self):
192 header = self.main_view.get_header()
193 header.switch_to_next_tab()
194 current_tab = self.main_view.get_tabs().get_current_tab()
195 self.assertEqual(current_tab.title, 'Test tab 2')
196
197 def test_switch_to_next_tab_from_main_view(self):
198 current_tab = self.main_view.switch_to_next_tab()
199 self.assertEqual(current_tab.title, 'Test tab 2')
200
201 def test_switch_to_tab_by_index(self):
202 current_tab = self.main_view.switch_to_tab_by_index(1)
203 self.assertEqual(current_tab.title, 'Test tab 2')
204 current_tab = self.main_view.switch_to_tab_by_index(0)
205 self.assertEqual(current_tab.title, 'Test tab 1')
206
207 def test_get_number_of_tabs(self):
208 tabs = self.main_view.get_tabs()
209 self.assertEqual(tabs.get_number_of_tabs(), 2)
210
211 def test_swith_to_tab_by_index_out_of_range(self):
212 error = self.assertRaises(
213 IndexError, self.main_view.switch_to_tab_by_index, 2)
214 self.assertEqual(error.message, 'Tab index out of range.')
215
216 def test_switch_to_previous_tab_from_first(self):
217 current_tab = self.main_view.switch_to_previous_tab()
218 self.assertEqual(current_tab.title, 'Test tab 2')
219
220 def test_switch_to_previous_tab_not_from_first(self):
221 self.main_view.switch_to_next_tab()
222 current_tab = self.main_view.switch_to_previous_tab()
223 self.assertEqual(current_tab.title, 'Test tab 1')

Subscribers

People subscribed via source and target branches

to status/vote changes: