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
6from autotest_lib.client.bin import test
7from autotest_lib.client.common_lib import error
8from autotest_lib.client.common_lib import utils
9from autotest_lib.client.cros import service_stopper
10from autotest_lib.client.cros.graphics import graphics_utils
11
12
13class graphics_Drm(test.test):
14    """Runs one of the drm-tests.
15    """
16    version = 1
17    GSC = None
18    _services = None
19    _timeout = 120
20
21    def initialize(self):
22        self.GSC = graphics_utils.GraphicsStateChecker()
23        self._services = service_stopper.ServiceStopper(['ui'])
24
25    def cleanup(self):
26        if self.GSC:
27            self.GSC.finalize()
28        if self._services:
29            self._services.restore_services()
30
31    def run_once(self, cmd, stop_ui=True, display_required=True):
32        num_displays = graphics_utils.get_num_outputs_on()
33        # Sanity check to guard against incorrect silent passes.
34        if num_displays == 0 and utils.get_device_type() == 'CHROMEBOOK':
35            raise error.TestFail('Error: found Chromebook without display.')
36        if display_required and num_displays == 0:
37            # If a test needs a display and we don't have a display,
38            # consider it a pass.
39            logging.warning('No display connected, skipping test.')
40            return
41        if stop_ui:
42            self._services.stop_services()
43        try:
44            result = utils.run(cmd,
45                               timeout=self._timeout,
46                               ignore_status=True,
47                               stderr_is_expected=True,
48                               verbose=True,
49                               stdout_tee=utils.TEE_TO_LOGS,
50                               stderr_tee=utils.TEE_TO_LOGS)
51        except Exception:
52            # Fail on exceptions.
53            raise error.TestFail('Failed: Exception running %s' % cmd)
54
55        # Last but not least check return code and use it for triage.
56        if result.exit_status != 0:
57            raise error.TestFail('Failed: %s (exit=%d)' %
58                                    (cmd, result.exit_status))
59