util_unittest.py revision cef7893435aa41160dd1255c43cb8498279738cc
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. 4import os 5import shutil 6import tempfile 7import unittest 8 9from telemetry.core import exceptions 10from telemetry.core import util 11 12 13class TestWait(unittest.TestCase): 14 15 def testNonTimeout(self): 16 17 def test(): 18 return True 19 20 util.WaitFor(test, 0.1) 21 22 def testTimeout(self): 23 24 def test(): 25 return False 26 27 self.assertRaises(exceptions.TimeoutException, 28 lambda: util.WaitFor(test, 0.1)) 29 30 def testCallable(self): 31 """Test methods and anonymous functions, functions are tested elsewhere.""" 32 33 class Test(object): 34 35 def Method(self): 36 return 'test' 37 38 util.WaitFor(Test().Method, 0.1) 39 40 util.WaitFor(lambda: 1, 0.1) 41 42 # Test noncallable condition. 43 self.assertRaises(TypeError, lambda: util.WaitFor('test', 0.1)) 44 45 def testReturn(self): 46 self.assertEquals('test', util.WaitFor(lambda: 'test', 0.1)) 47 48 49class TestGetSequentialFileName(unittest.TestCase): 50 51 def __init__(self, *args, **kwargs): 52 super(TestGetSequentialFileName, self).__init__(*args, **kwargs) 53 self.test_directory = None 54 55 def setUp(self): 56 self.test_directory = tempfile.mkdtemp() 57 58 def testGetSequentialFileNameNoOtherSequentialFile(self): 59 next_json_test_file_path = util.GetSequentialFileName(os.path.join( 60 self.test_directory, 'test')) 61 self.assertEquals( 62 os.path.join(self.test_directory, 'test_000'), next_json_test_file_path) 63 64 def testGetSequentialFileNameWithOtherSequentialFiles(self): 65 # Create test_000.json, test_001.json, test_002.json in test directory. 66 for i in xrange(3): 67 with open( 68 os.path.join(self.test_directory, 'test_%03d.json' % i), 'w') as _: 69 pass 70 next_json_test_file_path = util.GetSequentialFileName(os.path.join( 71 self.test_directory, 'test')) 72 self.assertEquals( 73 os.path.join(self.test_directory, 'test_003'), next_json_test_file_path) 74 75 def tearDown(self): 76 shutil.rmtree(self.test_directory) 77