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
5"""This is test switching the external display mode."""
6
7import logging, time
8
9from autotest_lib.client.common_lib import error
10from autotest_lib.client.cros.chameleon import chameleon_port_finder
11from autotest_lib.client.cros.chameleon import chameleon_screen_test
12from autotest_lib.server import test
13from autotest_lib.server.cros.multimedia import remote_facade_factory
14
15
16class display_SwitchMode(test.test):
17    """External Display switch between extended and mirrored modes.
18
19    This test switches the external display mode between extended
20    and mirrored modes, and checks resolution and static test image.
21    """
22    version = 1
23    WAIT_AFTER_SWITCH = 5
24
25    def check_external_display(self, test_mirrored):
26        """Display status check
27
28        @param test_mirrored: is mirrored mode active
29
30        """
31        resolution = self.display_facade.get_external_resolution()
32        # Check connector
33        if self.screen_test.check_external_display_connected(
34                self.connector_used, self.errors) is None:
35            # Check test image
36            self.screen_test.test_screen_with_image(
37                    resolution, test_mirrored, self.errors)
38        if self.errors:
39            raise error.TestFail('; '.join(set(self.errors)))
40
41
42    def set_mode_and_check(self, test_mirrored):
43        """Sets display mode and checks status
44
45        @param test_mirrored: is mirrored mode active
46
47        """
48        logging.info('Set mirrored: %s', test_mirrored)
49        self.display_facade.set_mirrored(test_mirrored)
50        time.sleep(self.WAIT_AFTER_SWITCH)
51        self.check_external_display(test_mirrored)
52
53
54    def run_once(self, host, repeat):
55        factory = remote_facade_factory.RemoteFacadeFactory(host)
56        self.display_facade = factory.create_display_facade()
57        chameleon_board = host.chameleon
58
59        chameleon_board.reset()
60        finder = chameleon_port_finder.ChameleonVideoInputFinder(
61                chameleon_board, self.display_facade)
62
63        self.errors = []
64        for chameleon_port in finder.iterate_all_ports():
65            self.chameleon_port = chameleon_port
66            self.screen_test = chameleon_screen_test.ChameleonScreenTest(
67                    chameleon_port, self.display_facade, self.outputdir)
68
69            logging.debug('See the display on Chameleon: port %d (%s)',
70                         self.chameleon_port.get_connector_id(),
71                         self.chameleon_port.get_connector_type())
72            # Keep the original connector name, for later comparison.
73            self.connector_used = (
74                    self.display_facade.get_external_connector_name())
75
76            for i in xrange(repeat):
77                logging.info("Iteration %d", (i + 1))
78                self.set_mode_and_check(False)
79                self.set_mode_and_check(True)
80