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