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

Unified Diff: common/battor/battor/battor_wrapper_unittest.py

Issue 1828143004: [Battor] Add python wrapper for communicating with battor_agent_binary (Closed) Base URL: git@github.com:catapult-project/catapult@master
Patch Set: Created 4 years, 8 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « common/battor/battor/battor_wrapper.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: common/battor/battor/battor_wrapper_unittest.py
diff --git a/common/battor/battor/battor_wrapper_unittest.py b/common/battor/battor/battor_wrapper_unittest.py
new file mode 100644
index 0000000000000000000000000000000000000000..cf4454f536d92f197ddfbe40c0ae39e18a79c2da
--- /dev/null
+++ b/common/battor/battor/battor_wrapper_unittest.py
@@ -0,0 +1,166 @@
+# Copyright 2016 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 logging
+import unittest
+
+import dependency_manager
+
+from battor import battor_wrapper
+from devil.utils import battor_device_mapping
+from devil.utils import find_usb_devices
+
+
+class DependencyManagerMock(object):
+ def __init__(self, _):
+ self._fetch_return = 'path'
+
+ def FetchPath(self, _, *unused):
+ return self._fetch_return
+
+
+class PopenMock(object):
+ def __init__(self, *unused):
+ pass
+
+
+class BattorWrapperTest(unittest.TestCase):
+ def setUp(self):
+ self.battor = None
+ self._MAPPING = {}
+ self._IS_BATTOR = True
+ self._battor_list = ['battor1']
+ self._should_pass = True
+ self._fake_map = {'battor1': 'device1'}
+
+ self._get_battor_path_from_phone_serial = (
+ battor_device_mapping.GetBattorPathFromPhoneSerial)
+ self._get_bus_number_to_device_tree_map = (
+ find_usb_devices.GetBusNumberToDeviceTreeMap)
+ self._dependency_manager = dependency_manager.DependencyManager
+ self._get_battor_list = battor_device_mapping.GetBattorList
+ self._is_battor = battor_device_mapping.IsBattor
+ self._generate_serial_map = battor_device_mapping.GenerateSerialMap
+
+
+ battor_device_mapping.GetBattorPathFromPhoneSerial = (
+ lambda x, serial_map_file=None, serial_map=None: x + '_battor')
+ find_usb_devices.GetBusNumberToDeviceTreeMap = lambda fast=False: True
+ dependency_manager.DependencyManager = DependencyManagerMock
+ battor_device_mapping.GetBattorList = lambda x: self._battor_list
+ battor_device_mapping.IsBattor = lambda x, y: self._IS_BATTOR
+ battor_device_mapping.GenerateSerialMap = lambda: self._fake_map
+
+ def tearDown(self):
+ battor_device_mapping.GetBattorPathFromPhoneSerial = (
+ self._get_battor_path_from_phone_serial)
+ find_usb_devices.GetBusNumberToDeviceTreeMap = (
+ self._get_bus_number_to_device_tree_map)
+ dependency_manager.DependencyManager = self._dependency_manager
+ battor_device_mapping.GetBattorList = self._get_battor_list
+ battor_device_mapping.IsBattor = self._is_battor
+ battor_device_mapping.GenerateSerialMap = self._generate_serial_map
+
+ def _DefaultBattorReplacements(self):
+ self._battor._StartShellImpl = lambda *unused: PopenMock
+ self._battor.IsShellRunning = lambda *unused: True
+ self._battor._SendBattorCommandImpl = lambda x, return_results: 'Done.\n'
+ self._battor._StopTracingImpl = lambda *unused: ('Done.\n', None)
+
+ def testInitAndroidWithBattor(self):
+ self._battor = battor_wrapper.BattorWrapper('android', android_device='abc')
+ self.assertEquals(self._battor._battor_path, 'abc_battor')
+
+ def testInitAndroidWithoutBattor(self):
+ self._battor_list = []
+ self._fake_map = {}
+ battor_device_mapping.GetBattorPathFromPhoneSerial = (
+ self._get_battor_path_from_phone_serial)
+ with self.assertRaises(KeyError):
+ self._battor = battor_wrapper.BattorWrapper('android',
+ android_device='abc')
+
+ def testInitBattorPathIsBattor(self):
+ battor_path = 'battor/path/here'
+ self._battor = battor_wrapper.BattorWrapper(
+ 'android', android_device='abc', battor_path=battor_path)
+ self.assertEquals(self._battor._battor_path, battor_path)
+
+ def testInitNonAndroidWithBattor(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self.assertEquals(self._battor._battor_path, '/dev/battor1')
+
+ def testInitNonAndroidWithMultipleBattor(self):
+ self._battor_list.append('battor2')
+ with self.assertRaises(battor_wrapper.BattorError):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+
+ def testInitNonAndroidWithoutBattor(self):
+ self._battor_list = []
+ with self.assertRaises(battor_wrapper.BattorError):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+
+ def testStartShellPass(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor.StartShell()
+ self.assertIsNotNone(self._battor._battor_shell)
+
+ def testStartShellDoubleStart(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor.StartShell()
+ with self.assertRaises(AssertionError):
+ self._battor.StartShell()
+
+ def testStartShellFail(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor.IsShellRunning = lambda *unused: False
+ with self.assertRaises(AssertionError):
+ self._battor.StartShell()
+
+ def testStartTracingPass(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor.StartShell()
+ self._battor.StartTracing()
+ self.assertTrue(self._battor._tracing)
+
+ def testStartTracingDoubleStart(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor.StartShell()
+ self._battor.StartTracing()
+ with self.assertRaises(AssertionError):
+ self._battor.StartTracing()
+
+ def testStartTracingCommandFails(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor._SendBattorCommandImpl = lambda x, return_results: 'Fail.\n'
+ self._battor.StartShell()
+ with self.assertRaises(battor_wrapper.BattorError):
+ self._battor.StartTracing()
+
+ def testStopTracingPass(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ self._battor.StartShell()
+ self._battor.StartTracing()
+ self._battor.IsShellRunning = lambda *unused: False
+ self._battor.StopTracing()
+ self.assertFalse(self._battor._tracing)
+
+ def testStopTracingNotRunning(self):
+ self._battor = battor_wrapper.BattorWrapper('platform')
+ self._DefaultBattorReplacements()
+ with self.assertRaises(AssertionError):
+ self._battor.StopTracing()
+
+
+if __name__ == '__main__':
+ logging.getLogger().setLevel(logging.DEBUG)
+ unittest.main(verbosity=2)
+
« no previous file with comments | « common/battor/battor/battor_wrapper.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698