blob: e9d92b54141ad25f3ef7e48c56fa46f6e76fcd2c [file] [log] [blame]
[email protected]5c1cea3f2009-08-31 17:15:471// extension_apitest.js
2// mini-framework for ExtensionApiTest browser tests
3
4var chrome = chrome || {};
5(function() {
6 chrome.test = chrome.test || {};
[email protected]9b6d31d2009-09-01 01:44:037
[email protected]5c1cea3f2009-08-31 17:15:478 chrome.test.tests = chrome.test.tests || [];
9
10 var completed = false;
[email protected]173bf0632009-09-08 15:49:5211 var currentTest = null;
12 var lastTest = null;
[email protected]5c1cea3f2009-08-31 17:15:4713
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]9b6d31d2009-09-01 01:44:0324 chrome.test.log("( FAILED ) " + currentTest.name);
[email protected]5c1cea3f2009-08-31 17:15:4725
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]9b6d31d2009-09-01 01:44:0342 };
[email protected]5c1cea3f2009-08-31 17:15:4743
44 function allTestsSucceeded() {
45 console.log("All tests succeeded");
46 if (completed) throw "completed";
47
48 chrome.test.notifyPass();
49 complete();
50 }
[email protected]d3284132009-09-15 20:16:4151
[email protected]173bf0632009-09-08 15:49:5252 var pendingCallbacks = 0;
[email protected]b7c2041e2009-09-11 20:13:2153
[email protected]d3284132009-09-15 20:16:4154 chrome.test.callbackAdded = function () {
[email protected]173bf0632009-09-08 15:49:5255 pendingCallbacks++;
[email protected]b7c2041e2009-09-11 20:13:2156
[email protected]d3284132009-09-15 20:16:4157 return function() {
58 pendingCallbacks--;
59 if (pendingCallbacks == 0) {
60 chrome.test.succeed();
61 }
[email protected]173bf0632009-09-08 15:49:5262 }
63 };
64
[email protected]5c1cea3f2009-08-31 17:15:4765 chrome.test.runNextTest = function() {
[email protected]173bf0632009-09-08 15:49:5266 chrome.test.assertEq(pendingCallbacks, 0);
67 lastTest = currentTest;
[email protected]5c1cea3f2009-08-31 17:15:4768 currentTest = chrome.test.tests.shift();
69 if (!currentTest) {
70 allTestsSucceeded();
71 return;
72 }
73 try {
[email protected]9b6d31d2009-09-01 01:44:0374 chrome.test.log("( RUN ) " + currentTest.name);
[email protected]5c1cea3f2009-08-31 17:15:4775 currentTest.call();
76 } catch (e) {
[email protected]9b6d31d2009-09-01 01:44:0377 var message = e.stack;
[email protected]5c1cea3f2009-08-31 17:15:4778 console.log("[FAIL] " + currentTest.name + ": " + message);
79 chrome.test.notifyFail(message);
80 complete();
81 }
[email protected]9b6d31d2009-09-01 01:44:0382 };
[email protected]5c1cea3f2009-08-31 17:15:4783
84 chrome.test.succeed = function() {
85 console.log("[SUCCESS] " + currentTest.name);
[email protected]9b6d31d2009-09-01 01:44:0386 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]5c1cea3f2009-08-31 17:15:4791
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]9b6d31d2009-09-01 01:44:03103 };
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]5c1cea3f2009-08-31 17:15:47116
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]9b6d31d2009-09-01 01:44:03122 };
[email protected]b7c2041e2009-09-11 20:13:21123
[email protected]173bf0632009-09-08 15:49:52124 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]5c1cea3f2009-08-31 17:15:47144
145 // Wrapper for generating test functions, that takes care of calling
[email protected]9b6d31d2009-09-01 01:44:03146 // assertNoLastError() and (optionally) succeed() for you.
[email protected]173bf0632009-09-08 15:49:52147 chrome.test.callback = function(func, expectedError) {
[email protected]c7ad50f2009-09-11 06:28:15148 if (func) {
149 chrome.test.assertEq(typeof(func), 'function');
150 }
[email protected]d3284132009-09-15 20:16:41151 var callbackCompleted = chrome.test.callbackAdded();
[email protected]c7ad50f2009-09-11 06:28:15152
[email protected]5c1cea3f2009-08-31 17:15:47153 return function() {
[email protected]173bf0632009-09-08 15:49:52154 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]5c1cea3f2009-08-31 17:15:47159 }
[email protected]c7ad50f2009-09-11 06:28:15160
161 if (func) {
162 safeFunctionApply(func, arguments);
163 }
164
[email protected]173bf0632009-09-08 15:49:52165 callbackCompleted();
[email protected]5c1cea3f2009-08-31 17:15:47166 };
[email protected]9b6d31d2009-09-01 01:44:03167 };
[email protected]b7c2041e2009-09-11 20:13:21168
[email protected]173bf0632009-09-08 15:49:52169 chrome.test.listenOnce = function(event, func) {
[email protected]d3284132009-09-15 20:16:41170 var callbackCompleted = chrome.test.callbackAdded();
[email protected]173bf0632009-09-08 15:49:52171 var listener = function() {
172 event.removeListener(listener);
[email protected]b7c2041e2009-09-11 20:13:21173 safeFunctionApply(func, arguments);
[email protected]173bf0632009-09-08 15:49:52174 callbackCompleted();
175 };
176 event.addListener(listener);
177 };
178
[email protected]b7c2041e2009-09-11 20:13:21179 chrome.test.listenForever = function(event, func) {
[email protected]d3284132009-09-15 20:16:41180 var callbackCompleted = chrome.test.callbackAdded();
181
[email protected]b7c2041e2009-09-11 20:13:21182 var listener = function() {
183 safeFunctionApply(func, arguments);
184 };
185
[email protected]d3284132009-09-15 20:16:41186 var done = function() {
[email protected]b7c2041e2009-09-11 20:13:21187 event.removeListener(listener);
188 callbackCompleted();
189 };
190
191 event.addListener(listener);
192 return done;
193 };
194
[email protected]173bf0632009-09-08 15:49:52195 chrome.test.callbackPass = function(func) {
196 return chrome.test.callback(func);
197 };
[email protected]9b6d31d2009-09-01 01:44:03198
[email protected]c7ad50f2009-09-11 06:28:15199 chrome.test.callbackFail = function(expectedError) {
200 return chrome.test.callback(null, expectedError);
[email protected]173bf0632009-09-08 15:49:52201 };
202
[email protected]5c1cea3f2009-08-31 17:15:47203 chrome.test.runTests = function(tests) {
204 chrome.test.tests = tests;
205 chrome.test.runNextTest();
[email protected]9b6d31d2009-09-01 01:44:03206 };
[email protected]5c1cea3f2009-08-31 17:15:47207})();