1# Lint as: python2, python3 2# Copyright 2014 The Chromium OS Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5 6"""Classes to do resolution comparison.""" 7 8import logging 9import six.moves.xmlrpc_client 10 11 12class ExactMatchResolutionComparer(object): 13 """A class to compare the resolutions by using exact match. 14 15 Calling its member method compare() does the comparison. 16 17 """ 18 19 def __init__(self, chameleon_port, display_facade): 20 """Initializes the ExactMatchResolutionComparer objects.""" 21 self._chameleon_port = chameleon_port 22 self._display_facade = display_facade 23 24 25 def compare(self, expected_resolution): 26 """Compares the resolutions among the given one, Chameleon's, and CrOS'. 27 28 This method first checks if CrOS is under mirrored mode or not. 29 30 If under extended mode, checks the resolutions of both CrOS and 31 Chameleon exactly match the expected one. 32 33 If under mirror mode, only checks the resolution of CrOS exactly 34 matches the one of Chameleon. 35 36 @param expected_resolution: A tuple (width, height) for the expected 37 resolution. 38 @return: None if the check passes; otherwise, a string of error message. 39 """ 40 try: 41 chameleon_resolution = self._chameleon_port.get_resolution() 42 except six.moves.xmlrpc_client.Fault as e: 43 logging.exception(e) 44 return str(e) 45 cros_resolution = self._display_facade.get_external_resolution() 46 47 logging.info('Checking the resolutions of Chameleon and CrOS...') 48 if expected_resolution != cros_resolution or ( 49 chameleon_resolution != cros_resolution): 50 message = ('Detected mis-matched resolutions: ' 51 'CrOS %r; Chameleon %r; Expected %r.' % 52 (cros_resolution, chameleon_resolution, 53 expected_resolution)) 54 # Note: In mirrored mode, the device may be in hardware mirror 55 # (as opposed to software mirror). If so, the actual resolution 56 # could be different from the expected one. So we skip the check 57 # in mirrored mode. The resolution of the CrOS and Chameleon 58 # should be same no matter the device in mirror mode or not. 59 if chameleon_resolution != cros_resolution or ( 60 not self._display_facade.is_mirrored_enabled()): 61 logging.error(message) 62 return message 63 else: 64 logging.warn(message) 65 else: 66 logging.info('Resolutions across CrOS and Chameleon match: %dx%d', 67 *expected_resolution) 68 return None 69 70 71class VgaResolutionComparer(object): 72 """A class to compare the resolutions for VGA interface. 73 74 Calling its member method compare() does the comparison. 75 76 """ 77 78 def __init__(self, chameleon_port, display_facade): 79 """Initializes the VgaResolutionComparer objects.""" 80 self._chameleon_port = chameleon_port 81 self._display_facade = display_facade 82 83 84 def compare(self, expected_resolution): 85 """Compares the resolutions among the given one, Chameleon's, and CrOS'. 86 87 There is no DE (data enable) signal in the VGA standard, the captured 88 image of Chameleon side is larger than the expected image. 89 90 It checks the resolution of CrOS matches the expected one and 91 the resolution of Chameleon is larger than or equal to the one of CrOS. 92 93 @param expected_resolution: A tuple (width, height) of the expected 94 resolution. 95 @return: None if the check passes; otherwise, a string of error message. 96 """ 97 try: 98 chameleon_resolution = self._chameleon_port.get_resolution() 99 except six.moves.xmlrpc_client.Fault as e: 100 logging.exception(e) 101 return str(e) 102 cros_resolution = self._display_facade.get_external_resolution() 103 104 logging.info('Checking the resolutions of Chameleon and CrOS...') 105 if expected_resolution != cros_resolution or ( 106 chameleon_resolution[0] < cros_resolution[0] or 107 chameleon_resolution[1] < cros_resolution[1]): 108 message = ('Detected mis-matched VGA resolutions: ' 109 'CrOS %r; Chameleon %r; Expected %r.' % 110 (cros_resolution, chameleon_resolution, 111 expected_resolution)) 112 logging.error(message) 113 return message 114 else: 115 logging.info('Detected VGA resolutions: ' 116 'CrOS: %dx%d; Chameleon: %dx%d.', 117 *(cros_resolution + chameleon_resolution)) 118 return None 119