OLD | NEW |
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 |
11 # pylint: disable=W0212 | 11 # pylint: disable=W0212 |
12 # pylint: disable=W0613 | 12 # pylint: disable=W0613 |
13 | 13 |
14 import collections | 14 import collections |
15 import datetime | 15 import datetime |
16 import logging | 16 import logging |
17 import os | 17 import os |
18 import re | 18 import re |
19 import sys | 19 import sys |
20 import unittest | 20 import unittest |
21 | 21 |
22 from pylib import android_commands | 22 from pylib import android_commands |
23 from pylib import cmd_helper | 23 from pylib import cmd_helper |
24 from pylib import constants | 24 from pylib import constants |
25 from pylib import device_signal | 25 from pylib import device_signal |
26 from pylib.device import adb_wrapper | 26 from pylib.device import adb_wrapper |
| 27 from pylib.device import device_blacklist |
27 from pylib.device import device_errors | 28 from pylib.device import device_errors |
28 from pylib.device import device_utils | 29 from pylib.device import device_utils |
29 from pylib.device import intent | 30 from pylib.device import intent |
30 from pylib.sdk import split_select | 31 from pylib.sdk import split_select |
31 from pylib.utils import mock_calls | 32 from pylib.utils import mock_calls |
32 | 33 |
33 # RunCommand from third_party/android_testrunner/run_command.py is mocked | 34 # RunCommand from third_party/android_testrunner/run_command.py is mocked |
34 # below, so its path needs to be in sys.path. | 35 # below, so its path needs to be in sys.path. |
35 sys.path.append(os.path.join( | 36 sys.path.append(os.path.join( |
36 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) | 37 constants.DIR_SOURCE_ROOT, 'third_party', 'android_testrunner')) |
(...skipping 1823 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1860 client_cache_one = self.device.GetClientCache('ClientOne') | 1861 client_cache_one = self.device.GetClientCache('ClientOne') |
1861 client_cache_one['test'] = 1 | 1862 client_cache_one['test'] = 1 |
1862 client_cache_two = self.device.GetClientCache('ClientOne') | 1863 client_cache_two = self.device.GetClientCache('ClientOne') |
1863 self.assertEqual(client_cache_one, {'test': 1}) | 1864 self.assertEqual(client_cache_one, {'test': 1}) |
1864 self.assertEqual(client_cache_two, {'test': 1}) | 1865 self.assertEqual(client_cache_two, {'test': 1}) |
1865 self.device._ClearCache() | 1866 self.device._ClearCache() |
1866 self.assertEqual(client_cache_one, {}) | 1867 self.assertEqual(client_cache_one, {}) |
1867 self.assertEqual(client_cache_two, {}) | 1868 self.assertEqual(client_cache_two, {}) |
1868 | 1869 |
1869 | 1870 |
1870 class DeviceUtilsParallelTest(mock_calls.TestCase): | |
1871 | |
1872 def testParallel_default(self): | |
1873 test_serials = ['0123456789abcdef', 'fedcba9876543210'] | |
1874 with self.assertCall( | |
1875 mock.call.pylib.device.device_utils.DeviceUtils.HealthyDevices(), | |
1876 [device_utils.DeviceUtils(s) for s in test_serials]): | |
1877 parallel_devices = device_utils.DeviceUtils.parallel() | |
1878 for serial, device in zip(test_serials, parallel_devices.pGet(None)): | |
1879 self.assertTrue(isinstance(device, device_utils.DeviceUtils)) | |
1880 self.assertEquals(serial, device.adb.GetDeviceSerial()) | |
1881 | |
1882 def testParallel_noDevices(self): | |
1883 with self.assertCall( | |
1884 mock.call.pylib.device.device_utils.DeviceUtils.HealthyDevices(), []): | |
1885 with self.assertRaises(device_errors.NoDevicesError): | |
1886 device_utils.DeviceUtils.parallel() | |
1887 | |
1888 | |
1889 class DeviceUtilsHealthyDevicesTest(mock_calls.TestCase): | 1871 class DeviceUtilsHealthyDevicesTest(mock_calls.TestCase): |
1890 | 1872 |
1891 def _createAdbWrapperMock(self, serial, is_ready=True): | 1873 def _createAdbWrapperMock(self, serial, is_ready=True): |
1892 adb = _AdbWrapperMock(serial) | 1874 adb = _AdbWrapperMock(serial) |
1893 adb.is_ready = is_ready | 1875 adb.is_ready = is_ready |
1894 return adb | 1876 return adb |
1895 | 1877 |
1896 def testHealthyDevices_default(self): | 1878 def testHealthyDevices_emptyBlacklist(self): |
1897 test_serials = ['0123456789abcdef', 'fedcba9876543210'] | 1879 test_serials = ['0123456789abcdef', 'fedcba9876543210'] |
1898 with self.assertCalls( | 1880 with self.assertCalls( |
1899 (mock.call.pylib.device.device_blacklist.ReadBlacklist(), []), | |
1900 (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(), | 1881 (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(), |
1901 [self._createAdbWrapperMock(s) for s in test_serials])): | 1882 [self._createAdbWrapperMock(s) for s in test_serials])): |
1902 devices = device_utils.DeviceUtils.HealthyDevices() | 1883 blacklist = mock.NonCallableMock(**{'Read.return_value': []}) |
| 1884 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) |
1903 for serial, device in zip(test_serials, devices): | 1885 for serial, device in zip(test_serials, devices): |
1904 self.assertTrue(isinstance(device, device_utils.DeviceUtils)) | 1886 self.assertTrue(isinstance(device, device_utils.DeviceUtils)) |
1905 self.assertEquals(serial, device.adb.GetDeviceSerial()) | 1887 self.assertEquals(serial, device.adb.GetDeviceSerial()) |
1906 | 1888 |
1907 def testHealthyDevices_blacklisted(self): | 1889 def testHealthyDevices_blacklist(self): |
1908 test_serials = ['0123456789abcdef', 'fedcba9876543210'] | 1890 test_serials = ['0123456789abcdef', 'fedcba9876543210'] |
1909 with self.assertCalls( | 1891 with self.assertCalls( |
1910 (mock.call.pylib.device.device_blacklist.ReadBlacklist(), | |
1911 ['fedcba9876543210']), | |
1912 (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(), | 1892 (mock.call.pylib.device.adb_wrapper.AdbWrapper.Devices(), |
1913 [self._createAdbWrapperMock(s) for s in test_serials])): | 1893 [self._createAdbWrapperMock(s) for s in test_serials])): |
1914 devices = device_utils.DeviceUtils.HealthyDevices() | 1894 blacklist = mock.NonCallableMock( |
| 1895 **{'Read.return_value': ['fedcba9876543210']}) |
| 1896 devices = device_utils.DeviceUtils.HealthyDevices(blacklist) |
1915 self.assertEquals(1, len(devices)) | 1897 self.assertEquals(1, len(devices)) |
1916 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) | 1898 self.assertTrue(isinstance(devices[0], device_utils.DeviceUtils)) |
1917 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) | 1899 self.assertEquals('0123456789abcdef', devices[0].adb.GetDeviceSerial()) |
1918 | 1900 |
1919 | 1901 |
1920 if __name__ == '__main__': | 1902 if __name__ == '__main__': |
1921 logging.getLogger().setLevel(logging.DEBUG) | 1903 logging.getLogger().setLevel(logging.DEBUG) |
1922 unittest.main(verbosity=2) | 1904 unittest.main(verbosity=2) |
1923 | 1905 |
OLD | NEW |