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

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: 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.device_serial)
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.device_serial)
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.device_serial)
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.device_serial)
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 1467 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_noPublicAdbCalls(self):
1558 with self.assertNoAdbCalls(): 1560 with self.assertCalls():
1559 self.assertEqual('0123456789abcdef', str(self.device)) 1561 self.assertEqual('0123456789abcdef', str(self.device))
1560 1562
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 1563
1573 if __name__ == '__main__': 1564 if __name__ == '__main__':
1574 logging.getLogger().setLevel(logging.DEBUG) 1565 logging.getLogger().setLevel(logging.DEBUG)
1575 unittest.main(verbosity=2) 1566 unittest.main(verbosity=2)
1576 1567
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698