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 // 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 = | 11 var GetExtensionAPIDefinitionsForTest = |
12 requireNative('apiDefinitions').GetExtensionAPIDefinitionsForTest; | 12 requireNative('apiDefinitions').GetExtensionAPIDefinitionsForTest; |
13 var GetAvailability = requireNative('v8_context').GetAvailability; | |
14 var GetAPIFeatures = requireNative('test_features').GetAPIFeatures; | 13 var GetAPIFeatures = requireNative('test_features').GetAPIFeatures; |
15 var uncaughtExceptionHandler = require('uncaught_exception_handler'); | 14 var uncaughtExceptionHandler = require('uncaught_exception_handler'); |
16 var userGestures = requireNative('user_gestures'); | 15 var userGestures = requireNative('user_gestures'); |
17 | 16 |
18 var RunWithNativesEnabledModuleSystem = | 17 var RunWithNativesEnabledModuleSystem = |
19 requireNative('v8_context').RunWithNativesEnabledModuleSystem; | 18 requireNative('v8_context').RunWithNativesEnabledModuleSystem; |
20 | 19 |
21 binding.registerCustomHook(function(api) { | 20 binding.registerCustomHook(function(api) { |
22 var chromeTest = api.compiledApi; | 21 var chromeTest = api.compiledApi; |
23 var apiFunctions = api.apiFunctions; | 22 var apiFunctions = api.apiFunctions; |
24 | 23 |
25 chromeTest.tests = chromeTest.tests || []; | 24 chromeTest.tests = chromeTest.tests || []; |
26 | 25 |
27 var currentTest = null; | 26 var currentTest = null; |
28 var lastTest = null; | 27 var lastTest = null; |
29 var testsFailed = 0; | 28 var testsFailed = 0; |
30 var testCount = 1; | 29 var testCount = 1; |
31 var failureException = 'chrome.test.failure'; | 30 var failureException = 'chrome.test.failure'; |
32 | 31 |
33 // Helper function to get around the fact that function names in javascript | 32 // 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. | 33 // are read-only, and you can't assign one to anonymous functions. |
35 function testName(test) { | 34 function testName(test) { |
36 return test ? (test.name || test.generatedName) : "(no test)"; | 35 return test ? (test.name || test.generatedName) : "(no test)"; |
37 } | 36 } |
38 | 37 |
39 function testDone() { | 38 function testDone() { |
40 // Use setTimeout here to allow previous test contexts to be | 39 environmentSpecificBindings.testDone(chromeTest.runNextTest); |
41 // eligible for garbage collection. | |
42 setTimeout(chromeTest.runNextTest, 0); | |
43 } | 40 } |
44 | 41 |
45 function allTestsDone() { | 42 function allTestsDone() { |
46 if (testsFailed == 0) { | 43 if (testsFailed == 0) { |
47 chromeTest.notifyPass(); | 44 chromeTest.notifyPass(); |
48 } else { | 45 } else { |
49 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' + | 46 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' + |
50 testCount + ' tests'); | 47 testCount + ' tests'); |
51 } | 48 } |
52 } | 49 } |
53 | 50 |
54 var pendingCallbacks = 0; | 51 var pendingCallbacks = 0; |
55 | 52 |
56 apiFunctions.setHandleRequest('callbackAdded', function() { | 53 apiFunctions.setHandleRequest('callbackAdded', function() { |
57 pendingCallbacks++; | 54 pendingCallbacks++; |
58 | 55 |
59 var called = null; | 56 var called = null; |
60 return function() { | 57 return function() { |
61 if (called != null) { | 58 if (called != null) { |
62 var redundantPrefix = 'Error\n'; | 59 var redundantPrefix = 'Error\n'; |
63 chrome.test.fail( | 60 chromeTest.fail( |
64 'Callback has already been run. ' + | 61 'Callback has already been run. ' + |
65 'First call:\n' + | 62 'First call:\n' + |
66 $String.slice(called, redundantPrefix.length) + '\n' + | 63 $String.slice(called, redundantPrefix.length) + '\n' + |
67 'Second call:\n' + | 64 'Second call:\n' + |
68 $String.slice(new Error().stack, redundantPrefix.length)); | 65 $String.slice(new Error().stack, redundantPrefix.length)); |
69 } | 66 } |
70 called = new Error().stack; | 67 called = new Error().stack; |
71 | 68 |
72 pendingCallbacks--; | 69 pendingCallbacks--; |
73 if (pendingCallbacks == 0) { | 70 if (pendingCallbacks == 0) { |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 | 345 |
349 apiFunctions.setHandleRequest('runWithoutUserGesture', function(callback) { | 346 apiFunctions.setHandleRequest('runWithoutUserGesture', function(callback) { |
350 chromeTest.assertEq(typeof(callback), 'function'); | 347 chromeTest.assertEq(typeof(callback), 'function'); |
351 return userGestures.RunWithoutUserGesture(callback); | 348 return userGestures.RunWithoutUserGesture(callback); |
352 }); | 349 }); |
353 | 350 |
354 apiFunctions.setHandleRequest('setExceptionHandler', function(callback) { | 351 apiFunctions.setHandleRequest('setExceptionHandler', function(callback) { |
355 chromeTest.assertEq(typeof(callback), 'function'); | 352 chromeTest.assertEq(typeof(callback), 'function'); |
356 uncaughtExceptionHandler.setHandler(callback); | 353 uncaughtExceptionHandler.setHandler(callback); |
357 }); | 354 }); |
| 355 |
| 356 environmentSpecificBindings.registerHooks(api); |
358 }); | 357 }); |
359 | 358 |
360 exports.binding = binding.generate(); | 359 exports.binding = binding.generate(); |
OLD | NEW |