| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 var expectedError = "No offscreen tab with id: " |
| 75 chrome.experimental.offscreenTabs.get( |
| 76 tabId, |
| 77 fail(expectedError + tabId + ".")); |
| 78 } |
| 79 |
| 80 // Tab Management (create, get, getAll, update, remove) ------------------------ |
| 81 |
| 82 function startTabManagement() { |
| 83 var nCallbacksNotDone = inTabs.length; |
| 84 |
| 85 for (var i=0; i<inTabs.length; i++) { |
| 86 chrome.experimental.offscreenTabs.create( |
| 87 { |
| 88 "url": inTabs[i].url, |
| 89 "width": inTabs[i].width, |
| 90 "height": inTabs[i].height |
| 91 }, |
| 92 pass(function() { |
| 93 var j = i; |
| 94 return function(tab) { |
| 95 tabs[j] = tab; |
| 96 |
| 97 nCallbacksNotDone--; |
| 98 |
| 99 if (nCallbacksNotDone == 0) |
| 100 getAll(); |
| 101 } |
| 102 }()) |
| 103 ); |
| 104 } |
| 105 } |
| 106 |
| 107 function getAll() { |
| 108 chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { |
| 109 assertEq(tabs.length, tabsResult.length); |
| 110 |
| 111 tabs.sort(sortTab); |
| 112 tabsResult.sort(sortTab); |
| 113 |
| 114 for (var i=0; i<tabs.length; i++) |
| 115 compareTabs(tabs[i], tabsResult[i]); |
| 116 |
| 117 get(); |
| 118 })); |
| 119 } |
| 120 |
| 121 function get() { |
| 122 var comparedTab = tabs[0]; |
| 123 |
| 124 chrome.experimental.offscreenTabs.get(comparedTab.id, pass(function(tab) { |
| 125 compareTabs(comparedTab, tab); |
| 126 |
| 127 update(); |
| 128 })); |
| 129 } |
| 130 |
| 131 function update() { |
| 132 var replicatedTab = tabs[0]; |
| 133 |
| 134 chrome.experimental.offscreenTabs.update(tabs[1].id, |
| 135 { |
| 136 "url": replicatedTab.url, |
| 137 "width": replicatedTab.width, |
| 138 "height": replicatedTab.height |
| 139 }, |
| 140 pass(function(tab) { |
| 141 assertEq(replicatedTab.url, tab.url); |
| 142 assertEq(replicatedTab.width, tab.width); |
| 143 assertEq(replicatedTab.height, tab.height); |
| 144 |
| 145 remove(); |
| 146 }) |
| 147 ); |
| 148 } |
| 149 |
| 150 function remove() { |
| 151 for (var i=0; i<nTabsCaptured; i++) { |
| 152 chrome.experimental.offscreenTabs.remove(tabs[i].id); |
| 153 verifyTabDoesNotExist(tabs[i].id); |
| 154 } |
| 155 } |
| 156 |
| 157 // Mouse (sendMouseEvent) ------------------------------------------------------ |
| 158 |
| 159 function startMouseEvents() { |
| 160 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = |
| 161 function (tabId, changeInfo, tab) { |
| 162 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); |
| 163 |
| 164 assertEq(inTabs[0].url, changeInfo.url); |
| 165 assertEq(inTabs[0].url, tab.url); |
| 166 assertEq(inTabs[0].width, tab.width); |
| 167 assertEq(inTabs[0].height, tab.height); |
| 168 |
| 169 tabMouse = tab; |
| 170 |
| 171 mouseClick(); |
| 172 }); |
| 173 |
| 174 chrome.experimental.offscreenTabs.create( |
| 175 { |
| 176 "url": inTabs[0].url, |
| 177 "width": inTabs[0].width, |
| 178 "height": inTabs[0].height |
| 179 }); |
| 180 } |
| 181 |
| 182 function mouseClick() { |
| 183 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = |
| 184 function(tabId, changeInfo, tab) { |
| 185 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); |
| 186 |
| 187 assertEq(tabMouse.id, tabId); |
| 188 assertEq(tabMouse.id, tab.id); |
| 189 assertEq(inTabs[1].url, changeInfo.url); |
| 190 assertEq(inTabs[1].url, tab.url); |
| 191 assertEq(tabMouse.width, tab.width); |
| 192 assertEq(tabMouse.height, tab.height); |
| 193 |
| 194 mouseWheel(); |
| 195 }); |
| 196 |
| 197 mouseEvent.type = "click"; |
| 198 chrome.experimental.offscreenTabs. |
| 199 sendMouseEvent(tabMouse.id, mouseEvent, x, y); |
| 200 } |
| 201 |
| 202 function mouseWheel() { |
| 203 mouseEvent.type = "mousewheel"; |
| 204 mouseEvent.wheelDeltaX = 0; |
| 205 mouseEvent.wheelDeltaY = -100; |
| 206 chrome.experimental.offscreenTabs. |
| 207 sendMouseEvent(tabMouse.id, mouseEvent, 0, 0, function(tab) { |
| 208 mouseDownUp(); |
| 209 } |
| 210 ); |
| 211 } |
| 212 |
| 213 function mouseDownUp() { |
| 214 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = |
| 215 function(tabId, changeInfo, tab) { |
| 216 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); |
| 217 |
| 218 assertEq(inTabs[2].url, tab.url); |
| 219 |
| 220 chrome.experimental.offscreenTabs.remove(tabMouse.id); |
| 221 verifyTabDoesNotExist(tabMouse.id); |
| 222 }); |
| 223 |
| 224 mouseEvent.type = "mousedown"; |
| 225 chrome.experimental.offscreenTabs. |
| 226 sendMouseEvent(tabMouse.id, mouseEvent, x, y); |
| 227 |
| 228 mouseEvent.type = "mouseup"; |
| 229 chrome.experimental.offscreenTabs. |
| 230 sendMouseEvent(tabMouse.id, mouseEvent, x, y); |
| 231 } |
| 232 |
| 233 // Keyboard (sendKeyboardEvent) ------------------------------------------------ |
| 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 { |
| 247 "url": inTabs[0].url, |
| 248 "width": inTabs[0].width, |
| 249 "height": inTabs[0].height |
| 250 } |
| 251 ); |
| 252 } |
| 253 |
| 254 function keyPress() { |
| 255 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = |
| 256 function(tabId, changeInfo, tab) { |
| 257 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); |
| 258 |
| 259 assertEq(inTabs[1].url, tab.url); |
| 260 |
| 261 chrome.experimental.offscreenTabs.remove(tabKeyboard.id); |
| 262 verifyTabDoesNotExist(tabKeyboard.id); |
| 263 }); |
| 264 |
| 265 var keyboardEvent = { |
| 266 "type": "keypress", |
| 267 "charCode": 113, // q |
| 268 "keyCode": 113, |
| 269 "altKey": false, |
| 270 "ctrlKey": false, |
| 271 "shiftKey": false |
| 272 }; |
| 273 |
| 274 chrome.experimental.offscreenTabs. |
| 275 sendKeyboardEvent(tabKeyboard.id, keyboardEvent); |
| 276 } |
| 277 |
| 278 |
| 279 // Display (toDataUrl) --------------------------------------------------------- |
| 280 |
| 281 // In order to test that we don't get empty images back we can compare two |
| 282 // images that are supposed to be different. We only need to make sure the two |
| 283 // offscreen tabs have the same size (i.e. inTabs[0] and inTabs[1]) |
| 284 function startDisplay() { |
| 285 var nCallbacksNotDone = nTabsCaptured; |
| 286 |
| 287 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = |
| 288 function (tabId, changeInfo, tab) { |
| 289 tabsCaptured[nTabsCaptured - nCallbacksNotDone] = tab; |
| 290 |
| 291 nCallbacksNotDone--; |
| 292 |
| 293 if (nCallbacksNotDone == 0) { |
| 294 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); |
| 295 |
| 296 toDataUrl(); |
| 297 } |
| 298 }); |
| 299 |
| 300 for (var i=0; i<nTabsCaptured; i++) { |
| 301 chrome.experimental.offscreenTabs.create( |
| 302 { |
| 303 "url": inTabs[i].url, |
| 304 "width": inTabs[i].width, |
| 305 "height": inTabs[i].height |
| 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 pass(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 for (var i=0; i<nTabsCaptured; i++) { |
| 333 chrome.experimental.offscreenTabs.remove(tabsCaptured[i].id); |
| 334 verifyTabDoesNotExist(tabsCaptured[i].id); |
| 335 } |
| 336 } |
| 337 }) |
| 338 }() |
| 339 ); |
| 340 } |
| 341 } |
| 342 |
| 343 // Run tests ------------------------------------------------------------------ |
| 344 |
| 345 chrome.test.runTests([ |
| 346 // Tab Management (create, get, getAll, update, remove) |
| 347 function tabManagement() { |
| 348 startTabManagement(); |
| 349 }, |
| 350 |
| 351 // Mouse (sendMouseEvent) |
| 352 function mouseEvents() { |
| 353 startMouseEvents(); |
| 354 }, |
| 355 |
| 356 // Keyboard (sendKeyboardEvent) |
| 357 function keyboardEvents() { |
| 358 startKeyboardEvents(); |
| 359 }, |
| 360 |
| 361 // Display (toDataUrl) |
| 362 function display() { |
| 363 startDisplay(); |
| 364 } |
| 365 ]); |
| 366 |
| OLD | NEW |