• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Lint as: python2, python3
2# Copyright 2017 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import glob
7import os
8
9from autotest_lib.client.bin import test
10from autotest_lib.client.bin import utils
11from autotest_lib.client.common_lib import error
12from autotest_lib.client.common_lib.cros import chrome
13from autotest_lib.client.cros.input_playback import input_playback
14
15
16class platform_InputScreenshot(test.test):
17    """Tests if key combinations will create a screenshot."""
18    version = 1
19    _TMP = '/tmp'
20    _DOWNLOADS = '/home/chronos/user/Downloads'
21    _SCREENSHOT = 'Screenshot*'
22    _ERROR = list()
23    _MIN_SIZE = 1000
24
25    def warmup(self):
26        """Test setup."""
27        # Emulate keyboard.
28        # See input_playback. The keyboard is used to play back shortcuts.
29        # Remove all screenshots.
30        self.player = input_playback.InputPlayback()
31        self.player.emulate(input_type='keyboard')
32        self.player.find_connected_inputs()
33        self.remove_screenshot()
34
35
36    def remove_screenshot(self):
37        """Remove all screenshots."""
38        utils.system_output('rm -f %s/%s %s/%s' %(self._TMP, self._SCREENSHOT,
39                            self._DOWNLOADS, self._SCREENSHOT))
40
41
42    def confirm_file_exist(self, filepath):
43        """Check if screenshot file can be found and with minimum size.
44
45        @param filepath file path.
46
47        @raises: error.TestFail if screenshot file does not exist.
48
49        """
50        if not os.path.isdir(filepath):
51            raise error.TestNAError("%s folder is not found" % filepath)
52
53        try:
54            paths = utils.poll_for_condition(lambda: glob.glob(
55                    os.path.join(filepath, self._SCREENSHOT)),
56                                             timeout=20,
57                                             sleep_interval=1)
58        except utils.TimeoutError:
59            self._ERROR.append('Screenshot was not found under: %s' % filepath)
60            return
61
62        if len(paths) > 1:
63            self._ERROR.append('Found too many screenshots: %s' % paths)
64            return
65
66        filesize = os.stat(paths[0]).st_size
67        if filesize < self._MIN_SIZE:
68            self._ERROR.append('Screenshot size:%d at %s is wrong' %
69                               (filesize, filepath))
70
71
72    def create_screenshot(self):
73        """Create a screenshot."""
74        self.player.blocking_playback_of_default_file(
75               input_type='keyboard', filename='keyboard_ctrl+f5')
76
77
78    def run_once(self):
79        # Screenshot under /tmp without login.
80        self.create_screenshot()
81        self.confirm_file_exist(self._TMP)
82
83        # Screenshot under /Downloads after login.
84        with chrome.Chrome() as cr:
85            self.create_screenshot()
86            self.confirm_file_exist(self._DOWNLOADS)
87
88        if self._ERROR:
89            raise error.TestFail('; '.join(self._ERROR))
90
91    def cleanup(self):
92        """Test cleanup."""
93        self.player.close()
94        self.remove_screenshot()
95