1 # Copyright (c) 2014 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 5 import os, shutil, tempfile 6 7 from autotest_lib.client.bin import test, utils 8 from autotest_lib.client.common_lib import error 9 from autotest_lib.client.cros import constants 10 from autotest_lib.client.cros.graphics import graphics_utils 11 12 13 class ChromeBinaryTest(test.test): 14 """ 15 Base class for tests to run chrome test binaries without signing in and 16 running Chrome. 17 """ 18 19 CHROME_TEST_DEP = 'chrome_test' 20 CHROME_SANDBOX = '/opt/google/chrome/chrome-sandbox' 21 home_dir = None 22 23 def setup(self): 24 self.job.setup_dep([self.CHROME_TEST_DEP]) 25 26 27 def initialize(self): 28 test_dep_dir = os.path.join(self.autodir, 'deps', 29 self.CHROME_TEST_DEP) 30 self.job.install_pkg(self.CHROME_TEST_DEP, 'dep', test_dep_dir) 31 32 self.cr_source_dir = '%s/test_src' % test_dep_dir 33 self.test_binary_dir = '%s/out/Release' % self.cr_source_dir 34 self.home_dir = tempfile.mkdtemp() 35 36 37 def cleanup(self): 38 if self.home_dir: 39 shutil.rmtree(self.home_dir, ignore_errors=True) 40 41 42 def get_chrome_binary_path(self, binary_to_run): 43 return os.path.join(self.test_binary_dir, binary_to_run) 44 45 46 def run_chrome_test_binary(self, binary_to_run, extra_params='', prefix='', 47 as_chronos=True): 48 """ 49 Run chrome test binary. 50 51 @param binary_to_run: The name of the browser test binary. 52 @param extra_params: Arguments for the browser test binary. 53 @param prefix: Prefix to the command that invokes the test binary. 54 @param as_chronos: Boolean indicating if the tests should run in a 55 chronos shell. 56 57 @raises: error.TestFail if there is error running the command. 58 """ 59 binary = self.get_chrome_binary_path(binary_to_run) 60 cmd = '%s/%s %s' % (self.test_binary_dir, binary_to_run, extra_params) 61 env_vars = 'HOME=%s CR_SOURCE_ROOT=%s CHROME_DEVEL_SANDBOX=%s' % ( 62 self.home_dir, self.cr_source_dir, self.CHROME_SANDBOX) 63 cmd = '%s %s' % (env_vars, prefix + cmd) 64 65 try: 66 if utils.is_freon(): 67 if as_chronos: 68 utils.system('su %s -c \'%s\'' % ('chronos', cmd)) 69 else: 70 utils.system(cmd) 71 else: 72 if as_chronos: 73 graphics_utils.xsystem(cmd, user='chronos') 74 else: 75 graphics_utils.xsystem(cmd) 76 except error.CmdError as e: 77 raise error.TestFail('%s failed! %s' % (binary_to_run, e)) 78 79 80 def nuke_chrome(func): 81 """Decorator to nuke the Chrome browser processes.""" 82 83 def wrapper(*args, **kargs): 84 open(constants.DISABLE_BROWSER_RESTART_MAGIC_FILE, 'w').close() 85 try: 86 try: 87 utils.nuke_process_by_name( 88 name=constants.BROWSER, with_prejudice=True) 89 except error.AutoservPidAlreadyDeadError: 90 pass 91 return func(*args, **kargs) 92 finally: 93 # Allow chrome to be restarted again later. 94 os.unlink(constants.DISABLE_BROWSER_RESTART_MAGIC_FILE) 95 return wrapper 96