| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 // extension_apitest.js | |
| 6 // mini-framework for ExtensionApiTest browser tests | |
| 7 | |
| 8 var chrome = chrome || {}; | |
| 9 (function() { | |
| 10 chrome.test = chrome.test || {}; | |
| 11 | |
| 12 chrome.test.tests = chrome.test.tests || []; | |
| 13 | |
| 14 var completed = false; | |
| 15 var currentTest = null; | |
| 16 var lastTest = null; | |
| 17 | |
| 18 function complete() { | |
| 19 completed = true; | |
| 20 | |
| 21 // Try to get the script to stop running immediately. | |
| 22 // This isn't an error, just an attempt at saying "done". | |
| 23 throw "completed"; | |
| 24 } | |
| 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 chrome.test.fail = function(message) { | |
| 33 if (completed) throw "completed"; | |
| 34 chrome.test.log("( FAILED ) " + testName(currentTest)); | |
| 35 | |
| 36 var stack = "(no stack available)"; | |
| 37 try { | |
| 38 crash.me += 0; // An intentional exception to get the stack trace. | |
| 39 } catch (e) { | |
| 40 if (typeof(e.stack) != undefined) { | |
| 41 stack = e.stack.split("\n"); | |
| 42 stack = stack.slice(2); // Remove title and fail() lines. | |
| 43 stack = stack.join("\n"); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 if (!message) { | |
| 48 message = "FAIL (no message)"; | |
| 49 } | |
| 50 message += "\n" + stack; | |
| 51 console.log("[FAIL] " + testName(currentTest) + ": " + message); | |
| 52 chrome.test.notifyFail(message); | |
| 53 complete(); | |
| 54 }; | |
| 55 | |
| 56 function allTestsSucceeded() { | |
| 57 console.log("All tests succeeded"); | |
| 58 if (completed) throw "completed"; | |
| 59 | |
| 60 chrome.test.notifyPass(); | |
| 61 complete(); | |
| 62 } | |
| 63 | |
| 64 var pendingCallbacks = 0; | |
| 65 | |
| 66 chrome.test.callbackAdded = function() { | |
| 67 pendingCallbacks++; | |
| 68 | |
| 69 return function() { | |
| 70 pendingCallbacks--; | |
| 71 if (pendingCallbacks == 0) { | |
| 72 chrome.test.succeed(); | |
| 73 } | |
| 74 }; | |
| 75 }; | |
| 76 | |
| 77 chrome.test.runNextTest = function() { | |
| 78 chrome.test.assertEq(0, pendingCallbacks); | |
| 79 lastTest = currentTest; | |
| 80 currentTest = chrome.test.tests.shift(); | |
| 81 if (!currentTest) { | |
| 82 allTestsSucceeded(); | |
| 83 return; | |
| 84 } | |
| 85 try { | |
| 86 chrome.test.log("( RUN ) " + testName(currentTest)); | |
| 87 currentTest.call(); | |
| 88 } catch (e) { | |
| 89 var message = e.stack || "(no stack available)"; | |
| 90 console.log("[FAIL] " + testName(currentTest) + ": " + message); | |
| 91 chrome.test.notifyFail(message); | |
| 92 complete(); | |
| 93 } | |
| 94 }; | |
| 95 | |
| 96 chrome.test.succeed = function() { | |
| 97 console.log("[SUCCESS] " + testName(currentTest)); | |
| 98 chrome.test.log("( SUCCESS )"); | |
| 99 // Use setTimeout here to allow previous test contexts to be | |
| 100 // eligible for garbage collection. | |
| 101 setTimeout(chrome.test.runNextTest, 0); | |
| 102 }; | |
| 103 | |
| 104 chrome.test.assertTrue = function(test, message) { | |
| 105 chrome.test.assertBool(test, true, message); | |
| 106 }; | |
| 107 | |
| 108 chrome.test.assertFalse = function(test, message) { | |
| 109 chrome.test.assertBool(test, false, message); | |
| 110 }; | |
| 111 | |
| 112 chrome.test.assertBool = function(test, expected, message) { | |
| 113 if (test !== expected) { | |
| 114 if (typeof(test) == "string") { | |
| 115 if (message) { | |
| 116 message = test + "\n" + message; | |
| 117 } else { | |
| 118 message = test; | |
| 119 } | |
| 120 } | |
| 121 chrome.test.fail(message); | |
| 122 } | |
| 123 }; | |
| 124 | |
| 125 chrome.test.checkDeepEq = function (expected, actual) { | |
| 126 if ((expected === null) != (actual === null)) | |
| 127 return false; | |
| 128 | |
| 129 if (expected === actual) | |
| 130 return true; | |
| 131 | |
| 132 if (typeof(expected) !== typeof(actual)) | |
| 133 return false; | |
| 134 | |
| 135 for (var p in actual) { | |
| 136 if (actual.hasOwnProperty(p) && !expected.hasOwnProperty(p)) | |
| 137 return false; | |
| 138 } | |
| 139 for (var p in expected) { | |
| 140 if (expected.hasOwnProperty(p) && !actual.hasOwnProperty(p)) | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 for (var p in expected) { | |
| 145 var eq = true; | |
| 146 switch (typeof(expected[p])) { | |
| 147 case 'object': | |
| 148 eq = chrome.test.checkDeepEq(expected[p], actual[p]); | |
| 149 break; | |
| 150 case 'function': | |
| 151 eq = (typeof(actual[p]) != 'undefined' && | |
| 152 expected[p].toString() == actual[p].toString()); | |
| 153 break; | |
| 154 default: | |
| 155 eq = (expected[p] == actual[p] && | |
| 156 typeof(expected[p]) == typeof(actual[p])); | |
| 157 break; | |
| 158 } | |
| 159 if (!eq) | |
| 160 return false; | |
| 161 } | |
| 162 return true; | |
| 163 }; | |
| 164 | |
| 165 chrome.test.assertEq = function(expected, actual, message) { | |
| 166 var error_msg = "API Test Error in " + testName(currentTest); | |
| 167 if (message) | |
| 168 error_msg += ": " + message; | |
| 169 if (typeof(expected) == 'object') { | |
| 170 if (!chrome.test.checkDeepEq(expected, actual)) { | |
| 171 chrome.test.fail(error_msg + | |
| 172 "\nActual: " + JSON.stringify(actual) + | |
| 173 "\nExpected: " + JSON.stringify(expected)); | |
| 174 } | |
| 175 return; | |
| 176 } | |
| 177 if (expected != actual) { | |
| 178 chrome.test.fail(error_msg + | |
| 179 "\nActual: " + actual + "\nExpected: " + expected); | |
| 180 } | |
| 181 if (typeof(expected) != typeof(actual)) { | |
| 182 chrome.test.fail(error_msg + | |
| 183 " (type mismatch)\nActual Type: " + typeof(actual) + | |
| 184 "\nExpected Type:" + typeof(expected)); | |
| 185 } | |
| 186 }; | |
| 187 | |
| 188 chrome.test.assertNoLastError = function() { | |
| 189 if (chrome.extension.lastError != undefined) { | |
| 190 chrome.test.fail("lastError.message == " + | |
| 191 chrome.extension.lastError.message); | |
| 192 } | |
| 193 }; | |
| 194 | |
| 195 function safeFunctionApply(func, arguments) { | |
| 196 try { | |
| 197 if (func) { | |
| 198 func.apply(null, arguments); | |
| 199 } | |
| 200 } catch (e) { | |
| 201 var stack = "(no stack available)"; | |
| 202 if (typeof(e.stack) != "undefined") { | |
| 203 stack = e.stack.toString(); | |
| 204 } | |
| 205 var msg = "Exception during execution of callback in " + | |
| 206 testName(currentTest); | |
| 207 msg += "\n" + stack; | |
| 208 console.log("[FAIL] " + testName(currentTest) + ": " + msg); | |
| 209 chrome.test.notifyFail(msg); | |
| 210 complete(); | |
| 211 } | |
| 212 }; | |
| 213 | |
| 214 // Wrapper for generating test functions, that takes care of calling | |
| 215 // assertNoLastError() and (optionally) succeed() for you. | |
| 216 chrome.test.callback = function(func, expectedError) { | |
| 217 if (func) { | |
| 218 chrome.test.assertEq(typeof(func), 'function'); | |
| 219 } | |
| 220 var callbackCompleted = chrome.test.callbackAdded(); | |
| 221 | |
| 222 return function() { | |
| 223 if (expectedError == null) { | |
| 224 chrome.test.assertNoLastError(); | |
| 225 } else { | |
| 226 chrome.test.assertEq(typeof(expectedError), 'string'); | |
| 227 chrome.test.assertTrue(chrome.extension.lastError != undefined, | |
| 228 "No lastError, but expected " + expectedError); | |
| 229 chrome.test.assertEq(expectedError, chrome.extension.lastError.message); | |
| 230 } | |
| 231 | |
| 232 if (func) { | |
| 233 safeFunctionApply(func, arguments); | |
| 234 } | |
| 235 | |
| 236 callbackCompleted(); | |
| 237 }; | |
| 238 }; | |
| 239 | |
| 240 chrome.test.listenOnce = function(event, func) { | |
| 241 var callbackCompleted = chrome.test.callbackAdded(); | |
| 242 var listener = function() { | |
| 243 event.removeListener(listener); | |
| 244 safeFunctionApply(func, arguments); | |
| 245 callbackCompleted(); | |
| 246 }; | |
| 247 event.addListener(listener); | |
| 248 }; | |
| 249 | |
| 250 chrome.test.listenForever = function(event, func) { | |
| 251 var callbackCompleted = chrome.test.callbackAdded(); | |
| 252 | |
| 253 var listener = function() { | |
| 254 safeFunctionApply(func, arguments); | |
| 255 }; | |
| 256 | |
| 257 var done = function() { | |
| 258 event.removeListener(listener); | |
| 259 callbackCompleted(); | |
| 260 }; | |
| 261 | |
| 262 event.addListener(listener); | |
| 263 return done; | |
| 264 }; | |
| 265 | |
| 266 chrome.test.callbackPass = function(func) { | |
| 267 return chrome.test.callback(func); | |
| 268 }; | |
| 269 | |
| 270 chrome.test.callbackFail = function(expectedError, func) { | |
| 271 return chrome.test.callback(func, expectedError); | |
| 272 }; | |
| 273 | |
| 274 chrome.test.runTests = function(tests) { | |
| 275 chrome.test.tests = tests; | |
| 276 chrome.test.runNextTest(); | |
| 277 }; | |
| 278 })(); | |
| OLD | NEW |