1# Copyright 2017 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 time
6import os.path
7
8from autotest_lib.client.bin import test
9from autotest_lib.client.bin import utils
10from autotest_lib.client.common_lib import error
11from autotest_lib.client.common_lib.cros import chrome
12from autotest_lib.client.cros.input_playback import input_playback
13
14
15class platform_InputScreenshot(test.test):
16    """Tests if key combinations will create a screenshot."""
17    version = 1
18    _WAIT = 5
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        if not (utils.system_output('sync; sleep 2; find %s -name "%s"'
54                                    % (filepath, self._SCREENSHOT))):
55            self._ERROR.append('Screenshot was not found under:%s' % filepath)
56
57        filesize = utils.system_output('ls -l %s/%s | cut -d" " -f5'
58                                       % (filepath, self._SCREENSHOT))
59        if filesize < self._MIN_SIZE:
60            self._ERROR.append('Screenshot size:%s at %s is wrong'
61                               % (filesize, filepath))
62
63
64    def create_screenshot(self):
65        """Create a screenshot."""
66        self.player.blocking_playback_of_default_file(
67               input_type='keyboard', filename='keyboard_ctrl+f5')
68        time.sleep(self._WAIT)
69
70
71    def run_once(self):
72        # Screenshot under /tmp without login.
73        self.create_screenshot()
74        self.confirm_file_exist(self._TMP)
75
76        # Screenshot under /Downloads after login.
77        with chrome.Chrome() as cr:
78            self.create_screenshot()
79            self.confirm_file_exist(self._DOWNLOADS)
80
81        if self._ERROR:
82            raise error.TestFail('; '.join(self._ERROR))
83
84    def cleanup(self):
85        """Test cleanup."""
86        self.player.close()
87        self.remove_screenshot()
88