1 # Copyright 2012 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 import os 5 import shutil 6 import tempfile 7 import unittest 8 9 from telemetry.core import util 10 11 12 class TestWait(unittest.TestCase): 13 @staticmethod 14 def testNonTimeout(): 15 def test(): 16 return True 17 util.WaitFor(test, 0.1) 18 19 def testTimeout(self): 20 def test(): 21 return False 22 self.assertRaises(util.TimeoutException, lambda: util.WaitFor(test, 0.1)) 23 24 def testCallable(self): 25 """Test methods and anonymous functions, functions are tested elsewhere.""" 26 class Test(object): 27 def Method(self): 28 return 'test' 29 util.WaitFor(Test().Method, 0.1) 30 31 util.WaitFor(lambda: 1, 0.1) 32 33 # Test noncallable condition. 34 self.assertRaises(TypeError, lambda: util.WaitFor('test', 0.1)) 35 36 def testReturn(self): 37 self.assertEquals('test', util.WaitFor(lambda: 'test', 0.1)) 38 39 class TestGetSequentialFileName(unittest.TestCase): 40 def __init__(self, *args, **kwargs): 41 super(TestGetSequentialFileName, self).__init__(*args, **kwargs) 42 self.test_directory = None 43 44 def setUp(self): 45 self.test_directory = tempfile.mkdtemp() 46 47 def testGetSequentialFileNameNoOtherSequentialFile(self): 48 next_json_test_file_path = util.GetSequentialFileName( 49 os.path.join(self.test_directory, 'test')) 50 self.assertEquals(os.path.join(self.test_directory, 'test_000'), 51 next_json_test_file_path) 52 53 def testGetSequentialFileNameWithOtherSequentialFiles(self): 54 # Create test_000.json, test_001.json, test_002.json in test directory. 55 for i in xrange(3): 56 with open( 57 os.path.join(self.test_directory, 'test_%03d.json' % i), 'w') as _: 58 pass 59 next_json_test_file_path = util.GetSequentialFileName( 60 os.path.join(self.test_directory, 'test')) 61 self.assertEquals(os.path.join(self.test_directory, 'test_003'), 62 next_json_test_file_path) 63 64 def tearDown(self): 65 shutil.rmtree(self.test_directory) 66