Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: chrome/renderer/resources/extensions/apitest.js

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

Powered by Google App Engine
This is Rietveld 408576698