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 logging
6import time
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
13from telemetry.core import exceptions
14
15
16class platform_InputNewTab(test.test):
17    """Tests if device suspends using shortcut keys."""
18    version = 1
19    _WAIT = 15
20
21    def warmup(self):
22        """Test setup."""
23        # Emulate keyboard.
24        # See input_playback. The keyboard is used to play back shortcuts.
25        self._player = input_playback.InputPlayback()
26        self._player.emulate(input_type='keyboard')
27        self._player.find_connected_inputs()
28
29    def _open_tab(self):
30        """Use keyboard shortcut to start a new tab."""
31        self._player.blocking_playback_of_default_file(
32            input_type='keyboard', filename='keyboard_ctrl+t')
33        time.sleep(self._WAIT)
34
35    def _close_tab(self):
36        """Use keyboard shortcut to start a new tab."""
37        self._player.blocking_playback_of_default_file(
38            input_type='keyboard', filename='keyboard_ctrl+w')
39        time.sleep(self._WAIT)
40
41    def verify_num_tabs(self, expected_num):
42        """Verify number of tabs open.
43
44        @param expected_num: expected number of tabs
45
46        @raises: error.TestFail if number of expected tabs does not equal
47                 number of current tabs.
48
49        """
50        current_num = len(self.cr.browser.tabs)
51        utils.poll_for_condition(
52            lambda: current_num == expected_num,
53            exception=error.TestFail('Incorrect number of tabs: %s vs %s'
54                                     % (current_num, expected_num)),
55            timeout=self._WAIT)
56
57
58    def run_once(self):
59        """
60        Open and close tabs in new browser. Verify expected
61        number of tabs.
62        """
63        with chrome.Chrome() as cr:
64            self.cr = cr
65            time.sleep(self._WAIT)
66
67            init_tabs = len(cr.browser.tabs)
68            self._open_tab()
69            self.verify_num_tabs(init_tabs + 1)
70            self._open_tab()
71            self.verify_num_tabs(init_tabs + 2)
72
73            self._close_tab()
74            self.verify_num_tabs(init_tabs + 1)
75
76    def cleanup(self):
77        """Test cleanup."""
78        self._player.close()
79