ui_SystemTray.py revision 58c7537a883f29260b117e13e9dc06bdf3db91bf
1# Copyright (c) 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 logging
6import os
7
8from autotest_lib.client.bin import site_utils
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib.cros import chrome
11from autotest_lib.client.cros import service_stopper
12from autotest_lib.client.cros.graphics import graphics_utils
13from autotest_lib.client.cros.image_comparison import rgb_image_comparer
14from autotest_lib.client.cros.ui import ui_test_base
15
16class ui_SystemTray(ui_test_base.ui_TestBase):
17    """
18    Collects system tray screenshots.
19
20    See comments on parent class for overview of how things flow.
21
22    """
23
24    def initialize(self):
25        """Perform necessary initialization prior to test run.
26
27        Private Attributes:
28          _services: service_stopper.ServiceStopper object
29        """
30        # Do not switch off screen for screenshot utility.
31        self._services = service_stopper.ServiceStopper(['powerd'])
32        self._services.stop_services()
33
34
35    def cleanup(self):
36        self._services.restore_services()
37
38
39    def capture_screenshot(self, filepath):
40        """
41        Sets the portion of the screenshot to crop.
42        Calls into take_screenshot_crop to take the screenshot and crop it.
43
44        self.logged_in controls which logged-in state we are testing when we
45        take the screenshot.
46
47        if None, we don't login at all
48        if True, we login as the test user
49        if False, we login as guest
50
51        For the logged in user we mask the profile photo so that the randomly
52        generated profile pictures don't break the tests.
53
54        @param filepath: path, fullpath to where the screenshot will be saved to
55
56        """
57
58        w, h = graphics_utils.get_display_resolution()
59        box = (w - self.width, h - self.height, w, h)
60
61        if self.logged_in is None:
62            graphics_utils.take_screenshot_crop(filepath, box)
63            return
64
65        with chrome.Chrome(logged_in=self.logged_in):
66            # set up a pixel comparer
67            image_name = os.path.splitext(filepath)[0]
68            temp_file_path = '%s_temp.png' % image_name
69            comparer = rgb_image_comparer.RGBImageComparer(
70                    rgb_pixel_threshold=0)
71
72            def has_animation_stopped():
73                """
74                Takes two screenshots. Checks if they are identical to
75                indicate the shelf has stop animating.
76
77                """
78
79                graphics_utils.take_screenshot_crop(filepath, box)
80                graphics_utils.take_screenshot_crop(temp_file_path, box)
81                diff = comparer.compare(filepath, temp_file_path)
82                logging.debug("Pixel diff count: %d", diff.diff_pixel_count)
83                return diff.diff_pixel_count == 0
84
85            site_utils.poll_for_condition(has_animation_stopped,
86                                          timeout=30,
87                                          desc='end of system tray animation')
88
89            if self.logged_in and self.mask_points is not None:
90                self.draw_image_mask(filepath, self.mask_points)
91
92
93    def run_once(self, width, height, mask_points=None, logged_in=None):
94        self.width = width
95        self.height = height
96        self.logged_in = logged_in
97        self.mask_points = mask_points
98
99        if utils.get_board() != 'link':
100            logging.info('This test should only be run on link so exiting.')
101            return
102
103        self.run_screenshot_comparison_test()
104