Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
1 """A script to generate the browser-element test boilerplate.
3 This script requires Python 2.7."""
5 from __future__ import print_function
7 import sys
8 import os
9 import stat
10 import argparse
11 import textwrap
12 import subprocess
14 html_template = textwrap.dedent("""\
15 <!DOCTYPE HTML>
16 <html>
17 <head>
18 <title>Test for Bug {bug}</title>
19 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
20 <script type="application/javascript" src="browserElementTestHelpers.js"></script>
21 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
22 </head>
23 <body>
24 <script type="application/javascript;version=1.7" src="browserElement_{test}.js">
25 </script>
26 </body>
27 </html>""")
29 # Note: Curly braces are escaped as "{{".
30 js_template = textwrap.dedent("""\
31 /* Any copyright is dedicated to the public domain.
32 http://creativecommons.org/publicdomain/zero/1.0/ */
34 // Bug {bug} - FILL IN TEST DESCRIPTION
35 "use strict";
37 SimpleTest.waitForExplicitFinish();
38 browserElementTestHelpers.setEnabledPref(true);
39 browserElementTestHelpers.addPermission();
41 function runTest() {{
42 var iframe = document.createElement('iframe');
43 SpecialPowers.wrap(iframe).mozbrowser = true;
45 // FILL IN TEST
47 document.body.appendChild(iframe);
48 }}
50 addEventListener('testready', runTest);
51 """)
53 def print_fill(s):
54 print(textwrap.fill(textwrap.dedent(s)))
56 def add_to_makefile(filenames):
57 """Add a list of filenames to this directory's Makefile.in, then open
58 $EDITOR and let the user move the filenames to their appropriate places in
59 the file.
61 """
62 lines_to_write = [''] + ['\t\t%s \\' % n for n in filenames]
63 with open('Makefile.in', 'a') as f:
64 f.write('\n'.join(lines_to_write))
66 if 'EDITOR' not in os.environ or not os.environ['EDITOR']:
67 print_fill("""\
68 Now open Makefile.in and move the filenames to their correct places.")
69 (Define $EDITOR and I'll open your editor for you next time.)""")
70 return
72 # Count the number of lines in Makefile.in.
73 with open('Makefile.in', 'r') as f:
74 num_lines = len(f.readlines())
76 try:
77 subprocess.call([os.environ['EDITOR'],
78 '+%d' % (num_lines - len(lines_to_write) + 2),
79 'Makefile.in'])
80 except Exception as e:
81 print_fill("Error opening $EDITOR: %s." % str(e))
82 print()
83 print_fill("""\
84 Please open Makefile.in and move the filenames at the bottom of the
85 file to their correct places.""")
87 def main(test_name, bug_number):
88 global html_template, js_template
90 def format(str):
91 return str.format(bug=bug_number, test=test_name)
93 def create_file(filename, template):
94 path = os.path.join(os.path.dirname(sys.argv[0]), format(filename))
95 # Create a new file, bailing with an error if the file exists.
96 fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL)
98 try:
99 # This file has 777 permission when created, for whatever reason. Make it rw-rw-r---.
100 os.fchmod(fd, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IWGRP | stat.S_IROTH)
101 except:
102 # fchmod doesn't work on Windows.
103 pass
105 with os.fdopen(fd, 'w') as file:
106 file.write(format(template))
108 create_file('browserElement_{test}.js', js_template)
109 create_file('test_browserElement_inproc_{test}.html', html_template)
110 create_file('test_browserElement_oop_{test}.html', html_template)
112 add_to_makefile([format(x) for x in ['browserElement_{test}.js',
113 'test_browserElement_inproc_{test}.html',
114 'test_browserElement_oop_{test}.html']])
116 if __name__ == '__main__':
117 parser = argparse.ArgumentParser(description="Create a new browser-element testcase.")
118 parser.add_argument('test_name')
119 parser.add_argument('bug_number', type=int)
120 args = parser.parse_args()
121 main(args.test_name, args.bug_number)