1 # Copyright 2016 The Chromium Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import logging 6 import platform 7 import os 8 import sys 9 import time 10 import unittest 11 12 if __name__ == '__main__': 13 sys.path.append( 14 os.path.join(os.path.dirname(__file__), '..')) 15 16 from battor import battor_wrapper 17 from devil.utils import battor_device_mapping 18 from devil.utils import find_usb_devices 19 import py_utils 20 from py_utils import cloud_storage 21 22 23 _SUPPORTED_CQ_PLATFORMS = ['win', 'linux', 'mac'] 24 25 class BattOrWrapperDeviceTest(unittest.TestCase): 26 def setUp(self): 27 self._platform = py_utils.GetHostOsName() 28 self._battor_list = None 29 30 if self._platform == 'linux': 31 device_tree = find_usb_devices.GetBusNumberToDeviceTreeMap() 32 self._battor_list = battor_device_mapping.GetBattOrList(device_tree) 33 34 if not battor_wrapper.IsBattOrConnected(self._platform): 35 self._battor_list = [] 36 37 def testFullRun(self): 38 # If battor_list is an empty list, a BattOr was expected but not found. 39 if self._battor_list is not None and not self._battor_list: 40 logging.critical('No BattOrs attached. Cannot run tests.') 41 return 42 43 if self._platform not in _SUPPORTED_CQ_PLATFORMS: 44 logging.critical('Platform %s is not supported on CQ.' % self._platform) 45 return 46 47 48 battor_path = (None if not self._battor_list 49 else '/dev/%s' % self._battor_list[0]) 50 battor = battor_wrapper.BattOrWrapper( 51 self._platform, battor_path=battor_path, 52 serial_log_bucket=cloud_storage.TELEMETRY_OUTPUT) 53 try: 54 battor.StartShell() 55 self.assertTrue(isinstance(battor.GetFirmwareGitHash(), basestring)) 56 # We expect the git hash to be a valid 6 character hexstring. This will 57 # throw a ValueError exception otherwise. 58 int(battor.GetFirmwareGitHash(), 16) 59 self.assertTrue(len(battor.GetFirmwareGitHash()) == 7) 60 battor.StopShell() 61 62 battor.StartShell() 63 battor.StartTracing() 64 # TODO(rnephew): This sleep is required for now because crbug.com/602266 65 # causes the BattOr to crash when the trace time is too short. Once that 66 # bug is fixed, we should remove this delay. 67 time.sleep(1) 68 battor.RecordClockSyncMarker('abc') 69 # Sleep here because clock sync marker will be flaky if not. 70 time.sleep(1) 71 battor.StopTracing() 72 73 # Below is a work around for crbug.com/603309. On this short of a trace, 5 74 # seconds is enough to ensure that the trace will finish flushing to the 75 # file. The process is then killed so that BattOrWrapper knows that the 76 # process has been closed after tracing stops. 77 if self._platform == 'win': 78 time.sleep(5) 79 battor._battor_shell.kill() 80 results = battor.CollectTraceData().splitlines() 81 except: 82 if battor._battor_shell is not None: 83 battor._battor_shell.kill() 84 battor._battor_shell = None 85 raise 86 87 self.assertTrue('# BattOr' in results[0]) 88 self.assertTrue('# voltage_range' in results[1]) 89 self.assertTrue('# current_range' in results[2]) 90 self.assertTrue('# sample_rate' in results[3]) 91 # First line with results. Should be 3 'words'. 92 self.assertTrue(len(results[4].split()) == 3) 93 clock_sync_found = False 94 for entry in results: 95 if '<abc>' in entry: 96 clock_sync_found = True 97 break 98 self.assertTrue(clock_sync_found, 'BattOr Data:%s\n' % repr(results)) 99 100 101 if __name__ == '__main__': 102 logging.getLogger().setLevel(logging.DEBUG) 103 unittest.main(verbosity=2) 104