Chromium Code Reviews
|
| 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 { "url": extensionPath + "a.html", | |
|
jstritar
2011/09/16 21:02:41
nit: can you put the first property on the next li
alexbost
2011/09/16 21:46:53
Done.
| |
| 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 (create, get, getAll, update, remove) | |
| 32 var tabs = []; | |
| 33 | |
| 34 // Mouse (sendMouseEvent) | |
| 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 (sendKeyboardEvent) | |
| 48 | |
| 49 var tabKeyboard = new Object(); | |
| 50 | |
| 51 // Display (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 (create, get, getAll, update, remove) ------------------------ | |
| 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 (sendMouseEvent) ------------------------------------------------------ | |
| 152 | |
| 153 function startMouseEvents() { | |
| 154 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = | |
| 155 function (tabId, changeInfo, tab) { | |
| 156 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); | |
| 157 | |
| 158 assertEq(inTabs[0].url, changeInfo.url); | |
| 159 assertEq(inTabs[0].url, tab.url); | |
| 160 assertEq(inTabs[0].width, tab.width); | |
| 161 assertEq(inTabs[0].height, tab.height); | |
| 162 | |
| 163 tabMouse = tab; | |
| 164 | |
| 165 mouseClick(); | |
| 166 }); | |
| 167 | |
| 168 chrome.experimental.offscreenTabs.create( | |
| 169 { "url": inTabs[0].url, | |
| 170 "width": inTabs[0].width, | |
| 171 "height": inTabs[0].height | |
| 172 }); | |
| 173 } | |
| 174 | |
| 175 function mouseClick() { | |
| 176 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = | |
| 177 function(tabId, changeInfo, tab) { | |
| 178 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); | |
| 179 | |
| 180 assertEq(tabMouse.id, tabId); | |
| 181 assertEq(tabMouse.id, tab.id); | |
| 182 assertEq(inTabs[1].url, changeInfo.url); | |
| 183 assertEq(inTabs[1].url, tab.url); | |
| 184 assertEq(tabMouse.width, tab.width); | |
| 185 assertEq(tabMouse.height, tab.height); | |
| 186 | |
| 187 mouseWheel(); | |
| 188 }); | |
| 189 | |
| 190 mouseEvent.type = "click"; | |
| 191 chrome.experimental.offscreenTabs. | |
| 192 sendMouseEvent(tabMouse.id, mouseEvent, x, y); | |
| 193 } | |
| 194 | |
| 195 function mouseWheel() { | |
| 196 mouseEvent.type = "mousewheel"; | |
| 197 mouseEvent.wheelDeltaX = 0; | |
| 198 mouseEvent.wheelDeltaY = -100; | |
| 199 chrome.experimental.offscreenTabs. | |
| 200 sendMouseEvent(tabMouse.id, mouseEvent, 0, 0, function(tab) { | |
| 201 mouseDownUp(); | |
| 202 } | |
| 203 ); | |
| 204 } | |
| 205 | |
| 206 function mouseDownUp() { | |
| 207 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = | |
| 208 function(tabId, changeInfo, tab) { | |
| 209 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); | |
| 210 | |
| 211 assertEq(inTabs[2].url, tab.url); | |
| 212 | |
| 213 chrome.experimental.offscreenTabs.remove(tabMouse.id, function() { | |
| 214 verifyTabDoesNotExist(tabMouse.id); | |
| 215 }); | |
| 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 // Keyboard (sendKeyboardEvent) ------------------------------------------------ | |
| 228 | |
| 229 function startKeyboardEvents() { | |
| 230 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = | |
| 231 function(tabId, changeInfo, tab) { | |
| 232 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); | |
| 233 | |
| 234 tabKeyboard = tab; | |
| 235 | |
| 236 keyPress(); | |
| 237 }); | |
| 238 | |
| 239 chrome.experimental.offscreenTabs.create( | |
| 240 { "url": inTabs[0].url, | |
| 241 "width": inTabs[0].width, | |
| 242 "height": inTabs[0].height | |
| 243 } | |
| 244 ); | |
| 245 } | |
| 246 | |
| 247 function keyPress() { | |
| 248 chrome.experimental.offscreenTabs.onUpdated.addListener(listener = | |
| 249 function(tabId, changeInfo, tab) { | |
| 250 chrome.experimental.offscreenTabs.onUpdated.removeListener(listener); | |
| 251 | |
| 252 assertEq(inTabs[1].url, tab.url); | |
| 253 | |
| 254 chrome.experimental.offscreenTabs.remove(tabKeyboard.id, function() { | |
| 255 verifyTabDoesNotExist(tabKeyboard.id); | |
| 256 }); | |
| 257 }); | |
| 258 | |
| 259 var keyboardEvent = { | |
| 260 "type": "keypress", | |
| 261 "charCode": 113, // q | |
| 262 "keyCode": 113, | |
| 263 "altKey": false, | |
| 264 "ctrlKey": false, | |
| 265 "shiftKey": false | |
| 266 }; | |
| 267 | |
| 268 chrome.experimental.offscreenTabs. | |
| 269 sendKeyboardEvent(tabKeyboard.id, keyboardEvent); | |
| 270 } | |
| 271 | |
| 272 | |
| 273 // Display (toDataUrl) --------------------------------------------------------- | |
| 274 | |
| 275 // In order to test that we don't get empty images back we can compare two | |
| 276 // images that are supposed to be different. We only need to make sure the two | |
| 277 // offscreen tabs have the same size (i.e. inTabs[0] and inTabs[1]) | |
| 278 function startDisplay() { | |
| 279 var nCallbacksNotDone = nTabsCaptured; | |
| 280 | |
| 281 for (var i=0; i<nTabsCaptured; i++) { | |
| 282 chrome.experimental.offscreenTabs.create( | |
| 283 { "url": inTabs[i].url, | |
| 284 "width": inTabs[i].width, | |
| 285 "height": inTabs[i].height | |
| 286 }, | |
| 287 function() { | |
| 288 var j = i; | |
| 289 return function(tab) { | |
| 290 tabsCaptured[j] = tab; | |
| 291 | |
| 292 nCallbacksNotDone--; | |
| 293 | |
| 294 if (nCallbacksNotDone == 0) | |
| 295 toDataUrl(); | |
| 296 } | |
| 297 }() | |
| 298 ); | |
| 299 } | |
| 300 } | |
| 301 | |
| 302 function toDataUrl() { | |
| 303 var nCallbacksNotDone = nTabsCaptured; | |
| 304 | |
| 305 for (var i=0; i<nTabsCaptured; i++) { | |
| 306 chrome.experimental.offscreenTabs.toDataUrl( | |
| 307 tabsCaptured[i].id, | |
| 308 {"format": "png"}, | |
| 309 function(dataUrl) { | |
| 310 var j = i; | |
| 311 return function(dataUrl) { | |
| 312 assertEq('string', typeof(dataUrl)); | |
| 313 assertEq('data:image/png;base64,', dataUrl.substr(0,22)); | |
| 314 | |
| 315 tabsCaptured[j].dataUrl = dataUrl; | |
| 316 | |
| 317 nCallbacksNotDone--; | |
| 318 | |
| 319 if (nCallbacksNotDone == 0) { | |
| 320 // Compare the dataUrls | |
| 321 assertTrue(tabsCaptured[0].dataUrl != tabsCaptured[1].dataUrl); | |
| 322 | |
| 323 for (var i=0; i<nTabsCaptured; i++) { | |
| 324 chrome.experimental.offscreenTabs.remove(tabsCaptured[i].id, | |
| 325 function() { | |
| 326 var j = i; | |
| 327 return function() { | |
| 328 verifyTabDoesNotExist(tabsCaptured[j].id); | |
| 329 } | |
| 330 }() | |
| 331 ); | |
| 332 } | |
| 333 } | |
| 334 } | |
| 335 }() | |
| 336 ); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 // Run tests ------------------------------------------------------------------ | |
| 341 | |
| 342 chrome.test.runTests([ | |
| 343 // Tab Management (create, get, getAll, update, remove) | |
| 344 function() { | |
| 345 startTabManagement(); | |
| 346 }, | |
|
jstritar
2011/09/16 21:02:41
nit: new lines between these methods
alexbost
2011/09/16 21:46:53
Done.
| |
| 347 // Mouse (sendMouseEvent) | |
| 348 function() { | |
|
jstritar
2011/09/16 21:02:41
I think you got rid of these function names just n
alexbost
2011/09/16 21:46:53
Done.
| |
| 349 startMouseEvents(); | |
| 350 }, | |
| 351 // Keyboard (sendKeyboardEvent) | |
| 352 function() { | |
| 353 startKeyboardEvents(); | |
| 354 }, | |
| 355 // Display (toDataUrl) | |
| 356 function() { | |
| 357 startDisplay(); | |
| 358 } | |
| 359 ]); | |
| 360 | |
| OLD | NEW |