OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import time | |
6 | |
7 from telemetry.page.actions import gesture_action | 5 from telemetry.page.actions import gesture_action |
| 6 from telemetry.page.actions import wait |
8 from telemetry.unittest import tab_test_case | 7 from telemetry.unittest import tab_test_case |
| 8 from telemetry.unittest import simple_mock |
9 | 9 |
10 class MockGestureAction(gesture_action.GestureAction): | 10 class MockGestureAction(gesture_action.GestureAction): |
11 """Mock gesture action that simply sleeps for a specified amount of time.""" | 11 """Mock gesture action that simply sleeps for a specified amount of time.""" |
12 def __init__(self, attributes=None): | 12 def __init__(self, sleep_func, attributes=None): |
| 13 self.sleep_func = sleep_func |
13 super(MockGestureAction, self).__init__(attributes) | 14 super(MockGestureAction, self).__init__(attributes) |
14 | 15 |
15 def RunGesture(self, page, tab): | 16 def RunGesture(self, page, tab): |
16 duration = getattr(self, 'duration', 2) | 17 duration = getattr(self, 'duration', 2) |
17 | 18 |
18 time.sleep(duration) | 19 self.sleep_func(duration) |
19 | 20 |
20 | 21 |
21 class GestureActionTest(tab_test_case.TabTestCase): | 22 class GestureActionTest(tab_test_case.TabTestCase): |
22 def testGestureAction(self): | 23 def testGestureAction(self): |
23 """Test that GestureAction.RunAction() calls RunGesture().""" | 24 """Test that GestureAction.RunAction() calls RunGesture().""" |
24 action = MockGestureAction({ 'duration': 1 }) | 25 mock_timer = simple_mock.MockTimer() |
| 26 action = MockGestureAction(mock_timer.Sleep, { 'duration': 1 }) |
25 | 27 |
26 start_time = time.time() | |
27 action.RunAction(None, self._tab) | 28 action.RunAction(None, self._tab) |
28 self.assertGreaterEqual(time.time() - start_time, 1.0) | 29 self.assertEqual(mock_timer.GetTime(), 1) |
29 | 30 |
30 def testWaitAfter(self): | 31 def testWaitAfter(self): |
31 action = MockGestureAction({ 'duration': 1, | 32 mock_timer = simple_mock.MockTimer() |
32 'wait_after': { 'seconds': 1 } }) | 33 real_time_sleep = wait.time.sleep |
| 34 wait.time.sleep = mock_timer.Sleep |
33 | 35 |
34 start_time = time.time() | 36 try: |
35 action.RunAction(None, self._tab) | 37 action = MockGestureAction(mock_timer.Sleep, |
36 self.assertGreaterEqual(time.time() - start_time, 2.0) | 38 { 'duration': 1, |
| 39 'wait_after': { 'seconds': 1 } }) |
| 40 |
| 41 action.RunAction(None, self._tab) |
| 42 self.assertEqual(mock_timer.GetTime(), 2) |
| 43 finally: |
| 44 wait.time.sleep = real_time_sleep |
OLD | NEW |