[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 1 | // extension_apitest.js |
| 2 | // mini-framework for ExtensionApiTest browser tests |
| 3 | |
| 4 | var chrome = chrome || {}; |
| 5 | (function() { |
| 6 | chrome.test = chrome.test || {}; |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 7 | |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 8 | chrome.test.tests = chrome.test.tests || []; |
| 9 | |
| 10 | var completed = false; |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 11 | var currentTest = null; |
| 12 | var lastTest = null; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 13 | |
| 14 | function complete() { |
| 15 | completed = true; |
| 16 | |
| 17 | // Try to get the script to stop running immediately. |
| 18 | // This isn't an error, just an attempt at saying "done". |
| 19 | throw "completed"; |
| 20 | } |
| 21 | |
| 22 | chrome.test.fail = function(message) { |
| 23 | if (completed) throw "completed"; |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 24 | chrome.test.log("( FAILED ) " + currentTest.name); |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 25 | |
| 26 | var stack; |
| 27 | try { |
| 28 | crash.me += 0; // An intentional exception to get the stack trace. |
| 29 | } catch (e) { |
| 30 | stack = e.stack.split("\n"); |
| 31 | stack = stack.slice(2); // Remove title and fail() lines. |
| 32 | stack = stack.join("\n"); |
| 33 | } |
| 34 | |
| 35 | if (!message) { |
| 36 | message = "FAIL (no message)"; |
| 37 | } |
| 38 | message += "\n" + stack; |
| 39 | console.log("[FAIL] " + currentTest.name + ": " + message); |
| 40 | chrome.test.notifyFail(message); |
| 41 | complete(); |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 42 | }; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 43 | |
| 44 | function allTestsSucceeded() { |
| 45 | console.log("All tests succeeded"); |
| 46 | if (completed) throw "completed"; |
| 47 | |
| 48 | chrome.test.notifyPass(); |
| 49 | complete(); |
| 50 | } |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 51 | |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 52 | var pendingCallbacks = 0; |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 53 | |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 54 | chrome.test.callbackAdded = function () { |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 55 | pendingCallbacks++; |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 56 | |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 57 | return function() { |
| 58 | pendingCallbacks--; |
| 59 | if (pendingCallbacks == 0) { |
| 60 | chrome.test.succeed(); |
| 61 | } |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 62 | } |
| 63 | }; |
| 64 | |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 65 | chrome.test.runNextTest = function() { |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 66 | chrome.test.assertEq(pendingCallbacks, 0); |
| 67 | lastTest = currentTest; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 68 | currentTest = chrome.test.tests.shift(); |
| 69 | if (!currentTest) { |
| 70 | allTestsSucceeded(); |
| 71 | return; |
| 72 | } |
| 73 | try { |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 74 | chrome.test.log("( RUN ) " + currentTest.name); |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 75 | currentTest.call(); |
| 76 | } catch (e) { |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 77 | var message = e.stack; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 78 | console.log("[FAIL] " + currentTest.name + ": " + message); |
| 79 | chrome.test.notifyFail(message); |
| 80 | complete(); |
| 81 | } |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 82 | }; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 83 | |
| 84 | chrome.test.succeed = function() { |
| 85 | console.log("[SUCCESS] " + currentTest.name); |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 86 | chrome.test.log("( SUCCESS )"); |
| 87 | // Use setTimeout here to allow previous test contexts to be |
| 88 | // eligible for garbage collection. |
| 89 | setTimeout(chrome.test.runNextTest, 0); |
| 90 | }; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 91 | |
| 92 | chrome.test.assertTrue = function(test, message) { |
| 93 | if (test !== true) { |
| 94 | if (typeof(test) == "string") { |
| 95 | if (message) { |
| 96 | message = test + "\n" + message; |
| 97 | } else { |
| 98 | message = test; |
| 99 | } |
| 100 | } |
| 101 | chrome.test.fail(message); |
| 102 | } |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | chrome.test.assertEq = function(expected, actual) { |
| 106 | if (expected != actual) { |
| 107 | chrome.test.fail("API Test Error in " + currentTest.name + |
| 108 | "\nActual: " + actual + "\nExpected: " + expected); |
| 109 | } |
| 110 | if (typeof(expected) != typeof(actual)) { |
| 111 | chrome.test.fail("API Test Error in " + currentTest.name + |
| 112 | " (type mismatch)\nActual Type: " + typeof(actual) + |
| 113 | "\nExpected Type:" + typeof(expected)); |
| 114 | } |
| 115 | }; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 116 | |
| 117 | chrome.test.assertNoLastError = function() { |
| 118 | if (chrome.extension.lastError != undefined) { |
| 119 | chrome.test.fail("lastError.message == " + |
| 120 | chrome.extension.lastError.message); |
| 121 | } |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 122 | }; |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 123 | |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 124 | function safeFunctionApply(func, arguments) { |
| 125 | try { |
| 126 | if (func) { |
| 127 | func.apply(null, arguments); |
| 128 | } |
| 129 | } catch (e) { |
| 130 | var stack = null; |
| 131 | if (typeof(e.stack) != "undefined") { |
| 132 | stack = e.stack.toString(); |
| 133 | } |
| 134 | var msg = "Exception during execution of callback in " + |
| 135 | currentTest.name; |
| 136 | if (stack) { |
| 137 | msg += "\n" + stack; |
| 138 | } else { |
| 139 | msg += "\n(no stack available)"; |
| 140 | } |
| 141 | chrome.test.fail(msg); |
| 142 | } |
| 143 | }; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 144 | |
| 145 | // Wrapper for generating test functions, that takes care of calling |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 146 | // assertNoLastError() and (optionally) succeed() for you. |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 147 | chrome.test.callback = function(func, expectedError) { |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 148 | if (func) { |
| 149 | chrome.test.assertEq(typeof(func), 'function'); |
| 150 | } |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 151 | var callbackCompleted = chrome.test.callbackAdded(); |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 152 | |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 153 | return function() { |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 154 | if (expectedError == null) { |
| 155 | chrome.test.assertNoLastError(); |
| 156 | } else { |
| 157 | chrome.test.assertEq(typeof(expectedError), 'string'); |
| 158 | chrome.test.assertEq(expectedError, chrome.extension.lastError.message); |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 159 | } |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 160 | |
| 161 | if (func) { |
| 162 | safeFunctionApply(func, arguments); |
| 163 | } |
| 164 | |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 165 | callbackCompleted(); |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 166 | }; |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 167 | }; |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 168 | |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 169 | chrome.test.listenOnce = function(event, func) { |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 170 | var callbackCompleted = chrome.test.callbackAdded(); |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 171 | var listener = function() { |
| 172 | event.removeListener(listener); |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 173 | safeFunctionApply(func, arguments); |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 174 | callbackCompleted(); |
| 175 | }; |
| 176 | event.addListener(listener); |
| 177 | }; |
| 178 | |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 179 | chrome.test.listenForever = function(event, func) { |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 180 | var callbackCompleted = chrome.test.callbackAdded(); |
| 181 | |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 182 | var listener = function() { |
| 183 | safeFunctionApply(func, arguments); |
| 184 | }; |
| 185 | |
[email protected] | d328413 | 2009-09-15 20:16:41 | [diff] [blame] | 186 | var done = function() { |
[email protected] | b7c2041e | 2009-09-11 20:13:21 | [diff] [blame] | 187 | event.removeListener(listener); |
| 188 | callbackCompleted(); |
| 189 | }; |
| 190 | |
| 191 | event.addListener(listener); |
| 192 | return done; |
| 193 | }; |
| 194 | |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 195 | chrome.test.callbackPass = function(func) { |
| 196 | return chrome.test.callback(func); |
| 197 | }; |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 198 | |
[email protected] | c7ad50f | 2009-09-11 06:28:15 | [diff] [blame] | 199 | chrome.test.callbackFail = function(expectedError) { |
| 200 | return chrome.test.callback(null, expectedError); |
[email protected] | 173bf063 | 2009-09-08 15:49:52 | [diff] [blame] | 201 | }; |
| 202 | |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 203 | chrome.test.runTests = function(tests) { |
| 204 | chrome.test.tests = tests; |
| 205 | chrome.test.runNextTest(); |
[email protected] | 9b6d31d | 2009-09-01 01:44:03 | [diff] [blame] | 206 | }; |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 207 | })(); |