1# Lint as: python2, python3 2# Copyright 2016 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_InputBrowserNav(test.test): 18 """Tests if device suspends using shortcut keys.""" 19 version = 1 20 _WAIT = 15 21 URL_1 = 'https://www.youtube.com/' 22 URL_2 = 'https://www.yahoo.com/' 23 24 def warmup(self): 25 """Test setup.""" 26 # Emulate keyboard. 27 # See input_playback. The keyboard is used to play back shortcuts. 28 self._player = input_playback.InputPlayback() 29 self._player.emulate(input_type='keyboard') 30 self._player.find_connected_inputs() 31 32 def test_back(self, tab): 33 """Use keyboard shortcut to test Back (F1) key. 34 35 @param tab: current tab. 36 37 """ 38 self._player.blocking_playback_of_default_file( 39 input_type='keyboard', filename='keyboard_f1') 40 time.sleep(self._WAIT) 41 self.verify_url(tab.url, self.URL_1) 42 43 def test_forward(self, tab): 44 """Use keyboard shortcut to test Forward (F2) key. 45 46 @param tab: current tab. 47 48 """ 49 self._player.blocking_playback_of_default_file( 50 input_type='keyboard', filename='keyboard_f2') 51 time.sleep(self._WAIT) 52 self.verify_url(tab.url, self.URL_2) 53 54 def test_refresh(self, tab): 55 """Use keyboard shortcut to test Refresh (F3) key. 56 57 @param tab: current tab. 58 59 @raises error.TestFail if refresh unsuccessful. 60 61 """ 62 # Set JS variable to initial true value to confirm refresh worked. 63 # We are expecting this variable not to exist after the refresh. 64 tab.EvaluateJavaScript("not_refreshed=true") 65 js = 'not_refreshed == true' 66 utils.wait_for_value( 67 lambda: tab.EvaluateJavaScript(js), 68 expected_value=True) 69 70 self._player.blocking_playback_of_default_file( 71 input_type='keyboard', filename='keyboard_f3') 72 time.sleep(self._WAIT) 73 74 # Verify we are still on the second url. 75 self.verify_url(tab.url, self.URL_2) 76 # Check to see not_refresh does not exist (results in exception). 77 # If it does, the refresh was not successful. 78 try: 79 not_refresh = tab.EvaluateJavaScript(js) 80 raise error.TestFail("Refresh unsuccesful.") 81 except exceptions.EvaluateException: 82 logging.info("Refresh successful.") 83 84 def verify_url(self, current_url, correct_url): 85 """Verify tab's current url is the url wanted. 86 87 @param tab: current tab. 88 @param correct_url: url wanted. 89 90 @raises: error.TestFail if incorrect url. 91 92 """ 93 utils.poll_for_condition( 94 lambda: current_url == correct_url, 95 exception=error.TestFail('Incorrect navigation: %s' 96 % current_url), 97 timeout=self._WAIT) 98 99 def run_once(self): 100 """ 101 Open browser, navigate to urls and test 102 forward, backward, and refresh functions. 103 104 """ 105 with chrome.Chrome() as cr: 106 tab = cr.browser.tabs[0] 107 logging.info('Initially navigate to %s.' % self.URL_1) 108 tab.Navigate(self.URL_1) 109 time.sleep(self._WAIT) 110 logging.info('Next, navigate to %s.' % self.URL_2) 111 tab.Navigate(self.URL_2) 112 113 self.test_back(tab) 114 self.test_forward(tab) 115 self.test_refresh(tab) 116 117 def cleanup(self): 118 """Test cleanup.""" 119 self._player.close() 120