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.
7 Unit tests for the contents of mock_calls.py.
15 from devil
.android
.sdk
import version_codes
16 from devil
.utils
import mock_calls
17 from pylib
import constants
19 sys
.path
.append(os
.path
.join(
20 constants
.DIR_SOURCE_ROOT
, 'third_party', 'pymock'))
21 import mock
# pylint: disable=F0401
24 class _DummyAdb(object):
26 return '0123456789abcdef'
28 def Push(self
, host_path
, device_path
):
29 logging
.debug('(device %s) pushing %r to %r', self
, host_path
, device_path
)
32 logging
.debug('(device %s) checking device online', self
)
36 logging
.debug('(device %s) running command %r', self
, cmd
)
37 return "nice output\n"
40 logging
.debug('(device %s) rebooted!', self
)
43 def build_version_sdk(self
):
44 logging
.debug('(device %s) getting build_version_sdk', self
)
45 return version_codes
.LOLLIPOP
48 class TestCaseWithAssertCallsTest(mock_calls
.TestCase
):
50 self
.adb
= _DummyAdb()
54 raise ValueError('(device %s) command %r is not nice' % (self
.adb
, cmd
))
58 logging
.debug("called 'get_answer' of %r object", self
)
61 def echo(self
, thing
):
62 logging
.debug("called 'echo' of %r object", self
)
65 def testCallTarget_succeds(self
):
66 self
.assertEquals(self
.adb
.Shell
,
67 self
.call_target(self
.call
.adb
.Shell
))
69 def testCallTarget_failsExternal(self
):
70 with self
.assertRaises(ValueError):
71 self
.call_target(mock
.call
.sys
.getcwd
)
73 def testCallTarget_failsUnknownAttribute(self
):
74 with self
.assertRaises(AttributeError):
75 self
.call_target(self
.call
.adb
.Run
)
77 def testCallTarget_failsIntermediateCalls(self
):
78 with self
.assertRaises(AttributeError):
79 self
.call_target(self
.call
.adb
.RunShell('cmd').append
)
81 def testPatchCall_method(self
):
82 self
.assertEquals(42, self
.get_answer())
83 with self
.patch_call(self
.call
.get_answer
, return_value
=123):
84 self
.assertEquals(123, self
.get_answer())
85 self
.assertEquals(42, self
.get_answer())
87 def testPatchCall_attribute_method(self
):
88 with self
.patch_call(self
.call
.adb
.Shell
, return_value
='hello'):
89 self
.assertEquals('hello', self
.adb
.Shell('echo hello'))
91 def testPatchCall_global(self
):
92 with self
.patch_call(mock
.call
.os
.getcwd
, return_value
='/some/path'):
93 self
.assertEquals('/some/path', os
.getcwd())
95 def testPatchCall_withSideEffect(self
):
96 with self
.patch_call(self
.call
.adb
.Shell
, side_effect
=ValueError):
97 with self
.assertRaises(ValueError):
98 self
.adb
.Shell('echo hello')
100 def testPatchCall_property(self
):
101 self
.assertEquals(version_codes
.LOLLIPOP
, self
.adb
.build_version_sdk
)
102 with self
.patch_call(
103 self
.call
.adb
.build_version_sdk
,
104 return_value
=version_codes
.KITKAT
):
105 self
.assertEquals(version_codes
.KITKAT
, self
.adb
.build_version_sdk
)
106 self
.assertEquals(version_codes
.LOLLIPOP
, self
.adb
.build_version_sdk
)
108 def testAssertCalls_succeeds_simple(self
):
109 self
.assertEquals(42, self
.get_answer())
110 with self
.assertCall(self
.call
.get_answer(), 123):
111 self
.assertEquals(123, self
.get_answer())
112 self
.assertEquals(42, self
.get_answer())
114 def testAssertCalls_succeeds_multiple(self
):
115 with self
.assertCalls(
116 (mock
.call
.os
.getcwd(), '/some/path'),
117 (self
.call
.echo('hello'), 'hello'),
118 (self
.call
.get_answer(), 11),
119 self
.call
.adb
.Push('this_file', 'that_file'),
120 (self
.call
.get_answer(), 12)):
121 self
.assertEquals(os
.getcwd(), '/some/path')
122 self
.assertEquals('hello', self
.echo('hello'))
123 self
.assertEquals(11, self
.get_answer())
124 self
.adb
.Push('this_file', 'that_file')
125 self
.assertEquals(12, self
.get_answer())
127 def testAsserCalls_succeeds_withAction(self
):
128 with self
.assertCall(
129 self
.call
.adb
.Shell('echo hello'), self
.ShellError()):
130 with self
.assertRaises(ValueError):
131 self
.adb
.Shell('echo hello')
133 def testAssertCalls_fails_tooManyCalls(self
):
134 with self
.assertRaises(AssertionError):
135 with self
.assertCalls(self
.call
.adb
.IsOnline()):
139 def testAssertCalls_fails_tooFewCalls(self
):
140 with self
.assertRaises(AssertionError):
141 with self
.assertCalls(self
.call
.adb
.IsOnline()):
144 def testAssertCalls_succeeds_extraCalls(self
):
145 # we are not watching Reboot, so the assertion succeeds
146 with self
.assertCalls(self
.call
.adb
.IsOnline()):
150 def testAssertCalls_fails_extraCalls(self
):
151 self
.watchCalls([self
.call
.adb
.Reboot
])
152 # this time we are also watching Reboot, so the assertion fails
153 with self
.assertRaises(AssertionError):
154 with self
.assertCalls(self
.call
.adb
.IsOnline()):
158 def testAssertCalls_succeeds_NoCalls(self
):
159 self
.watchMethodCalls(self
.call
.adb
) # we are watching all adb methods
160 with self
.assertCalls():
163 def testAssertCalls_fails_NoCalls(self
):
164 self
.watchMethodCalls(self
.call
.adb
)
165 with self
.assertRaises(AssertionError):
166 with self
.assertCalls():
170 if __name__
== '__main__':
171 logging
.getLogger().setLevel(logging
.DEBUG
)
172 unittest
.main(verbosity
=2)