xref: /aosp_15_r20/external/autotest/autotest_lib/client/bin/telemetry_check.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
2#
3# Copyright 2016 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6'''Confidence tests for Chrome on ChromeOS.
7
8This script runs a number of confidence tests to ensure that Chrome browser on
9ChromeOS is functional.
10'''
11
12from __future__ import absolute_import
13from __future__ import division
14from __future__ import print_function
15
16import argparse
17import datetime
18import logging
19import sys
20
21# This sets up import paths for autotest.
22import common
23from autotest_lib.client.bin import utils
24from autotest_lib.client.common_lib.cros import arc, arc_common, chrome
25from autotest_lib.client.common_lib.error import TestFail
26from autotest_lib.client.cros import cryptohome
27from six.moves import range
28
29
30class TelemetryCheck(object):
31    """Class for running confidence tests to verify telemetry."""
32
33    def __init__(self,
34                 count=1,
35                 run_cryptohome=True,
36                 run_incognito=True,
37                 run_screenlock=True):
38        self.count = count
39        self.run_cryptohome = run_cryptohome
40        self.run_incognito = run_incognito
41        self.run_screenlock = run_screenlock
42
43    def Run(self):
44        """Run tests."""
45        start = datetime.datetime.now()
46
47        for i in range(self.count):
48            if self.count > 1:
49                logging.info('Starting iteration %d.', i)
50            if self.run_cryptohome:
51                self.RunCryptohomeTest()
52            if self.run_incognito:
53                self.RunIncognitoTest()
54            if self.run_screenlock:
55                self.RunScreenlockTest()
56
57        elapsed = datetime.datetime.now() - start
58        logging.info('Tests succeeded in %s seconds.', elapsed.seconds)
59
60    def RunCryptohomeTest(self):
61        """Test Cryptohome."""
62        logging.info('RunCryptohomeTest: Starting chrome and logging in.')
63        # Only run ARC tests for P.
64        run_arc_tests = (utils.is_arc_available()
65                         and arc.get_android_sdk_version() <= 28)
66        arc_mode = arc_common.ARC_MODE_ENABLED if run_arc_tests else None
67        with chrome.Chrome(arc_mode=arc_mode, num_tries=1) as cr:
68            # Check that the cryptohome is mounted.
69            # is_vault_mounted throws an exception if it fails.
70            logging.info('Checking mounted cryptohome.')
71            cryptohome.is_vault_mounted(user=cr.username, allow_fail=False)
72            # Navigate to about:blank.
73            tab = cr.browser.tabs[0]
74            tab.Navigate('about:blank')
75
76            # Evaluate some javascript.
77            logging.info('Evaluating JavaScript.')
78            if tab.EvaluateJavaScript('2+2') != 4:
79                raise TestFail('EvaluateJavaScript failed')
80
81            # ARC test.
82            if run_arc_tests:
83                arc.wait_for_adb_ready()
84                logging.info('Android booted successfully.')
85                arc.wait_for_android_process('org.chromium.arc.intent_helper')
86                if not arc.is_package_installed('android'):
87                    raise TestFail(
88                            '"android" system package was not listed by '
89                            'Package Manager.')
90
91        if run_arc_tests:
92            utils.poll_for_condition(lambda: not arc.
93                                     is_android_container_alive(),
94                                     timeout=15,
95                                     desc='Android container still running '
96                                     'after Chrome shutdown.')
97
98    def RunIncognitoTest(self):
99        """Test Incognito mode."""
100        logging.info('RunIncognitoTest')
101        with chrome.Chrome(logged_in=False):
102            if not cryptohome.is_guest_vault_mounted():
103                raise TestFail('Expected to find a guest vault mounted.')
104        if cryptohome.is_guest_vault_mounted(allow_fail=True):
105            raise TestFail('Expected to NOT find a guest vault mounted.')
106
107    def RunScreenlockTest(self):
108        """Run a test that locks the screen."""
109        logging.info('RunScreenlockTest')
110        with chrome.Chrome(autotest_ext=True) as cr:
111            cr.autotest_ext.ExecuteJavaScript(
112                    'chrome.autotestPrivate.lockScreen();')
113            utils.poll_for_condition(lambda: cr.login_status['isScreenLocked'],
114                                     timeout=15,
115                                     exception=TestFail('Screen not locked'))
116
117    @staticmethod
118    def ParseArgs(argv):
119        """Parse command line.
120
121    Args:
122      argv: List of command line arguments.
123
124    Returns:
125      List of parsed opts.
126    """
127        parser = argparse.ArgumentParser(description=__doc__)
128        parser.add_argument('--count',
129                            type=int,
130                            default=1,
131                            help='Number of iterations of the test to run.')
132        parser.add_argument('--run-all',
133                            default=False,
134                            action='store_true',
135                            help='Run all tests.')
136        parser.add_argument('--run-cryptohome',
137                            default=False,
138                            action='store_true',
139                            help='Run Cryptohome test.')
140        parser.add_argument('--run-incognito',
141                            default=False,
142                            action='store_true',
143                            help='Run Incognito test.')
144        parser.add_argument('--run-screenlock',
145                            default=False,
146                            action='store_true',
147                            help='Run Screenlock test.')
148        return parser.parse_args(argv)
149
150
151def main(argv):
152    '''The main function.'''
153    opts = TelemetryCheck.ParseArgs(argv)
154
155    # Run all tests if none are specified.
156    if opts.run_all or not (opts.run_cryptohome or opts.run_incognito
157                            or opts.run_screenlock):
158        opts.run_cryptohome = opts.run_screenlock = True
159        opts.run_incognito = False  # crbug.com/970065
160
161    TelemetryCheck(opts.count, opts.run_cryptohome, opts.run_incognito,
162                   opts.run_screenlock).Run()
163
164
165if __name__ == '__main__':
166    sys.exit(main(sys.argv[1:]))
167