1# Copyright 2016 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_InputBrowserNav(test.test): 17 """Tests if device suspends using shortcut keys.""" 18 version = 1 19 _WAIT = 15 20 URL_1 = 'https://www.youtube.com' 21 URL_2 = 'https://www.yahoo.com' 22 23 def warmup(self): 24 """Test setup.""" 25 # Emulate keyboard. 26 # See input_playback. The keyboard is used to play back shortcuts. 27 self._player = input_playback.InputPlayback() 28 self._player.emulate(input_type='keyboard') 29 self._player.find_connected_inputs() 30 31 def test_back(self, tab): 32 """Use keyboard shortcut to test Back (F1) key. 33 34 @param tab: current tab. 35 36 """ 37 self._player.blocking_playback_of_default_file( 38 input_type='keyboard', filename='keyboard_f1') 39 time.sleep(self._WAIT) 40 self.verify_url(tab, self.URL_1) 41 42 def test_forward(self, tab): 43 """Use keyboard shortcut to test Forward (F2) key. 44 45 @param tab: current tab. 46 47 """ 48 self._player.blocking_playback_of_default_file( 49 input_type='keyboard', filename='keyboard_f2') 50 time.sleep(self._WAIT) 51 self.verify_url(tab, self.URL_2) 52 53 def test_refresh(self, tab): 54 """Use keyboard shortcut to test Refresh (F3) key. 55 56 @param tab: current tab. 57 58 @raises error.TestFail if refresh unsuccessful. 59 60 """ 61 # Set JS variable to initial true value to confirm refresh worked. 62 # We are expecting this variable not to exist after the refresh. 63 tab.EvaluateJavaScript("not_refreshed=true") 64 js = 'not_refreshed == true' 65 utils.wait_for_value( 66 lambda: tab.EvaluateJavaScript(js), 67 expected_value=True) 68 69 self._player.blocking_playback_of_default_file( 70 input_type='keyboard', filename='keyboard_f3') 71 time.sleep(self._WAIT) 72 73 # Verify we are still on the second url. 74 self.verify_url(tab, self.URL_2) 75 # Check to see not_refresh does not exist (results in exception). 76 # If it does, the refresh was not successful. 77 try: 78 not_refresh = tab.EvaluateJavaScript(js) 79 raise error.TestFail("Refresh unsuccesful.") 80 except exceptions.EvaluateException: 81 logging.info("Refresh successful.") 82 83 def verify_url(self, tab, correct_url): 84 """Verify tab's current url is the url wanted. 85 86 @param tab: current tab. 87 @param correct_url: url wanted. 88 89 @raises: error.TestFail if incorrect url. 90 91 """ 92 current_url = tab.url.encode('utf8').rstrip('/') 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