| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """ | |
| 7 Unit tests for the contents of mock_calls.py. | |
| 8 """ | |
| 9 | |
| 10 import logging | |
| 11 import os | |
| 12 import sys | |
| 13 import unittest | |
| 14 | |
| 15 from pylib import constants | |
| 16 from pylib.utils import mock_calls | |
| 17 | |
| 18 sys.path.append(os.path.join( | |
| 19 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) | |
| 20 import mock # pylint: disable=F0401 | |
| 21 | |
| 22 | |
| 23 class _DummyAdb(object): | |
| 24 def __str__(self): | |
| 25 return '0123456789abcdef' | |
| 26 | |
| 27 def Push(self, host_path, device_path): | |
| 28 logging.debug('(device %s) pushing %r to %r', self, host_path, device_path) | |
| 29 | |
| 30 def IsOnline(self): | |
| 31 logging.debug('(device %s) checking device online', self) | |
| 32 return True | |
| 33 | |
| 34 def Shell(self, cmd): | |
| 35 logging.debug('(device %s) running command %r', self, cmd) | |
| 36 return "nice output\n" | |
| 37 | |
| 38 def Reboot(self): | |
| 39 logging.debug('(device %s) rebooted!', self) | |
| 40 | |
| 41 @property | |
| 42 def build_version_sdk(self): | |
| 43 logging.debug('(device %s) getting build_version_sdk', self) | |
| 44 return constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP | |
| 45 | |
| 46 | |
| 47 class TestCaseWithAssertCallsTest(mock_calls.TestCase): | |
| 48 def setUp(self): | |
| 49 self.adb = _DummyAdb() | |
| 50 | |
| 51 def ShellError(self): | |
| 52 def action(cmd): | |
| 53 raise ValueError('(device %s) command %r is not nice' % (self.adb, cmd)) | |
| 54 return action | |
| 55 | |
| 56 def get_answer(self): | |
| 57 logging.debug("called 'get_answer' of %r object", self) | |
| 58 return 42 | |
| 59 | |
| 60 def echo(self, thing): | |
| 61 logging.debug("called 'echo' of %r object", self) | |
| 62 return thing | |
| 63 | |
| 64 def testCallTarget_succeds(self): | |
| 65 self.assertEquals(self.adb.Shell, | |
| 66 self.call_target(self.call.adb.Shell)) | |
| 67 | |
| 68 def testCallTarget_failsExternal(self): | |
| 69 with self.assertRaises(ValueError): | |
| 70 self.call_target(mock.call.sys.getcwd) | |
| 71 | |
| 72 def testCallTarget_failsUnknownAttribute(self): | |
| 73 with self.assertRaises(AttributeError): | |
| 74 self.call_target(self.call.adb.Run) | |
| 75 | |
| 76 def testCallTarget_failsIntermediateCalls(self): | |
| 77 with self.assertRaises(AttributeError): | |
| 78 self.call_target(self.call.adb.RunShell('cmd').append) | |
| 79 | |
| 80 def testPatchCall_method(self): | |
| 81 self.assertEquals(42, self.get_answer()) | |
| 82 with self.patch_call(self.call.get_answer, return_value=123): | |
| 83 self.assertEquals(123, self.get_answer()) | |
| 84 self.assertEquals(42, self.get_answer()) | |
| 85 | |
| 86 def testPatchCall_attribute_method(self): | |
| 87 with self.patch_call(self.call.adb.Shell, return_value='hello'): | |
| 88 self.assertEquals('hello', self.adb.Shell('echo hello')) | |
| 89 | |
| 90 def testPatchCall_global(self): | |
| 91 with self.patch_call(mock.call.os.getcwd, return_value='/some/path'): | |
| 92 self.assertEquals('/some/path', os.getcwd()) | |
| 93 | |
| 94 def testPatchCall_withSideEffect(self): | |
| 95 with self.patch_call(self.call.adb.Shell, side_effect=ValueError): | |
| 96 with self.assertRaises(ValueError): | |
| 97 self.adb.Shell('echo hello') | |
| 98 | |
| 99 def testPatchCall_property(self): | |
| 100 self.assertEquals(constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP, | |
| 101 self.adb.build_version_sdk) | |
| 102 with self.patch_call( | |
| 103 self.call.adb.build_version_sdk, | |
| 104 return_value=constants.ANDROID_SDK_VERSION_CODES.KITKAT): | |
| 105 self.assertEquals(constants.ANDROID_SDK_VERSION_CODES.KITKAT, | |
| 106 self.adb.build_version_sdk) | |
| 107 self.assertEquals(constants.ANDROID_SDK_VERSION_CODES.LOLLIPOP, | |
| 108 self.adb.build_version_sdk) | |
| 109 | |
| 110 def testAssertCalls_succeeds_simple(self): | |
| 111 self.assertEquals(42, self.get_answer()) | |
| 112 with self.assertCall(self.call.get_answer(), 123): | |
| 113 self.assertEquals(123, self.get_answer()) | |
| 114 self.assertEquals(42, self.get_answer()) | |
| 115 | |
| 116 def testAssertCalls_succeeds_multiple(self): | |
| 117 with self.assertCalls( | |
| 118 (mock.call.os.getcwd(), '/some/path'), | |
| 119 (self.call.echo('hello'), 'hello'), | |
| 120 (self.call.get_answer(), 11), | |
| 121 self.call.adb.Push('this_file', 'that_file'), | |
| 122 (self.call.get_answer(), 12)): | |
| 123 self.assertEquals(os.getcwd(), '/some/path') | |
| 124 self.assertEquals('hello', self.echo('hello')) | |
| 125 self.assertEquals(11, self.get_answer()) | |
| 126 self.adb.Push('this_file', 'that_file') | |
| 127 self.assertEquals(12, self.get_answer()) | |
| 128 | |
| 129 def testAsserCalls_succeeds_withAction(self): | |
| 130 with self.assertCall( | |
| 131 self.call.adb.Shell('echo hello'), self.ShellError()): | |
| 132 with self.assertRaises(ValueError): | |
| 133 self.adb.Shell('echo hello') | |
| 134 | |
| 135 def testAssertCalls_fails_tooManyCalls(self): | |
| 136 with self.assertRaises(AssertionError): | |
| 137 with self.assertCalls(self.call.adb.IsOnline()): | |
| 138 self.adb.IsOnline() | |
| 139 self.adb.IsOnline() | |
| 140 | |
| 141 def testAssertCalls_fails_tooFewCalls(self): | |
| 142 with self.assertRaises(AssertionError): | |
| 143 with self.assertCalls(self.call.adb.IsOnline()): | |
| 144 pass | |
| 145 | |
| 146 def testAssertCalls_succeeds_extraCalls(self): | |
| 147 # we are not watching Reboot, so the assertion succeeds | |
| 148 with self.assertCalls(self.call.adb.IsOnline()): | |
| 149 self.adb.IsOnline() | |
| 150 self.adb.Reboot() | |
| 151 | |
| 152 def testAssertCalls_fails_extraCalls(self): | |
| 153 self.watchCalls([self.call.adb.Reboot]) | |
| 154 # this time we are also watching Reboot, so the assertion fails | |
| 155 with self.assertRaises(AssertionError): | |
| 156 with self.assertCalls(self.call.adb.IsOnline()): | |
| 157 self.adb.IsOnline() | |
| 158 self.adb.Reboot() | |
| 159 | |
| 160 def testAssertCalls_succeeds_NoCalls(self): | |
| 161 self.watchMethodCalls(self.call.adb) # we are watching all adb methods | |
| 162 with self.assertCalls(): | |
| 163 pass | |
| 164 | |
| 165 def testAssertCalls_fails_NoCalls(self): | |
| 166 self.watchMethodCalls(self.call.adb) | |
| 167 with self.assertRaises(AssertionError): | |
| 168 with self.assertCalls(): | |
| 169 self.adb.IsOnline() | |
| 170 | |
| 171 | |
| 172 if __name__ == '__main__': | |
| 173 logging.getLogger().setLevel(logging.DEBUG) | |
| 174 unittest.main(verbosity=2) | |
| 175 | |
| OLD | NEW |