Unity 8
test_action_latency.py
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 )
25 from unity8.indicators import tests
26 
27 from autopilot.matchers import Eventually
28 from testtools.matchers import Equals, NotEquals
29 
30 
31 class TestIndicatorBaseTestCase(tests.IndicatorTestCase):
32 
33  scenarios = [tests.IndicatorTestCase.device_emulation_scenarios[0]]
34 
35  def setUp(self):
36  super(TestIndicatorBaseTestCase, self).setUp()
37 
38  self.launch_indicator_service()
39 
40  # wait for the indicator to appear in unity
41  self.indicator = indicators.TestIndicator(self.main_window)
42  self.assertThat(
43  self.indicator.is_indicator_icon_visible(),
44  Eventually(Equals(True), timeout=20)
45  )
46  self.indicator_page = self.indicator.open()
47 
48  def launch_indicator_service(self):
49  launch_service_fixture = \
50  fixture_setup.LaunchMockIndicatorService(self.action_delay)
51  self.useFixture(launch_service_fixture)
52 
53 
54 class TestServerValueUpdate(TestIndicatorBaseTestCase):
55 
56  """Test that an action causes the server to update"""
57 
58  time_scenarios = [
59  ('Low', {'action_delay': 0}),
60  ('Medium', {'action_delay': 2500}),
61  ('High', {'action_delay': 8000}),
62  ]
63  scenarios = multiply_scenarios(
64  time_scenarios,
65  TestIndicatorBaseTestCase.scenarios
66  )
67 
68  def test_switch_reaches_server_value(self):
69  switch = self.indicator_page.get_switcher()
70  switch_menu = self.indicator_page.get_switch_menu()
71 
72  switch.change_state()
73  final_value = switch.checked
74 
75  self.assertThat(
76  switch_menu.serverChecked,
77  Eventually(Equals(final_value), timeout=20)
78  )
79 
80  def test_slider_reaches_server_value(self):
81  slider = self.indicator_page.get_slider()
82  slider_menu = self.indicator_page.get_slider_menu()
83 
84  old_value = slider.value
85  slider.slide_left()
86  final_value = slider.value
87 
88  self.assertThat(
89  slider_menu.serverValue,
90  Eventually(NotEquals(old_value), timeout=20)
91  )
92 
93  self.assertThat(
94  slider_menu.serverValue,
95  Eventually(Equals(final_value), timeout=20)
96  )
97 
98 
99 class TestBuffering(TestIndicatorBaseTestCase):
100 
101  """Test that switching multiple times will buffer activations
102 
103  See https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1390136 .
104  """
105  action_delay = 2500
106 
107  def test_switch_buffers_actvations(self):
108 
109  switch = self.indicator_page.get_switcher()
110  switch.change_state()
111  intermediate_value = switch.checked
112 
113  # will buffer change until it receives the change from server
114  switch.change_state()
115  final_value = switch.checked
116 
117  # backend will respond to first switch.
118  switch_menu = self.indicator_page.get_switch_menu()
119  self.assertThat(
120  switch_menu.serverChecked,
121  Eventually(Equals(intermediate_value), timeout=20)
122  )
123  # The buffered activation should have gone to server now.
124 
125  # front-end should not change as a result of server update
126  # while it is buffered
127  self.assertThat(
128  switch.checked,
129  Equals(final_value)
130  )
131 
132  # server will respond to the second activate
133  self.assertThat(
134  switch_menu.serverChecked,
135  Eventually(Equals(final_value), timeout=20)
136  )
137 
138  # make sure we've got the server value set.
139  self.assertThat(
140  switch.checked,
141  Equals(switch_menu.serverChecked)
142  )
143 
144  def test_slider_buffers_activations(self):
145 
146  slider = self.indicator_page.get_slider()
147  original_value = slider.value
148  slider.slide_left()
149 
150  # will buffer change until it receives the change from server
151  slider.slide_right()
152  final_value = slider.value
153 
154  # backend will respond to first slider. Since it's a live slider
155  # it'll probably be a random value along the slide.
156  slider_menu = self.indicator_page.get_slider_menu()
157  self.assertThat(
158  slider_menu.serverValue,
159  Eventually(NotEquals(original_value), timeout=20)
160  )
161  # It wont yet have reached the final value due to the buffering
162  # Second activate should have gone out by now
163  self.assertThat(
164  slider_menu.serverValue,
165  NotEquals(final_value)
166  )
167 
168  # front-end should not change as a result of server update
169  # 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
206  # (30 seconds in real usage)
207  self.assertThat(
208  switch.checked,
209  Eventually(Equals(original_value), timeout=20)
210  )
211 
212  # make sure we've got the server value set.
213  self.assertThat(
214  switch.checked,
215  Equals(switch_menu.serverChecked)
216  )
217 
218  def test_slider_reverts_on_late_response(self):
219 
220  slider = self.indicator_page.get_slider()
221  slider_menu = self.indicator_page.get_slider_menu()
222 
223  original_value = slider.value
224  slider.slide_left()
225 
226  # slider should revert to original value after 5 seconds
227  # (30 seconds in real usage)
228  self.assertThat(
229  slider.value,
230  Eventually(Equals(original_value), timeout=20)
231  )
232 
233  # make sure we've got the server value set.
234  self.assertThat(
235  slider.value,
236  Equals(slider_menu.serverValue)
237  )