1# Copyright (c) 2010 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 os
6import logging
7
8from autotest_lib.client.bin import utils
9from autotest_lib.client.common_lib import error
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
13
14# to run this test manually on a test target
15# ssh root@machine
16# cd /usr/local/autotest/deps/glbench
17# stop ui
18# ./windowmanagertest --screenshot1_sec 2 --screenshot2_sec 1 --cooldown_sec 1 \
19#    --screenshot1_cmd "screenshot screenshot1_generated.png" \
20#    --screenshot2_cmd "screenshot screenshot2_generated.png"
21# start ui
22
23
24class graphics_Sanity(graphics_utils.GraphicsTest):
25    """
26    This test is meant to be used as a quick sanity check for GL/GLES.
27    """
28    version = 1
29
30    # None-init vars used by cleanup() here, in case setup() fails
31    _services = None
32
33    def setup(self):
34        self.job.setup_dep(['glbench'])
35        dep = 'glbench'
36        dep_dir = os.path.join(self.autodir, 'deps', dep)
37        self.job.install_pkg(dep, 'dep', dep_dir)
38
39    def cleanup(self):
40        super(graphics_Sanity, self).cleanup()
41        if self._services:
42            self._services.restore_services()
43
44    def test_something_on_screen(self):
45        """Check if something is drawn on screen: i.e. not a black screen.
46
47        @raises TestFail if we cannot determine there was something on screen.
48        """
49
50        def can_take_screenshot():
51            """Check that taking a screenshot can succeed.
52
53            There are cases when trying to take a screenshot on the device
54            fails. e.g. the display has gone to sleep, we have logged out and
55            the UI has not come back up yet etc.
56            """
57            try:
58                graphics_utils.take_screenshot(self.resultsdir,
59                                               'temp screenshot')
60                return True
61            except:
62                return False
63
64        utils.poll_for_condition(
65            can_take_screenshot,
66            sleep_interval=1,
67            desc='Failed to take a screenshot. There may be an issue with this '
68            'ChromeOS image.')
69
70        w, h = graphics_utils.get_internal_resolution()
71        megapixels = (w * h) / 1000000
72        filesize_threshold = 25 * megapixels
73        screenshot1 = graphics_utils.take_screenshot(self.resultsdir,
74                                                     'oobe or signin')
75
76        with chrome.Chrome() as cr:
77            tab = cr.browser.tabs[0]
78            tab.Navigate('chrome://settings')
79            tab.WaitForDocumentReadyStateToBeComplete()
80
81            screenshot2 = graphics_utils.take_screenshot(
82                self.resultsdir, 'settings page')
83
84        for screenshot in [screenshot1, screenshot2]:
85            file_size_kb = os.path.getsize(screenshot) / 1000
86
87            # Use compressed file size to tell if anything is on screen.
88            if file_size_kb > filesize_threshold:
89                return
90
91        raise error.TestFail(
92            'Screenshot filesize is very small. This indicates that '
93            'there is nothing on screen. This ChromeOS image could be '
94            'unusable. Check the screenshot in the results folder.')
95
96    def test_generated_screenshots_match_expectation(self):
97        """Draws a texture with a soft ellipse twice and captures each image.
98        Compares the output fuzzily against reference images.
99        """
100        self._services = service_stopper.ServiceStopper(['ui'])
101        self._services.stop_services()
102
103        screenshot1_reference = os.path.join(self.bindir,
104                                             'screenshot1_reference.png')
105        screenshot1_generated = os.path.join(self.resultsdir,
106                                             'screenshot1_generated.png')
107        screenshot1_resized = os.path.join(self.resultsdir,
108                                           'screenshot1_generated_resized.png')
109        screenshot2_reference = os.path.join(self.bindir,
110                                             'screenshot2_reference.png')
111        screenshot2_generated = os.path.join(self.resultsdir,
112                                             'screenshot2_generated.png')
113        screenshot2_resized = os.path.join(self.resultsdir,
114                                           'screenshot2_generated_resized.png')
115
116        exefile = os.path.join(self.autodir, 'deps/glbench/windowmanagertest')
117
118        # Delay before screenshot: 1 second has caused failures.
119        options = ' --screenshot1_sec 2'
120        options += ' --screenshot2_sec 1'
121        options += ' --cooldown_sec 1'
122        # perceptualdiff can handle only 8 bit images.
123        screenshot_cmd = ' "screenshot %s"'
124        options += ' --screenshot1_cmd' + screenshot_cmd % screenshot1_generated
125        options += ' --screenshot2_cmd' + screenshot_cmd % screenshot2_generated
126
127        cmd = exefile + ' ' + options
128        utils.run(
129            cmd, stdout_tee=utils.TEE_TO_LOGS, stderr_tee=utils.TEE_TO_LOGS)
130
131        convert_cmd = ('convert -channel RGB -colorspace RGB -depth 8'
132                       " -resize '100x100!' %s %s")
133        utils.system(convert_cmd % (screenshot1_generated, screenshot1_resized))
134        utils.system(convert_cmd % (screenshot2_generated, screenshot2_resized))
135        os.remove(screenshot1_generated)
136        os.remove(screenshot2_generated)
137
138        diff_cmd = 'perceptualdiff -verbose %s %s'
139        utils.system(diff_cmd % (screenshot1_reference, screenshot1_resized))
140        utils.system(diff_cmd % (screenshot2_reference, screenshot2_resized))
141
142    def run_once(self):
143        if graphics_utils.get_display_resolution() is None:
144            logging.warning('Skipping test because there is no screen')
145            return
146        self.add_failures('graphics_Sanity')
147        self.wake_screen_with_keyboard()
148        self.test_something_on_screen()
149        self.test_generated_screenshots_match_expectation()
150        self.remove_failures('graphics_Sanity')
151