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