|
OLD | NEW |
---|---|
(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 listen for onUpdated once its implemented in the API | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
TODO(alexbost):. Also, "it's".
alexbost
2011/08/24 20:06:21
Done.
| |
30 var sleepTime = 100; | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
In general the use of setTimeout and arbitrary del
alexbost
2011/08/24 20:06:21
Agreed. Putting it on top of my list.
| |
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 } | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
Missing ";".
alexbost
2011/08/24 20:06:21
Done.
| |
45 | |
46 var x = 11; | |
47 var y = 11; | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
It seems pretty delicate to aim for some text on t
alexbost
2011/08/24 20:06:21
I think aiming for the text is OK. It has absolute
| |
48 | |
49 // Keyboard | |
50 | |
51 var tabKeyboard; | |
52 | |
53 var keyboardEvent = { | |
54 "charCode":113, // q | |
55 "keyCode":113, | |
56 "altKey":false, | |
57 "ctrlKey":false, | |
58 "shiftKey":false | |
59 } | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
This could be a local variable in the "keyPress" f
alexbost
2011/08/24 20:06:21
Done.
| |
60 | |
61 // ToDataUrl | |
62 | |
63 var tabsCaptured = []; | |
64 | |
65 var nTabsCaptured = 2; | |
66 | |
67 // Util | |
68 | |
69 function compareTabs(t1, t2) { | |
70 assertEq(t1.url, t2.url); | |
71 assertEq(t1.width, t2.width); | |
72 assertEq(t1.height, t2.height); | |
73 } | |
74 | |
75 function waitToNavigate(tabId, callback) { | |
76 function func(callMeAgain) { | |
77 if (callMeAgain) | |
78 setTimeout(func, sleepTime, false); | |
79 else | |
80 chrome.experimental.offscreenTabs.get(tabId, pass(function(tab) { | |
81 callback(tab); | |
82 })); | |
83 } | |
84 func(true); | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
I don't understand what this function is supposed
alexbost
2011/08/24 20:06:21
Done.
| |
85 } | |
86 | |
87 // Tab management -------------------------------------------------------------- | |
88 | |
89 function startTabManagement() { | |
90 var nCallbacksNotDone = inTabs.length; | |
91 | |
92 for (var i=0; i<inTabs.length; i++) { | |
93 chrome.experimental.offscreenTabs.create( | |
94 { "url":inTabs[i].url, | |
95 "width":inTabs[i].width, | |
96 "height":inTabs[i].height | |
97 }, | |
98 pass(function() { | |
99 var j = i; | |
100 return function(tab) { | |
101 tabs[j] = tab; | |
102 | |
103 nCallbacksNotDone--; | |
104 | |
105 if (nCallbacksNotDone == 0) | |
106 getAll(); | |
107 } | |
108 }()) | |
109 ); | |
110 } | |
111 } | |
112 | |
113 function getAll() { | |
114 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { | |
115 assertEq(tabsResult.length, tabs.length); | |
116 | |
117 for (var i=0; i<tabs.length; i++) { | |
118 tabs[i].id = tabsResult[i].id; | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
This looks wrong. If you are trying to make sure t
alexbost
2011/08/24 20:06:21
Good point.
| |
119 | |
120 compareTabs(tabs[i], tabsResult[i]); | |
121 } | |
122 | |
123 get(); | |
124 })); | |
125 } | |
126 | |
127 function get() { | |
128 chrome.experimental.offscreenTabs.get(tabs[0].id, pass(function(tab) { | |
129 compareTabs(tabs[0], tab); | |
130 | |
131 update(); | |
132 })); | |
133 | |
134 chrome.experimental.offscreenTabs. | |
135 get(-1, fail("No offscreen tab with id: -1.")); | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
This negative test is nice.
alexbost
2011/08/24 20:06:21
Done.
| |
136 } | |
137 | |
138 function update() { | |
139 chrome.experimental.offscreenTabs.update(tabs[1].id, | |
140 { "url":tabs[0].url, | |
141 "width":tabs[0].width, | |
142 "height":tabs[0].height | |
143 }, | |
144 pass(function(tab) { | |
145 compareTabs(tabs[0], tab); | |
146 | |
147 remove(); | |
148 }) | |
149 ); | |
150 } | |
151 | |
152 function remove() { | |
153 for (var i=0; i<inTabs.length; i++) | |
154 chrome.experimental.offscreenTabs.remove(tabs[i].id); | |
155 | |
156 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { | |
157 assertEq(tabsResult.length, 0); | |
158 })); | |
159 } | |
160 | |
161 // Mouse ----------------------------------------------------------------------- | |
162 | |
163 function startMouseEvents() { | |
164 chrome.experimental.offscreenTabs.create( | |
165 { "url":inTabs[0].url, | |
166 "width":inTabs[0].width, | |
167 "height":inTabs[0].height | |
168 }, | |
169 pass(function(tab) { | |
170 waitToNavigate(tab.id, pass(function(tab) { | |
171 tabMouse = tab; | |
172 | |
173 mouseClick(); | |
174 })); | |
175 }) | |
176 ); | |
177 } | |
178 | |
179 function mouseClick() { | |
180 mouseEvent.type = "click"; | |
181 chrome.experimental.offscreenTabs. | |
182 sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) { | |
183 | |
184 waitToNavigate(tabMouse.id, pass(function(tab) { | |
185 assertEq(tab.url, inTabs[1].url); | |
186 | |
187 tabMouse = tab; | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
This looks unnecessary.
alexbost
2011/08/24 20:06:21
This is just to set tabMouse.id which is used late
| |
188 | |
189 mouseWheel(); | |
190 })); | |
191 }) | |
192 ); | |
193 } | |
194 | |
195 function mouseWheel() { | |
196 mouseEvent.type = "mousewheel"; | |
197 mouseEvent.wheelDeltaX = 0; | |
198 mouseEvent.wheelDeltaY = -100; | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
See question at top about using a div or something
alexbost
2011/08/24 20:06:21
See above.
| |
199 chrome.experimental.offscreenTabs. | |
200 sendMouseEvent(tabMouse.id, mouseEvent, null, null, pass(function(tab) { | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
Pass 0 instead of null since the arguments are sup
alexbost
2011/08/24 20:06:21
Done.
| |
201 | |
202 waitToNavigate(tabMouse.id, pass(function(tab) { | |
203 assertEq(tab.url, inTabs[1].url); | |
204 | |
205 mouseDownUp(); | |
206 })); | |
207 }) | |
208 ); | |
209 } | |
210 | |
211 function mouseDownUp() { | |
212 mouseEvent.type = "mousedown"; | |
213 chrome.experimental.offscreenTabs. | |
214 sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) { | |
215 }) | |
216 ); | |
217 | |
218 mouseEvent.type = "mouseup"; | |
219 chrome.experimental.offscreenTabs. | |
220 sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) { | |
221 | |
222 waitToNavigate(tabMouse.id, pass(function(tab) { | |
223 assertEq(tab.url, inTabs[2].url); | |
224 | |
225 removeMouse(); | |
226 })); | |
227 }) | |
228 ); | |
229 } | |
230 | |
231 function removeMouse() { | |
232 chrome.experimental.offscreenTabs.remove(tabMouse.id); | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
Indentation is off.
alexbost
2011/08/24 20:06:21
Done.
| |
233 | |
234 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { | |
235 assertEq(tabsResult.length, 0); | |
236 })); | |
237 } | |
238 | |
239 // Keyboard -------------------------------------------------------------------- | |
240 | |
241 function startKeyboardEvents() { | |
242 chrome.experimental.offscreenTabs.create( | |
243 { "url":inTabs[0].url, | |
244 "width":inTabs[0].width, | |
245 "height":inTabs[0].height | |
246 }, | |
247 pass(function(tab) { | |
248 waitToNavigate(tab.id, pass(function(tab) { | |
249 tabKeyboard = tab; | |
250 | |
251 keyPress(); | |
252 })); | |
253 }) | |
254 ); | |
255 } | |
256 | |
257 function keyPress() { | |
258 keyboardEvent.type = "keypress"; | |
259 chrome.experimental.offscreenTabs. | |
260 sendKeyboardEvent(tabKeyboard.id, keyboardEvent, pass(function(tab) { | |
261 waitToNavigate(tabKeyboard.id, pass(function(tab) { | |
262 assertEq(tab.url, inTabs[1].url); | |
263 | |
264 removeKeyboard(); | |
265 })); | |
266 }) | |
267 ); | |
268 } | |
269 | |
270 function removeKeyboard() { | |
271 chrome.experimental.offscreenTabs.remove(tabKeyboard.id); | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
Indentation is off.
alexbost
2011/08/24 20:06:21
Done.
| |
272 | |
273 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { | |
274 assertEq(tabsResult.length, 0); | |
275 })); | |
276 | |
277 } | |
278 | |
279 // toDataUrl ------------------------------------------------------------------- | |
280 | |
281 // The two tabs should have the same size. We want to make sure we dont get | |
282 // empty images back so this way we can compare two images that are supposed | |
283 // to be different | |
284 function startToDataUrl() { | |
285 var nCallbacksNotDone = nTabsCaptured; | |
286 | |
287 for (var i=0; i<nTabsCaptured; i++) { | |
288 chrome.experimental.offscreenTabs.create( | |
289 { "url":inTabs[i].url, | |
290 "width":inTabs[i].width, | |
291 "height":inTabs[i].height | |
292 }, | |
293 pass(function() { | |
294 var j = i; | |
295 return function(tab) { | |
296 tabsCaptured[j] = tab; | |
297 | |
298 nCallbacksNotDone--; | |
299 | |
300 if (nCallbacksNotDone == 0) | |
301 toDataUrl(); | |
302 } | |
303 }()) | |
304 ); | |
305 } | |
306 } | |
307 | |
308 function toDataUrl() { | |
309 var nCallbacksNotDone = nTabsCaptured; | |
310 | |
311 for (var i=0; i<nTabsCaptured; i++) { | |
312 chrome.experimental.offscreenTabs.toDataUrl( | |
313 tabsCaptured[i].id, | |
314 {"format":"png"}, | |
315 pass(function(dataUrl) { | |
316 var j = i; | |
317 return function(dataUrl) { | |
318 assertEq('string', typeof(dataUrl)); | |
319 assertEq('data:image/png;base64,', dataUrl.substr(0,22)); | |
320 | |
321 tabsCaptured[j].dataUrl = dataUrl; | |
322 | |
323 nCallbacksNotDone--; | |
324 | |
325 if (nCallbacksNotDone == 0) { | |
326 // compare the dataUrls | |
327 assertTrue(tabsCaptured[0].dataUrl != tabsCaptured[1].dataUrl); | |
328 | |
329 removeToDataUrl(); | |
330 } | |
331 } | |
332 }()) | |
333 ); | |
334 } | |
335 } | |
336 | |
337 function removeToDataUrl() { | |
338 for (var i=0; i<nTabsCaptured; i++) | |
339 chrome.experimental.offscreenTabs.remove(tabsCaptured[i].id); | |
340 | |
341 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { | |
342 assertEq(tabsResult.length, 0); | |
343 })); | |
344 } | |
345 | |
346 // Run tests ------------------------------------------------------------------ | |
347 | |
348 function run() { | |
349 chrome.test.runTests([ | |
Ken Russell (switch to Gerrit)
2011/08/23 22:46:53
Indentation.
alexbost
2011/08/24 20:06:21
This looks right according to:
http://src.chromium
| |
350 function tabManagement() { | |
351 startTabManagement(); | |
352 }, | |
353 | |
354 function mouseEvents() { | |
355 startMouseEvents(); | |
356 }, | |
357 | |
358 function keyboardEvents() { | |
359 startKeyboardEvents(); | |
360 }, | |
361 | |
362 function toDataUrl() { | |
363 startToDataUrl(); | |
364 } | |
365 ]); | |
366 } | |
367 | |
368 run(); | |
OLD | NEW |