blob: c446883770cd179cc83db392f989a168601a0015 [file] [log] [blame]
Josh Gao908c4db2017-11-08 14:35:26 -08001#
2# Copyright (C) 2017 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16import os
17import unittest
18import mock
19
20import adb
21
22class GetDeviceTest(unittest.TestCase):
23 def setUp(self):
24 self.android_serial = os.getenv('ANDROID_SERIAL')
25 if 'ANDROID_SERIAL' in os.environ:
26 del os.environ['ANDROID_SERIAL']
27
28 def tearDown(self):
29 if self.android_serial is not None:
30 os.environ['ANDROID_SERIAL'] = self.android_serial
31 else:
32 if 'ANDROID_SERIAL' in os.environ:
33 del os.environ['ANDROID_SERIAL']
34
35 @mock.patch('adb.device.get_devices')
36 def test_explicit(self, mock_get_devices):
37 mock_get_devices.return_value = ['foo', 'bar']
38 device = adb.get_device('foo')
39 self.assertEqual(device.serial, 'foo')
40
41 @mock.patch('adb.device.get_devices')
42 def test_from_env(self, mock_get_devices):
43 mock_get_devices.return_value = ['foo', 'bar']
44 os.environ['ANDROID_SERIAL'] = 'foo'
45 device = adb.get_device()
46 self.assertEqual(device.serial, 'foo')
47
48 @mock.patch('adb.device.get_devices')
49 def test_arg_beats_env(self, mock_get_devices):
50 mock_get_devices.return_value = ['foo', 'bar']
51 os.environ['ANDROID_SERIAL'] = 'bar'
52 device = adb.get_device('foo')
53 self.assertEqual(device.serial, 'foo')
54
55 @mock.patch('adb.device.get_devices')
56 def test_no_such_device(self, mock_get_devices):
57 mock_get_devices.return_value = ['foo', 'bar']
58 self.assertRaises(adb.DeviceNotFoundError, adb.get_device, ['baz'])
59
60 os.environ['ANDROID_SERIAL'] = 'baz'
61 self.assertRaises(adb.DeviceNotFoundError, adb.get_device)
62
63 @mock.patch('adb.device.get_devices')
64 def test_unique_device(self, mock_get_devices):
65 mock_get_devices.return_value = ['foo']
66 device = adb.get_device()
67 self.assertEqual(device.serial, 'foo')
68
69 @mock.patch('adb.device.get_devices')
70 def test_no_unique_device(self, mock_get_devices):
71 mock_get_devices.return_value = ['foo', 'bar']
72 self.assertRaises(adb.NoUniqueDeviceError, adb.get_device)
73
74
75def main():
76 suite = unittest.TestLoader().loadTestsFromName(__name__)
77 unittest.TextTestRunner(verbosity=3).run(suite)
78
79if __name__ == '__main__':
80 main()