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