Home | History | Annotate | Download | only in hardware_VideoOutSemiAuto
      1 # Copyright (c) 2010 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 
      5 import logging
      6 import os.path
      7 
      8 from autotest_lib.client.bin import test, utils
      9 from autotest_lib.client.common_lib import error
     10 from autotest_lib.client.cros.graphics import graphics_utils
     11 
     12 class hardware_VideoOutSemiAuto(test.test):
     13     version = 1
     14     XRANDR_PATH = "/usr/bin/xrandr"
     15     RECONFIG_PATH = "/usr/bin/monitor_reconfigure"
     16     HDMI_ID = "HDMI"
     17     VGA_ID = "VGA"
     18 
     19 
     20     # Returns True if given |output| port is found on system.
     21     def __query_for_output(self, output):
     22         query_cmd = "%s -q | grep %s -c" % (self.XRANDR_PATH, output)
     23         xrandr_out = utils.system_output(graphics_utils.xcommand(query_cmd),
     24                                          ignore_status=True)
     25         return int(xrandr_out) > 0
     26 
     27 
     28     # Returns True if given |output| port has a connected device.
     29     def __output_connected(self, output):
     30         query_cmd = "%s -q | grep '%s[0-9] connected' -c" % \
     31             (self.XRANDR_PATH, output)
     32         xrandr_out = utils.system_output(graphics_utils.xcommand(query_cmd),
     33                                          ignore_status=True)
     34         return int(xrandr_out) > 0
     35 
     36 
     37     # Returns if given |output| port has a device that has been configured
     38     # otherwise raises TestFail
     39     def __output_is_set(self, output):
     40         query_cmd = "%s -q | grep '%s[0-9] connected' -n" % \
     41             (self.XRANDR_PATH, output)
     42         start_line = int(utils.system_output(graphics_utils.xcommand(
     43                                              query_cmd)).split(':')[0])
     44 
     45         # Gets 100 lines (to be safe) after context to get output after
     46         query_cmd = \
     47             "%s -q | grep '%s[0-9] connected' -n -A 100 | grep connected" % \
     48                 (self.XRANDR_PATH, output)
     49 
     50         try:
     51             end_line = int(utils.system_output(graphics_utils_ui.xcommand(
     52                            query_cmd)).split('\n')[1].split('-')[0])
     53         except:
     54             logging.info("End line not found, assuming last output")
     55             end_line = -1
     56 
     57         if end_line != -1:
     58             lines_between = end_line - start_line - 1
     59         else:
     60             line_between = 100
     61         query_cmd = "%s -q | grep '%s[0-9] connected' -A %d | grep \\*" % \
     62                 (self.XRANDR_PATH, output, lines_between)
     63         try:
     64             utils.system(graphics_utils.xcommand(query_cmd))
     65         except:
     66             raise error.TestFail("%s not set with monitor_reconfigure" % output)
     67 
     68 
     69     # Configures |output| and returns if |output| has been configured.
     70     # Also will return false immediately if no device detected on the port
     71     def __configure_and_check_output(self, output):
     72         connected = self.__output_connected(output)
     73         if not connected:
     74             logging.warning(
     75                 "%s port detected but no connected device" % output
     76                 )
     77             return False
     78         else:
     79             #TODO(sosa (at] chromium.org) - Verify this is synchronous.
     80             utils.system(graphics_utils.xcommand(self.RECONFIG_PATH))
     81             self.__output_is_set(output)
     82             return True
     83 
     84 
     85     def run_once(self):
     86         # Sanity check for xrandr application.
     87         if not os.path.isfile(self.XRANDR_PATH):
     88             raise error.TestFail("xrandr not at %s" % self.XRANDR_PATH)
     89 
     90         # Determine if devices of interest are on system.
     91         hdmi_exists = self.__query_for_output(self.HDMI_ID)
     92         vga_exists = self.__query_for_output(self.VGA_ID)
     93 
     94         # Raises NAError since these are optional devices.
     95         if (not hdmi_exists) and (not vga_exists):
     96             raise error.TestFail("Neither VGA nor HDMI ports detected")
     97 
     98         # Sanity check to make sure we can configure the devices.
     99         if not os.path.isfile(self.RECONFIG_PATH):
    100             raise error.TestFail("monitor_reconfigure not at %s" %
    101                                  self.RECONFIG_PATH)
    102 
    103         # If either device is connected and able to be configured
    104         # the test is successful.
    105         success = False
    106 
    107         # If devices exist, we should be able to configure and enable them
    108         if hdmi_exists:
    109             success |= self.__configure_and_check_output(self.HDMI_ID)
    110         if vga_exists:
    111             success |= self.__configure_and_check_output(self.VGA_ID)
    112 
    113         if not success:
    114             raise error.TestFail("""
    115                 HDMI port or VGA port detected but no actual device connected.
    116           """)
    117