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