Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(526)

Side by Side Diff: build/android/pylib/device/device_utils_test.py

Issue 762883002: Make device serial required in AdbWrapper/DeviceUtils (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: renamed: exclude -> ignore Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved. 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 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """ 6 """
7 Unit tests for the contents of device_utils.py (mostly DeviceUtils). 7 Unit tests for the contents of device_utils.py (mostly DeviceUtils).
8 """ 8 """
9 9
10 # pylint: disable=C0321 10 # pylint: disable=C0321
(...skipping 25 matching lines...) Expand all
36 sys.path.append(os.path.join( 36 sys.path.append(os.path.join(
37 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) 37 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock'))
38 import mock # pylint: disable=F0401 38 import mock # pylint: disable=F0401
39 39
40 40
41 class DeviceUtilsTest(unittest.TestCase): 41 class DeviceUtilsTest(unittest.TestCase):
42 42
43 def testInitWithStr(self): 43 def testInitWithStr(self):
44 serial_as_str = str('0123456789abcdef') 44 serial_as_str = str('0123456789abcdef')
45 d = device_utils.DeviceUtils('0123456789abcdef') 45 d = device_utils.DeviceUtils('0123456789abcdef')
46 self.assertEqual(serial_as_str, d.old_interface.GetDevice()) 46 self.assertEqual(serial_as_str, d.adb.GetDeviceSerial())
47 47
48 def testInitWithUnicode(self): 48 def testInitWithUnicode(self):
49 serial_as_unicode = unicode('fedcba9876543210') 49 serial_as_unicode = unicode('fedcba9876543210')
50 d = device_utils.DeviceUtils(serial_as_unicode) 50 d = device_utils.DeviceUtils(serial_as_unicode)
51 self.assertEqual(serial_as_unicode, d.old_interface.GetDevice()) 51 self.assertEqual(serial_as_unicode, d.adb.GetDeviceSerial())
52 52
53 def testInitWithAdbWrapper(self): 53 def testInitWithAdbWrapper(self):
54 serial = '123456789abcdef0' 54 serial = '123456789abcdef0'
55 a = adb_wrapper.AdbWrapper(serial) 55 a = adb_wrapper.AdbWrapper(serial)
56 d = device_utils.DeviceUtils(a) 56 d = device_utils.DeviceUtils(a)
57 self.assertEqual(serial, d.old_interface.GetDevice()) 57 self.assertEqual(serial, d.adb.GetDeviceSerial())
58 58
59 def testInitWithAndroidCommands(self): 59 def testInitWithAndroidCommands(self):
60 serial = '0fedcba987654321' 60 serial = '0fedcba987654321'
61 a = android_commands.AndroidCommands(device=serial) 61 a = android_commands.AndroidCommands(device=serial)
62 d = device_utils.DeviceUtils(a) 62 d = device_utils.DeviceUtils(a)
63 self.assertEqual(serial, d.old_interface.GetDevice()) 63 self.assertEqual(serial, d.adb.GetDeviceSerial())
64 64
65 def testInitWithNone(self): 65 def testInitWithMissing_fails(self):
66 d = device_utils.DeviceUtils(None) 66 with self.assertRaises(ValueError):
67 self.assertIsNone(d.old_interface.GetDevice()) 67 device_utils.DeviceUtils(None)
68 with self.assertRaises(ValueError):
69 device_utils.DeviceUtils('')
68 70
69 71
70 class MockTempFile(object): 72 class MockTempFile(object):
71 73
72 def __init__(self, name='/tmp/some/file'): 74 def __init__(self, name='/tmp/some/file'):
73 self.file = mock.MagicMock(spec=file) 75 self.file = mock.MagicMock(spec=file)
74 self.file.name = name 76 self.file.name = name
75 77
76 def __enter__(self): 78 def __enter__(self):
77 return self.file 79 return self.file
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 241
240 class DeviceUtilsNewImplTest(mock_calls.TestCase): 242 class DeviceUtilsNewImplTest(mock_calls.TestCase):
241 243
242 def setUp(self): 244 def setUp(self):
243 test_serial = '0123456789abcdef' 245 test_serial = '0123456789abcdef'
244 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper) 246 self.adb = mock.Mock(spec=adb_wrapper.AdbWrapper)
245 self.adb.__str__ = mock.Mock(return_value=test_serial) 247 self.adb.__str__ = mock.Mock(return_value=test_serial)
246 self.adb.GetDeviceSerial.return_value = test_serial 248 self.adb.GetDeviceSerial.return_value = test_serial
247 self.device = device_utils.DeviceUtils( 249 self.device = device_utils.DeviceUtils(
248 self.adb, default_timeout=10, default_retries=0) 250 self.adb, default_timeout=10, default_retries=0)
249 self.watchMethodCalls(self.call.adb) 251 self.watchMethodCalls(self.call.adb, ignore=['GetDeviceSerial'])
250 252
251 def ShellError(self, output=None, exit_code=1): 253 def ShellError(self, output=None, exit_code=1):
252 def action(cmd, *args, **kwargs): 254 def action(cmd, *args, **kwargs):
253 raise device_errors.AdbCommandFailedError( 255 raise device_errors.AdbCommandFailedError(
254 cmd, output, exit_code, str(self.device)) 256 cmd, output, exit_code, str(self.device))
255 if output is None: 257 if output is None:
256 output = 'Permission denied\n' 258 output = 'Permission denied\n'
257 return action 259 return action
258 260
259 def TimeoutError(self, msg=None): 261 def TimeoutError(self, msg=None):
(...skipping 1285 matching lines...) Expand 10 before | Expand all | Expand 10 after
1545 }, 1547 },
1546 self.device.GetMemoryUsageForPid(1234)) 1548 self.device.GetMemoryUsageForPid(1234))
1547 1549
1548 def testGetMemoryUsageForPid_invalidPid(self): 1550 def testGetMemoryUsageForPid_invalidPid(self):
1549 with self.assertCalls( 1551 with self.assertCalls(
1550 "adb -s 0123456789abcdef shell 'showmap 4321'", 1552 "adb -s 0123456789abcdef shell 'showmap 4321'",
1551 'cannot open /proc/4321/smaps: No such file or directory\r\n'): 1553 'cannot open /proc/4321/smaps: No such file or directory\r\n'):
1552 self.assertEqual({}, self.device.GetMemoryUsageForPid(4321)) 1554 self.assertEqual({}, self.device.GetMemoryUsageForPid(4321))
1553 1555
1554 1556
1555 class DeviceUtilsStrTest(DeviceUtilsOldImplTest): 1557 class DeviceUtilsStrTest(DeviceUtilsNewImplTest):
1556 1558
1557 def testStr_noAdbCalls(self): 1559 def testStr_returnsSerial(self):
1558 with self.assertNoAdbCalls(): 1560 with self.assertCalls(
1561 (self.call.adb.GetDeviceSerial(), '0123456789abcdef')):
1559 self.assertEqual('0123456789abcdef', str(self.device)) 1562 self.assertEqual('0123456789abcdef', str(self.device))
1560 1563
1561 def testStr_noSerial(self):
1562 self.device = device_utils.DeviceUtils(None)
1563 with self.assertCalls('adb get-serialno', '0123456789abcdef'):
1564 self.assertEqual('0123456789abcdef', str(self.device))
1565
1566 def testStr_noSerial_noDevices(self):
1567 self.device = device_utils.DeviceUtils(None)
1568 with self.assertCalls('adb get-serialno', 'unknown'), (
1569 self.assertRaises(device_errors.NoDevicesError)):
1570 str(self.device)
1571
1572 1564
1573 if __name__ == '__main__': 1565 if __name__ == '__main__':
1574 logging.getLogger().setLevel(logging.DEBUG) 1566 logging.getLogger().setLevel(logging.DEBUG)
1575 unittest.main(verbosity=2) 1567 unittest.main(verbosity=2)
1576 1568
OLDNEW
« no previous file with comments | « build/android/pylib/device/device_utils.py ('k') | build/android/pylib/instrumentation/test_runner_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698