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