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