1#!/usr/bin/python2 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 7'''Sanity tests for Chrome on Chrome OS. 8 9This script runs a number of sanity tests to ensure that Chrome browser on 10Chrome OS is functional. 11''' 12 13from __future__ import absolute_import 14from __future__ import division 15from __future__ import print_function 16 17import argparse 18import datetime 19import logging 20import sys 21 22# This sets up import paths for autotest. 23import common 24from autotest_lib.client.bin import utils 25from autotest_lib.client.common_lib.cros import arc, arc_common, chrome 26from autotest_lib.client.common_lib.error import TestFail 27from autotest_lib.client.cros import cryptohome 28from six.moves import range 29 30 31class TelemetrySanity(object): 32 """Class for running sanity tests to verify telemetry.""" 33 34 35 def __init__(self, count=1, run_cryptohome=True, run_incognito=True, 36 run_screenlock=True): 37 self.count = count 38 self.run_cryptohome = run_cryptohome 39 self.run_incognito = run_incognito 40 self.run_screenlock = run_screenlock 41 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 61 def RunCryptohomeTest(self): 62 """Test Cryptohome.""" 63 logging.info('RunCryptohomeTest: Starting chrome and logging in.') 64 # Only run ARC tests for P. 65 run_arc_tests = (utils.is_arc_available() and 66 arc.get_android_sdk_version() <= 28) 67 arc_mode = arc_common.ARC_MODE_ENABLED if run_arc_tests else None 68 with chrome.Chrome(arc_mode=arc_mode, num_tries=1) as cr: 69 # Check that the cryptohome is mounted. 70 # is_vault_mounted throws an exception if it fails. 71 logging.info('Checking mounted cryptohome.') 72 cryptohome.is_vault_mounted(user=cr.username, allow_fail=False) 73 # Navigate to about:blank. 74 tab = cr.browser.tabs[0] 75 tab.Navigate('about:blank') 76 77 # Evaluate some javascript. 78 logging.info('Evaluating JavaScript.') 79 if tab.EvaluateJavaScript('2+2') != 4: 80 raise TestFail('EvaluateJavaScript failed') 81 82 # ARC test. 83 if run_arc_tests: 84 arc.wait_for_adb_ready() 85 logging.info('Android booted successfully.') 86 arc.wait_for_android_process('org.chromium.arc.intent_helper') 87 if not arc.is_package_installed('android'): 88 raise TestFail('"android" system package was not listed by ' 89 'Package Manager.') 90 91 if run_arc_tests: 92 utils.poll_for_condition(lambda: not arc.is_android_container_alive(), 93 timeout=15, 94 desc='Android container still running ' 95 'after Chrome shutdown.') 96 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 108 def RunScreenlockTest(self): 109 """Run a test that locks the screen.""" 110 logging.info('RunScreenlockTest') 111 with chrome.Chrome(autotest_ext=True) as cr: 112 cr.autotest_ext.ExecuteJavaScript('chrome.autotestPrivate.lockScreen();') 113 utils.poll_for_condition( 114 lambda: cr.login_status['isScreenLocked'], 115 timeout=15, 116 exception=TestFail('Screen not locked')) 117 118 119 @staticmethod 120 def ParseArgs(argv): 121 """Parse command line. 122 123 Args: 124 argv: List of command line arguments. 125 126 Returns: 127 List of parsed opts. 128 """ 129 parser = argparse.ArgumentParser(description=__doc__) 130 parser.add_argument('--count', type=int, default=1, 131 help='Number of iterations of the test to run.') 132 parser.add_argument('--run-all', default=False, action='store_true', 133 help='Run all tests.') 134 parser.add_argument('--run-cryptohome', default=False, action='store_true', 135 help='Run Cryptohome test.') 136 parser.add_argument('--run-incognito', default=False, action='store_true', 137 help='Run Incognito test.') 138 parser.add_argument('--run-screenlock', default=False, action='store_true', 139 help='Run Screenlock test.') 140 return parser.parse_args(argv) 141 142 143def main(argv): 144 '''The main function.''' 145 opts = TelemetrySanity.ParseArgs(argv) 146 147 # Run all tests if none are specified. 148 if opts.run_all or not (opts.run_cryptohome or opts.run_incognito or 149 opts.run_screenlock): 150 opts.run_cryptohome = opts.run_screenlock = True 151 opts.run_incognito = False # crbug.com/970065 152 153 TelemetrySanity(opts.count, opts.run_cryptohome, opts.run_incognito, 154 opts.run_screenlock).Run() 155 156 157if __name__ == '__main__': 158 sys.exit(main(sys.argv[1:])) 159