Merge lp:~nskaggs/ubuntu-calendar-app/refactor-tests into lp:ubuntu-calendar-app
- refactor-tests
- Merge into trunk
Proposed by
Nicholas Skaggs
Status: | Rejected |
---|---|
Rejected by: | Leo Arias |
Proposed branch: | lp:~nskaggs/ubuntu-calendar-app/refactor-tests |
Merge into: | lp:ubuntu-calendar-app |
Diff against target: |
397 lines (+157/-109) 4 files modified
tests/autopilot/calendar_app/emulators.py (+104/-13) tests/autopilot/calendar_app/tests/__init__.py (+32/-37) tests/autopilot/calendar_app/tests/test_calendar.py (+9/-13) tests/autopilot/calendar_app/tests/test_monthview.py (+12/-46) |
To merge this branch: | bzr merge lp:~nskaggs/ubuntu-calendar-app/refactor-tests |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Leo Arias (community) | Disapprove | ||
Review via email: mp+211812@code.launchpad.net |
Commit message
Refactor tests to use page object model
Description of the change
Refactor tests to use page object model
To post a comment you must log in.
- 214. By Nicholas Skaggs
-
wip monthview
- 215. By Nicholas Skaggs
-
incorporate fixes from stable
Unmerged revisions
- 215. By Nicholas Skaggs
-
incorporate fixes from stable
- 214. By Nicholas Skaggs
-
wip monthview
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'tests/autopilot/calendar_app/emulators.py' |
2 | --- tests/autopilot/calendar_app/emulators.py 2013-12-16 21:32:43 +0000 |
3 | +++ tests/autopilot/calendar_app/emulators.py 2014-03-20 03:45:39 +0000 |
4 | @@ -7,22 +7,44 @@ |
5 | |
6 | """Calendar app autopilot emulators.""" |
7 | |
8 | +import logging |
9 | from autopilot.introspection import dbus |
10 | +from autopilot import logging as autopilot_logging |
11 | + |
12 | from ubuntuuitoolkit import emulators as toolkit_emulators |
13 | |
14 | +logger = logging.getLogger(__name__) |
15 | + |
16 | + |
17 | +class CalendarEmulatorException(toolkit_emulators.ToolkitEmulatorException): |
18 | + """Exception raised when there is an error with the emulator.""" |
19 | |
20 | class MainView(toolkit_emulators.MainView): |
21 | |
22 | - """ |
23 | - An emulator class that makes it easy to interact with the calendar-app. |
24 | - """ |
25 | + @autopilot_logging.log_action(logger.info) |
26 | + def open_dayview(self): |
27 | + """Open the Dayview tab. |
28 | + |
29 | + :return: the Dayview page. |
30 | + |
31 | + """ |
32 | + self.switch_to_tab('dayTab') |
33 | + return self.wait_select_single(DayviewPage) |
34 | + |
35 | + @autopilot_logging.log_action(logger.info) |
36 | + def open_monthview(self): |
37 | + """Open the Monthview Tab. |
38 | + |
39 | + :return: the Monthview page. |
40 | + |
41 | + """ |
42 | + self.switch_to_tab('monthTab') |
43 | + return self.wait_select_single(MonthviewPage) |
44 | + |
45 | |
46 | def get_event_view(self): |
47 | return self.wait_select_single("EventView") |
48 | |
49 | - def get_month_view(self): |
50 | - return self.wait_select_single("MonthView") |
51 | - |
52 | def get_year_view(self): |
53 | return self.wait_select_single("YearView") |
54 | |
55 | @@ -78,6 +100,30 @@ |
56 | except dbus.StateNotFoundError: |
57 | return None |
58 | |
59 | + def get_year(self, component): |
60 | + return int(component.wait_select_single( |
61 | + "Label", objectName="yearLabel").text) |
62 | + |
63 | + def get_num_events(self): |
64 | + return len(self.select_many("EventBubble")) |
65 | + |
66 | +class Page(toolkit_emulators.UbuntuUIToolkitEmulatorBase): |
67 | + """Autopilot helper for Pages.""" |
68 | + |
69 | + def __init__(self, *args): |
70 | + super(Page, self).__init__(*args) |
71 | + # XXX we need a better way to keep reference to the main view. |
72 | + # --elopio - 2014-01-31 |
73 | + self.main_view = self.get_root_instance().select_single(MainView) |
74 | + |
75 | + def drag_page_up(self): |
76 | + """Drag the given page up.""" |
77 | + self._drag_page(direction='up') |
78 | + |
79 | + def drag_page_down(self): |
80 | + """Drag the given page down.""" |
81 | + self._drag_page(direction='down') |
82 | + |
83 | def swipe_view(self, direction, view, x_pad=0.15): |
84 | """Swipe the given view to left or right. |
85 | |
86 | @@ -96,10 +142,55 @@ |
87 | |
88 | self.pointing_device.drag(x_start, y_line, x_stop, y_line) |
89 | |
90 | - def get_year(self, component): |
91 | - return int(component.wait_select_single( |
92 | - "Label", objectName="yearLabel").text) |
93 | - |
94 | - def get_month_name(self, component): |
95 | - return component.wait_select_single( |
96 | - "Label", objectName="monthLabel").text |
97 | + def _drag_page(self, direction): |
98 | + """Function to drag the page up/down.""" |
99 | + self._wait_to_stop_moving() |
100 | + |
101 | + x, y, w, h = self.globalRect |
102 | + start_x = stop_x = x + (w / 2) |
103 | + start_y = y + (h / 2) |
104 | + |
105 | + if direction == "down": |
106 | + stop_y = start_y + h / 3 |
107 | + self.pointing_device.drag(start_x, start_y, stop_x, stop_y) |
108 | + else: |
109 | + stop_y = start_y - h / 3 |
110 | + self.pointing_device.drag(start_x, start_y, stop_x, stop_y) |
111 | + |
112 | + self._wait_to_stop_moving() |
113 | + |
114 | + def _wait_to_stop_moving(self): |
115 | + self.select_single( |
116 | + 'QQuickFlickable', |
117 | + objectName='animationContainer').moving.wait_for(False) |
118 | + |
119 | +class DayviewPage(Page): |
120 | + """Autopilot helper for the Dayview page.""" |
121 | + |
122 | + |
123 | +class MonthviewPage(Page): |
124 | + """Autopilot helper for the Monthview page.""" |
125 | + |
126 | + def __init__(self): |
127 | + self.view = self.wait_select_single("MonthView") |
128 | + |
129 | + def change_month(self, delta): |
130 | + sign = int(math.copysign(1, delta)) |
131 | + |
132 | + for _ in range(abs(delta)): |
133 | + before = month_view.currentMonth.datetime |
134 | + |
135 | + #prevent timing issues with swiping |
136 | + old_month = self.currentMonth.datetime |
137 | + self.swipe_view(sign, month_view) |
138 | + #self.assertThat(lambda: self.currentMonth.datetime, |
139 | + # Eventually(NotEquals(old_month))) |
140 | + |
141 | + after = before + relativedelta(months=sign) |
142 | + |
143 | + #self.assertThat(lambda: |
144 | + # self.currentMonth.datetime.month, |
145 | + # Eventually(Equals(after.month))) |
146 | + #self.assertThat(lambda: |
147 | + # self.currentMonth.datetime.year, |
148 | + # Eventually(Equals(after.year))) |
149 | |
150 | === modified file 'tests/autopilot/calendar_app/tests/__init__.py' |
151 | --- tests/autopilot/calendar_app/tests/__init__.py 2014-01-31 21:15:52 +0000 |
152 | +++ tests/autopilot/calendar_app/tests/__init__.py 2014-03-20 03:45:39 +0000 |
153 | @@ -22,6 +22,12 @@ |
154 | import shutil |
155 | import logging |
156 | |
157 | +try: |
158 | + from unittest import mock |
159 | +except ImportError: |
160 | + import mock |
161 | +import tempfile |
162 | + |
163 | from autopilot.input import Mouse, Touch, Pointer |
164 | from autopilot.platform import model |
165 | from autopilot.testcase import AutopilotTestCase |
166 | @@ -45,20 +51,16 @@ |
167 | |
168 | local_location = "../../calendar.qml" |
169 | installed_location = "/usr/share/calendar-app/calendar.qml" |
170 | - sqlite_dir = os.path.expanduser( |
171 | - "~/.local/share/com.ubuntu.calendar/Databases") |
172 | - backup_dir = sqlite_dir + ".backup" |
173 | |
174 | def setUp(self): |
175 | self.pointing_device = Pointer(self.input_device_class.create()) |
176 | + self.home_dir = self._patch_home() |
177 | super(CalendarTestCase, self).setUp() |
178 | - self.temp_move_sqlite_db() |
179 | - self.addCleanup(self.restore_sqlite_db) |
180 | |
181 | #turn off the OSK so it doesn't block screen elements |
182 | - if model() != 'Desktop': |
183 | - os.system("stop maliit-server") |
184 | - self.addCleanup(os.system, "start maliit-server") |
185 | + #if model() != 'Desktop': |
186 | + # os.system("stop maliit-server") |
187 | + # self.addCleanup(os.system, "start maliit-server") |
188 | |
189 | # Unset the current locale to ensure locale-specific data |
190 | # (day and month names, first day of the week, …) doesn’t get |
191 | @@ -73,6 +75,7 @@ |
192 | self.launch_test_click() |
193 | |
194 | def launch_test_local(self): |
195 | + logger.debug("Running via local installation") |
196 | self.app = self.launch_test_application( |
197 | "qmlscene", |
198 | self.local_location, |
199 | @@ -80,6 +83,7 @@ |
200 | emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase) |
201 | |
202 | def launch_test_installed(self): |
203 | + logger.debug("Running via installed debian package") |
204 | self.app = self.launch_test_application( |
205 | "qmlscene", |
206 | self.installed_location, |
207 | @@ -87,39 +91,30 @@ |
208 | emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase) |
209 | |
210 | def launch_test_click(self): |
211 | + logger.debug("Running via click package") |
212 | self.app = self.launch_click_package( |
213 | "com.ubuntu.calendar", |
214 | emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase) |
215 | |
216 | - def temp_move_sqlite_db(self): |
217 | - try: |
218 | - shutil.rmtree(self.backup_dir) |
219 | - except: |
220 | - pass |
221 | - else: |
222 | - logger.warning("Prexisting backup database found and removed") |
223 | - |
224 | - try: |
225 | - shutil.move(self.sqlite_dir, self.backup_dir) |
226 | - except: |
227 | - logger.warning("No current database found") |
228 | - else: |
229 | - logger.debug("Backed up database") |
230 | - |
231 | - def restore_sqlite_db(self): |
232 | - if os.path.exists(self.backup_dir): |
233 | - if os.path.exists(self.sqlite_dir): |
234 | - try: |
235 | - shutil.rmtree(self.sqlite_dir) |
236 | - except: |
237 | - logger.error("Failed to remove test database and restore" / |
238 | - "database") |
239 | - return |
240 | - try: |
241 | - shutil.move(self.backup_dir, self.sqlite_dir) |
242 | - except: |
243 | - logger.error("Failed to restore database") |
244 | + def _patch_home(self): |
245 | + #make a temp dir |
246 | + temp_dir = tempfile.mkdtemp() |
247 | + logger.debug("Created fake home directory " + temp_dir) |
248 | + self.addCleanup(shutil.rmtree, temp_dir) |
249 | + #if the Xauthority file is in home directory |
250 | + #make sure we copy it to temp home, otherwise do nothing |
251 | + xauth = os.path.expanduser(os.path.join('~', '.Xauthority')) |
252 | + if os.path.isfile(xauth): |
253 | + logger.debug("Copying .Xauthority to fake home " + temp_dir) |
254 | + shutil.copyfile( |
255 | + os.path.expanduser(os.path.join('~', '.Xauthority')), |
256 | + os.path.join(temp_dir, '.Xauthority')) |
257 | + patcher = mock.patch.dict('os.environ', {'HOME': temp_dir}) |
258 | + patcher.start() |
259 | + logger.debug("Patched home to fake home directory " + temp_dir) |
260 | + self.addCleanup(patcher.stop) |
261 | + return temp_dir |
262 | |
263 | @property |
264 | def main_view(self): |
265 | - return self.app.select_single(emulators.MainView) |
266 | + return self.app.wait_select_single(emulators.MainView) |
267 | |
268 | === modified file 'tests/autopilot/calendar_app/tests/test_calendar.py' |
269 | --- tests/autopilot/calendar_app/tests/test_calendar.py 2014-01-11 06:05:42 +0000 |
270 | +++ tests/autopilot/calendar_app/tests/test_calendar.py 2014-03-20 03:45:39 +0000 |
271 | @@ -11,7 +11,7 @@ |
272 | |
273 | from autopilot.matchers import Eventually |
274 | |
275 | -from testtools.matchers import Equals, Not, Is |
276 | +from testtools.matchers import Equals, Not, Is, NotEquals |
277 | |
278 | import time |
279 | |
280 | @@ -46,6 +46,10 @@ |
281 | |
282 | def test_new_event(self): |
283 | """test add new event """ |
284 | + #go to today |
285 | + self.main_view.switch_to_tab("dayTab") |
286 | + self.main_view.open_toolbar().click_button("todaybutton") |
287 | + num_events = self.main_view.get_num_events() |
288 | |
289 | #click on new event button |
290 | self.main_view.open_toolbar().click_button("neweventbutton") |
291 | @@ -68,7 +72,7 @@ |
292 | ok = picker.select_single("Button", objectName="TimePickerOKButton") |
293 | self.pointing_device.click_object(ok) |
294 | |
295 | - # Set the end time |
296 | + ## Set the end time |
297 | end_time_field = self.main_view.get_event_end_time_field() |
298 | self.pointing_device.click_object(end_time_field) |
299 | picker = self.main_view.get_time_picker() |
300 | @@ -83,18 +87,10 @@ |
301 | self.keyboard.type("My location") |
302 | self.assertThat(location_field.text, Eventually(Equals("My location"))) |
303 | |
304 | - #input people |
305 | - people_field = self.main_view.get_event_people_field() |
306 | - self.pointing_device.click_object(people_field) |
307 | - self.assertThat(people_field.activeFocus, Eventually(Equals(True))) |
308 | - self.keyboard.type("Me") |
309 | - self.assertThat(people_field.text, Eventually(Equals("Me"))) |
310 | - |
311 | #click save button |
312 | self.main_view.open_toolbar().click_button("eventSaveButton") |
313 | |
314 | #verify that the event has been created in timeline |
315 | - self.main_view.switch_to_tab("dayTab") |
316 | - self.assertThat(lambda: self.main_view.get_label_with_text( |
317 | - eventTitle, root=self.main_view.get_day_view()), |
318 | - Eventually(Not(Is(None)))) |
319 | + self.main_view.open_toolbar().click_button("todaybutton") |
320 | + self.assertThat(self.main_view.get_num_events, |
321 | + Eventually(NotEquals(num_events))) |
322 | |
323 | === modified file 'tests/autopilot/calendar_app/tests/test_monthview.py' |
324 | --- tests/autopilot/calendar_app/tests/test_monthview.py 2014-01-09 23:30:55 +0000 |
325 | +++ tests/autopilot/calendar_app/tests/test_monthview.py 2014-03-20 03:45:39 +0000 |
326 | @@ -24,59 +24,25 @@ |
327 | |
328 | def setUp(self): |
329 | super(TestMonthView, self).setUp() |
330 | - self.assertThat(self.main_view.visible, Eventually(Equals(True))) |
331 | - self.main_view.switch_to_tab("monthTab") |
332 | - |
333 | self.assertThat( |
334 | - self.main_view.get_month_view, Eventually(NotEquals(None))) |
335 | - |
336 | - self.month_view = self.main_view.get_month_view() |
337 | - |
338 | - def change_month(self, delta): |
339 | - month_view = self.main_view.get_month_view() |
340 | - sign = int(math.copysign(1, delta)) |
341 | - |
342 | - for _ in range(abs(delta)): |
343 | - before = month_view.currentMonth.datetime |
344 | - |
345 | - #prevent timing issues with swiping |
346 | - old_month = month_view.currentMonth.datetime |
347 | - self.main_view.swipe_view(sign, month_view) |
348 | - self.assertThat(lambda: month_view.currentMonth.datetime, |
349 | - Eventually(NotEquals(old_month))) |
350 | - |
351 | - after = before + relativedelta(months=sign) |
352 | - |
353 | - self.assertThat(lambda: |
354 | - self.month_view.currentMonth.datetime.month, |
355 | - Eventually(Equals(after.month))) |
356 | - self.assertThat(lambda: |
357 | - self.month_view.currentMonth.datetime.year, |
358 | - Eventually(Equals(after.year))) |
359 | - |
360 | - def _assert_today(self): |
361 | - today = datetime.today() |
362 | - self.assertThat(lambda: self.month_view.currentMonth.datetime.day, |
363 | + self.main_view.visible, Eventually(Equals(True))) |
364 | + self.page = self.main_view.open_monthview() |
365 | + |
366 | + def test_monthview_go_to_today_next_month(self): |
367 | + self.page.change_month(1) |
368 | + self.main_view.open_toolbar().click_button("todaybutton") |
369 | + self.assertThat(lambda: self.page.currentMonth.datetime.day, |
370 | Eventually(Equals(today.day))) |
371 | - self.assertThat(lambda: self.month_view.currentMonth.datetime.month, |
372 | + self.assertThat(lambda: self.page.currentMonth.datetime.month, |
373 | Eventually(Equals(today.month))) |
374 | - self.assertThat(lambda: self.month_view.currentMonth.datetime.year, |
375 | + self.assertThat(lambda: self.page.currentMonth.datetime.year, |
376 | Eventually(Equals(today.year))) |
377 | |
378 | - def _test_go_to_today(self, delta): |
379 | - self._assert_today() |
380 | - self.change_month(delta) |
381 | - self.main_view.open_toolbar().click_button("todaybutton") |
382 | - self._assert_today() |
383 | - |
384 | - def test_monthview_go_to_today_next_month(self): |
385 | - self._test_go_to_today(1) |
386 | - |
387 | def test_monthview_go_to_today_prev_month(self): |
388 | - self._test_go_to_today(-1) |
389 | + self.page.change_month(-1) |
390 | |
391 | def test_monthview_go_to_today_next_year(self): |
392 | - self._test_go_to_today(12) |
393 | + self.page.change_month(12) |
394 | |
395 | def test_monthview_go_to_today_prev_year(self): |
396 | - self._test_go_to_today(-12) |
397 | + self.page.change_month(-12) |
Balloons, this is already too old and the code has changed a lot. I'm rejecting it.