Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / tools / run_webapp_unittests.py
blobbff71282bc91c480fd6c04effbeb2285246cc183
1 #!/usr/bin/env python
2 # Copyright 2014 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Launches the remoting webapp unit tests in chrome with the appropriate flags.
7 """
9 import argparse
10 import os
11 import platform
12 import sys
13 import tempfile
14 import urllib
16 def GetChromePath():
17 """Locates the chrome binary on the system."""
18 chrome_path = ''
19 if platform.system() == 'Darwin': # Darwin == MacOSX
20 chrome_path = (
21 '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome')
22 elif platform.system() == 'Linux':
23 chrome_path = '/usr/bin/google-chrome'
24 else:
25 # TODO(kelvinp): Support chrome path location on Windows.
26 print 'Unsupported OS.'
27 return chrome_path
30 def BuildTestPageUri(opt_module=None, opt_coverage=False):
31 """Builds the Uri for the test page with params."""
32 script_path = os.path.dirname(__file__)
34 test_page_path = os.path.join(script_path,
35 '../../out/Debug/remoting/unittests/unittests.html')
36 test_page_path = 'file://' + os.path.abspath(test_page_path)
38 test_page_params = {}
39 if opt_coverage:
40 test_page_params['coverage'] = 'true'
41 if opt_module:
42 test_page_params['module'] = opt_module
43 if test_page_params:
44 test_page_path = test_page_path + '?%s' % urllib.urlencode(test_page_params)
45 return '"' + test_page_path + '"'
48 def BuildCommandLine(chrome_path, opt_module, opt_coverage):
49 """Builds the command line to execute."""
50 command = []
51 command.append('"' + chrome_path + '"')
52 command.append('--user-data-dir=' + tempfile.gettempdir())
53 # The flag |--allow-file-access-from-files| is required so that we can open
54 # JavaScript files using XHR and instrument them for code coverage.
55 command.append(' --allow-file-access-from-files')
56 test_page_path = BuildTestPageUri(opt_module, opt_coverage)
57 command.append(test_page_path)
58 return ' '.join(command)
61 def ParseArgs():
62 parser = argparse.ArgumentParser()
63 chrome_path = GetChromePath()
65 parser.add_argument(
66 '--chrome-path',
67 help='The path of the chrome binary to run the test.',
68 default=chrome_path)
69 parser.add_argument(
70 '--module', help='only run tests that belongs to MODULE')
71 parser.add_argument(
72 '--coverage', help='run the test with code coverage', action='store_true')
74 return parser.parse_args(sys.argv[1:])
77 def main():
78 args = ParseArgs()
79 command_line = ""
81 if not os.path.exists(args.chrome_path):
82 print 'Cannot locate the chrome binary in your system.'
83 print 'Please use the flag --chrome_path=CHROME_PATH to specify the chrome '
84 print 'binary to run the test.'
85 return 1
87 command_line = BuildCommandLine(args.chrome_path, args.module, args.coverage)
88 os.system(command_line)
89 return 0
92 if __name__ == '__main__':
93 sys.exit(main())