Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
|
rnephew (Reviews Here)
2016/03/22 18:43:14
Is it possible for these tests to just live in fin
| |
| 2 | |
| 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 import unittest | |
| 8 import logging | |
| 9 | |
| 10 from devil.utils import find_usb_devices | |
| 11 from devil import devil_env | |
| 12 with devil_env.SysPath(devil_env.PYMOCK_PATH): | |
| 13 import mock # pylint: disable=import-error | |
| 14 | |
| 15 def setup_battor_test(serial, tty, battor): | |
| 16 serial_mapper = mock.Mock(return_value=serial) | |
| 17 tty_mapper = mock.Mock(return_value=tty) | |
| 18 battor_lister = mock.Mock(return_value=battor) | |
| 19 devtree = mock.Mock(return_value=None) | |
| 20 is_battor = mock.Mock(side_effect=lambda x, y: x in battor) | |
| 21 find_usb_devices.GetAllPhysicalPortToSerialMaps = serial_mapper | |
| 22 find_usb_devices.GetAllPhysicalPortToTTYMaps = tty_mapper | |
| 23 find_usb_devices.GetBattorList = battor_lister | |
| 24 find_usb_devices.GetBusNumberToDeviceTreeMap = devtree | |
| 25 find_usb_devices.IsBattor = is_battor | |
| 26 | |
| 27 class BattorMappingTest(unittest.TestCase): | |
| 28 def test_port_map_one_battor(self): | |
| 29 # simulate setup with no hubs, just one BattOr attached | |
| 30 setup_battor_test([], [], ['ttyUSB0']) | |
| 31 self.assertEqual(find_usb_devices.GetBattorPathFromPhoneSerial('Phn2'), | |
| 32 '/dev/ttyUSB0') | |
| 33 | |
| 34 def test_port_map_three_battors(self): | |
| 35 # simulate setup with two hubs; first has three phones, | |
| 36 # second has three BattOrs | |
| 37 setup_battor_test([{1:'Phn1', 2:'Phn2', 3:'Phn3'}, | |
| 38 {1:'Bat1', 2:'Bat2', 3:'Bat3'}], | |
| 39 [{}, | |
| 40 {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], | |
| 41 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) | |
| 42 self.assertEqual(find_usb_devices.GetBattorPathFromPhoneSerial('Phn2'), | |
| 43 '/dev/ttyUSB1') | |
| 44 | |
| 45 def test_port_map_incorrect_ports(self): | |
| 46 # simulate setup with two hubs; first has three phones, | |
| 47 # second has three BattOrs, but they're plugged into the | |
| 48 # wrong ports | |
| 49 setup_battor_test([{1:'Phn1', 2:'Phn2', 3:'Phn3'}, | |
| 50 {1:'Bat1', 3:'Bat2', 4:'Bat3'}], | |
| 51 [{}, | |
| 52 {1:'ttyUSB0', 3:'ttyUSB1', 4:'ttyUSB2'}], | |
| 53 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) | |
| 54 with self.assertRaises(find_usb_devices.BattorError): | |
| 55 find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') | |
| 56 | |
| 57 def test_port_map_incorrect_serial(self): | |
| 58 # simulate setup with two hubs; first has three phones, | |
| 59 # second has three BattOrs, none of the serial numbers match | |
| 60 # the serial number provided | |
| 61 setup_battor_test([{1:'Phn1', 2:'Phn00', 3:'Phn3'}, | |
| 62 {1:'Bat1', 2:'Bat2', 3:'Bat3'}], | |
| 63 [{}, | |
| 64 {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], | |
| 65 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) | |
| 66 with self.assertRaises(find_usb_devices.BattorError): | |
| 67 find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') | |
| 68 | |
| 69 def test_port_map_battors_on_same_port(self): | |
| 70 # simulate setup with three hubs; first has three phones, | |
| 71 # second and third each have three BattOrs, this should fail because | |
| 72 # it can't tell which one to use | |
| 73 setup_battor_test([{1:'Phn1', 2:'Phn00', 3:'Phn3'}, | |
| 74 {1:'Bat1', 2:'Bat2', 3:'Bat3'}, | |
| 75 {1:'Bat4', 2:'Bat5', 3:'Bat6'}], | |
| 76 [{}, | |
| 77 {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}, | |
| 78 {1:'ttyUSB3', 2:'ttyUSB4', 3:'ttyUSB5'}], | |
| 79 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', | |
| 80 'ttyUSB3', 'ttyUSB4', 'ttyUSB5']) | |
| 81 with self.assertRaises(find_usb_devices.BattorError): | |
| 82 find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') | |
| 83 | |
| 84 def test_port_map_duplicate_serial(self): | |
| 85 # simulate setup with two hubs; first has three phones, | |
| 86 # second has three BattOrs, serial number is duplicated | |
| 87 setup_battor_test([{1:'Phn2', 2:'Phn2', 3:'Phn3'}, | |
| 88 {1:'Bat1', 2:'Bat2', 3:'Bat3'}], | |
| 89 [{}, | |
| 90 {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], | |
| 91 ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) | |
| 92 with self.assertRaises(find_usb_devices.BattorError): | |
| 93 find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') | |
| 94 | |
| 95 | |
| 96 if __name__ == "__main__": | |
| 97 logging.getLogger().setLevel(logging.DEBUG) | |
| 98 unittest.main(verbosity=2) | |
| OLD | NEW |