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