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