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

Side by Side Diff: chrome/test/data/extensions/api_test/offscreen_tabs/test.js

Issue 7720002: Chrome Extensions chrome.experimental.offscreenTabs.* API implementation, docs, and test. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 3 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7 var assertEq = chrome.test.assertEq;
8 var assertTrue = chrome.test.assertTrue;
9
10 var extensionPath =
11 location.href.substring(0, location.href.lastIndexOf("/") + 1);
12
13 var inTabs = [
14 { "url":extensionPath + "a.html",
15 "width":200,
16 "height":200
17 },
18 { "url":extensionPath + "b.html",
19 "width":200,
20 "height":200
21 },
22 { "url":extensionPath + "c.html",
23 "width":1000,
24 "height":800
25 }
26 ];
27
28 // Wait this long for chrome to navigate
29 // TODO(alexbost): Listen for onUpdated once it's implemented in the API.
30 var sleepTime = 250;
31
32 // Tab management
33 var tabs = [];
34
35 // Mouse
36
37 var tabMouse;
38
39 var mouseEvent = {
40 "button":0,
41 "altKey":false,
42 "ctrlKey":false,
43 "shiftKey":false
44 };
45
46 var x = 11;
47 var y = 11;
48
49 // Keyboard
50
51 var tabKeyboard;
52
53 // ToDataUrl
54
55 var tabsCaptured = [];
56
57 var nTabsCaptured = 2;
58
59 // Util
60
61 function compareTabs(tabA, tabB) {
62 assertEq(tabA.url, tabB.url);
63 assertEq(tabA.width, tabB.width);
64 assertEq(tabA.height, tabB.height);
Ken Russell (switch to Gerrit) 2011/08/26 00:57:48 Also assert that the IDs are equal.
alexbost 2011/08/26 22:07:23 Done.
65 }
66
67 function sortTab(tabA, tabB) {
68 return tabB.id - tabA.id;
69 }
70
71 function waitToNavigate(tabId, callback) {
72 setTimeout(function() {
73 chrome.experimental.offscreenTabs.get(tabId, pass(function(tab) {
74 callback(tab);
75 }));
76 }, sleepTime, false);
77 }
78
79 // Tab management --------------------------------------------------------------
80
81 function startTabManagement() {
82 var nCallbacksNotDone = inTabs.length;
83
84 for (var i=0; i<inTabs.length; i++) {
85 chrome.experimental.offscreenTabs.create(
86 { "url":inTabs[i].url,
87 "width":inTabs[i].width,
88 "height":inTabs[i].height
89 },
90 pass(function() {
91 var j = i;
92 return function(tab) {
93 tabs[j] = tab;
94
95 nCallbacksNotDone--;
96
97 if (nCallbacksNotDone == 0)
98 getAll();
99 }
100 }())
101 );
102 }
103 }
104
105 function getAll() {
106 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) {
107 assertEq(tabs.length, tabsResult.length);
108
109 tabs.sort(sortTab);
110 tabsResult.sort(sortTab);
111
112 for (var i=0; i<tabs.length; i++) {
113 tabs[i].id = tabsResult[i].id;
Ken Russell (switch to Gerrit) 2011/08/26 00:57:48 This should be unnecessary and is skewing the resu
alexbost 2011/08/26 22:07:23 Done.
114
115 compareTabs(tabs[i], tabsResult[i]);
116 }
117
118 get();
119 }));
120 }
121
122 function get() {
123 chrome.experimental.offscreenTabs.get(tabs[0].id, pass(function(tab) {
124 compareTabs(tabs[0], tab);
125
126 update();
127 }));
128
129 chrome.experimental.offscreenTabs.
130 get(-1, fail("No offscreen tab with id: -1."));
131 }
132
133 function update() {
134 chrome.experimental.offscreenTabs.update(tabs[1].id,
135 { "url":tabs[0].url,
136 "width":tabs[0].width,
137 "height":tabs[0].height
138 },
139 pass(function(tab) {
140 compareTabs(tabs[0], tab);
141
142 remove();
143 })
144 );
145 }
146
147 function remove() {
148 for (var i=0; i<inTabs.length; i++)
149 chrome.experimental.offscreenTabs.remove(tabs[i].id);
150 }
151
152 // Mouse -----------------------------------------------------------------------
153
154 function startMouseEvents() {
155 chrome.experimental.offscreenTabs.create(
156 { "url":inTabs[0].url,
157 "width":inTabs[0].width,
158 "height":inTabs[0].height
159 },
160 pass(function(tab) {
161 waitToNavigate(tab.id, pass(function(tab) {
162 tabMouse = tab;
163
164 mouseClick();
165 }));
166 })
167 );
168 }
169
170 function mouseClick() {
171 mouseEvent.type = "click";
172 chrome.experimental.offscreenTabs.
173 sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) {
174
175 waitToNavigate(tabMouse.id, pass(function(tab) {
176 assertEq(inTabs[1].url, tab.url);
177
178 tabMouse.id = tab.id;
Ken Russell (switch to Gerrit) 2011/08/26 00:57:48 You should never reassign the IDs within the offsc
alexbost 2011/08/26 22:07:23 Done.
179
180 mouseWheel();
181 }));
182 })
183 );
184 }
185
186 function mouseWheel() {
187 mouseEvent.type = "mousewheel";
188 mouseEvent.wheelDeltaX = 0;
189 mouseEvent.wheelDeltaY = -100;
190 chrome.experimental.offscreenTabs.
191 sendMouseEvent(tabMouse.id, mouseEvent, 0, 0, pass(function(tab) {
192
193 waitToNavigate(tabMouse.id, pass(function(tab) {
194 assertEq(inTabs[1].url, tab.url);
195
196 mouseDownUp();
197 }));
198 })
199 );
200 }
201
202 function mouseDownUp() {
203 mouseEvent.type = "mousedown";
204 chrome.experimental.offscreenTabs.
205 sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) {
206 })
207 );
208
209 mouseEvent.type = "mouseup";
210 chrome.experimental.offscreenTabs.
211 sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) {
212
213 waitToNavigate(tabMouse.id, pass(function(tab) {
214 assertEq(inTabs[2].url, tab.url);
215
216 removeMouse();
217 }));
218 })
219 );
220 }
221
222 function removeMouse() {
223 chrome.experimental.offscreenTabs.remove(tabMouse.id);
224 }
225
226 // Keyboard --------------------------------------------------------------------
227
228 function startKeyboardEvents() {
229 chrome.experimental.offscreenTabs.create(
230 { "url":inTabs[0].url,
231 "width":inTabs[0].width,
232 "height":inTabs[0].height
233 },
234 pass(function(tab) {
235 waitToNavigate(tab.id, pass(function(tab) {
236 tabKeyboard = tab;
237
238 keyPress();
239 }));
240 })
241 );
242 }
243
244 function keyPress() {
245 var keyboardEvent = {
246 "type":"keypress",
247 "charCode":113, // q
248 "keyCode":113,
249 "altKey":false,
250 "ctrlKey":false,
251 "shiftKey":false
252 };
253
254 chrome.experimental.offscreenTabs.
255 sendKeyboardEvent(tabKeyboard.id, keyboardEvent, pass(function(tab) {
256 waitToNavigate(tabKeyboard.id, pass(function(tab) {
257 assertEq(inTabs[1].url, tab.url);
258
259 removeKeyboard();
260 }));
261 })
262 );
263 }
264
265 function removeKeyboard() {
266 chrome.experimental.offscreenTabs.remove(tabKeyboard.id);
267 }
268
269 // toDataUrl -------------------------------------------------------------------
270
271 // The two tabs should have the same size. We want to make sure we dont get
272 // empty images back so this way we can compare two images that are supposed
273 // to be different
274 function startToDataUrl() {
275 var nCallbacksNotDone = nTabsCaptured;
276
277 for (var i=0; i<nTabsCaptured; i++) {
278 chrome.experimental.offscreenTabs.create(
279 { "url":inTabs[i].url,
280 "width":inTabs[i].width,
281 "height":inTabs[i].height
282 },
283 pass(function() {
284 var j = i;
285 return function(tab) {
286 tabsCaptured[j] = tab;
287
288 nCallbacksNotDone--;
289
290 if (nCallbacksNotDone == 0)
291 toDataUrl();
292 }
293 }())
294 );
295 }
296 }
297
298 function toDataUrl() {
299 var nCallbacksNotDone = nTabsCaptured;
300
301 for (var i=0; i<nTabsCaptured; i++) {
302 chrome.experimental.offscreenTabs.toDataUrl(
303 tabsCaptured[i].id,
304 {"format":"png"},
305 pass(function(dataUrl) {
306 var j = i;
307 return function(dataUrl) {
308 assertEq('string', typeof(dataUrl));
309 assertEq('data:image/png;base64,', dataUrl.substr(0,22));
310
311 tabsCaptured[j].dataUrl = dataUrl;
312
313 nCallbacksNotDone--;
314
315 if (nCallbacksNotDone == 0) {
316 // compare the dataUrls
317 assertTrue(tabsCaptured[0].dataUrl != tabsCaptured[1].dataUrl);
318
319 removeToDataUrl();
320 }
321 }
322 }())
323 );
324 }
325 }
326
327 function removeToDataUrl() {
328 for (var i=0; i<nTabsCaptured; i++)
329 chrome.experimental.offscreenTabs.remove(tabsCaptured[i].id);
330 }
331
332 // Run tests ------------------------------------------------------------------
333
334 function run() {
335 chrome.test.runTests([
336 function tabManagement() {
337 startTabManagement();
338 },
339
340 function mouseEvents() {
341 startMouseEvents();
342 },
343
344 function keyboardEvents() {
345 startKeyboardEvents();
346 },
347
348 function toDataUrl() {
349 startToDataUrl();
350 }
351 ]);
352 }
353
354 run();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698