OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 |
| 5 /** |
| 6 * @fileoverview Generator script for creating gtest-style JavaScript |
| 7 * tests for WebUI and unit tests. Generates C++ gtest wrappers |
| 8 * which will invoke the appropriate JavaScript for each test. |
| 9 * @author scr@chromium.org (Sheridan Rawlins) |
| 10 * @see WebUI testing: http://goo.gl/ZWFXF |
| 11 * @see gtest documentation: http://goo.gl/Ujj3H |
| 12 * @see chrome/chrome_tests.gypi |
| 13 * @see tools/gypv8sh.py |
| 14 */ |
| 15 |
| 16 // Arguments from rules in chrome_tests.gypi are passed in through |
| 17 // python script gypv8sh.py. |
4 if (arguments.length < 4) { | 18 if (arguments.length < 4) { |
5 print('usage: ' + | 19 print('usage: ' + |
6 arguments[0] + ' path-to-testfile.js testfile.js output.cc test-type'); | 20 arguments[0] + ' path-to-testfile.js testfile.js output.cc test-type'); |
7 quit(-1); | 21 quit(-1); |
8 } | 22 } |
| 23 |
| 24 /** |
| 25 * Full path to the test input file. |
| 26 * @type {string} |
| 27 */ |
9 var jsFile = arguments[1]; | 28 var jsFile = arguments[1]; |
| 29 |
| 30 /** |
| 31 * Relative path to the test input file appropriate for use in the |
| 32 * C++ TestFixture's addLibrary method. |
| 33 * @type {string} |
| 34 */ |
10 var jsFileBase = arguments[2]; | 35 var jsFileBase = arguments[2]; |
| 36 |
| 37 /** |
| 38 * Path to C++ file generation is outputting to. |
| 39 * @type {string} |
| 40 */ |
11 var outputFile = arguments[3]; | 41 var outputFile = arguments[3]; |
| 42 |
| 43 /** |
| 44 * Type of this test. |
| 45 * @type {string} ('unit'| 'webui') |
| 46 */ |
12 var testType = arguments[4]; | 47 var testType = arguments[4]; |
13 | 48 |
| 49 /** |
| 50 * C++ gtest macro to use for TEST_F depending on |testType|. |
| 51 * @type {string} ('TEST_F'|'IN_PROC_BROWSER_TEST_F') |
| 52 */ |
| 53 var testF; |
| 54 |
| 55 /** |
| 56 * Keeps track of whether a typedef has been generated for each test |
| 57 * fixture. |
| 58 * @type {Object.<string, string>} |
| 59 */ |
| 60 var typedeffedCppFixtures = {}; |
| 61 |
| 62 /** |
| 63 * Maintains a list of relative file paths to add to each gtest body |
| 64 * for inclusion at runtime before running each JavaScript test. |
| 65 * @type {Array.<string>} |
| 66 */ |
| 67 var genIncludes = []; |
| 68 |
14 // Generate the file to stdout. | 69 // Generate the file to stdout. |
15 print('// GENERATED FILE'); | 70 print('// GENERATED FILE'); |
16 print('// ' + arguments.join(' ')); | 71 print('// ' + arguments.join(' ')); |
17 print('// PLEASE DO NOT HAND EDIT!'); | 72 print('// PLEASE DO NOT HAND EDIT!'); |
18 print(); | 73 print(); |
19 | 74 |
20 var testF; | 75 // Output some C++ headers based upon the |testType|. |
21 | 76 // |
| 77 // Currently supports: |
| 78 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass. |
| 79 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest superclass. |
22 if (testType === 'unit') { | 80 if (testType === 'unit') { |
23 print('#include "chrome/test/base/v8_unit_test.h"'); | 81 print('#include "chrome/test/base/v8_unit_test.h"'); |
24 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; | 82 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; |
25 testF = 'TEST_F'; | 83 testF = 'TEST_F'; |
26 } else { | 84 } else { |
27 print('#include "chrome/browser/ui/webui/web_ui_browsertest.h"'); | 85 print('#include "chrome/browser/ui/webui/web_ui_browsertest.h"'); |
28 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; | 86 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; |
29 testF = 'IN_PROC_BROWSER_TEST_F'; | 87 testF = 'IN_PROC_BROWSER_TEST_F'; |
30 } | 88 } |
31 print('#include "googleurl/src/gurl.h"'); | 89 print('#include "googleurl/src/gurl.h"'); |
32 print('#include "testing/gtest/include/gtest/gtest.h"'); | 90 print('#include "testing/gtest/include/gtest/gtest.h"'); |
33 print(); | 91 print(); |
34 | 92 |
| 93 /** |
| 94 * Convert the |includeFile| to paths appropriate for immediate |
| 95 * inclusion (path) and runtime inclusion (base). |
| 96 * @param {string} includeFile The file to include. |
| 97 * @return {{path: string, base: string}} Object describing the paths |
| 98 * for |includeFile|. |
| 99 */ |
| 100 function includeFileToPaths(includeFile) { |
| 101 return { |
| 102 path: jsFile.replace(/[^\/]+$/, includeFile), |
| 103 base: jsFileBase.replace(/[^\/]+$/, includeFile), |
| 104 }; |
| 105 } |
| 106 |
| 107 /** |
| 108 * Output |code| verbatim. |
| 109 * @param {string} code The code to output. |
| 110 */ |
35 function GEN(code) { | 111 function GEN(code) { |
36 print(code); | 112 print(code); |
37 } | 113 } |
38 | 114 |
39 var typedeffedCppFixtures = {}; | 115 /** |
| 116 * Generate includes for the current |jsFile| by including them |
| 117 * immediately and at runtime. |
| 118 * @param {Array.<string>} includes Paths to JavaScript files to |
| 119 * include immediately and at runtime. |
| 120 */ |
| 121 function GEN_INCLUDE(includes) { |
| 122 for (var i = 0; i < includes.length; i++) { |
| 123 var includePaths = includeFileToPaths(includes[i]); |
| 124 var js = read(includePaths.path); |
| 125 ('global', eval)(js); |
| 126 genIncludes.push(includePaths.base); |
| 127 } |
| 128 } |
40 | 129 |
| 130 /** |
| 131 * Generate gtest-style TEST_F definitions for C++ with a body that |
| 132 * will invoke the |testBody| for |testFixture|.|testFunction|. |
| 133 * @param {string} testFixture The name of this test's fixture. |
| 134 * @param {string} testFunction The name of this test's function. |
| 135 * @param {Function} testBody The function body to execute for this test. |
| 136 */ |
41 function TEST_F(testFixture, testFunction, testBody) { | 137 function TEST_F(testFixture, testFunction, testBody) { |
42 var browsePreload = this[testFixture].prototype.browsePreload; | 138 var browsePreload = this[testFixture].prototype.browsePreload; |
43 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload; | 139 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload; |
44 var testGenPreamble = this[testFixture].prototype.testGenPreamble; | 140 var testGenPreamble = this[testFixture].prototype.testGenPreamble; |
45 var testGenPostamble = this[testFixture].prototype.testGenPostamble; | 141 var testGenPostamble = this[testFixture].prototype.testGenPostamble; |
46 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture; | 142 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture; |
47 var isAsyncParam = testType === 'unit' ? '' : | 143 var isAsyncParam = testType === 'unit' ? '' : |
48 this[testFixture].prototype.isAsync + ', '; | 144 this[testFixture].prototype.isAsync + ', '; |
49 var testShouldFail = this[testFixture].prototype.testShouldFail; | 145 var testShouldFail = this[testFixture].prototype.testShouldFail; |
50 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE'; | 146 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE'; |
| 147 var extraLibraries = genIncludes.concat( |
| 148 this[testFixture].prototype.extraLibraries.map( |
| 149 function(includeFile) { |
| 150 return includeFileToPaths(includeFile).base; |
| 151 })); |
51 | 152 |
52 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) { | 153 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) { |
53 print('typedef ' + typedefCppFixture + ' ' + testFixture + ';'); | 154 print('typedef ' + typedefCppFixture + ' ' + testFixture + ';'); |
54 typedeffedCppFixtures[testFixture] = typedefCppFixture; | 155 typedeffedCppFixtures[testFixture] = typedefCppFixture; |
55 } | 156 } |
56 | 157 |
57 print(testF + '(' + testFixture + ', ' + testFunction + ') {'); | 158 print(testF + '(' + testFixture + ', ' + testFunction + ') {'); |
58 if (testGenPreamble) | 159 if (testGenPreamble) |
59 testGenPreamble(testFixture, testFunction); | 160 testGenPreamble(testFixture, testFunction); |
| 161 for (var i = 0; i < extraLibraries.length; i++) { |
| 162 print(' AddLibrary(FilePath(FILE_PATH_LITERAL("' + |
| 163 extraLibraries[i].replace(/\\/g, '/') + '")));'); |
| 164 } |
60 print(' AddLibrary(FilePath(FILE_PATH_LITERAL("' + | 165 print(' AddLibrary(FilePath(FILE_PATH_LITERAL("' + |
61 jsFileBase.replace(/\\/g, '/') + '")));'); | 166 jsFileBase.replace(/\\/g, '/') + '")));'); |
62 if (browsePreload) { | 167 if (browsePreload) { |
63 print(' BrowsePreload(GURL("' + browsePreload + '"), "' + testFixture + | 168 print(' BrowsePreload(GURL("' + browsePreload + '"), "' + testFixture + |
64 '", "' + testFunction + '");'); | 169 '", "' + testFunction + '");'); |
65 } | 170 } |
66 if (browsePrintPreload) { | 171 if (browsePrintPreload) { |
67 print(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' + | 172 print(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' + |
68 ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))),\n' + | 173 ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))),\n' + |
69 ' "' + testFixture + '", "' + testFunction + '");'); | 174 ' "' + testFixture + '", "' + testFunction + '");'); |
70 } | 175 } |
71 print(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam + | 176 print(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam + |
72 '"' + testFixture + '", ' + | 177 '"' + testFixture + '", ' + |
73 '"' + testFunction + '"));'); | 178 '"' + testFunction + '"));'); |
74 if (testGenPostamble) | 179 if (testGenPostamble) |
75 testGenPostamble(testFixture, testFunction); | 180 testGenPostamble(testFixture, testFunction); |
76 print('}'); | 181 print('}'); |
77 print(); | 182 print(); |
78 } | 183 } |
79 | 184 |
| 185 // Now that generation functions are defined, load in |jsFile|. |
80 var js = read(jsFile); | 186 var js = read(jsFile); |
81 eval(js); | 187 eval(js); |
OLD | NEW |