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

Side by Side Diff: extensions/renderer/resources/test_custom_bindings.js

Issue 399363002: Add support for writing unit tests for Mojo-backed apps/extensions APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 5 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
OLDNEW
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 // test_custom_bindings.js 5 // test_custom_bindings.js
6 // mini-framework for ExtensionApiTest browser tests 6 // mini-framework for ExtensionApiTest browser tests
7 7
8 var binding = require('binding').Binding.create('test'); 8 var binding = require('binding').Binding.create('test');
9 9
10 var chrome = requireNative('chrome').GetChrome(); 10 var environmentSpecificBindings = require('test_environment_specific_bindings');
11 var GetExtensionAPIDefinitionsForTest =
12 requireNative('apiDefinitions').GetExtensionAPIDefinitionsForTest;
13 var GetAvailability = requireNative('v8_context').GetAvailability;
14 var GetAPIFeatures = requireNative('test_features').GetAPIFeatures;
15 var uncaughtExceptionHandler = require('uncaught_exception_handler'); 11 var uncaughtExceptionHandler = require('uncaught_exception_handler');
16 var userGestures = requireNative('user_gestures');
17
18 var RunWithNativesEnabledModuleSystem =
19 requireNative('v8_context').RunWithNativesEnabledModuleSystem;
20 12
21 binding.registerCustomHook(function(api) { 13 binding.registerCustomHook(function(api) {
22 var chromeTest = api.compiledApi; 14 var chromeTest = api.compiledApi;
23 var apiFunctions = api.apiFunctions; 15 var apiFunctions = api.apiFunctions;
24 16
25 chromeTest.tests = chromeTest.tests || []; 17 chromeTest.tests = chromeTest.tests || [];
26 18
27 var currentTest = null; 19 var currentTest = null;
28 var lastTest = null; 20 var lastTest = null;
29 var testsFailed = 0; 21 var testsFailed = 0;
30 var testCount = 1; 22 var testCount = 1;
31 var failureException = 'chrome.test.failure'; 23 var failureException = 'chrome.test.failure';
32 24
33 // Helper function to get around the fact that function names in javascript 25 // Helper function to get around the fact that function names in javascript
34 // are read-only, and you can't assign one to anonymous functions. 26 // are read-only, and you can't assign one to anonymous functions.
35 function testName(test) { 27 function testName(test) {
36 return test ? (test.name || test.generatedName) : "(no test)"; 28 return test ? (test.name || test.generatedName) : "(no test)";
37 } 29 }
38 30
39 function testDone() { 31 function testDone() {
40 // Use setTimeout here to allow previous test contexts to be 32 environmentSpecificBindings.testDone(chromeTest.runNextTest);
41 // eligible for garbage collection.
42 setTimeout(chromeTest.runNextTest, 0);
43 } 33 }
44 34
45 function allTestsDone() { 35 function allTestsDone() {
46 if (testsFailed == 0) { 36 if (testsFailed == 0) {
47 chromeTest.notifyPass(); 37 chromeTest.notifyPass();
48 } else { 38 } else {
49 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' + 39 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' +
50 testCount + ' tests'); 40 testCount + ' tests');
51 } 41 }
52 } 42 }
53 43
54 var pendingCallbacks = 0; 44 var pendingCallbacks = 0;
55 45
56 apiFunctions.setHandleRequest('callbackAdded', function() { 46 apiFunctions.setHandleRequest('callbackAdded', function() {
57 pendingCallbacks++; 47 pendingCallbacks++;
58 48
59 var called = null; 49 var called = null;
60 return function() { 50 return function() {
61 if (called != null) { 51 if (called != null) {
62 var redundantPrefix = 'Error\n'; 52 var redundantPrefix = 'Error\n';
63 chrome.test.fail( 53 chromeTest.fail(
64 'Callback has already been run. ' + 54 'Callback has already been run. ' +
65 'First call:\n' + 55 'First call:\n' +
66 $String.slice(called, redundantPrefix.length) + '\n' + 56 $String.slice(called, redundantPrefix.length) + '\n' +
67 'Second call:\n' + 57 'Second call:\n' +
68 $String.slice(new Error().stack, redundantPrefix.length)); 58 $String.slice(new Error().stack, redundantPrefix.length));
69 } 59 }
70 called = new Error().stack; 60 called = new Error().stack;
71 61
72 pendingCallbacks--; 62 pendingCallbacks--;
73 if (pendingCallbacks == 0) { 63 if (pendingCallbacks == 0) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 // Interrupt the rest of the test. 108 // Interrupt the rest of the test.
119 throw failureException; 109 throw failureException;
120 }); 110 });
121 111
122 apiFunctions.setHandleRequest('succeed', function() { 112 apiFunctions.setHandleRequest('succeed', function() {
123 console.log("[SUCCESS] " + testName(currentTest)); 113 console.log("[SUCCESS] " + testName(currentTest));
124 chromeTest.log("( SUCCESS )"); 114 chromeTest.log("( SUCCESS )");
125 testDone(); 115 testDone();
126 }); 116 });
127 117
128 apiFunctions.setHandleRequest('runWithModuleSystem', function(callback) {
129 RunWithNativesEnabledModuleSystem(callback);
130 });
131
132 apiFunctions.setHandleRequest('assertTrue', function(test, message) { 118 apiFunctions.setHandleRequest('assertTrue', function(test, message) {
133 chromeTest.assertBool(test, true, message); 119 chromeTest.assertBool(test, true, message);
134 }); 120 });
135 121
136 apiFunctions.setHandleRequest('assertFalse', function(test, message) { 122 apiFunctions.setHandleRequest('assertFalse', function(test, message) {
137 chromeTest.assertBool(test, false, message); 123 chromeTest.assertBool(test, false, message);
138 }); 124 });
139 125
140 apiFunctions.setHandleRequest('assertBool', 126 apiFunctions.setHandleRequest('assertBool',
141 function(test, expected, message) { 127 function(test, expected, message) {
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 apiFunctions.setHandleRequest('callbackFail', function(expectedError, func) { 308 apiFunctions.setHandleRequest('callbackFail', function(expectedError, func) {
323 return chromeTest.callback(func, expectedError); 309 return chromeTest.callback(func, expectedError);
324 }); 310 });
325 311
326 apiFunctions.setHandleRequest('runTests', function(tests) { 312 apiFunctions.setHandleRequest('runTests', function(tests) {
327 chromeTest.tests = tests; 313 chromeTest.tests = tests;
328 testCount = chromeTest.tests.length; 314 testCount = chromeTest.tests.length;
329 chromeTest.runNextTest(); 315 chromeTest.runNextTest();
330 }); 316 });
331 317
332 apiFunctions.setHandleRequest('getApiDefinitions', function() {
333 return GetExtensionAPIDefinitionsForTest();
334 });
335
336 apiFunctions.setHandleRequest('getApiFeatures', function() {
337 return GetAPIFeatures();
338 });
339
340 apiFunctions.setHandleRequest('isProcessingUserGesture', function() {
341 return userGestures.IsProcessingUserGesture();
342 });
343
344 apiFunctions.setHandleRequest('runWithUserGesture', function(callback) {
not at google - send to devlin 2014/07/25 00:17:57 I guess you need to pull these out because userGes
Sam McNally 2014/07/25 07:38:59 Done, except for getApiFeatures which requires a r
345 chromeTest.assertEq(typeof(callback), 'function');
346 return userGestures.RunWithUserGesture(callback);
347 });
348
349 apiFunctions.setHandleRequest('runWithoutUserGesture', function(callback) {
350 chromeTest.assertEq(typeof(callback), 'function');
351 return userGestures.RunWithoutUserGesture(callback);
352 });
353
354 apiFunctions.setHandleRequest('setExceptionHandler', function(callback) { 318 apiFunctions.setHandleRequest('setExceptionHandler', function(callback) {
355 chromeTest.assertEq(typeof(callback), 'function'); 319 chromeTest.assertEq(typeof(callback), 'function');
356 uncaughtExceptionHandler.setHandler(callback); 320 uncaughtExceptionHandler.setHandler(callback);
357 }); 321 });
322
323 environmentSpecificBindings.registerHooks(api);
358 }); 324 });
359 325
360 exports.binding = binding.generate(); 326 exports.binding = binding.generate();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698