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