Merge lp:~nskaggs/ubuntu-calendar-app/refactor-tests into lp:ubuntu-calendar-app

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
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

Revision history for this message
Leo Arias (elopio) wrote :

Balloons, this is already too old and the code has changed a lot. I'm rejecting it.

review: Disapprove

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
=== modified file 'tests/autopilot/calendar_app/emulators.py'
--- tests/autopilot/calendar_app/emulators.py 2013-12-16 21:32:43 +0000
+++ tests/autopilot/calendar_app/emulators.py 2014-03-20 03:45:39 +0000
@@ -7,22 +7,44 @@
77
8"""Calendar app autopilot emulators."""8"""Calendar app autopilot emulators."""
99
10import logging
10from autopilot.introspection import dbus11from autopilot.introspection import dbus
12from autopilot import logging as autopilot_logging
13
11from ubuntuuitoolkit import emulators as toolkit_emulators14from ubuntuuitoolkit import emulators as toolkit_emulators
1215
16logger = logging.getLogger(__name__)
17
18
19class CalendarEmulatorException(toolkit_emulators.ToolkitEmulatorException):
20 """Exception raised when there is an error with the emulator."""
1321
14class MainView(toolkit_emulators.MainView):22class MainView(toolkit_emulators.MainView):
1523
16 """24 @autopilot_logging.log_action(logger.info)
17 An emulator class that makes it easy to interact with the calendar-app.25 def open_dayview(self):
18 """26 """Open the Dayview tab.
27
28 :return: the Dayview page.
29
30 """
31 self.switch_to_tab('dayTab')
32 return self.wait_select_single(DayviewPage)
33
34 @autopilot_logging.log_action(logger.info)
35 def open_monthview(self):
36 """Open the Monthview Tab.
37
38 :return: the Monthview page.
39
40 """
41 self.switch_to_tab('monthTab')
42 return self.wait_select_single(MonthviewPage)
43
1944
20 def get_event_view(self):45 def get_event_view(self):
21 return self.wait_select_single("EventView")46 return self.wait_select_single("EventView")
2247
23 def get_month_view(self):
24 return self.wait_select_single("MonthView")
25
26 def get_year_view(self):48 def get_year_view(self):
27 return self.wait_select_single("YearView")49 return self.wait_select_single("YearView")
2850
@@ -78,6 +100,30 @@
78 except dbus.StateNotFoundError:100 except dbus.StateNotFoundError:
79 return None101 return None
80102
103 def get_year(self, component):
104 return int(component.wait_select_single(
105 "Label", objectName="yearLabel").text)
106
107 def get_num_events(self):
108 return len(self.select_many("EventBubble"))
109
110class Page(toolkit_emulators.UbuntuUIToolkitEmulatorBase):
111 """Autopilot helper for Pages."""
112
113 def __init__(self, *args):
114 super(Page, self).__init__(*args)
115 # XXX we need a better way to keep reference to the main view.
116 # --elopio - 2014-01-31
117 self.main_view = self.get_root_instance().select_single(MainView)
118
119 def drag_page_up(self):
120 """Drag the given page up."""
121 self._drag_page(direction='up')
122
123 def drag_page_down(self):
124 """Drag the given page down."""
125 self._drag_page(direction='down')
126
81 def swipe_view(self, direction, view, x_pad=0.15):127 def swipe_view(self, direction, view, x_pad=0.15):
82 """Swipe the given view to left or right.128 """Swipe the given view to left or right.
83129
@@ -96,10 +142,55 @@
96142
97 self.pointing_device.drag(x_start, y_line, x_stop, y_line)143 self.pointing_device.drag(x_start, y_line, x_stop, y_line)
98144
99 def get_year(self, component):145 def _drag_page(self, direction):
100 return int(component.wait_select_single(146 """Function to drag the page up/down."""
101 "Label", objectName="yearLabel").text)147 self._wait_to_stop_moving()
102148
103 def get_month_name(self, component):149 x, y, w, h = self.globalRect
104 return component.wait_select_single(150 start_x = stop_x = x + (w / 2)
105 "Label", objectName="monthLabel").text151 start_y = y + (h / 2)
152
153 if direction == "down":
154 stop_y = start_y + h / 3
155 self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
156 else:
157 stop_y = start_y - h / 3
158 self.pointing_device.drag(start_x, start_y, stop_x, stop_y)
159
160 self._wait_to_stop_moving()
161
162 def _wait_to_stop_moving(self):
163 self.select_single(
164 'QQuickFlickable',
165 objectName='animationContainer').moving.wait_for(False)
166
167class DayviewPage(Page):
168 """Autopilot helper for the Dayview page."""
169
170
171class MonthviewPage(Page):
172 """Autopilot helper for the Monthview page."""
173
174 def __init__(self):
175 self.view = self.wait_select_single("MonthView")
176
177 def change_month(self, delta):
178 sign = int(math.copysign(1, delta))
179
180 for _ in range(abs(delta)):
181 before = month_view.currentMonth.datetime
182
183 #prevent timing issues with swiping
184 old_month = self.currentMonth.datetime
185 self.swipe_view(sign, month_view)
186 #self.assertThat(lambda: self.currentMonth.datetime,
187 # Eventually(NotEquals(old_month)))
188
189 after = before + relativedelta(months=sign)
190
191 #self.assertThat(lambda:
192 # self.currentMonth.datetime.month,
193 # Eventually(Equals(after.month)))
194 #self.assertThat(lambda:
195 # self.currentMonth.datetime.year,
196 # Eventually(Equals(after.year)))
106197
=== modified file 'tests/autopilot/calendar_app/tests/__init__.py'
--- tests/autopilot/calendar_app/tests/__init__.py 2014-01-31 21:15:52 +0000
+++ tests/autopilot/calendar_app/tests/__init__.py 2014-03-20 03:45:39 +0000
@@ -22,6 +22,12 @@
22import shutil22import shutil
23import logging23import logging
2424
25try:
26 from unittest import mock
27except ImportError:
28 import mock
29import tempfile
30
25from autopilot.input import Mouse, Touch, Pointer31from autopilot.input import Mouse, Touch, Pointer
26from autopilot.platform import model32from autopilot.platform import model
27from autopilot.testcase import AutopilotTestCase33from autopilot.testcase import AutopilotTestCase
@@ -45,20 +51,16 @@
4551
46 local_location = "../../calendar.qml"52 local_location = "../../calendar.qml"
47 installed_location = "/usr/share/calendar-app/calendar.qml"53 installed_location = "/usr/share/calendar-app/calendar.qml"
48 sqlite_dir = os.path.expanduser(
49 "~/.local/share/com.ubuntu.calendar/Databases")
50 backup_dir = sqlite_dir + ".backup"
5154
52 def setUp(self):55 def setUp(self):
53 self.pointing_device = Pointer(self.input_device_class.create())56 self.pointing_device = Pointer(self.input_device_class.create())
57 self.home_dir = self._patch_home()
54 super(CalendarTestCase, self).setUp()58 super(CalendarTestCase, self).setUp()
55 self.temp_move_sqlite_db()
56 self.addCleanup(self.restore_sqlite_db)
5759
58 #turn off the OSK so it doesn't block screen elements60 #turn off the OSK so it doesn't block screen elements
59 if model() != 'Desktop':61 #if model() != 'Desktop':
60 os.system("stop maliit-server")62 # os.system("stop maliit-server")
61 self.addCleanup(os.system, "start maliit-server")63 # self.addCleanup(os.system, "start maliit-server")
6264
63 # Unset the current locale to ensure locale-specific data65 # Unset the current locale to ensure locale-specific data
64 # (day and month names, first day of the week, …) doesn’t get66 # (day and month names, first day of the week, …) doesn’t get
@@ -73,6 +75,7 @@
73 self.launch_test_click()75 self.launch_test_click()
7476
75 def launch_test_local(self):77 def launch_test_local(self):
78 logger.debug("Running via local installation")
76 self.app = self.launch_test_application(79 self.app = self.launch_test_application(
77 "qmlscene",80 "qmlscene",
78 self.local_location,81 self.local_location,
@@ -80,6 +83,7 @@
80 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)83 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
8184
82 def launch_test_installed(self):85 def launch_test_installed(self):
86 logger.debug("Running via installed debian package")
83 self.app = self.launch_test_application(87 self.app = self.launch_test_application(
84 "qmlscene",88 "qmlscene",
85 self.installed_location,89 self.installed_location,
@@ -87,39 +91,30 @@
87 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)91 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
8892
89 def launch_test_click(self):93 def launch_test_click(self):
94 logger.debug("Running via click package")
90 self.app = self.launch_click_package(95 self.app = self.launch_click_package(
91 "com.ubuntu.calendar",96 "com.ubuntu.calendar",
92 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)97 emulator_base=toolkit_emulators.UbuntuUIToolkitEmulatorBase)
9398
94 def temp_move_sqlite_db(self):99 def _patch_home(self):
95 try:100 #make a temp dir
96 shutil.rmtree(self.backup_dir)101 temp_dir = tempfile.mkdtemp()
97 except:102 logger.debug("Created fake home directory " + temp_dir)
98 pass103 self.addCleanup(shutil.rmtree, temp_dir)
99 else:104 #if the Xauthority file is in home directory
100 logger.warning("Prexisting backup database found and removed")105 #make sure we copy it to temp home, otherwise do nothing
101106 xauth = os.path.expanduser(os.path.join('~', '.Xauthority'))
102 try:107 if os.path.isfile(xauth):
103 shutil.move(self.sqlite_dir, self.backup_dir)108 logger.debug("Copying .Xauthority to fake home " + temp_dir)
104 except:109 shutil.copyfile(
105 logger.warning("No current database found")110 os.path.expanduser(os.path.join('~', '.Xauthority')),
106 else:111 os.path.join(temp_dir, '.Xauthority'))
107 logger.debug("Backed up database")112 patcher = mock.patch.dict('os.environ', {'HOME': temp_dir})
108113 patcher.start()
109 def restore_sqlite_db(self):114 logger.debug("Patched home to fake home directory " + temp_dir)
110 if os.path.exists(self.backup_dir):115 self.addCleanup(patcher.stop)
111 if os.path.exists(self.sqlite_dir):116 return temp_dir
112 try:
113 shutil.rmtree(self.sqlite_dir)
114 except:
115 logger.error("Failed to remove test database and restore" /
116 "database")
117 return
118 try:
119 shutil.move(self.backup_dir, self.sqlite_dir)
120 except:
121 logger.error("Failed to restore database")
122117
123 @property118 @property
124 def main_view(self):119 def main_view(self):
125 return self.app.select_single(emulators.MainView)120 return self.app.wait_select_single(emulators.MainView)
126121
=== modified file 'tests/autopilot/calendar_app/tests/test_calendar.py'
--- tests/autopilot/calendar_app/tests/test_calendar.py 2014-01-11 06:05:42 +0000
+++ tests/autopilot/calendar_app/tests/test_calendar.py 2014-03-20 03:45:39 +0000
@@ -11,7 +11,7 @@
1111
12from autopilot.matchers import Eventually12from autopilot.matchers import Eventually
1313
14from testtools.matchers import Equals, Not, Is14from testtools.matchers import Equals, Not, Is, NotEquals
1515
16import time16import time
1717
@@ -46,6 +46,10 @@
4646
47 def test_new_event(self):47 def test_new_event(self):
48 """test add new event """48 """test add new event """
49 #go to today
50 self.main_view.switch_to_tab("dayTab")
51 self.main_view.open_toolbar().click_button("todaybutton")
52 num_events = self.main_view.get_num_events()
4953
50 #click on new event button54 #click on new event button
51 self.main_view.open_toolbar().click_button("neweventbutton")55 self.main_view.open_toolbar().click_button("neweventbutton")
@@ -68,7 +72,7 @@
68 ok = picker.select_single("Button", objectName="TimePickerOKButton")72 ok = picker.select_single("Button", objectName="TimePickerOKButton")
69 self.pointing_device.click_object(ok)73 self.pointing_device.click_object(ok)
7074
71 # Set the end time75 ## Set the end time
72 end_time_field = self.main_view.get_event_end_time_field()76 end_time_field = self.main_view.get_event_end_time_field()
73 self.pointing_device.click_object(end_time_field)77 self.pointing_device.click_object(end_time_field)
74 picker = self.main_view.get_time_picker()78 picker = self.main_view.get_time_picker()
@@ -83,18 +87,10 @@
83 self.keyboard.type("My location")87 self.keyboard.type("My location")
84 self.assertThat(location_field.text, Eventually(Equals("My location")))88 self.assertThat(location_field.text, Eventually(Equals("My location")))
8589
86 #input people
87 people_field = self.main_view.get_event_people_field()
88 self.pointing_device.click_object(people_field)
89 self.assertThat(people_field.activeFocus, Eventually(Equals(True)))
90 self.keyboard.type("Me")
91 self.assertThat(people_field.text, Eventually(Equals("Me")))
92
93 #click save button90 #click save button
94 self.main_view.open_toolbar().click_button("eventSaveButton")91 self.main_view.open_toolbar().click_button("eventSaveButton")
9592
96 #verify that the event has been created in timeline93 #verify that the event has been created in timeline
97 self.main_view.switch_to_tab("dayTab")94 self.main_view.open_toolbar().click_button("todaybutton")
98 self.assertThat(lambda: self.main_view.get_label_with_text(95 self.assertThat(self.main_view.get_num_events,
99 eventTitle, root=self.main_view.get_day_view()),96 Eventually(NotEquals(num_events)))
100 Eventually(Not(Is(None))))
10197
=== modified file 'tests/autopilot/calendar_app/tests/test_monthview.py'
--- tests/autopilot/calendar_app/tests/test_monthview.py 2014-01-09 23:30:55 +0000
+++ tests/autopilot/calendar_app/tests/test_monthview.py 2014-03-20 03:45:39 +0000
@@ -24,59 +24,25 @@
2424
25 def setUp(self):25 def setUp(self):
26 super(TestMonthView, self).setUp()26 super(TestMonthView, self).setUp()
27 self.assertThat(self.main_view.visible, Eventually(Equals(True)))
28 self.main_view.switch_to_tab("monthTab")
29
30 self.assertThat(27 self.assertThat(
31 self.main_view.get_month_view, Eventually(NotEquals(None)))28 self.main_view.visible, Eventually(Equals(True)))
3229 self.page = self.main_view.open_monthview()
33 self.month_view = self.main_view.get_month_view()30
3431 def test_monthview_go_to_today_next_month(self):
35 def change_month(self, delta):32 self.page.change_month(1)
36 month_view = self.main_view.get_month_view()33 self.main_view.open_toolbar().click_button("todaybutton")
37 sign = int(math.copysign(1, delta))34 self.assertThat(lambda: self.page.currentMonth.datetime.day,
38
39 for _ in range(abs(delta)):
40 before = month_view.currentMonth.datetime
41
42 #prevent timing issues with swiping
43 old_month = month_view.currentMonth.datetime
44 self.main_view.swipe_view(sign, month_view)
45 self.assertThat(lambda: month_view.currentMonth.datetime,
46 Eventually(NotEquals(old_month)))
47
48 after = before + relativedelta(months=sign)
49
50 self.assertThat(lambda:
51 self.month_view.currentMonth.datetime.month,
52 Eventually(Equals(after.month)))
53 self.assertThat(lambda:
54 self.month_view.currentMonth.datetime.year,
55 Eventually(Equals(after.year)))
56
57 def _assert_today(self):
58 today = datetime.today()
59 self.assertThat(lambda: self.month_view.currentMonth.datetime.day,
60 Eventually(Equals(today.day)))35 Eventually(Equals(today.day)))
61 self.assertThat(lambda: self.month_view.currentMonth.datetime.month,36 self.assertThat(lambda: self.page.currentMonth.datetime.month,
62 Eventually(Equals(today.month)))37 Eventually(Equals(today.month)))
63 self.assertThat(lambda: self.month_view.currentMonth.datetime.year,38 self.assertThat(lambda: self.page.currentMonth.datetime.year,
64 Eventually(Equals(today.year)))39 Eventually(Equals(today.year)))
6540
66 def _test_go_to_today(self, delta):
67 self._assert_today()
68 self.change_month(delta)
69 self.main_view.open_toolbar().click_button("todaybutton")
70 self._assert_today()
71
72 def test_monthview_go_to_today_next_month(self):
73 self._test_go_to_today(1)
74
75 def test_monthview_go_to_today_prev_month(self):41 def test_monthview_go_to_today_prev_month(self):
76 self._test_go_to_today(-1)42 self.page.change_month(-1)
7743
78 def test_monthview_go_to_today_next_year(self):44 def test_monthview_go_to_today_next_year(self):
79 self._test_go_to_today(12)45 self.page.change_month(12)
8046
81 def test_monthview_go_to_today_prev_year(self):47 def test_monthview_go_to_today_prev_year(self):
82 self._test_go_to_today(-12)48 self.page.change_month(-12)

Subscribers

People subscribed via source and target branches

to status/vote changes: