1# Copyright 2018 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 os 6import time 7 8from autotest_lib.client.bin import utils 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.common_lib.cros import chrome 11from autotest_lib.client.cros.update_engine import update_engine_test 12 13class autoupdate_UserData(update_engine_test.UpdateEngineTest): 14 """ 15 Logs in, stats an update, waits for a while, then logs out. 16 17 This test is used as part of the server test autoupdate_Interruptions. 18 19 """ 20 version = 1 21 22 # Input method to change to. 23 _NEW_INPUT_METHOD = 'US International keyboard' 24 25 # Javascript code for interacting with chrome://settings. 26 _CLICK_TIMEZONE_BUTTON = "document.querySelector('* /deep/ #timeZoneButton').click()" 27 _CLICK_AUTODETECT_OFF = "document.querySelector('* /deep/ #timeZoneAutoDetectOff').click()" 28 _IS_AUTODETECT_OFF = "document.querySelector('* /deep/ #timeZoneAutoDetectOff').checked" 29 _CLICK_INPUT_METHOD_BUTTON = "document.querySelector('* /deep/ #manageInputMethodsSubpageTrigger').click()" 30 _CLICK_MANAGE_INPUT_METHODS_BUTTON = "document.querySelector('* /deep/ #manageInputMethods').click()" 31 _CLICK_ADD_US_INTL_OPTION = "document.querySelectorAll('* /deep/ paper-checkbox')[1].click()" 32 _CLICK_MAKE_US_INTL_DEFAULT = "document.querySelector('* /deep/ #inputMethodsCollapse div.vertical-list').children[1].click()" 33 _GET_DEFAULT_INPUT_METHOD = "document.querySelector('* /deep/ #inputMethodsCollapse div.vertical-list').children[0].innerText" 34 _TEST_FILE = '/home/chronos/user/Downloads/test.txt' 35 36 37 def _execute_javascript(self, tab, code): 38 """ 39 Exeutes javascript code in the tab provided. 40 41 @param tab: The chrome tab to run code in. 42 @param code: The javascript code to execute. 43 44 """ 45 tab.ExecuteJavaScript(code) 46 time.sleep(5) 47 48 49 def _navigate(self, tab, url): 50 """ 51 Navigate a chrome tab to a URL. 52 53 @param tab: The chrome tab. 54 @param url: The URL to navigate to. 55 56 """ 57 tab.Navigate(url) 58 tab.WaitForDocumentReadyStateToBeComplete() 59 time.sleep(5) 60 61 62 def _modify_input_methods(self, tab): 63 """ 64 Change default input method to US International. 65 66 @param tab: The chrome tab to user. 67 68 """ 69 # TODO(dhaddock): A better way to interact with chrome://settings. 70 self._navigate(tab, 'chrome://settings/languages') 71 self._execute_javascript(tab, self._CLICK_INPUT_METHOD_BUTTON) 72 self._execute_javascript(tab, self._CLICK_MANAGE_INPUT_METHODS_BUTTON) 73 self._execute_javascript(tab, self._CLICK_ADD_US_INTL_OPTION) 74 self._navigate(tab, 'chrome://settings/languages') 75 self._execute_javascript(tab, self._CLICK_INPUT_METHOD_BUTTON) 76 self._execute_javascript(tab, self._CLICK_MAKE_US_INTL_DEFAULT) 77 78 79 def _modify_time_zone(self, tab): 80 """ 81 Change time zone to by user selected instead of automatic. 82 83 @param tab: The chrome tab to user. 84 85 """ 86 # TODO(dhaddock): A better way to interact with chrome://settings. 87 self._navigate(tab, 'chrome://settings/dateTime') 88 self._execute_javascript(tab, self._CLICK_TIMEZONE_BUTTON) 89 self._execute_javascript(tab, self._CLICK_AUTODETECT_OFF) 90 91 92 def _perform_after_update_checks(self): 93 """Check the user preferences and files are the same.""" 94 with chrome.Chrome(logged_in=True, dont_override_profile=True) as cr: 95 tab = cr.browser.tabs[0] 96 97 # Open input methods and ensure the default is the one 98 # we selected before the update. 99 self._navigate(tab, 'chrome://settings/languages') 100 self._execute_javascript(tab, self._CLICK_INPUT_METHOD_BUTTON) 101 result = tab.EvaluateJavaScript(self._GET_DEFAULT_INPUT_METHOD) 102 if self._NEW_INPUT_METHOD not in result: 103 raise error.TestFail('Input method was not preserved.') 104 105 # Make sure we are not autodetecting timezone. 106 self._navigate(tab, 'chrome://settings/dateTime') 107 self._execute_javascript(tab, self._CLICK_TIMEZONE_BUTTON) 108 result = tab.EvaluateJavaScript(self._IS_AUTODETECT_OFF) 109 if not result: 110 raise error.TestFail('Time zone is back to automatic.') 111 112 if not os.path.exists(self._TEST_FILE): 113 raise error.TestFail('Test file is gone after update.') 114 utils.run('rm %s' % self._TEST_FILE) 115 116 117 def run_once(self, update_url=None, after_update=False): 118 """ 119 Tests that user settings are not reset by update. 120 121 @param update_url: The update url to use. 122 @param after_update: True for post update checks. 123 124 """ 125 if after_update: 126 self._perform_after_update_checks() 127 else: 128 with chrome.Chrome(logged_in=True) as cr: 129 utils.run('echo hello > %s' % self._TEST_FILE) 130 tab = cr.browser.tabs[0] 131 self._modify_input_methods(tab) 132 self._modify_time_zone(tab) 133 self._check_for_update(update_url) 134 self._wait_for_progress(0.2) 135