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 Library providing basic test framework functionality. | 6 * @fileoverview Library providing basic test framework functionality. |
7 */ | 7 */ |
8 | 8 |
9 /** | 9 /** |
10 * Namespace for |Test|. | 10 * Namespace for |Test|. |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 isAsync: false, | 128 isAsync: false, |
129 | 129 |
130 /** | 130 /** |
131 * True when the test is expected to fail for testing the test framework. | 131 * True when the test is expected to fail for testing the test framework. |
132 * @type {boolean} | 132 * @type {boolean} |
133 */ | 133 */ |
134 testShouldFail: false, | 134 testShouldFail: false, |
135 | 135 |
136 /** | 136 /** |
137 * Extra libraries to add before loading this test file. | 137 * Extra libraries to add before loading this test file. |
138 * @type {Array.<string>} | 138 * @type {Array<string>} |
139 */ | 139 */ |
140 extraLibraries: [], | 140 extraLibraries: [], |
141 | 141 |
142 /** | 142 /** |
143 * Extra libraries to add before loading this test file. | 143 * Extra libraries to add before loading this test file. |
144 * This list is in the form of Closure library style object | 144 * This list is in the form of Closure library style object |
145 * names. To support this, a closure deps.js file must | 145 * names. To support this, a closure deps.js file must |
146 * be specified when generating the test C++ source. | 146 * be specified when generating the test C++ source. |
147 * The specified libraries will be included with their transitive | 147 * The specified libraries will be included with their transitive |
148 * dependencies according to the deps file. | 148 * dependencies according to the deps file. |
149 * @type {Array.<string>} | 149 * @type {Array<string>} |
150 */ | 150 */ |
151 closureModuleDeps: [], | 151 closureModuleDeps: [], |
152 | 152 |
153 /** | 153 /** |
154 * Whether to run the accessibility checks. | 154 * Whether to run the accessibility checks. |
155 * @type {boolean} | 155 * @type {boolean} |
156 */ | 156 */ |
157 runAccessibilityChecks: true, | 157 runAccessibilityChecks: true, |
158 | 158 |
159 /** | 159 /** |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 * failures. If true, any accessibility issues will cause the test to fail. | 203 * failures. If true, any accessibility issues will cause the test to fail. |
204 * If false, accessibility issues will cause a console.warn. | 204 * If false, accessibility issues will cause a console.warn. |
205 * Off by default to begin with; as we add the ability to suppress false | 205 * Off by default to begin with; as we add the ability to suppress false |
206 * positives, we will transition this to true. | 206 * positives, we will transition this to true. |
207 * @type {boolean} | 207 * @type {boolean} |
208 */ | 208 */ |
209 accessibilityIssuesAreErrors: false, | 209 accessibilityIssuesAreErrors: false, |
210 | 210 |
211 /** | 211 /** |
212 * Holds any accessibility results found during the accessibility audit. | 212 * Holds any accessibility results found during the accessibility audit. |
213 * @type {Array.<Object>} | 213 * @type {Array<Object>} |
214 */ | 214 */ |
215 a11yResults_: [], | 215 a11yResults_: [], |
216 | 216 |
217 /** | 217 /** |
218 * Gets the list of accessibility errors found during the accessibility | 218 * Gets the list of accessibility errors found during the accessibility |
219 * audit. Only for use in testing. | 219 * audit. Only for use in testing. |
220 * @return {Array.<Object>} | 220 * @return {Array<Object>} |
221 */ | 221 */ |
222 getAccessibilityResults: function() { | 222 getAccessibilityResults: function() { |
223 return this.a11yResults_; | 223 return this.a11yResults_; |
224 }, | 224 }, |
225 | 225 |
226 /** | 226 /** |
227 * Run accessibility checks after this test completes. | 227 * Run accessibility checks after this test completes. |
228 */ | 228 */ |
229 enableAccessibilityChecks: function() { | 229 enableAccessibilityChecks: function() { |
230 this.runAccessibilityChecks = true; | 230 this.runAccessibilityChecks = true; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 }); | 276 }); |
277 | 277 |
278 this.mockApis = makeMockFunctions(apiMockNames); | 278 this.mockApis = makeMockFunctions(apiMockNames); |
279 registerMockApis(this.mockApis); | 279 registerMockApis(this.mockApis); |
280 return this.mockApis; | 280 return this.mockApis; |
281 }, | 281 }, |
282 | 282 |
283 /** | 283 /** |
284 * Create a container of mocked standalone functions to handle | 284 * Create a container of mocked standalone functions to handle |
285 * |functionNames|, assign it to |this.mockLocalFunctions| and return it. | 285 * |functionNames|, assign it to |this.mockLocalFunctions| and return it. |
286 * @param {!Array.<string>} functionNames | 286 * @param {!Array<string>} functionNames |
287 * @return {Mock} Mock handler class. | 287 * @return {Mock} Mock handler class. |
288 * @see makeMockFunctions | 288 * @see makeMockFunctions |
289 */ | 289 */ |
290 makeMockLocalFunctions: function(functionNames) { | 290 makeMockLocalFunctions: function(functionNames) { |
291 this.mockLocalFunctions = makeMockFunctions(functionNames); | 291 this.mockLocalFunctions = makeMockFunctions(functionNames); |
292 return this.mockLocalFunctions; | 292 return this.mockLocalFunctions; |
293 }, | 293 }, |
294 | 294 |
295 /** | 295 /** |
296 * Override this method to perform initialization during preload (such as | 296 * Override this method to perform initialization during preload (such as |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 } | 642 } |
643 | 643 |
644 /** | 644 /** |
645 * Empty function for use in making mocks. | 645 * Empty function for use in making mocks. |
646 * @const | 646 * @const |
647 */ | 647 */ |
648 function emptyFunction() {} | 648 function emptyFunction() {} |
649 | 649 |
650 /** | 650 /** |
651 * Make a mock from the supplied |methodNames| array. | 651 * Make a mock from the supplied |methodNames| array. |
652 * @param {Array.<string>} methodNames Array of names of methods to mock. | 652 * @param {Array<string>} methodNames Array of names of methods to mock. |
653 * @return {Function} Constructor with prototype filled in with methods | 653 * @return {Function} Constructor with prototype filled in with methods |
654 * matching |methodNames|. | 654 * matching |methodNames|. |
655 */ | 655 */ |
656 function makeMockClass(methodNames) { | 656 function makeMockClass(methodNames) { |
657 function MockConstructor() {} | 657 function MockConstructor() {} |
658 for(var i = 0; i < methodNames.length; i++) | 658 for(var i = 0; i < methodNames.length; i++) |
659 MockConstructor.prototype[methodNames[i]] = emptyFunction; | 659 MockConstructor.prototype[methodNames[i]] = emptyFunction; |
660 return MockConstructor; | 660 return MockConstructor; |
661 } | 661 } |
662 | 662 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 * interesting caller is one more level up the stack. | 734 * interesting caller is one more level up the stack. |
735 */ | 735 */ |
736 function CallHelper() { | 736 function CallHelper() { |
737 this.__proto__ = CallHelper.prototype; | 737 this.__proto__ = CallHelper.prototype; |
738 } | 738 } |
739 | 739 |
740 CallHelper.prototype = { | 740 CallHelper.prototype = { |
741 /** | 741 /** |
742 * Holds the mapping of (callerCallerString, callerName) -> count of times | 742 * Holds the mapping of (callerCallerString, callerName) -> count of times |
743 * called. | 743 * called. |
744 * @type {Object.<string, Object.<string, number>>} | 744 * @type {Object<string, Object<string, number>>} |
745 */ | 745 */ |
746 counts_: {}, | 746 counts_: {}, |
747 | 747 |
748 /** | 748 /** |
749 * This information about the caller is needed from most of the following | 749 * This information about the caller is needed from most of the following |
750 * routines. | 750 * routines. |
751 * @param {Function} caller the caller of the assert* routine. | 751 * @param {Function} caller the caller of the assert* routine. |
752 * @return {{callerName: string, callercallerString: string}} stackInfo | 752 * @return {{callerName: string, callercallerString: string}} stackInfo |
753 * @private | 753 * @private |
754 */ | 754 */ |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 | 824 |
825 /** | 825 /** |
826 * true when testDone has been called. | 826 * true when testDone has been called. |
827 * @type {boolean} | 827 * @type {boolean} |
828 */ | 828 */ |
829 var testIsDone = false; | 829 var testIsDone = false; |
830 | 830 |
831 /** | 831 /** |
832 * Holds the errors, if any, caught by expects so that the test case can | 832 * Holds the errors, if any, caught by expects so that the test case can |
833 * fail. Cleared when results are reported from runTest() or testDone(). | 833 * fail. Cleared when results are reported from runTest() or testDone(). |
834 * @type {Array.<Error>} | 834 * @type {Array<Error>} |
835 */ | 835 */ |
836 var errors = []; | 836 var errors = []; |
837 | 837 |
838 /** | 838 /** |
839 * URL to dummy WebUI page for testing framework. | 839 * URL to dummy WebUI page for testing framework. |
840 * @type {string} | 840 * @type {string} |
841 */ | 841 */ |
842 var DUMMY_URL = 'chrome://DummyURL'; | 842 var DUMMY_URL = 'chrome://DummyURL'; |
843 | 843 |
844 /** | 844 /** |
845 * Resets test state by clearing |errors| and |testIsDone| flags. | 845 * Resets test state by clearing |errors| and |testIsDone| flags. |
846 */ | 846 */ |
847 function resetTestState() { | 847 function resetTestState() { |
848 errors.splice(0, errors.length); | 848 errors.splice(0, errors.length); |
849 testIsDone = false; | 849 testIsDone = false; |
850 } | 850 } |
851 | 851 |
852 /** | 852 /** |
853 * Notifies the running browser test of the test results. Clears |errors|. | 853 * Notifies the running browser test of the test results. Clears |errors|. |
854 * @param {Array.<boolean, string>=} result When passed, this is used for the | 854 * @param {Array<boolean, string>=} result When passed, this is used for the |
855 * testResult message. | 855 * testResult message. |
856 */ | 856 */ |
857 function testDone(result) { | 857 function testDone(result) { |
858 if (!testIsDone) { | 858 if (!testIsDone) { |
859 testIsDone = true; | 859 testIsDone = true; |
860 if (currentTestCase) { | 860 if (currentTestCase) { |
861 var ok = true; | 861 var ok = true; |
862 ok = createExpect(currentTestCase.runAccessibilityAudit.bind( | 862 ok = createExpect(currentTestCase.runAccessibilityAudit.bind( |
863 currentTestCase)).call(null) && ok; | 863 currentTestCase)).call(null) && ok; |
864 ok = createExpect(currentTestCase.tearDown.bind( | 864 ok = createExpect(currentTestCase.tearDown.bind( |
(...skipping 16 matching lines...) Expand all Loading... |
881 } | 881 } |
882 errors.splice(0, errors.length); | 882 errors.splice(0, errors.length); |
883 } else { | 883 } else { |
884 console.warn('testIsDone already'); | 884 console.warn('testIsDone already'); |
885 } | 885 } |
886 } | 886 } |
887 | 887 |
888 /** | 888 /** |
889 * Converts each Error in |errors| to a suitable message, adding them to | 889 * Converts each Error in |errors| to a suitable message, adding them to |
890 * |message|, and returns the message string. | 890 * |message|, and returns the message string. |
891 * @param {Array.<Error>} errors Array of errors to add to |message|. | 891 * @param {Array<Error>} errors Array of errors to add to |message|. |
892 * @param {string?} message When supplied, error messages are appended to it. | 892 * @param {string?} message When supplied, error messages are appended to it. |
893 * @return {string} |message| + messages of all |errors|. | 893 * @return {string} |message| + messages of all |errors|. |
894 */ | 894 */ |
895 function errorsToMessage(errors, message) { | 895 function errorsToMessage(errors, message) { |
896 for (var i = 0; i < errors.length; ++i) { | 896 for (var i = 0; i < errors.length; ++i) { |
897 var errorMessage = errors[i].stack || errors[i].message; | 897 var errorMessage = errors[i].stack || errors[i].message; |
898 if (message) | 898 if (message) |
899 message += '\n'; | 899 message += '\n'; |
900 | 900 |
901 message += 'Failed: ' + currentTestFunction + '(' + | 901 message += 'Failed: ' + currentTestFunction + '(' + |
902 currentTestArguments.map(JSON.stringify) + | 902 currentTestArguments.map(JSON.stringify) + |
903 ')\n' + errorMessage; | 903 ')\n' + errorMessage; |
904 } | 904 } |
905 return message; | 905 return message; |
906 } | 906 } |
907 | 907 |
908 /** | 908 /** |
909 * Returns [success, message] & clears |errors|. | 909 * Returns [success, message] & clears |errors|. |
910 * @param {boolean} errorsOk When true, errors are ok. | 910 * @param {boolean} errorsOk When true, errors are ok. |
911 * @return {Array.<boolean, string>} | 911 * @return {Array<boolean, string>} |
912 */ | 912 */ |
913 function testResult(errorsOk) { | 913 function testResult(errorsOk) { |
914 var result = [true, '']; | 914 var result = [true, '']; |
915 if (errors.length) | 915 if (errors.length) |
916 result = [!!errorsOk, errorsToMessage(errors)]; | 916 result = [!!errorsOk, errorsToMessage(errors)]; |
917 | 917 |
918 return result; | 918 return result; |
919 } | 919 } |
920 | 920 |
921 // Asserts. | 921 // Asserts. |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1074 // (warnings ok) | 1074 // (warnings ok) |
1075 // TODO(aboxhall): some kind of info logging for warnings only?? | 1075 // TODO(aboxhall): some kind of info logging for warnings only?? |
1076 return (a11yResults.length == 0); | 1076 return (a11yResults.length == 0); |
1077 } | 1077 } |
1078 | 1078 |
1079 /** | 1079 /** |
1080 * Concatenates the accessibility error messages for each result in | 1080 * Concatenates the accessibility error messages for each result in |
1081 * |a11yResults| and | 1081 * |a11yResults| and |
1082 * |a11yWarnings| in to an accessibility report, appends it to the given | 1082 * |a11yWarnings| in to an accessibility report, appends it to the given |
1083 * |message| and returns the resulting message string. | 1083 * |message| and returns the resulting message string. |
1084 * @param {Array.<string>} a11yResults The list of accessibility results | 1084 * @param {Array<string>} a11yResults The list of accessibility results |
1085 * @return {string} |message| + accessibility report. | 1085 * @return {string} |message| + accessibility report. |
1086 */ | 1086 */ |
1087 function accessibilityAuditReport(a11yResults, message) { | 1087 function accessibilityAuditReport(a11yResults, message) { |
1088 message = message ? message + '\n\n' : '\n'; | 1088 message = message ? message + '\n\n' : '\n'; |
1089 message += 'Accessibility issues found on ' + window.location.href + '\n'; | 1089 message += 'Accessibility issues found on ' + window.location.href + '\n'; |
1090 message += axs.Audit.createReport(a11yResults); | 1090 message += axs.Audit.createReport(a11yResults); |
1091 return message; | 1091 return message; |
1092 } | 1092 } |
1093 | 1093 |
1094 /** | 1094 /** |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1175 * This is the guts of WebUIBrowserTest. It runs the test surrounded by an | 1175 * This is the guts of WebUIBrowserTest. It runs the test surrounded by an |
1176 * expect to catch Errors. If |errors| is non-empty, it reports a failure and | 1176 * expect to catch Errors. If |errors| is non-empty, it reports a failure and |
1177 * a message by joining |errors|. Consumers can use this to use assert/expect | 1177 * a message by joining |errors|. Consumers can use this to use assert/expect |
1178 * functions asynchronously, but are then responsible for reporting errors to | 1178 * functions asynchronously, but are then responsible for reporting errors to |
1179 * the browser themselves through testDone(). | 1179 * the browser themselves through testDone(). |
1180 * @param {string} testFunction The function name to report on failure. | 1180 * @param {string} testFunction The function name to report on failure. |
1181 * @param {Function} testBody The function to call. | 1181 * @param {Function} testBody The function to call. |
1182 * @param {Array} testArguments The arguments to call |testBody| with. | 1182 * @param {Array} testArguments The arguments to call |testBody| with. |
1183 * @param {boolean} onlyAssertFails When true, only assertions cause failing | 1183 * @param {boolean} onlyAssertFails When true, only assertions cause failing |
1184 * testResult. | 1184 * testResult. |
1185 * @return {Array.<boolean, string>} [test-succeeded, message-if-failed] | 1185 * @return {Array<boolean, string>} [test-succeeded, message-if-failed] |
1186 * @see createExpect | 1186 * @see createExpect |
1187 * @see testResult | 1187 * @see testResult |
1188 */ | 1188 */ |
1189 function runTestFunction(testFunction, testBody, testArguments, | 1189 function runTestFunction(testFunction, testBody, testArguments, |
1190 onlyAssertFails) { | 1190 onlyAssertFails) { |
1191 currentTestFunction = testFunction; | 1191 currentTestFunction = testFunction; |
1192 currentTestArguments = testArguments; | 1192 currentTestArguments = testArguments; |
1193 var ok = createExpect(testBody).apply(null, testArguments); | 1193 var ok = createExpect(testBody).apply(null, testArguments); |
1194 return testResult(onlyAssertFails && ok); | 1194 return testResult(onlyAssertFails && ok); |
1195 } | 1195 } |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1583 * Always call testDone(). | 1583 * Always call testDone(). |
1584 */ | 1584 */ |
1585 ALWAYS: 3, | 1585 ALWAYS: 3, |
1586 }; | 1586 }; |
1587 | 1587 |
1588 /** | 1588 /** |
1589 * Runs all |actions|. | 1589 * Runs all |actions|. |
1590 * @param {boolean} isAsync When true, call testDone() on Errors. | 1590 * @param {boolean} isAsync When true, call testDone() on Errors. |
1591 * @param {WhenTestDone} whenTestDone Call testDone() at the appropriate | 1591 * @param {WhenTestDone} whenTestDone Call testDone() at the appropriate |
1592 * time. | 1592 * time. |
1593 * @param {Array.<Object>} actions Actions to run. | 1593 * @param {Array<Object>} actions Actions to run. |
1594 * @constructor | 1594 * @constructor |
1595 */ | 1595 */ |
1596 function RunAllAction(isAsync, whenTestDone, actions) { | 1596 function RunAllAction(isAsync, whenTestDone, actions) { |
1597 this.isAsync_ = isAsync; | 1597 this.isAsync_ = isAsync; |
1598 this.whenTestDone_ = whenTestDone; | 1598 this.whenTestDone_ = whenTestDone; |
1599 this.actions_ = actions; | 1599 this.actions_ = actions; |
1600 } | 1600 } |
1601 | 1601 |
1602 RunAllAction.prototype = { | 1602 RunAllAction.prototype = { |
1603 /** | 1603 /** |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1829 exports.TEST = TEST; | 1829 exports.TEST = TEST; |
1830 exports.TEST_F = TEST_F; | 1830 exports.TEST_F = TEST_F; |
1831 exports.RUNTIME_TEST_F = TEST_F; | 1831 exports.RUNTIME_TEST_F = TEST_F; |
1832 exports.GEN = GEN; | 1832 exports.GEN = GEN; |
1833 exports.GEN_INCLUDE = GEN_INCLUDE; | 1833 exports.GEN_INCLUDE = GEN_INCLUDE; |
1834 exports.WhenTestDone = WhenTestDone; | 1834 exports.WhenTestDone = WhenTestDone; |
1835 | 1835 |
1836 // Import the Mock4JS helpers. | 1836 // Import the Mock4JS helpers. |
1837 Mock4JS.addMockSupport(exports); | 1837 Mock4JS.addMockSupport(exports); |
1838 })(this); | 1838 })(this); |
OLD | NEW |