Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(386)

Side by Side Diff: chrome/test/base/js2gtest.js

Issue 320753002: Support javascript gtests in an extension background page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixup comment Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 4
5 /** 5 /**
6 * @fileoverview Generator script for creating gtest-style JavaScript 6 * @fileoverview Generator script for creating gtest-style JavaScript
7 * tests for WebUI and unit tests. Generates C++ gtest wrappers 7 * tests for extensions, WebUI and unit tests. Generates C++ gtest wrappers
8 * which will invoke the appropriate JavaScript for each test. 8 * which will invoke the appropriate JavaScript for each test.
9 * @author scr@chromium.org (Sheridan Rawlins) 9 * @author scr@chromium.org (Sheridan Rawlins)
10 * @see WebUI testing: http://goo.gl/ZWFXF 10 * @see WebUI testing: http://goo.gl/ZWFXF
11 * @see gtest documentation: http://goo.gl/Ujj3H 11 * @see gtest documentation: http://goo.gl/Ujj3H
12 * @see chrome/chrome_tests.gypi 12 * @see chrome/chrome_tests.gypi
13 * @see tools/gypv8sh.py 13 * @see tools/gypv8sh.py
14 */ 14 */
15 15
16 // Arguments from rules in chrome_tests.gypi are passed in through 16 // Arguments from rules in chrome_tests.gypi are passed in through
17 // python script gypv8sh.py. 17 // python script gypv8sh.py.
(...skipping 24 matching lines...) Expand all
42 var depsFile = arguments[3]; 42 var depsFile = arguments[3];
43 43
44 /** 44 /**
45 * Path to C++ file generation is outputting to. 45 * Path to C++ file generation is outputting to.
46 * @type {string} 46 * @type {string}
47 */ 47 */
48 var outputFile = arguments[4]; 48 var outputFile = arguments[4];
49 49
50 /** 50 /**
51 * Type of this test. 51 * Type of this test.
52 * @type {string} ('unit'| 'webui') 52 * @type {string} ('extension' | 'unit' | 'webui')
Peter Lundblad 2014/06/11 16:45:07 Add code to validate that the test is one of these
Peter Lundblad 2014/06/11 16:45:07 Add code to validate that the test is one of these
Peter Lundblad 2014/06/11 16:45:07 Add code to validate that the test is one of these
David Tseng 2014/06/11 17:52:55 Sure ok.
53 */ 53 */
54 var testType = arguments[5]; 54 var testType = arguments[5];
55 55
56 /** 56 /**
57 * C++ gtest macro to use for TEST_F depending on |testType|. 57 * C++ gtest macro to use for TEST_F depending on |testType|.
58 * @type {string} ('TEST_F'|'IN_PROC_BROWSER_TEST_F') 58 * @type {string} ('TEST_F'|'IN_PROC_BROWSER_TEST_F')
59 */ 59 */
60 var testF; 60 var testF;
61 61
62 /** 62 /**
63 * Keeps track of whether a typedef has been generated for each test 63 * Keeps track of whether a typedef has been generated for each test
64 * fixture. 64 * fixture.
65 * @type {Object.<string, string>} 65 * @type {Object.<string, string>}
66 */ 66 */
67 var typedeffedCppFixtures = {}; 67 var typedeffedCppFixtures = {};
68 68
69 /** 69 /**
70 * Maintains a list of relative file paths to add to each gtest body 70 * Maintains a list of relative file paths to add to each gtest body
71 * for inclusion at runtime before running each JavaScript test. 71 * for inclusion at runtime before running each JavaScript test.
72 * @type {Array.<string>} 72 * @type {Array.<string>}
73 */ 73 */
74 var genIncludes = []; 74 var genIncludes = [];
75 75
76 /** 76 /**
77 * When true, add calls to set_preload_test_(fixture|name). This is needed when 77 * When true, add calls to set_preload_test_(fixture|name). This is needed when
78 * |testType| === 'browser' to send an injection message before the page loads, 78 * |testType| === 'browser' to send an injection message before the page loads,
79 * but is not required or supported for |testType| === 'unit'. 79 * but is not required or supported for |testType| === 'extension' or
Peter Lundblad 2014/06/11 16:45:07 nit: Is this comment out of date talking about typ
Peter Lundblad 2014/06/11 16:45:07 nit: Is this comment out of date talking about typ
Peter Lundblad 2014/06/11 16:45:07 nit: Is this comment out of date talking about typ
David Tseng 2014/06/11 17:52:55 Updated.
80 * |testType| === 'unit'.
80 * @type {boolean} 81 * @type {boolean}
81 */ 82 */
82 var addSetPreloadInfo; 83 var addSetPreloadInfo;
83 84
84 // Generate the file to stdout. 85 /**
85 print('// GENERATED FILE'); 86 * Whether cc headers need to be generated.
86 print('// ' + arguments.join(' ')); 87 * @type {boolean}
87 print('// PLEASE DO NOT HAND EDIT!'); 88 */
88 print(); 89 var needGenHeader = true;
89 90
90 // Output some C++ headers based upon the |testType|. 91 /**
91 // 92 * Helpful hint pointing back to the source js.
92 // Currently supports: 93 * @type {string}
93 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass. 94 */
94 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest superclass. 95 var argHint = '// ' + this['arguments'].join(' ');
95 if (testType === 'unit') { 96
96 print('#include "chrome/test/base/v8_unit_test.h"'); 97
97 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; 98 /**
98 testF = 'TEST_F'; 99 * Generates the header of the cc file to stdout.
99 addSetPreloadInfo = false; 100 * @param {string} testFixture Name of test fixture.
100 } else { 101 */
101 print('#include "chrome/test/base/web_ui_browser_test.h"'); 102 function maybeGenHeader(testFixture) {
102 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; 103 if (!needGenHeader)
103 testF = 'IN_PROC_BROWSER_TEST_F'; 104 return;
104 addSetPreloadInfo = true; 105 needGenHeader = false;
106 print('// GENERATED FILE');
107 print(argHint);
108 print('// PLEASE DO NOT HAND EDIT!');
109 print();
110
111 // Output some C++ headers based upon the |testType|.
112 //
113 // Currently supports:
114 // 'extension' - browser_tests harness, js2extension rule,
115 // ExtensionJSBrowserTest superclass.
116 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass.
117 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest
118 // superclass.
119 if (testType === 'extension') {
120 print('#include "chrome/test/base/extension_js_browser_test.h"');
121 testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest';
122 addSetPreloadInfo = false;
123 testF = 'IN_PROC_BROWSER_TEST_F';
124 } else if (testType === 'unit') {
125 print('#include "chrome/test/base/v8_unit_test.h"');
126 testing.Test.prototype.typedefCppFixture = 'V8UnitTest';
127 testF = 'TEST_F';
128 addSetPreloadInfo = false;
129 } else {
130 print('#include "chrome/test/base/web_ui_browser_test.h"');
131 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest';
132 testF = 'IN_PROC_BROWSER_TEST_F';
133 addSetPreloadInfo = true;
134 }
135 print('#include "url/gurl.h"');
136 print('#include "testing/gtest/include/gtest/gtest.h"');
137 if (this[testFixture].prototype.testGenCppIncludes)
138 this[testFixture].prototype.testGenCppIncludes();
139 print();
105 } 140 }
106 print('#include "url/gurl.h"'); 141
107 print('#include "testing/gtest/include/gtest/gtest.h"');
108 print();
109 142
110 /** 143 /**
111 * Convert the |includeFile| to paths appropriate for immediate 144 * Convert the |includeFile| to paths appropriate for immediate
112 * inclusion (path) and runtime inclusion (base). 145 * inclusion (path) and runtime inclusion (base).
113 * @param {string} includeFile The file to include. 146 * @param {string} includeFile The file to include.
114 * @return {{path: string, base: string}} Object describing the paths 147 * @return {{path: string, base: string}} Object describing the paths
115 * for |includeFile|. 148 * for |includeFile|.
116 */ 149 */
117 function includeFileToPaths(includeFile) { 150 function includeFileToPaths(includeFile) {
118 return { 151 return {
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 250
218 /** 251 /**
219 * Output |code| verbatim. 252 * Output |code| verbatim.
220 * @param {string} code The code to output. 253 * @param {string} code The code to output.
221 */ 254 */
222 function GEN(code) { 255 function GEN(code) {
223 print(code); 256 print(code);
224 } 257 }
225 258
226 /** 259 /**
260 * Outputs |commentEncodedCode| by converting it to a js fragment.
261 * @param {function} commentEncodedCode A function in the following format (note
262 * the space in '/ *' and '* /' should be removed to form a comment delimiter):
263 * function() {/ *! my_cpp_code.DoSomething(); * /
264 * Code between / *! and * / will be extracted and written to stdout.
265 */
266 function GEN_BLOCK(commentEncodedCode) {
267 var code = commentEncodedCode.toString().
268 replace(/^[^\/]+\/\*!?/, '').
269 replace(/\*\/[^\/]+$/, '').
270 replace(/^\n|\n$/, '').
271 replace(/\s+$/, '');
272 GEN(code);
273 }
274
275 /**
227 * Generate includes for the current |jsFile| by including them 276 * Generate includes for the current |jsFile| by including them
228 * immediately and at runtime. 277 * immediately and at runtime.
229 * @param {Array.<string>} includes Paths to JavaScript files to 278 * @param {Array.<string>} includes Paths to JavaScript files to
230 * include immediately and at runtime. 279 * include immediately and at runtime.
231 */ 280 */
232 function GEN_INCLUDE(includes) { 281 function GEN_INCLUDE(includes) {
233 for (var i = 0; i < includes.length; i++) { 282 for (var i = 0; i < includes.length; i++) {
234 var includePaths = includeFileToPaths(includes[i]); 283 var includePaths = includeFileToPaths(includes[i]);
235 var js = read(includePaths.path); 284 var js = read(includePaths.path);
236 ('global', eval)(js); 285 ('global', eval)(js);
237 genIncludes.push(includePaths.base); 286 genIncludes.push(includePaths.base);
238 } 287 }
239 } 288 }
240 289
241 /** 290 /**
242 * Generate gtest-style TEST_F definitions for C++ with a body that 291 * Generate gtest-style TEST_F definitions for C++ with a body that
243 * will invoke the |testBody| for |testFixture|.|testFunction|. 292 * will invoke the |testBody| for |testFixture|.|testFunction|.
244 * @param {string} testFixture The name of this test's fixture. 293 * @param {string} testFixture The name of this test's fixture.
245 * @param {string} testFunction The name of this test's function. 294 * @param {string} testFunction The name of this test's function.
246 * @param {Function} testBody The function body to execute for this test. 295 * @param {Function} testBody The function body to execute for this test.
247 */ 296 */
248 function TEST_F(testFixture, testFunction, testBody) { 297 function TEST_F(testFixture, testFunction, testBody) {
298 maybeGenHeader(testFixture);
Peter Lundblad 2014/06/11 16:45:07 Does the defered generation of the file header pla
David Tseng 2014/06/11 17:52:55 I haven't had any problems; was there a specific c
249 var browsePreload = this[testFixture].prototype.browsePreload; 299 var browsePreload = this[testFixture].prototype.browsePreload;
250 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload; 300 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload;
251 var testGenPreamble = this[testFixture].prototype.testGenPreamble; 301 var testGenPreamble = this[testFixture].prototype.testGenPreamble;
252 var testGenPostamble = this[testFixture].prototype.testGenPostamble; 302 var testGenPostamble = this[testFixture].prototype.testGenPostamble;
253 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture; 303 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture;
254 var isAsyncParam = testType === 'unit' ? '' : 304 var isAsyncParam = testType === 'unit' ? '' :
255 this[testFixture].prototype.isAsync + ', '; 305 this[testFixture].prototype.isAsync + ', ';
256 var testShouldFail = this[testFixture].prototype.testShouldFail; 306 var testShouldFail = this[testFixture].prototype.testShouldFail;
257 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE'; 307 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE';
258 var extraLibraries = genIncludes.concat( 308 var extraLibraries = genIncludes.concat(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 '"' + testFunction + '"));'); 341 '"' + testFunction + '"));');
292 if (testGenPostamble) 342 if (testGenPostamble)
293 testGenPostamble(testFixture, testFunction); 343 testGenPostamble(testFixture, testFunction);
294 print('}'); 344 print('}');
295 print(); 345 print();
296 } 346 }
297 347
298 // Now that generation functions are defined, load in |jsFile|. 348 // Now that generation functions are defined, load in |jsFile|.
299 var js = read(jsFile); 349 var js = read(jsFile);
300 eval(js); 350 eval(js);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698