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

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: address comments 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 delegate = require('test_delegate');
raymes 2014/07/24 08:17:38 'test_environment_specific_bindings' (or anything
Sam McNally 2014/07/24 08:44:37 Done.
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
31 function runTests(tests) {
32 chromeTest.tests = tests;
33 testCount = chromeTest.tests.length;
34 chromeTest.runNextTest();
35 }
36
39 function testDone() { 37 function testDone() {
40 // Use setTimeout here to allow previous test contexts to be 38 delegate.testDone(chromeTest.runNextTest);
41 // eligible for garbage collection.
42 setTimeout(chromeTest.runNextTest, 0);
43 } 39 }
44 40
45 function allTestsDone() { 41 function allTestsDone() {
46 if (testsFailed == 0) { 42 if (testsFailed == 0) {
47 chromeTest.notifyPass(); 43 chromeTest.notifyPass();
48 } else { 44 } else {
49 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' + 45 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' +
50 testCount + ' tests'); 46 testCount + ' tests');
51 } 47 }
52 } 48 }
53 49
54 var pendingCallbacks = 0; 50 var pendingCallbacks = 0;
55 51
56 apiFunctions.setHandleRequest('callbackAdded', function() { 52 apiFunctions.setHandleRequest('callbackAdded', function() {
57 pendingCallbacks++; 53 pendingCallbacks++;
58 54
59 var called = null; 55 var called = null;
60 return function() { 56 return function() {
61 if (called != null) { 57 if (called != null) {
62 var redundantPrefix = 'Error\n'; 58 var redundantPrefix = 'Error\n';
63 chrome.test.fail( 59 chromeTest.fail(
64 'Callback has already been run. ' + 60 'Callback has already been run. ' +
65 'First call:\n' + 61 'First call:\n' +
66 $String.slice(called, redundantPrefix.length) + '\n' + 62 $String.slice(called, redundantPrefix.length) + '\n' +
67 'Second call:\n' + 63 'Second call:\n' +
68 $String.slice(new Error().stack, redundantPrefix.length)); 64 $String.slice(new Error().stack, redundantPrefix.length));
69 } 65 }
70 called = new Error().stack; 66 called = new Error().stack;
71 67
72 pendingCallbacks--; 68 pendingCallbacks--;
73 if (pendingCallbacks == 0) { 69 if (pendingCallbacks == 0) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 // Interrupt the rest of the test. 114 // Interrupt the rest of the test.
119 throw failureException; 115 throw failureException;
120 }); 116 });
121 117
122 apiFunctions.setHandleRequest('succeed', function() { 118 apiFunctions.setHandleRequest('succeed', function() {
123 console.log("[SUCCESS] " + testName(currentTest)); 119 console.log("[SUCCESS] " + testName(currentTest));
124 chromeTest.log("( SUCCESS )"); 120 chromeTest.log("( SUCCESS )");
125 testDone(); 121 testDone();
126 }); 122 });
127 123
128 apiFunctions.setHandleRequest('runWithModuleSystem', function(callback) {
129 RunWithNativesEnabledModuleSystem(callback);
130 });
131
132 apiFunctions.setHandleRequest('assertTrue', function(test, message) { 124 apiFunctions.setHandleRequest('assertTrue', function(test, message) {
133 chromeTest.assertBool(test, true, message); 125 chromeTest.assertBool(test, true, message);
134 }); 126 });
135 127
136 apiFunctions.setHandleRequest('assertFalse', function(test, message) { 128 apiFunctions.setHandleRequest('assertFalse', function(test, message) {
137 chromeTest.assertBool(test, false, message); 129 chromeTest.assertBool(test, false, message);
138 }); 130 });
139 131
140 apiFunctions.setHandleRequest('assertBool', 132 apiFunctions.setHandleRequest('assertBool',
141 function(test, expected, message) { 133 function(test, expected, message) {
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 }); 308 });
317 309
318 apiFunctions.setHandleRequest('callbackPass', function(func) { 310 apiFunctions.setHandleRequest('callbackPass', function(func) {
319 return chromeTest.callback(func); 311 return chromeTest.callback(func);
320 }); 312 });
321 313
322 apiFunctions.setHandleRequest('callbackFail', function(expectedError, func) { 314 apiFunctions.setHandleRequest('callbackFail', function(expectedError, func) {
323 return chromeTest.callback(func, expectedError); 315 return chromeTest.callback(func, expectedError);
324 }); 316 });
325 317
326 apiFunctions.setHandleRequest('runTests', function(tests) {
327 chromeTest.tests = tests;
328 testCount = chromeTest.tests.length;
329 chromeTest.runNextTest();
330 });
331
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) {
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 delegate.registerHooks(api, runTests, testName);
358 }); 324 });
359 325
360 exports.binding = binding.generate(); 326 exports.binding = binding.generate();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698