@@ -0,0 +1,236 @@
1+# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2+#
3+# Unity Indicators Autopilot Test Suite
4+# Copyright (C) 2015 Canonical
5+#
6+# This program is free software: you can redistribute it and/or modify
7+# it under the terms of the GNU General Public License as published by
8+# the Free Software Foundation, either version 3 of the License, or
9+# (at your option) any later version.
10+#
11+# This program is distributed in the hope that it will be useful,
12+# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+# GNU General Public License for more details.
15+#
16+# You should have received a copy of the GNU General Public License
17+# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+from testscenarios import multiply_scenarios
20+
21+from unity8 import (
22+ fixture_setup,
23+ indicators,
24+ process_helpers
25+)
26+from unity8.shell import tests
27+from unity8.indicators import tests
28+
29+from autopilot.matchers import Eventually
30+from testtools.matchers import Equals, NotEquals
31+import time
32+
33+
34+class TestIndicatorBaseTestCase(tests.IndicatorTestCase):
35+
36+ scenarios = [ tests.IndicatorTestCase.device_emulation_scenarios[0] ]
37+
38+ def setUp(self):
39+ super(TestIndicatorBaseTestCase, self).setUp()
40+
41+ environment = {}
42+ environment['ARGS'] = '-t {0}'\
43+ .format(self.action_delay)
44+ self.launch_indicator_service(environment)
45+
46+ # wait for the indicator to appear in unity
47+ self.indicator = indicators.TestIndicator(self.main_window)
48+ self.assertThat(
49+ self.indicator.is_indicator_icon_visible(),
50+ Eventually(Equals(True), timeout=20)
51+ )
52+ self.indicator_page = self.indicator.open()
53+
54+ def launch_indicator_service(self, variables):
55+ launch_service_fixture = fixture_setup.LaunchTestIndicatorService(variables)
56+ self.useFixture(launch_service_fixture)
57+
58+
59+class TestServerValueUpdate(TestIndicatorBaseTestCase):
60+
61+ """Test that an action causes the server to update"""
62+
63+ time_scenarios = [
64+ ('Low', dict(action_delay=0)),
65+ ('Medium', dict(action_delay=2500)),
66+ ('High', dict(action_delay=8000)),
67+ ]
68+ scenarios = multiply_scenarios(
69+ time_scenarios,
70+ TestIndicatorBaseTestCase.scenarios
71+ )
72+
73+ def test_switch_reaches_server_value(self):
74+ switch = self.indicator_page.get_switcher()
75+ switch_menu = self.indicator_page.get_switch_menu()
76+
77+ switch.change_state()
78+
79+ self.assertThat(
80+ switch_menu.serverChecked,
81+ Eventually(Equals(switch.checked), timeout=20)
82+ )
83+
84+ def test_slider_reaches_server_value(self):
85+ slider = self.indicator_page.get_slider()
86+ slider_menu = self.indicator_page.get_slider_menu()
87+
88+ old_value = slider.value
89+ slider.slide_left()
90+
91+ self.assertThat(
92+ slider_menu.serverValue,
93+ Eventually(NotEquals(old_value), timeout=20)
94+ )
95+
96+ self.assertThat(
97+ slider_menu.serverValue,
98+ Eventually(Equals(slider.value), timeout=20)
99+ )
100+
101+class TestBuffering(TestIndicatorBaseTestCase):
102+
103+ """Test that switching multiple times will buffer activations
104+
105+ See https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1390136 .
106+ """
107+ action_delay = 2500
108+
109+ def test_buffered_switch(self):
110+
111+ switch = self.indicator_page.get_switcher()
112+ switch.change_state()
113+ intermediate_value = switch.checked;
114+
115+ # will buffer change until it receives the change from server
116+ switch.change_state()
117+ final_value = switch.checked;
118+
119+ # backend will respond to first switch.
120+ switch_menu = self.indicator_page.get_switch_menu()
121+ self.assertThat(
122+ switch_menu.serverChecked,
123+ Eventually(Equals(intermediate_value), timeout=20)
124+ )
125+ # The buffered activation should have gone to server now.
126+
127+ # front-end should not change as a result of server update while it is buffered
128+ self.assertThat(
129+ switch.checked,
130+ Equals(final_value)
131+ )
132+
133+ # server will respond to the second activate
134+ self.assertThat(
135+ switch_menu.serverChecked,
136+ Eventually(Equals(final_value), timeout=20)
137+ )
138+
139+ # make sure we've got the server value set.
140+ self.assertThat(
141+ switch.checked,
142+ Equals(switch_menu.serverChecked)
143+ )
144+
145+ def test_buffered_slider(self):
146+
147+ slider = self.indicator_page.get_slider()
148+ original_value = slider.value;
149+ slider.slide_left()
150+
151+ # will buffer change until it receives the change from server
152+ slider.slide_right()
153+ final_value = slider.value;
154+
155+ # backend will respond to first slider. Since it's a live slider it'll probably
156+ # be a random value along the slide.
157+ slider_menu = self.indicator_page.get_slider_menu()
158+ self.assertThat(
159+ slider_menu.serverValue,
160+ Eventually(NotEquals(original_value), timeout=20)
161+ )
162+ # It wont yet have reached the final value due to the buffering
163+ # Second activate should have gone out by now
164+ self.assertThat(
165+ slider_menu.serverValue,
166+ NotEquals(final_value)
167+ )
168+
169+ # front-end should not change as a result of server update while it is buffered
170+ self.assertThat(
171+ slider.value,
172+ Equals(final_value)
173+ )
174+
175+ # server will respond to the second activate
176+ self.assertThat(
177+ slider_menu.serverValue,
178+ Eventually(Equals(final_value), timeout=20)
179+ )
180+
181+ # make sure we've got the server value set.
182+ self.assertThat(
183+ slider.value,
184+ Equals(slider_menu.serverValue)
185+ )
186+
187+
188+class TestClientRevertsToServerValue(TestIndicatorBaseTestCase):
189+
190+ """Test that an action which does not respond in time will revert
191+ to original value if not actioned in time.
192+
193+ See https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1390136 .
194+ """
195+ action_delay = -1 # never action.
196+
197+ def test_switch_reverts_on_late_response(self):
198+
199+ switch = self.indicator_page.get_switcher()
200+ switch_menu = self.indicator_page.get_switch_menu()
201+
202+ original_value = switch.checked;
203+ switch.change_state()
204+
205+ # switch should revert to original value after 5 seconds (30 seconds in real usage)
206+ self.assertThat(
207+ switch.checked,
208+ Eventually(Equals(original_value), timeout=20)
209+ )
210+
211+ # make sure we've got the server value set.
212+ self.assertThat(
213+ switch.checked,
214+ Equals(switch_menu.serverChecked)
215+ )
216+
217+ def test_slider_reverts_on_late_response(self):
218+
219+ slider = self.indicator_page.get_slider()
220+ slider_menu = self.indicator_page.get_slider_menu()
221+
222+ original_value = slider.value;
223+ slider.slide_left()
224+
225+ # slider should revert to original value after 5 seconds (30 seconds in real usage)
226+ self.assertThat(
227+ slider.value,
228+ Eventually(Equals(original_value), timeout=20)
229+ )
230+
231+ # make sure we've got the server value set.
232+ self.assertThat(
233+ slider.value,
234+ Equals(slider_menu.serverValue)
235+ )
236+