| OLD | NEW |
| 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 Loading... |
| 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') |
| 53 */ | 53 */ |
| 54 var testType = arguments[5]; | 54 var testType = arguments[5]; |
| 55 if (testType != 'extension' && |
| 56 testType != 'unit' && |
| 57 testType != 'webui') { |
| 58 print('Invalid test type: ' + testType); |
| 59 quit(-1); |
| 60 } |
| 55 | 61 |
| 56 /** | 62 /** |
| 57 * C++ gtest macro to use for TEST_F depending on |testType|. | 63 * C++ gtest macro to use for TEST_F depending on |testType|. |
| 58 * @type {string} ('TEST_F'|'IN_PROC_BROWSER_TEST_F') | 64 * @type {string} ('TEST_F'|'IN_PROC_BROWSER_TEST_F') |
| 59 */ | 65 */ |
| 60 var testF; | 66 var testF; |
| 61 | 67 |
| 62 /** | 68 /** |
| 63 * Keeps track of whether a typedef has been generated for each test | 69 * Keeps track of whether a typedef has been generated for each test |
| 64 * fixture. | 70 * fixture. |
| 65 * @type {Object.<string, string>} | 71 * @type {Object.<string, string>} |
| 66 */ | 72 */ |
| 67 var typedeffedCppFixtures = {}; | 73 var typedeffedCppFixtures = {}; |
| 68 | 74 |
| 69 /** | 75 /** |
| 70 * Maintains a list of relative file paths to add to each gtest body | 76 * Maintains a list of relative file paths to add to each gtest body |
| 71 * for inclusion at runtime before running each JavaScript test. | 77 * for inclusion at runtime before running each JavaScript test. |
| 72 * @type {Array.<string>} | 78 * @type {Array.<string>} |
| 73 */ | 79 */ |
| 74 var genIncludes = []; | 80 var genIncludes = []; |
| 75 | 81 |
| 76 /** | 82 /** |
| 77 * When true, add calls to set_preload_test_(fixture|name). This is needed when | 83 * 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, | 84 * |testType| === 'webui' to send an injection message before the page loads, |
| 79 * but is not required or supported for |testType| === 'unit'. | 85 * but is not required or supported by any other test type. |
| 80 * @type {boolean} | 86 * @type {boolean} |
| 81 */ | 87 */ |
| 82 var addSetPreloadInfo; | 88 var addSetPreloadInfo; |
| 83 | 89 |
| 84 // Generate the file to stdout. | 90 /** |
| 85 print('// GENERATED FILE'); | 91 * Whether cc headers need to be generated. |
| 86 print('// ' + arguments.join(' ')); | 92 * @type {boolean} |
| 87 print('// PLEASE DO NOT HAND EDIT!'); | 93 */ |
| 88 print(); | 94 var needGenHeader = true; |
| 89 | 95 |
| 90 // Output some C++ headers based upon the |testType|. | 96 /** |
| 91 // | 97 * Helpful hint pointing back to the source js. |
| 92 // Currently supports: | 98 * @type {string} |
| 93 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass. | 99 */ |
| 94 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest superclass. | 100 var argHint = '// ' + this['arguments'].join(' '); |
| 95 if (testType === 'unit') { | 101 |
| 96 print('#include "chrome/test/base/v8_unit_test.h"'); | 102 |
| 97 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; | 103 /** |
| 98 testF = 'TEST_F'; | 104 * Generates the header of the cc file to stdout. |
| 99 addSetPreloadInfo = false; | 105 * @param {string?} testFixture Name of test fixture. |
| 100 } else { | 106 */ |
| 101 print('#include "chrome/test/base/web_ui_browser_test.h"'); | 107 function maybeGenHeader(testFixture) { |
| 102 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; | 108 if (!needGenHeader) |
| 103 testF = 'IN_PROC_BROWSER_TEST_F'; | 109 return; |
| 104 addSetPreloadInfo = true; | 110 needGenHeader = false; |
| 111 print('// GENERATED FILE'); |
| 112 print(argHint); |
| 113 print('// PLEASE DO NOT HAND EDIT!'); |
| 114 print(); |
| 115 |
| 116 // Output some C++ headers based upon the |testType|. |
| 117 // |
| 118 // Currently supports: |
| 119 // 'extension' - browser_tests harness, js2extension rule, |
| 120 // ExtensionJSBrowserTest superclass. |
| 121 // 'unit' - unit_tests harness, js2unit rule, V8UnitTest superclass. |
| 122 // 'webui' - browser_tests harness, js2webui rule, WebUIBrowserTest |
| 123 // superclass. |
| 124 if (testType === 'extension') { |
| 125 print('#include "chrome/test/base/extension_js_browser_test.h"'); |
| 126 testing.Test.prototype.typedefCppFixture = 'ExtensionJSBrowserTest'; |
| 127 addSetPreloadInfo = false; |
| 128 testF = 'IN_PROC_BROWSER_TEST_F'; |
| 129 } else if (testType === 'unit') { |
| 130 print('#include "chrome/test/base/v8_unit_test.h"'); |
| 131 testing.Test.prototype.typedefCppFixture = 'V8UnitTest'; |
| 132 testF = 'TEST_F'; |
| 133 addSetPreloadInfo = false; |
| 134 } else { |
| 135 print('#include "chrome/test/base/web_ui_browser_test.h"'); |
| 136 testing.Test.prototype.typedefCppFixture = 'WebUIBrowserTest'; |
| 137 testF = 'IN_PROC_BROWSER_TEST_F'; |
| 138 addSetPreloadInfo = true; |
| 139 } |
| 140 print('#include "url/gurl.h"'); |
| 141 print('#include "testing/gtest/include/gtest/gtest.h"'); |
| 142 if (testFixture && this[testFixture].prototype.testGenCppIncludes) |
| 143 this[testFixture].prototype.testGenCppIncludes(); |
| 144 print(); |
| 105 } | 145 } |
| 106 print('#include "url/gurl.h"'); | 146 |
| 107 print('#include "testing/gtest/include/gtest/gtest.h"'); | |
| 108 print(); | |
| 109 | 147 |
| 110 /** | 148 /** |
| 111 * Convert the |includeFile| to paths appropriate for immediate | 149 * Convert the |includeFile| to paths appropriate for immediate |
| 112 * inclusion (path) and runtime inclusion (base). | 150 * inclusion (path) and runtime inclusion (base). |
| 113 * @param {string} includeFile The file to include. | 151 * @param {string} includeFile The file to include. |
| 114 * @return {{path: string, base: string}} Object describing the paths | 152 * @return {{path: string, base: string}} Object describing the paths |
| 115 * for |includeFile|. | 153 * for |includeFile|. |
| 116 */ | 154 */ |
| 117 function includeFileToPaths(includeFile) { | 155 function includeFileToPaths(includeFile) { |
| 118 return { | 156 return { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 }); | 251 }); |
| 214 | 252 |
| 215 return resultPaths; | 253 return resultPaths; |
| 216 } | 254 } |
| 217 | 255 |
| 218 /** | 256 /** |
| 219 * Output |code| verbatim. | 257 * Output |code| verbatim. |
| 220 * @param {string} code The code to output. | 258 * @param {string} code The code to output. |
| 221 */ | 259 */ |
| 222 function GEN(code) { | 260 function GEN(code) { |
| 261 maybeGenHeader(null); |
| 223 print(code); | 262 print(code); |
| 224 } | 263 } |
| 225 | 264 |
| 226 /** | 265 /** |
| 266 * Outputs |commentEncodedCode|, converting comment to enclosed C++ code. |
| 267 * @param {function} commentEncodedCode A function in the following format (note |
| 268 * the space in '/ *' and '* /' should be removed to form a comment delimiter): |
| 269 * function() {/ *! my_cpp_code.DoSomething(); * / |
| 270 * Code between / *! and * / will be extracted and written to stdout. |
| 271 */ |
| 272 function GEN_BLOCK(commentEncodedCode) { |
| 273 var code = commentEncodedCode.toString(). |
| 274 replace(/^[^\/]+\/\*!?/, ''). |
| 275 replace(/\*\/[^\/]+$/, ''). |
| 276 replace(/^\n|\n$/, ''). |
| 277 replace(/\s+$/, ''); |
| 278 GEN(code); |
| 279 } |
| 280 |
| 281 /** |
| 227 * Generate includes for the current |jsFile| by including them | 282 * Generate includes for the current |jsFile| by including them |
| 228 * immediately and at runtime. | 283 * immediately and at runtime. |
| 229 * @param {Array.<string>} includes Paths to JavaScript files to | 284 * @param {Array.<string>} includes Paths to JavaScript files to |
| 230 * include immediately and at runtime. | 285 * include immediately and at runtime. |
| 231 */ | 286 */ |
| 232 function GEN_INCLUDE(includes) { | 287 function GEN_INCLUDE(includes) { |
| 233 for (var i = 0; i < includes.length; i++) { | 288 for (var i = 0; i < includes.length; i++) { |
| 234 var includePaths = includeFileToPaths(includes[i]); | 289 var includePaths = includeFileToPaths(includes[i]); |
| 235 var js = read(includePaths.path); | 290 var js = read(includePaths.path); |
| 236 ('global', eval)(js); | 291 ('global', eval)(js); |
| 237 genIncludes.push(includePaths.base); | 292 genIncludes.push(includePaths.base); |
| 238 } | 293 } |
| 239 } | 294 } |
| 240 | 295 |
| 241 /** | 296 /** |
| 242 * Generate gtest-style TEST_F definitions for C++ with a body that | 297 * Generate gtest-style TEST_F definitions for C++ with a body that |
| 243 * will invoke the |testBody| for |testFixture|.|testFunction|. | 298 * will invoke the |testBody| for |testFixture|.|testFunction|. |
| 244 * @param {string} testFixture The name of this test's fixture. | 299 * @param {string} testFixture The name of this test's fixture. |
| 245 * @param {string} testFunction The name of this test's function. | 300 * @param {string} testFunction The name of this test's function. |
| 246 * @param {Function} testBody The function body to execute for this test. | 301 * @param {Function} testBody The function body to execute for this test. |
| 247 */ | 302 */ |
| 248 function TEST_F(testFixture, testFunction, testBody) { | 303 function TEST_F(testFixture, testFunction, testBody) { |
| 304 maybeGenHeader(testFixture); |
| 249 var browsePreload = this[testFixture].prototype.browsePreload; | 305 var browsePreload = this[testFixture].prototype.browsePreload; |
| 250 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload; | 306 var browsePrintPreload = this[testFixture].prototype.browsePrintPreload; |
| 251 var testGenPreamble = this[testFixture].prototype.testGenPreamble; | 307 var testGenPreamble = this[testFixture].prototype.testGenPreamble; |
| 252 var testGenPostamble = this[testFixture].prototype.testGenPostamble; | 308 var testGenPostamble = this[testFixture].prototype.testGenPostamble; |
| 253 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture; | 309 var typedefCppFixture = this[testFixture].prototype.typedefCppFixture; |
| 254 var isAsyncParam = testType === 'unit' ? '' : | 310 var isAsyncParam = testType === 'unit' ? '' : |
| 255 this[testFixture].prototype.isAsync + ', '; | 311 this[testFixture].prototype.isAsync + ', '; |
| 256 var testShouldFail = this[testFixture].prototype.testShouldFail; | 312 var testShouldFail = this[testFixture].prototype.testShouldFail; |
| 257 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE'; | 313 var testPredicate = testShouldFail ? 'ASSERT_FALSE' : 'ASSERT_TRUE'; |
| 258 var extraLibraries = genIncludes.concat( | 314 var extraLibraries = genIncludes.concat( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 '"' + testFunction + '"));'); | 347 '"' + testFunction + '"));'); |
| 292 if (testGenPostamble) | 348 if (testGenPostamble) |
| 293 testGenPostamble(testFixture, testFunction); | 349 testGenPostamble(testFixture, testFunction); |
| 294 print('}'); | 350 print('}'); |
| 295 print(); | 351 print(); |
| 296 } | 352 } |
| 297 | 353 |
| 298 // Now that generation functions are defined, load in |jsFile|. | 354 // Now that generation functions are defined, load in |jsFile|. |
| 299 var js = read(jsFile); | 355 var js = read(jsFile); |
| 300 eval(js); | 356 eval(js); |
| OLD | NEW |