1# Copyright (c) 2015 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
6import os
7
8from autotest_lib.client.common_lib.cros import chrome
9from autotest_lib.client.cros import service_stopper
10from autotest_lib.client.cros.input_playback import input_playback
11from autotest_lib.client.cros.graphics import graphics_utils
12from autotest_lib.client.cros.ui import ui_test_base
13
14
15class ui_AppLauncher(ui_test_base.ui_TestBase):
16    """
17    Collects screenshots of the App Launcher.
18    See comments on parent class for overview of how things flow.
19
20    """
21
22    # The keyboard we are emulating
23    _KEYBOARD_PROP = 'keyboard.prop'
24
25    # The keyboard playback data
26    _KEYBOARD_PLAYBACK = 'searchkey_tabs_enter'
27
28    def initialize(self):
29        """Perform necessary initialization prior to test run.
30
31        Private Attributes:
32          _services: service_stopper.ServiceStopper object
33        """
34        # Do not switch off screen for screenshot utility.
35        self._services = service_stopper.ServiceStopper(['powerd'])
36        self._services.stop_services()
37
38    def cleanup(self):
39        self._services.restore_services()
40
41    def capture_screenshot(self, filepath):
42        """
43        Take a screenshot of the App Launcher page.
44
45        Implements the abstract method capture_screenshot
46
47        @param filepath: string, Complete path to save the screenshot to.
48
49        """
50
51        # Login and load the default apps
52        with chrome.Chrome(disable_default_apps=False):
53
54            # Setup the keyboard file's paths
55            property_file = os.path.join(self.bindir, self._KEYBOARD_PROP)
56            playback_file = os.path.join(self.bindir, self._KEYBOARD_PLAYBACK)
57
58            # Setup and playback the keyboard commands to open the launcher
59            player = input_playback.InputPlayback()
60            player.emulate('keyboard', property_file)
61            player.find_connected_inputs()
62            player.blocking_playback(playback_file, 'keyboard')
63            player.close()
64
65            # Take a screenshot and crop to just the launcher
66            w, h = graphics_utils.get_internal_resolution()
67            upper_x = (w - self.launcher_width) / 2
68            upper_y = (h - self.launcher_height) / 2
69            box = (upper_x, upper_y, upper_x + self.launcher_width, upper_y +
70                   self.launcher_height)
71
72            graphics_utils.take_screenshot_crop(filepath, box)
73
74    def run_once(self):
75        # The default launcher dimensions
76        self.launcher_width = 768
77        self.launcher_height = 570
78
79        w, h = graphics_utils.get_internal_resolution()
80        logging.info('DUT screen width: %d' % w)
81        logging.info('DUT screen height: %d' % h)
82
83        # If we have a high DPI screen, launcher size is doubled
84        if self.launcher_width * 2 < w:
85            self.launcher_width *= 2
86            self.launcher_height *= 2
87            self.tagged_testname += '.large'
88
89        self.run_screenshot_comparison_test()
90