blob: 6062c0163d57d641eaff0457d873c0429295134a [file] [log] [blame]
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Launches the remoting webapp unit test in chrome with the appropriate flags.
"""
import argparse
import os
import platform
import sys
import tempfile
import urllib
def GetChromePath():
"""Locates the chrome binary on the system."""
chrome_path = ''
if platform.system() == 'Darwin': # Darwin == MacOSX
chrome_path = (
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
elif platform.system() == 'Linux':
chrome_path = '/usr/bin/google-chrome'
else:
# TODO(kelvinp): Support chrome path location on Windows.
print 'Unsupported OS.'
return chrome_path
def BuildTestPageUri(opt_module=None, opt_coverage=False):
"""Builds the Uri for the test page with params."""
script_path = os.path.dirname(__file__)
test_page_path = os.path.join(script_path,
'../../out/Debug/remoting/unittests/unittest.html')
test_page_path = 'file://' + os.path.abspath(test_page_path)
test_page_params = {}
if opt_coverage:
test_page_params['coverage'] = 'true'
if opt_module:
test_page_params['module'] = opt_module
if test_page_params:
test_page_path = test_page_path + '?%s' % urllib.urlencode(test_page_params)
return '"' + test_page_path + '"'
def BuildCommandLine(chrome_path, opt_module, opt_coverage):
"""Builds the command line to execute."""
command = []
command.append('"' + chrome_path + '"')
command.append('--user-data-dir=' + tempfile.gettempdir())
# The flag |--allow-file-access-from-files| is required so that we can open
# JavaScript files using XHR and instrument them for code coverage.
command.append(' --allow-file-access-from-files')
test_page_path = BuildTestPageUri(opt_module, opt_coverage)
command.append(test_page_path)
return ' '.join(command)
def ParseArgs():
parser = argparse.ArgumentParser()
chrome_path = GetChromePath()
parser.add_argument(
'--chrome-path',
help='The path of the chrome binary to run the test.',
default=chrome_path)
parser.add_argument(
'--module', help='only run tests that belongs to MODULE')
parser.add_argument(
'--coverage', help='run the test with code coverage', action='store_true')
return parser.parse_args(sys.argv[1:])
def main():
args = ParseArgs()
command_line = ""
if not os.path.exists(args.chrome_path):
print 'Cannot locate the chrome binary in your system.'
print 'Please use the flag --chrome_path=CHROME_PATH to specify the chrome '
print 'binary to run the test.'
return 1
command_line = BuildCommandLine(args.chrome_path, args.module, args.coverage)
os.system(command_line)
return 0
if __name__ == '__main__':
sys.exit(main())