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
3 // found in the LICENSE file.
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
16 // Arguments from rules in chrome_tests.gypi are passed in through
17 // python script gypv8sh.py.
18 if (arguments.length < 4) {
20 arguments[0] + ' path-to-testfile.js testfile.js output.cc test-type');
25 * Full path to the test input file.
28 var jsFile = arguments[1];
31 * Relative path to the test input file appropriate for use in the
32 * C++ TestFixture's addLibrary method.
35 var jsFileBase = arguments[2];
38 * Path to C++ file generation is outputting to.
41 var outputFile = arguments[3];
45 * @type {string} ('unit'| 'webui')
47 var testType = arguments[4];
50 * C++ gtest macro to use for TEST_F depending on |testType|.
51 * @type {string} ('TEST_F'|'IN_PROC_BROWSER_TEST_F')
56 * Keeps track of whether a typedef has been generated for each test
58 * @type {Object.<string, string>}
60 var typedeffedCppFixtures = {};
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>}
70 * When true, add calls to set_preload_test_(fixture|name). This is needed when
71 * |testType| === 'browser' to send an injection message before the page loads,
72 * but is not required or supported for |testType| === 'unit'.
75 var addSetPreloadInfo;
77 // Generate the file to stdout.
78 print('// GENERATED FILE');
79 print('// ' + arguments.join(' '));
80 print('// PLEASE DO NOT HAND EDIT!');
83 // Output some C++ headers based upon the |testType|.
85 // Currently supports:
86 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass.
87 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest superclass.
88 if (testType === 'unit') {
89 print('#include "chrome/test/base/v8_unit_test.h"');
90 testing.Test.prototype.typedefCppFixture = 'V8UnitTest';
92 addSetPreloadInfo = false;
94 print('#include "chrome/test/base/web_ui_browsertest.h"');
95 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest';
96 testF = 'IN_PROC_BROWSER_TEST_F';
97 addSetPreloadInfo = true;
99 print('#include "url/gurl.h"');
100 print('#include "testing/gtest/include/gtest/gtest.h"');
104 * Convert the |includeFile| to paths appropriate for immediate
105 * inclusion (path) and runtime inclusion (base).
106 * @param {string} includeFile The file to include.
107 * @return {{path: string, base: string}} Object describing the paths
110 function includeFileToPaths(includeFile) {
112 path: jsFile.replace(/[^\/\\]+$/, includeFile),
113 base: jsFileBase.replace(/[^\/\\]+$/, includeFile),
118 * Output |code| verbatim.
119 * @param {string} code The code to output.
126 * Generate includes for the current |jsFile| by including them
127 * immediately and at runtime.
128 * @param {Array.<string>} includes Paths to JavaScript files to
129 * include immediately and at runtime.
131 function GEN_INCLUDE(includes) {
132 for (var i = 0; i < includes.length; i++) {
133 var includePaths = includeFileToPaths(includes[i]);
134 var js = read(includePaths.path);
135 ('global', eval)(js);
136 genIncludes.push(includePaths.base);
141 * Generate gtest-style TEST_F definitions for C++ with a body that
142 * will invoke the |testBody| for |testFixture|.|testFunction|.
143 * @param {string} testFixture The name of this test's fixture.
144 * @param {string} testFunction The name of this test's function.
145 * @param {Function} testBody The function body to execute for this test.
147 function TEST_F(testFixture, testFunction, testBody) {
148 var browsePreload = this[testFixture].prototype.browsePreload;
149 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload;
150 var testGenPreamble = this[testFixture].prototype.testGenPreamble;
151 var testGenPostamble = this[testFixture].prototype.testGenPostamble;
152 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture;
153 var isAsyncParam = testType === 'unit' ? '' :
154 this[testFixture].prototype.isAsync + ', ';
155 var testShouldFail = this[testFixture].prototype.testShouldFail;
156 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE';
157 var extraLibraries = genIncludes.concat(
158 this[testFixture].prototype.extraLibraries.map(
159 function(includeFile) {
160 return includeFileToPaths(includeFile).base;
163 if (typedefCppFixture && !(testFixture in typedeffedCppFixtures)) {
164 print('typedef ' + typedefCppFixture + ' ' + testFixture + ';');
165 typedeffedCppFixtures[testFixture] = typedefCppFixture;
168 print(testF + '(' + testFixture + ', ' + testFunction + ') {');
169 for (var i = 0; i < extraLibraries.length; i++) {
170 print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' +
171 extraLibraries[i].replace(/\\/g, '/') + '")));');
173 print(' AddLibrary(base::FilePath(FILE_PATH_LITERAL("' +
174 jsFileBase.replace(/\\/g, '/') + '")));');
175 if (addSetPreloadInfo) {
176 print(' set_preload_test_fixture("' + testFixture + '");');
177 print(' set_preload_test_name("' + testFunction + '");');
180 testGenPreamble(testFixture, testFunction);
182 print(' BrowsePreload(GURL("' + browsePreload + '"));');
183 if (browsePrintPreload) {
184 print(' BrowsePrintPreload(GURL(WebUITestDataPathToURL(\n' +
185 ' FILE_PATH_LITERAL("' + browsePrintPreload + '"))));');
187 print(' ' + testPredicate + '(RunJavascriptTestF(' + isAsyncParam +
188 '"' + testFixture + '", ' +
189 '"' + testFunction + '"));');
190 if (testGenPostamble)
191 testGenPostamble(testFixture, testFunction);
196 // Now that generation functions are defined, load in |jsFile|.
197 var js = read(jsFile);