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 logging 6import os 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 nebraska_wrapper 12from autotest_lib.client.cros.update_engine import update_engine_test 13 14class autoupdate_UserData(update_engine_test.UpdateEngineTest): 15 """ 16 Modifies some user settings and checks they were not reset by an update. 17 18 This test is used as part of the server test autoupdate_DataPreserved. 19 This test will make use of several private Chrome APIs: 20 inputMethodPrivate: chrome/common/extensions/api/input_method_private.json 21 languageSettingsPrivate: chrome/common/extensions/api/ 22 language_settings_private.idl 23 settingsPrivate: chrome/common/extensions/api/settings_private.idl 24 25 """ 26 version = 1 27 28 _TEST_FILE = '/home/chronos/user/Downloads/test.txt' 29 _US_IME = '_comp_ime_jkghodnilhceideoidjikpgommlajknkxkb:us::eng' 30 _US_INTL_IME = '_comp_ime_jkghodnilhceideoidjikpgommlajknkxkb:us:intl:eng' 31 _TIME_ZONE_PREF = 'generated.resolve_timezone_by_geolocation_on_off' 32 33 _GET_IME_JS = ''' 34 new Promise(function(resolve, reject) { 35 chrome.inputMethodPrivate.getCurrentInputMethod( 36 function(id) { 37 resolve(id); 38 }); 39 }) 40 ''' 41 _GET_PREF_JS = ''' 42 new Promise(function(resolve, reject) { 43 chrome.settingsPrivate.getPref("%s", function(pref) { 44 resolve(pref['value']); 45 }); 46 }) 47 ''' 48 _SET_IME_JS = 'chrome.inputMethodPrivate.setCurrentInputMethod("%s")' 49 _SET_PREF_JS = ('chrome.settingsPrivate.setPref("%s", %s, ' 50 'x => console.log(x))') 51 52 53 def _modify_input_methods(self): 54 """ Change default Input Method to US International.""" 55 current_ime = self._cr.autotest_ext.EvaluateJavaScript( 56 self._GET_IME_JS, promise=True) 57 logging.info('Current IME is %s', current_ime) 58 add_ime_js = ('chrome.languageSettingsPrivate.addInputMethod("%s")' % 59 self._US_INTL_IME) 60 self._cr.autotest_ext.EvaluateJavaScript(add_ime_js) 61 self._cr.autotest_ext.EvaluateJavaScript(self._SET_IME_JS % 62 self._US_INTL_IME) 63 new_ime = self._cr.autotest_ext.EvaluateJavaScript(self._GET_IME_JS) 64 if current_ime == new_ime: 65 raise error.TestFail('IME could not be changed before update.') 66 67 68 def _modify_time_zone(self): 69 """Change time zone to be user selected instead of automatic by IP.""" 70 current_time_zone = self._cr.autotest_ext.EvaluateJavaScript( 71 self._GET_PREF_JS % self._TIME_ZONE_PREF, promise=True) 72 logging.info('Calculating timezone by IP: %s', current_time_zone) 73 self._cr.autotest_ext.EvaluateJavaScript( 74 self._SET_PREF_JS % (self._TIME_ZONE_PREF, 'false')) 75 new_timezone = self._cr.autotest_ext.EvaluateJavaScript( 76 self._GET_PREF_JS % self._TIME_ZONE_PREF, promise=True) 77 if current_time_zone == new_timezone: 78 raise error.TestFail('Timezone detection could not be changed.') 79 80 81 def _perform_after_update_checks(self): 82 """Check the user preferences and files are the same.""" 83 with chrome.Chrome(dont_override_profile=True, 84 autotest_ext=True) as cr: 85 # Check test file is still present. 86 if not os.path.exists(self._TEST_FILE): 87 raise error.TestFail('Test file was not present after update.') 88 89 # Check IME has not changed. 90 current_ime = cr.autotest_ext.EvaluateJavaScript( 91 self._GET_IME_JS, promise=True) 92 if current_ime != self._US_INTL_IME: 93 raise error.TestFail('Input method was not preserved after' 94 'update. Expected %s, Actual: %s' % 95 (self._US_INTL_IME, current_ime)) 96 97 # Check that timezone is user selected. 98 current_time_zone = cr.autotest_ext.EvaluateJavaScript( 99 self._GET_PREF_JS % self._TIME_ZONE_PREF, promise=True) 100 if current_time_zone: 101 raise error.TestFail('Time zone detection was changed back to ' 102 'automatic.') 103 104 105 def run_once(self, payload_url=None): 106 """ 107 Tests that user settings are not reset by update. 108 109 @param payload_url: The payload url to use. 110 111 """ 112 if payload_url: 113 with nebraska_wrapper.NebraskaWrapper( 114 log_dir=self.resultsdir, payload_url=payload_url) as nebraska: 115 with chrome.Chrome(autotest_ext=True) as cr: 116 self._cr = cr 117 utils.run(['echo', 'hello', '>', self._TEST_FILE]) 118 self._modify_input_methods() 119 self._modify_time_zone() 120 self._check_for_update( 121 nebraska.get_update_url(critical_update=True)) 122 # Sign out of Chrome and wait for the update to complete. 123 # If we waited for the update to complete and then logged out 124 # the DUT will auto-reboot and the client test cannot return. 125 self._wait_for_update_to_complete() 126 else: 127 self._perform_after_update_checks() 128