Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / build / android / devil / utils / mock_calls_test.py
blobae2acbb8322c372d1155d030ab0e242124570145
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.
6 """
7 Unit tests for the contents of mock_calls.py.
8 """
10 import logging
11 import os
12 import sys
13 import unittest
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):
25 def __str__(self):
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)
31 def IsOnline(self):
32 logging.debug('(device %s) checking device online', self)
33 return True
35 def Shell(self, cmd):
36 logging.debug('(device %s) running command %r', self, cmd)
37 return "nice output\n"
39 def Reboot(self):
40 logging.debug('(device %s) rebooted!', self)
42 @property
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):
49 def setUp(self):
50 self.adb = _DummyAdb()
52 def ShellError(self):
53 def action(cmd):
54 raise ValueError('(device %s) command %r is not nice' % (self.adb, cmd))
55 return action
57 def get_answer(self):
58 logging.debug("called 'get_answer' of %r object", self)
59 return 42
61 def echo(self, thing):
62 logging.debug("called 'echo' of %r object", self)
63 return thing
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()):
136 self.adb.IsOnline()
137 self.adb.IsOnline()
139 def testAssertCalls_fails_tooFewCalls(self):
140 with self.assertRaises(AssertionError):
141 with self.assertCalls(self.call.adb.IsOnline()):
142 pass
144 def testAssertCalls_succeeds_extraCalls(self):
145 # we are not watching Reboot, so the assertion succeeds
146 with self.assertCalls(self.call.adb.IsOnline()):
147 self.adb.IsOnline()
148 self.adb.Reboot()
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()):
155 self.adb.IsOnline()
156 self.adb.Reboot()
158 def testAssertCalls_succeeds_NoCalls(self):
159 self.watchMethodCalls(self.call.adb) # we are watching all adb methods
160 with self.assertCalls():
161 pass
163 def testAssertCalls_fails_NoCalls(self):
164 self.watchMethodCalls(self.call.adb)
165 with self.assertRaises(AssertionError):
166 with self.assertCalls():
167 self.adb.IsOnline()
170 if __name__ == '__main__':
171 logging.getLogger().setLevel(logging.DEBUG)
172 unittest.main(verbosity=2)