Chromium Code Reviews| Index: devil/devil/utils/battor_mapping_test.py |
| diff --git a/devil/devil/utils/battor_mapping_test.py b/devil/devil/utils/battor_mapping_test.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..7090fd3ad42c643f93e2bd9abc441f927f7d4a9d |
| --- /dev/null |
| +++ b/devil/devil/utils/battor_mapping_test.py |
| @@ -0,0 +1,98 @@ |
| +#!/usr/bin/env python |
|
rnephew (Reviews Here)
2016/03/22 18:43:14
Is it possible for these tests to just live in fin
|
| + |
| +# Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import unittest |
| +import logging |
| + |
| +from devil.utils import find_usb_devices |
| +from devil import devil_env |
| +with devil_env.SysPath(devil_env.PYMOCK_PATH): |
| + import mock # pylint: disable=import-error |
| + |
| +def setup_battor_test(serial, tty, battor): |
| + serial_mapper = mock.Mock(return_value=serial) |
| + tty_mapper = mock.Mock(return_value=tty) |
| + battor_lister = mock.Mock(return_value=battor) |
| + devtree = mock.Mock(return_value=None) |
| + is_battor = mock.Mock(side_effect=lambda x, y: x in battor) |
| + find_usb_devices.GetAllPhysicalPortToSerialMaps = serial_mapper |
| + find_usb_devices.GetAllPhysicalPortToTTYMaps = tty_mapper |
| + find_usb_devices.GetBattorList = battor_lister |
| + find_usb_devices.GetBusNumberToDeviceTreeMap = devtree |
| + find_usb_devices.IsBattor = is_battor |
| + |
| +class BattorMappingTest(unittest.TestCase): |
| + def test_port_map_one_battor(self): |
| + # simulate setup with no hubs, just one BattOr attached |
| + setup_battor_test([], [], ['ttyUSB0']) |
| + self.assertEqual(find_usb_devices.GetBattorPathFromPhoneSerial('Phn2'), |
| + '/dev/ttyUSB0') |
| + |
| + def test_port_map_three_battors(self): |
| + # simulate setup with two hubs; first has three phones, |
| + # second has three BattOrs |
| + setup_battor_test([{1:'Phn1', 2:'Phn2', 3:'Phn3'}, |
| + {1:'Bat1', 2:'Bat2', 3:'Bat3'}], |
| + [{}, |
| + {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], |
| + ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) |
| + self.assertEqual(find_usb_devices.GetBattorPathFromPhoneSerial('Phn2'), |
| + '/dev/ttyUSB1') |
| + |
| + def test_port_map_incorrect_ports(self): |
| + # simulate setup with two hubs; first has three phones, |
| + # second has three BattOrs, but they're plugged into the |
| + # wrong ports |
| + setup_battor_test([{1:'Phn1', 2:'Phn2', 3:'Phn3'}, |
| + {1:'Bat1', 3:'Bat2', 4:'Bat3'}], |
| + [{}, |
| + {1:'ttyUSB0', 3:'ttyUSB1', 4:'ttyUSB2'}], |
| + ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) |
| + with self.assertRaises(find_usb_devices.BattorError): |
| + find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') |
| + |
| + def test_port_map_incorrect_serial(self): |
| + # simulate setup with two hubs; first has three phones, |
| + # second has three BattOrs, none of the serial numbers match |
| + # the serial number provided |
| + setup_battor_test([{1:'Phn1', 2:'Phn00', 3:'Phn3'}, |
| + {1:'Bat1', 2:'Bat2', 3:'Bat3'}], |
| + [{}, |
| + {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], |
| + ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) |
| + with self.assertRaises(find_usb_devices.BattorError): |
| + find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') |
| + |
| + def test_port_map_battors_on_same_port(self): |
| + # simulate setup with three hubs; first has three phones, |
| + # second and third each have three BattOrs, this should fail because |
| + # it can't tell which one to use |
| + setup_battor_test([{1:'Phn1', 2:'Phn00', 3:'Phn3'}, |
| + {1:'Bat1', 2:'Bat2', 3:'Bat3'}, |
| + {1:'Bat4', 2:'Bat5', 3:'Bat6'}], |
| + [{}, |
| + {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}, |
| + {1:'ttyUSB3', 2:'ttyUSB4', 3:'ttyUSB5'}], |
| + ['ttyUSB0', 'ttyUSB1', 'ttyUSB2', |
| + 'ttyUSB3', 'ttyUSB4', 'ttyUSB5']) |
| + with self.assertRaises(find_usb_devices.BattorError): |
| + find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') |
| + |
| + def test_port_map_duplicate_serial(self): |
| + # simulate setup with two hubs; first has three phones, |
| + # second has three BattOrs, serial number is duplicated |
| + setup_battor_test([{1:'Phn2', 2:'Phn2', 3:'Phn3'}, |
| + {1:'Bat1', 2:'Bat2', 3:'Bat3'}], |
| + [{}, |
| + {1:'ttyUSB0', 2:'ttyUSB1', 3:'ttyUSB2'}], |
| + ['ttyUSB0', 'ttyUSB1', 'ttyUSB2']) |
| + with self.assertRaises(find_usb_devices.BattorError): |
| + find_usb_devices.GetBattorPathFromPhoneSerial('Phn2') |
| + |
| + |
| +if __name__ == "__main__": |
| + logging.getLogger().setLevel(logging.DEBUG) |
| + unittest.main(verbosity=2) |