blob: 18866f50afb184edb3df03d95c085c6c94e6fc0a [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
Eric Carusoa4ac7a82015-08-06 10:53:54 -07009from autotest_lib.client.common_lib.cros import constants
Ilja H. Friedel8753d832014-10-07 22:30:06 -070010from autotest_lib.client.cros.graphics import graphics_utils
Yuli Huang678435f2014-02-28 01:03:37 +080011
12
13class 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
Ilja Friedelef925372014-07-09 16:47:01 -070042 def get_chrome_binary_path(self, binary_to_run):
43 return os.path.join(self.test_binary_dir, binary_to_run)
44
45
Yuli Huang678435f2014-02-28 01:03:37 +080046 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 """
Ilja Friedelef925372014-07-09 16:47:01 -070059 binary = self.get_chrome_binary_path(binary_to_run)
Yuli Huang678435f2014-02-28 01:03:37 +080060 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:
Michael Spang93804932014-10-28 17:24:22 -040066 if utils.is_freon():
67 if as_chronos:
68 utils.system('su %s -c \'%s\'' % ('chronos', cmd))
69 else:
70 utils.system(cmd)
Yuli Huang678435f2014-02-28 01:03:37 +080071 else:
Michael Spang93804932014-10-28 17:24:22 -040072 if as_chronos:
73 graphics_utils.xsystem(cmd, user='chronos')
74 else:
75 graphics_utils.xsystem(cmd)
Yuli Huang678435f2014-02-28 01:03:37 +080076 except error.CmdError as e:
77 raise error.TestFail('%s failed! %s' % (binary_to_run, e))
Owen Lin8293df32014-05-30 15:14:15 +080078
79
80def 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