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 pylib
import constants
16 from pylib
.utils
import mock_calls
18 sys
.path
.append(os
.path
.join(
19 constants
.DIR_SOURCE_ROOT
, 'third_party', 'pymock'))
20 import mock
# pylint: disable=F0401
23 class _DummyAdb(object):
25 return '0123456789abcdef'
27 def Push(self
, host_path
, device_path
):
28 logging
.debug('(device %s) pushing %r to %r', self
, host_path
, device_path
)
31 logging
.debug('(device %s) checking device online', self
)
35 logging
.debug('(device %s) running command %r', self
, cmd
)
36 return "nice output\n"
39 logging
.debug('(device %s) rebooted!', self
)
42 def build_version_sdk(self
):
43 logging
.debug('(device %s) getting build_version_sdk', self
)
44 return constants
.ANDROID_SDK_VERSION_CODES
.LOLLIPOP
47 class TestCaseWithAssertCallsTest(mock_calls
.TestCase
):
49 self
.adb
= _DummyAdb()
53 raise ValueError('(device %s) command %r is not nice' % (self
.adb
, cmd
))
57 logging
.debug("called 'get_answer' of %r object", self
)
60 def echo(self
, thing
):
61 logging
.debug("called 'echo' of %r object", self
)
64 def testCallTarget_succeds(self
):
65 self
.assertEquals(self
.adb
.Shell
,
66 self
.call_target(self
.call
.adb
.Shell
))
68 def testCallTarget_failsExternal(self
):
69 with self
.assertRaises(ValueError):
70 self
.call_target(mock
.call
.sys
.getcwd
)
72 def testCallTarget_failsUnknownAttribute(self
):
73 with self
.assertRaises(AttributeError):
74 self
.call_target(self
.call
.adb
.Run
)
76 def testCallTarget_failsIntermediateCalls(self
):
77 with self
.assertRaises(AttributeError):
78 self
.call_target(self
.call
.adb
.RunShell('cmd').append
)
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())
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'))
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())
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')
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
)
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())
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())
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')
135 def testAssertCalls_fails_tooManyCalls(self
):
136 with self
.assertRaises(AssertionError):
137 with self
.assertCalls(self
.call
.adb
.IsOnline()):
141 def testAssertCalls_fails_tooFewCalls(self
):
142 with self
.assertRaises(AssertionError):
143 with self
.assertCalls(self
.call
.adb
.IsOnline()):
146 def testAssertCalls_succeeds_extraCalls(self
):
147 # we are not watching Reboot, so the assertion succeeds
148 with self
.assertCalls(self
.call
.adb
.IsOnline()):
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()):
160 def testAssertCalls_succeeds_NoCalls(self
):
161 self
.watchMethodCalls(self
.call
.adb
) # we are watching all adb methods
162 with self
.assertCalls():
165 def testAssertCalls_fails_NoCalls(self
):
166 self
.watchMethodCalls(self
.call
.adb
)
167 with self
.assertRaises(AssertionError):
168 with self
.assertCalls():
172 if __name__
== '__main__':
173 logging
.getLogger().setLevel(logging
.DEBUG
)
174 unittest
.main(verbosity
=2)