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 utils
     10 from autotest_lib.client.common_lib.cros import chrome
     11 from autotest_lib.client.cros import service_stopper
     12 from autotest_lib.client.cros.graphics import graphics_utils
     13 from autotest_lib.client.cros.image_comparison import rgb_image_comparer
     14 from autotest_lib.client.cros.ui import ui_test_base
     15 
     16 class 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             # crbug.com/476791 error when take screenshots too soon after login
     86             time.sleep(30)
     87             utils.poll_for_condition(has_animation_stopped,
     88                                      timeout=30,
     89                                      desc='end of system tray animation')
     90 
     91             if self.logged_in and self.mask_points is not None:
     92                 self.draw_image_mask(filepath, self.mask_points)
     93 
     94 
     95     def run_once(self, width, height, mask_points=None, logged_in=None):
     96         self.width = width
     97         self.height = height
     98         self.logged_in = logged_in
     99         self.mask_points = mask_points
    100 
    101         if utils.get_board() != 'link':
    102             logging.info('This test should only be run on link so exiting.')
    103             return
    104 
    105         self.run_screenshot_comparison_test()
    106