| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // test_custom_bindings.js | |
| 6 // mini-framework for ExtensionApiTest browser tests | |
| 7 | |
| 8 var binding = require('binding').Binding.create('test'); | |
| 9 | |
| 10 var chrome = requireNative('chrome').GetChrome(); | |
| 11 var GetExtensionAPIDefinition = | |
| 12 requireNative('apiDefinitions').GetExtensionAPIDefinition; | |
| 13 | |
| 14 binding.registerCustomHook(function(api) { | |
| 15 var chromeTest = api.compiledApi; | |
| 16 var apiFunctions = api.apiFunctions; | |
| 17 | |
| 18 chromeTest.tests = chromeTest.tests || []; | |
| 19 | |
| 20 var currentTest = null; | |
| 21 var lastTest = null; | |
| 22 var testsFailed = 0; | |
| 23 var testCount = 1; | |
| 24 var failureException = 'chrome.test.failure'; | |
| 25 | |
| 26 // Helper function to get around the fact that function names in javascript | |
| 27 // are read-only, and you can't assign one to anonymous functions. | |
| 28 function testName(test) { | |
| 29 return test ? (test.name || test.generatedName) : "(no test)"; | |
| 30 } | |
| 31 | |
| 32 function testDone() { | |
| 33 // Use setTimeout here to allow previous test contexts to be | |
| 34 // eligible for garbage collection. | |
| 35 setTimeout(chromeTest.runNextTest, 0); | |
| 36 } | |
| 37 | |
| 38 function allTestsDone() { | |
| 39 if (testsFailed == 0) { | |
| 40 chromeTest.notifyPass(); | |
| 41 } else { | |
| 42 chromeTest.notifyFail('Failed ' + testsFailed + ' of ' + | |
| 43 testCount + ' tests'); | |
| 44 } | |
| 45 | |
| 46 // Try to get the script to stop running immediately. | |
| 47 // This isn't an error, just an attempt at saying "done". | |
| 48 throw "completed"; | |
| 49 } | |
| 50 | |
| 51 var pendingCallbacks = 0; | |
| 52 | |
| 53 apiFunctions.setHandleRequest('callbackAdded', function() { | |
| 54 pendingCallbacks++; | |
| 55 | |
| 56 var called = false; | |
| 57 return function() { | |
| 58 chromeTest.assertFalse(called, 'callback has already been run'); | |
| 59 called = true; | |
| 60 | |
| 61 pendingCallbacks--; | |
| 62 if (pendingCallbacks == 0) { | |
| 63 chromeTest.succeed(); | |
| 64 } | |
| 65 }; | |
| 66 }); | |
| 67 | |
| 68 apiFunctions.setHandleRequest('runNextTest', function() { | |
| 69 // There may have been callbacks which were interrupted by failure | |
| 70 // exceptions. | |
| 71 pendingCallbacks = 0; | |
| 72 | |
| 73 lastTest = currentTest; | |
| 74 currentTest = chromeTest.tests.shift(); | |
| 75 | |
| 76 if (!currentTest) { | |
| 77 allTestsDone(); | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 try { | |
| 82 chromeTest.log("( RUN ) " + testName(currentTest)); | |
| 83 currentTest.call(); | |
| 84 } catch (e) { | |
| 85 if (e !== failureException) | |
| 86 chromeTest.fail('uncaught exception: ' + e); | |
| 87 } | |
| 88 }); | |
| 89 | |
| 90 apiFunctions.setHandleRequest('fail', function(message) { | |
| 91 chromeTest.log("( FAILED ) " + testName(currentTest)); | |
| 92 | |
| 93 var stack = {}; | |
| 94 Error.captureStackTrace(stack, chromeTest.fail); | |
| 95 | |
| 96 if (!message) | |
| 97 message = "FAIL (no message)"; | |
| 98 | |
| 99 message += "\n" + stack.stack; | |
| 100 console.log("[FAIL] " + testName(currentTest) + ": " + message); | |
| 101 testsFailed++; | |
| 102 testDone(); | |
| 103 | |
| 104 // Interrupt the rest of the test. | |
| 105 throw failureException; | |
| 106 }); | |
| 107 | |
| 108 apiFunctions.setHandleRequest('succeed', function() { | |
| 109 console.log("[SUCCESS] " + testName(currentTest)); | |
| 110 chromeTest.log("( SUCCESS )"); | |
| 111 testDone(); | |
| 112 }); | |
| 113 | |
| 114 apiFunctions.setHandleRequest('assertTrue', function(test, message) { | |
| 115 chromeTest.assertBool(test, true, message); | |
| 116 }); | |
| 117 | |
| 118 apiFunctions.setHandleRequest('assertFalse', function(test, message) { | |
| 119 chromeTest.assertBool(test, false, message); | |
| 120 }); | |
| 121 | |
| 122 apiFunctions.setHandleRequest('assertBool', | |
| 123 function(test, expected, message) { | |
| 124 if (test !== expected) { | |
| 125 if (typeof(test) == "string") { | |
| 126 if (message) | |
| 127 message = test + "\n" + message; | |
| 128 else | |
| 129 message = test; | |
| 130 } | |
| 131 chromeTest.fail(message); | |
| 132 } | |
| 133 }); | |
| 134 | |
| 135 apiFunctions.setHandleRequest('checkDeepEq', function(expected, actual) { | |
| 136 if ((expected === null) != (actual === null)) | |
| 137 return false; | |
| 138 | |
| 139 if (expected === actual) | |
| 140 return true; | |
| 141 | |
| 142 if (typeof(expected) !== typeof(actual)) | |
| 143 return false; | |
| 144 | |
| 145 for (var p in actual) { | |
| 146 if (actual.hasOwnProperty(p) && !expected.hasOwnProperty(p)) | |
| 147 return false; | |
| 148 } | |
| 149 for (var p in expected) { | |
| 150 if (expected.hasOwnProperty(p) && !actual.hasOwnProperty(p)) | |
| 151 return false; | |
| 152 } | |
| 153 | |
| 154 for (var p in expected) { | |
| 155 var eq = true; | |
| 156 switch (typeof(expected[p])) { | |
| 157 case 'object': | |
| 158 eq = chromeTest.checkDeepEq(expected[p], actual[p]); | |
| 159 break; | |
| 160 case 'function': | |
| 161 eq = (typeof(actual[p]) != 'undefined' && | |
| 162 expected[p].toString() == actual[p].toString()); | |
| 163 break; | |
| 164 default: | |
| 165 eq = (expected[p] == actual[p] && | |
| 166 typeof(expected[p]) == typeof(actual[p])); | |
| 167 break; | |
| 168 } | |
| 169 if (!eq) | |
| 170 return false; | |
| 171 } | |
| 172 return true; | |
| 173 }); | |
| 174 | |
| 175 apiFunctions.setHandleRequest('assertEq', | |
| 176 function(expected, actual, message) { | |
| 177 var error_msg = "API Test Error in " + testName(currentTest); | |
| 178 if (message) | |
| 179 error_msg += ": " + message; | |
| 180 if (typeof(expected) == 'object') { | |
| 181 if (!chromeTest.checkDeepEq(expected, actual)) { | |
| 182 chromeTest.fail(error_msg + | |
| 183 "\nActual: " + JSON.stringify(actual) + | |
| 184 "\nExpected: " + JSON.stringify(expected)); | |
| 185 } | |
| 186 return; | |
| 187 } | |
| 188 if (expected != actual) { | |
| 189 chromeTest.fail(error_msg + | |
| 190 "\nActual: " + actual + "\nExpected: " + expected); | |
| 191 } | |
| 192 if (typeof(expected) != typeof(actual)) { | |
| 193 chromeTest.fail(error_msg + | |
| 194 " (type mismatch)\nActual Type: " + typeof(actual) + | |
| 195 "\nExpected Type:" + typeof(expected)); | |
| 196 } | |
| 197 }); | |
| 198 | |
| 199 apiFunctions.setHandleRequest('assertNoLastError', function() { | |
| 200 if (chrome.runtime.lastError != undefined) { | |
| 201 chromeTest.fail("lastError.message == " + | |
| 202 chrome.runtime.lastError.message); | |
| 203 } | |
| 204 }); | |
| 205 | |
| 206 apiFunctions.setHandleRequest('assertLastError', function(expectedError) { | |
| 207 chromeTest.assertEq(typeof(expectedError), 'string'); | |
| 208 chromeTest.assertTrue(chrome.runtime.lastError != undefined, | |
| 209 "No lastError, but expected " + expectedError); | |
| 210 chromeTest.assertEq(expectedError, chrome.runtime.lastError.message); | |
| 211 }); | |
| 212 | |
| 213 function safeFunctionApply(func, args) { | |
| 214 try { | |
| 215 if (func) | |
| 216 func.apply(null, args); | |
| 217 } catch (e) { | |
| 218 var msg = "uncaught exception " + e; | |
| 219 chromeTest.fail(msg); | |
| 220 } | |
| 221 }; | |
| 222 | |
| 223 // Wrapper for generating test functions, that takes care of calling | |
| 224 // assertNoLastError() and (optionally) succeed() for you. | |
| 225 apiFunctions.setHandleRequest('callback', function(func, expectedError) { | |
| 226 if (func) { | |
| 227 chromeTest.assertEq(typeof(func), 'function'); | |
| 228 } | |
| 229 var callbackCompleted = chromeTest.callbackAdded(); | |
| 230 | |
| 231 return function() { | |
| 232 if (expectedError == null) { | |
| 233 chromeTest.assertNoLastError(); | |
| 234 } else { | |
| 235 chromeTest.assertLastError(expectedError); | |
| 236 } | |
| 237 | |
| 238 if (func) { | |
| 239 safeFunctionApply(func, arguments); | |
| 240 } | |
| 241 | |
| 242 callbackCompleted(); | |
| 243 }; | |
| 244 }); | |
| 245 | |
| 246 apiFunctions.setHandleRequest('listenOnce', function(event, func) { | |
| 247 var callbackCompleted = chromeTest.callbackAdded(); | |
| 248 var listener = function() { | |
| 249 event.removeListener(listener); | |
| 250 safeFunctionApply(func, arguments); | |
| 251 callbackCompleted(); | |
| 252 }; | |
| 253 event.addListener(listener); | |
| 254 }); | |
| 255 | |
| 256 apiFunctions.setHandleRequest('listenForever', function(event, func) { | |
| 257 var callbackCompleted = chromeTest.callbackAdded(); | |
| 258 | |
| 259 var listener = function() { | |
| 260 safeFunctionApply(func, arguments); | |
| 261 }; | |
| 262 | |
| 263 var done = function() { | |
| 264 event.removeListener(listener); | |
| 265 callbackCompleted(); | |
| 266 }; | |
| 267 | |
| 268 event.addListener(listener); | |
| 269 return done; | |
| 270 }); | |
| 271 | |
| 272 apiFunctions.setHandleRequest('callbackPass', function(func) { | |
| 273 return chromeTest.callback(func); | |
| 274 }); | |
| 275 | |
| 276 apiFunctions.setHandleRequest('callbackFail', function(expectedError, func) { | |
| 277 return chromeTest.callback(func, expectedError); | |
| 278 }); | |
| 279 | |
| 280 apiFunctions.setHandleRequest('runTests', function(tests) { | |
| 281 chromeTest.tests = tests; | |
| 282 testCount = chromeTest.tests.length; | |
| 283 chromeTest.runNextTest(); | |
| 284 }); | |
| 285 | |
| 286 apiFunctions.setHandleRequest('getApiDefinitions', function(apiNames) { | |
| 287 return GetExtensionAPIDefinition(); | |
| 288 }); | |
| 289 }); | |
| 290 | |
| 291 exports.binding = binding.generate(); | |
| OLD | NEW |