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  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  self.launch_indicator_service()
42 
43  # wait for the indicator to appear in unity
44  self.indicator = indicators.TestIndicator(self.main_window)
45  self.assertThat(
46  self.indicator.is_indicator_icon_visible(),
47  Eventually(Equals(True), timeout=20)
48  )
49  self.indicator_page = self.indicator.open()
50 
51  def launch_indicator_service(self):
52  launch_service_fixture = fixture_setup.LaunchMockIndicatorService(self.action_delay)
53  self.useFixture(launch_service_fixture)
54 
55 
56 class TestServerValueUpdate(TestIndicatorBaseTestCase):
57 
58  """Test that an action causes the server to update"""
59 
60  time_scenarios = [
61  ('Low', {'action_delay': 0}),
62  ('Medium', {'action_delay': 2500}),
63  ('High', {'action_delay': 8000}),
64  ]
65  scenarios = multiply_scenarios(
66  time_scenarios,
67  TestIndicatorBaseTestCase.scenarios
68  )
69 
70  def test_switch_reaches_server_value(self):
71  switch = self.indicator_page.get_switcher()
72  switch_menu = self.indicator_page.get_switch_menu()
73 
74  switch.change_state()
75 
76  self.assertThat(
77  switch_menu.serverChecked,
78  Eventually(Equals(switch.checked), timeout=20)
79  )
80 
81  def test_slider_reaches_server_value(self):
82  slider = self.indicator_page.get_slider()
83  slider_menu = self.indicator_page.get_slider_menu()
84 
85  old_value = slider.value
86  slider.slide_left()
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(slider.value), timeout=20)
96  )
97 
98 class TestBuffering(TestIndicatorBaseTestCase):
99 
100  """Test that switching multiple times will buffer activations
101 
102  See https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1390136 .
103  """
104  action_delay = 2500
105 
106  def test_switch_buffers_actvations(self):
107 
108  switch = self.indicator_page.get_switcher()
109  switch.change_state()
110  intermediate_value = switch.checked;
111 
112  # will buffer change until it receives the change from server
113  switch.change_state()
114  final_value = switch.checked;
115 
116  # backend will respond to first switch.
117  switch_menu = self.indicator_page.get_switch_menu()
118  self.assertThat(
119  switch_menu.serverChecked,
120  Eventually(Equals(intermediate_value), timeout=20)
121  )
122  # The buffered activation should have gone to server now.
123 
124  # front-end should not change as a result of server update while it is buffered
125  self.assertThat(
126  switch.checked,
127  Equals(final_value)
128  )
129 
130  # server will respond to the second activate
131  self.assertThat(
132  switch_menu.serverChecked,
133  Eventually(Equals(final_value), timeout=20)
134  )
135 
136  # make sure we've got the server value set.
137  self.assertThat(
138  switch.checked,
139  Equals(switch_menu.serverChecked)
140  )
141 
142  def test_slider_buffers_activations(self):
143 
144  slider = self.indicator_page.get_slider()
145  original_value = slider.value;
146  slider.slide_left()
147 
148  # will buffer change until it receives the change from server
149  slider.slide_right()
150  final_value = slider.value;
151 
152  # backend will respond to first slider. Since it's a live slider it'll probably
153  # be a random value along the slide.
154  slider_menu = self.indicator_page.get_slider_menu()
155  self.assertThat(
156  slider_menu.serverValue,
157  Eventually(NotEquals(original_value), timeout=20)
158  )
159  # It wont yet have reached the final value due to the buffering
160  # Second activate should have gone out by now
161  self.assertThat(
162  slider_menu.serverValue,
163  NotEquals(final_value)
164  )
165 
166  # front-end should not change as a result of server update while it is buffered
167  self.assertThat(
168  slider.value,
169  Equals(final_value)
170  )
171 
172  # server will respond to the second activate
173  self.assertThat(
174  slider_menu.serverValue,
175  Eventually(Equals(final_value), timeout=20)
176  )
177 
178  # make sure we've got the server value set.
179  self.assertThat(
180  slider.value,
181  Equals(slider_menu.serverValue)
182  )
183 
184 
185 class TestClientRevertsToServerValue(TestIndicatorBaseTestCase):
186 
187  """Test that an action which does not respond in time will revert
188  to original value if not actioned in time.
189 
190  See https://bugs.launchpad.net/ubuntu/+source/unity8/+bug/1390136 .
191  """
192  action_delay = -1 # never action.
193 
194  def test_switch_reverts_on_late_response(self):
195 
196  switch = self.indicator_page.get_switcher()
197  switch_menu = self.indicator_page.get_switch_menu()
198 
199  original_value = switch.checked;
200  switch.change_state()
201 
202  # switch should revert to original value after 5 seconds (30 seconds in real usage)
203  self.assertThat(
204  switch.checked,
205  Eventually(Equals(original_value), timeout=20)
206  )
207 
208  # make sure we've got the server value set.
209  self.assertThat(
210  switch.checked,
211  Equals(switch_menu.serverChecked)
212  )
213 
214  def test_slider_reverts_on_late_response(self):
215 
216  slider = self.indicator_page.get_slider()
217  slider_menu = self.indicator_page.get_slider_menu()
218 
219  original_value = slider.value;
220  slider.slide_left()
221 
222  # slider should revert to original value after 5 seconds (30 seconds in real usage)
223  self.assertThat(
224  slider.value,
225  Eventually(Equals(original_value), timeout=20)
226  )
227 
228  # make sure we've got the server value set.
229  self.assertThat(
230  slider.value,
231  Equals(slider_menu.serverValue)
232  )