1# Copyright 2014 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 os 6import time 7 8from autotest_lib.client.common_lib import error 9from autotest_lib.client.cros.chameleon import chameleon_port_finder 10from autotest_lib.client.cros.video import method_logger 11 12 13class ChameleonScreenshotCapturer(object): 14 """ 15 Provides an interface to capture a dut screenshot using a Chameleon Board. 16 17 Example use: 18 with ChameleonScreenshotCapturer(board, 'HDMI', dutil, '/tmp', 10) as c: 19 c.capture(filename) 20 """ 21 22 23 def __init__(self, chameleon_board, interface, display_facade, dest_dir, 24 timeout_video_input_s, box=None): 25 """ 26 @param chameleon_board: object representing the ChameleonBoard. 27 @param interface: string, display interface to use. eg.: HDMI 28 @param display_facade: display facade object to interact with DUT 29 @param dest_dir: path, full path to the dest dir to put the screenshot. 30 @param timeout_video_input_s: int, max time to wait for chameleon video 31 input to become stable. 32 @box: int tuple, left, upper, right, lower pixel coordinates 33 defining a desired image region 34 35 """ 36 self.chameleon_board = chameleon_board 37 self.display_facade = display_facade 38 self.interface = interface.lower() 39 self.dest_dir = dest_dir 40 self.port = None 41 self.box = box 42 self.timeout_video_input_s = timeout_video_input_s 43 self.was_plugged = False 44 self._find_connected_port() 45 46 47 @method_logger.log 48 def __enter__(self): 49 50 if not self.was_plugged: 51 self.port.plug() 52 53 self.port.wait_video_input_stable(self.timeout_video_input_s) 54 55 self.display_facade.set_mirrored(True) 56 time.sleep(self.timeout_video_input_s) 57 58 return self 59 60 61 @method_logger.log 62 def _find_connected_port(self): 63 """ 64 Gets a connected port of the pre-specified interface. 65 66 @raises TestError if desired port was not detected. 67 68 """ 69 self.chameleon_board.reset() 70 finder = chameleon_port_finder.ChameleonVideoInputFinder( 71 self.chameleon_board, self.display_facade) 72 73 connected_port = finder.find_port(self.interface) 74 75 if connected_port is None: 76 msg = 'No %s port found.\n' % self.interface 77 raise error.TestError(msg + str(finder)) 78 79 self.port = connected_port 80 self.was_plugged = connected_port.plugged 81 82 83 @method_logger.log 84 def capture(self, filename, box=None): 85 """ 86 Captures a screenshot using provided chameleon board. 87 88 We save to a file because comparers like bp take files. 89 90 @param filename: string, filename of the image to save to. 91 @param box: int tuple, left, upper, right, lower pixel coordinates 92 defining a box region of what the image should be. 93 @returns a fullpath to the image just captured. 94 95 """ 96 97 fullpath = os.path.join(self.dest_dir, filename) 98 99 if not box: 100 box = self.box 101 102 img = self.port.capture_screen() 103 img.crop(box).save(fullpath) 104 105 return fullpath 106 107 108 @method_logger.log 109 def __exit__(self, exc_type, exc_val, exc_tb): 110 if not self.was_plugged: 111 self.port.unplug() 112