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