Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: tools/telemetry/telemetry/page/actions/wait_unittest.py

Issue 240893004: Mock timer for Telemetry unit tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: hide time.sleep and time.time Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 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.core import util 5 from telemetry.core import util
8 from telemetry.page.actions import wait 6 from telemetry.page.actions import wait
9 from telemetry.unittest import tab_test_case 7 from telemetry.unittest import tab_test_case
8 from telemetry.unittest import simple_mock
10 9
11 10
12 class WaitActionTest(tab_test_case.TabTestCase): 11 class WaitActionTest(tab_test_case.TabTestCase):
13 def testWaitAction(self): 12 def testWaitAction(self):
14 self.Navigate('blank.html') 13 self.Navigate('blank.html')
15 self.assertEquals( 14 self.assertEquals(
16 self._tab.EvaluateJavaScript('document.location.pathname;'), 15 self._tab.EvaluateJavaScript('document.location.pathname;'),
17 '/blank.html') 16 '/blank.html')
18 17
19 i = wait.WaitAction({ 'condition': 'duration', 'seconds': 1 }) 18 mock_timer = simple_mock.MockTimer()
19 real_time_sleep = wait.time.sleep
20 wait.time.sleep = mock_timer.Sleep
20 21
21 start_time = time.time() 22 try:
22 i.RunAction(None, self._tab) 23 i = wait.WaitAction({ 'condition': 'duration', 'seconds': 1 })
23 self.assertTrue(time.time() - start_time >= 1.0) 24
25 i.RunAction(None, self._tab)
26 self.assertEqual(mock_timer.GetTime(), 1)
27 finally:
28 wait.time.sleep = real_time_sleep
24 29
25 def testWaitActionTimeout(self): 30 def testWaitActionTimeout(self):
26 wait_action = wait.WaitAction({ 31 mock_timer = simple_mock.MockTimer()
27 'condition': 'javascript', 32 real_wait_time_sleep = wait.time.sleep
28 'javascript': '1 + 1 === 3', 33 real_util_time_sleep = util.time.sleep
29 'timeout': 1 34 real_util_time_time = util.time.time
30 })
31 35
32 start_time = time.time() 36 wait.time.sleep = mock_timer.Sleep
33 self.assertRaises( 37 util.time.sleep = mock_timer.Sleep
34 util.TimeoutException, 38 util.time.time = mock_timer.GetTime
35 lambda: wait_action.RunAction(None, self._tab)) 39
36 self.assertTrue(time.time() - start_time < 5) 40 try:
41 wait_action = wait.WaitAction({
42 'condition': 'javascript',
43 'javascript': '1 + 1 === 3',
44 'timeout': 1
45 })
46
47 self.assertRaises(
48 util.TimeoutException,
49 lambda: wait_action.RunAction(None, self._tab))
50 self.assertLess(mock_timer.GetTime(), 5)
51 finally:
52 wait.time.sleep = real_wait_time_sleep
53 util.time.sleep = real_util_time_sleep
54 util.time.time = real_util_time_time
OLDNEW
« no previous file with comments | « tools/telemetry/telemetry/page/actions/gesture_action_unittest.py ('k') | tools/telemetry/telemetry/unittest/simple_mock.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698