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

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

Powered by Google App Engine
This is Rietveld 408576698