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.
5
6"""Launches the remoting webapp unit test in chrome with the appropriate flags.
7"""
8
9import argparse
10import os
11import platform
12import sys
13import tempfile
14import urllib
15
16def 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
28
29
30def BuildTestPageUri(opt_module=None, opt_coverage=False):
31  """Builds the Uri for the test page with params."""
32  script_path = os.path.dirname(__file__)
33
34  test_page_path = os.path.join(script_path,
35      '../../out/Debug/remoting/unittests/unittest.html')
36  test_page_path = 'file://' + os.path.abspath(test_page_path)
37
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 + '"'
46
47
48def 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)
59
60
61def ParseArgs():
62  parser = argparse.ArgumentParser()
63  chrome_path = GetChromePath()
64
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')
73
74  return parser.parse_args(sys.argv[1:])
75
76
77def main():
78  args = ParseArgs()
79  command_line = ""
80
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
86
87  command_line = BuildCommandLine(args.chrome_path, args.module, args.coverage)
88  os.system(command_line)
89  return 0
90
91
92if __name__ == '__main__':
93  sys.exit(main())
94