Update sdk/platform-tools to version 26.0.0.
[android_tools.git] / sdk / platform-tools / systrace / catapult / common / py_utils / py_utils / py_utils_unittest.py
blobe614a4541c05f8007644333c9dc1824b51095e5f
1 # Copyright 2015 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 sys
6 import unittest
8 import py_utils
11 class PathTest(unittest.TestCase):
13 def testIsExecutable(self):
14 self.assertFalse(py_utils.IsExecutable('nonexistent_file'))
15 # We use actual files on disk instead of pyfakefs because the executable is
16 # set different on win that posix platforms and pyfakefs doesn't support
17 # win platform well.
18 self.assertFalse(py_utils.IsExecutable(_GetFileInTestDir('foo.txt')))
19 self.assertTrue(py_utils.IsExecutable(sys.executable))
22 def _GetFileInTestDir(file_name):
23 return os.path.join(os.path.dirname(__file__), 'test_data', file_name)
26 class WaitForTest(unittest.TestCase):
28 def testWaitForTrue(self):
29 def ReturnTrue():
30 return True
31 self.assertTrue(py_utils.WaitFor(ReturnTrue, .1))
33 def testWaitForFalse(self):
34 def ReturnFalse():
35 return False
37 with self.assertRaises(py_utils.TimeoutException):
38 py_utils.WaitFor(ReturnFalse, .1)
40 def testWaitForEventuallyTrue(self):
41 # Use list to pass to inner function in order to allow modifying the
42 # variable from the outer scope.
43 c = [0]
44 def ReturnCounterBasedValue():
45 c[0] += 1
46 return c[0] > 2
48 self.assertTrue(py_utils.WaitFor(ReturnCounterBasedValue, .5))
50 def testWaitForTrueLambda(self):
51 self.assertTrue(py_utils.WaitFor(lambda: True, .1))
53 def testWaitForFalseLambda(self):
54 with self.assertRaises(py_utils.TimeoutException):
55 py_utils.WaitFor(lambda: False, .1)