1# Copyright 2016 The Chromium OS 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 5import logging 6import time 7from contextlib import contextmanager 8 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.cros.chameleon import chameleon_port_finder 11 12 13class _BaseChameleonMeasurer(object): 14 """Base class of performing measurement using Chameleon.""" 15 16 _TIME_WAIT_FADE_OUT = 10 17 _WAIT_TIME_LID_TRANSITION = 5 18 19 def __init__(self, cros_host, outputdir=None): 20 """Initializes the object.""" 21 raise NotImplementedError('_BaseChameleonMeasurer.__init__') 22 23 24 @contextmanager 25 def start_mirrored_mode_measurement(self): 26 """Starts the mirrored mode to measure. 27 28 It iterates the connection ports between DUT and Chameleon and uses 29 the first port. Sets DUT into the mirrored mode. Then yields the 30 connected ports. 31 32 It is used via a with statement, like the following: 33 34 measurer = LocalChameleonMeasurer(cros_host, args, chrome) 35 with measurer.start_mirrored_mode_measurement() as chameleon_port: 36 # chameleon_port is automatically plugged before this line. 37 do_some_test_on(chameleon_port) 38 # chameleon_port is automatically unplugged after this line. 39 40 @yields the first connected ChameleonVideoInput which is ensured plugged 41 before yielding. 42 43 @raises TestFail if no connected video port. 44 """ 45 finder = chameleon_port_finder.ChameleonVideoInputFinder( 46 self.chameleon, self.display_facade) 47 with finder.use_first_port() as chameleon_port: 48 logging.info('Used Chameleon port: %s', 49 chameleon_port.get_connector_type()) 50 51 logging.info('Setting to mirrored mode') 52 self.display_facade.set_mirrored(True) 53 54 # Hide the typing cursor. 55 self.display_facade.hide_typing_cursor() 56 57 # Sleep a while to wait the pop-up window faded-out. 58 time.sleep(self._TIME_WAIT_FADE_OUT) 59 60 # Get the resolution to make sure Chameleon in a good state. 61 resolution = chameleon_port.get_resolution() 62 logging.info('Detected the resolution: %dx%d', *resolution) 63 64 yield chameleon_port 65 66 @contextmanager 67 def start_dock_mode_measurement(self): 68 """Starts the dock mode to measure. 69 70 It iterates the connection ports between DUT and Chameleon and uses 71 the first port. Sets DUT into the dock mode. Then yields the 72 connected ports. 73 74 It is used via a with statement, like the following: 75 76 measurer = LocalChameleonMeasurer(cros_host, args, chrome) 77 with measurer.start_dock_mode_measurement() as chameleon_port: 78 # chameleon_port is automatically plugged before this line 79 # and lid is close to enter doc mode. 80 do_some_test_on(chameleon_port) 81 # chameleon_port is automatically unplugged after this line 82 # and lid is open again. 83 84 @yields the first connected ChameleonVideoInput which is ensured plugged 85 before yielding. 86 87 @raises TestFail if no connected video port or fail to enter dock mode. 88 """ 89 finder = chameleon_port_finder.ChameleonVideoInputFinder( 90 self.chameleon, self.display_facade) 91 try: 92 with finder.use_first_port() as chameleon_port: 93 logging.info('Used Chameleon port: %s', 94 chameleon_port.get_connector_type()) 95 96 logging.info('Close lid to switch into dock mode...') 97 self.host.servo.lid_close() 98 time.sleep(self._WAIT_TIME_LID_TRANSITION) 99 100 # Hide the typing cursor. 101 self.display_facade.hide_typing_cursor() 102 103 # Sleep a while to wait the pop-up window faded-out. 104 time.sleep(self._TIME_WAIT_FADE_OUT) 105 106 # Get the resolution to make sure Chameleon in a good state. 107 resolution = chameleon_port.get_resolution() 108 logging.info('Detected the resolution: %dx%d', *resolution) 109 110 # Check if it is dock mode, no internal screen. 111 if self.display_facade.get_internal_resolution() is not None: 112 raise error.TestError('Failed to enter dock mode: ' 113 'internal display still valid') 114 115 yield chameleon_port 116 117 finally: 118 logging.info('Open lid again...') 119 self.host.servo.lid_open() 120