OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 // Common testing utilities. | 5 // Common testing utilities. |
6 | 6 |
7 /** | 7 /** |
8 * Shortcut for document.getElementById. | 8 * Shortcut for document.getElementById. |
9 * @param {string} id of the element. | 9 * @param {string} id of the element. |
10 * @return {HTMLElement} with the id. | 10 * @return {HTMLElement} with the id. |
(...skipping 14 matching lines...) Expand all Loading... |
25 * this.appendDoc(function() {/*! | 25 * this.appendDoc(function() {/*! |
26 * <p>Html goes here</p> | 26 * <p>Html goes here</p> |
27 * * /}); | 27 * * /}); |
28 * | 28 * |
29 * @param {Function} commentEncodedHtml The html , embedded as a | 29 * @param {Function} commentEncodedHtml The html , embedded as a |
30 * comment inside an anonymous function - see example, above. | 30 * comment inside an anonymous function - see example, above. |
31 * @param {!Array=} opt_args Optional arguments to be substituted in the form | 31 * @param {!Array=} opt_args Optional arguments to be substituted in the form |
32 * $0, ... within the code block. | 32 * $0, ... within the code block. |
33 * @return {string} The html text. | 33 * @return {string} The html text. |
34 */ | 34 */ |
35 TestUtils.extractHtmlFromCommentEncodedString = | 35 TestUtils.extractHtmlFromCommentEncodedString = function( |
36 function(commentEncodedHtml, opt_args) { | 36 commentEncodedHtml, opt_args) { |
37 var stringified = commentEncodedHtml.toString(); | 37 var stringified = commentEncodedHtml.toString(); |
38 if (opt_args) { | 38 if (opt_args) { |
39 for (var i = 0; i < opt_args.length; i++) | 39 for (var i = 0; i < opt_args.length; i++) |
40 stringified = stringified.replace('$' + i, opt_args[i]); | 40 stringified = stringified.replace('$' + i, opt_args[i]); |
41 } | 41 } |
42 return stringified.replace(/^[^\/]+\/\*!?/, '').replace(/\*\/[^\/]+$/, ''); | 42 return stringified.replace(/^[^\/]+\/\*!?/, '').replace(/\*\/[^\/]+$/, ''); |
43 }; | 43 }; |
44 | 44 |
45 | 45 |
46 /** | 46 /** |
47 * Creates a data url for a document. | 47 * Creates a data url for a document. |
48 * @param {function() : void} doc Snippet wrapped inside of a function. | 48 * @param {function() : void} doc Snippet wrapped inside of a function. |
49 * @return {string} | 49 * @return {string} |
50 */ | 50 */ |
51 TestUtils.createUrlForDoc = function(doc) { | 51 TestUtils.createUrlForDoc = function(doc) { |
52 var docString = TestUtils.extractHtmlFromCommentEncodedString(doc); | 52 var docString = TestUtils.extractHtmlFromCommentEncodedString(doc); |
53 return TestUtils.collapseWhitespace('data:text/html,<!doctype html>' + | 53 return TestUtils.collapseWhitespace( |
54 docString.replace(/[\n\r]/g, '') | 54 'data:text/html,<!doctype html>' + |
55 .trim()); | 55 docString.replace(/[\n\r]/g, '').trim()); |
56 }; | 56 }; |
57 | 57 |
58 /** | 58 /** |
59 * Collapses inner whitespace. | 59 * Collapses inner whitespace. |
60 * @param {string} str | 60 * @param {string} str |
61 * @return {string} | 61 * @return {string} |
62 */ | 62 */ |
63 TestUtils.collapseWhitespace = function(str) { | 63 TestUtils.collapseWhitespace = function(str) { |
64 return str.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, ''); | 64 return str.replace(/\s+/g, ' ').replace(/^\s+|\s+$/g, ''); |
65 }; | 65 }; |
66 | 66 |
67 /** | 67 /** |
68 * Similar to |TEST_F|. Generates a test for the given |testFixture|, | 68 * Similar to |TEST_F|. Generates a test for the given |testFixture|, |
69 * |testName|, and |testFunction|. | 69 * |testName|, and |testFunction|. |
70 * Used this variant when an |isAsync| fixture wants to temporarily mix in an | 70 * Used this variant when an |isAsync| fixture wants to temporarily mix in an |
71 * sync test. | 71 * sync test. |
72 * @param {string} testFixture Fixture name. | 72 * @param {string} testFixture Fixture name. |
73 * @param {string} testName Test name. | 73 * @param {string} testName Test name. |
74 * @param {function} testFunction The test impl. | 74 * @param {function} testFunction The test impl. |
75 */ | 75 */ |
76 function SYNC_TEST_F(testFixture, testName, testFunction) { | 76 function SYNC_TEST_F(testFixture, testName, testFunction) { |
77 TEST_F(testFixture, testName, function() { | 77 TEST_F(testFixture, testName, function() { |
78 this.newCallback(testFunction)(); | 78 this.newCallback(testFunction)(); |
79 }); | 79 }); |
80 } | 80 } |
OLD | NEW |