Index: chrome/test/data/extensions/api_test/offscreen_tabs/test.js |
=================================================================== |
--- chrome/test/data/extensions/api_test/offscreen_tabs/test.js (revision 0) |
+++ chrome/test/data/extensions/api_test/offscreen_tabs/test.js (revision 0) |
@@ -0,0 +1,354 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+var pass = chrome.test.callbackPass; |
+var fail = chrome.test.callbackFail; |
+var assertEq = chrome.test.assertEq; |
+var assertTrue = chrome.test.assertTrue; |
+ |
+var extensionPath = |
+ location.href.substring(0, location.href.lastIndexOf("/") + 1); |
+ |
+var inTabs = [ |
+ { "url":extensionPath + "a.html", |
+ "width":200, |
+ "height":200 |
+ }, |
+ { "url":extensionPath + "b.html", |
+ "width":200, |
+ "height":200 |
+ }, |
+ { "url":extensionPath + "c.html", |
+ "width":1000, |
+ "height":800 |
+ } |
+]; |
+ |
+// Wait this long for chrome to navigate |
+// TODO(alexbost): Listen for onUpdated once it's implemented in the API. |
+var sleepTime = 250; |
+ |
+// Tab management |
+var tabs = []; |
+ |
+// Mouse |
+ |
+var tabMouse; |
+ |
+var mouseEvent = { |
+ "button":0, |
+ "altKey":false, |
+ "ctrlKey":false, |
+ "shiftKey":false |
+}; |
+ |
+var x = 11; |
+var y = 11; |
+ |
+// Keyboard |
+ |
+var tabKeyboard; |
+ |
+// ToDataUrl |
+ |
+var tabsCaptured = []; |
+ |
+var nTabsCaptured = 2; |
+ |
+// Util |
+ |
+function compareTabs(tabA, tabB) { |
+ assertEq(tabA.url, tabB.url); |
+ assertEq(tabA.width, tabB.width); |
+ assertEq(tabA.height, tabB.height); |
Ken Russell (switch to Gerrit)
2011/08/26 00:57:48
Also assert that the IDs are equal.
alexbost
2011/08/26 22:07:23
Done.
|
+} |
+ |
+function sortTab(tabA, tabB) { |
+ return tabB.id - tabA.id; |
+} |
+ |
+function waitToNavigate(tabId, callback) { |
+ setTimeout(function() { |
+ chrome.experimental.offscreenTabs.get(tabId, pass(function(tab) { |
+ callback(tab); |
+ })); |
+ }, sleepTime, false); |
+} |
+ |
+// Tab management -------------------------------------------------------------- |
+ |
+function startTabManagement() { |
+ var nCallbacksNotDone = inTabs.length; |
+ |
+ for (var i=0; i<inTabs.length; i++) { |
+ chrome.experimental.offscreenTabs.create( |
+ { "url":inTabs[i].url, |
+ "width":inTabs[i].width, |
+ "height":inTabs[i].height |
+ }, |
+ pass(function() { |
+ var j = i; |
+ return function(tab) { |
+ tabs[j] = tab; |
+ |
+ nCallbacksNotDone--; |
+ |
+ if (nCallbacksNotDone == 0) |
+ getAll(); |
+ } |
+ }()) |
+ ); |
+ } |
+} |
+ |
+function getAll() { |
+ chrome.experimental.offscreenTabs.getAll(pass(function(tabsResult) { |
+ assertEq(tabs.length, tabsResult.length); |
+ |
+ tabs.sort(sortTab); |
+ tabsResult.sort(sortTab); |
+ |
+ for (var i=0; i<tabs.length; i++) { |
+ tabs[i].id = tabsResult[i].id; |
Ken Russell (switch to Gerrit)
2011/08/26 00:57:48
This should be unnecessary and is skewing the resu
alexbost
2011/08/26 22:07:23
Done.
|
+ |
+ compareTabs(tabs[i], tabsResult[i]); |
+ } |
+ |
+ get(); |
+ })); |
+} |
+ |
+function get() { |
+ chrome.experimental.offscreenTabs.get(tabs[0].id, pass(function(tab) { |
+ compareTabs(tabs[0], tab); |
+ |
+ update(); |
+ })); |
+ |
+ chrome.experimental.offscreenTabs. |
+ get(-1, fail("No offscreen tab with id: -1.")); |
+} |
+ |
+function update() { |
+ chrome.experimental.offscreenTabs.update(tabs[1].id, |
+ { "url":tabs[0].url, |
+ "width":tabs[0].width, |
+ "height":tabs[0].height |
+ }, |
+ pass(function(tab) { |
+ compareTabs(tabs[0], tab); |
+ |
+ remove(); |
+ }) |
+ ); |
+} |
+ |
+function remove() { |
+ for (var i=0; i<inTabs.length; i++) |
+ chrome.experimental.offscreenTabs.remove(tabs[i].id); |
+} |
+ |
+// Mouse ----------------------------------------------------------------------- |
+ |
+function startMouseEvents() { |
+ chrome.experimental.offscreenTabs.create( |
+ { "url":inTabs[0].url, |
+ "width":inTabs[0].width, |
+ "height":inTabs[0].height |
+ }, |
+ pass(function(tab) { |
+ waitToNavigate(tab.id, pass(function(tab) { |
+ tabMouse = tab; |
+ |
+ mouseClick(); |
+ })); |
+ }) |
+ ); |
+} |
+ |
+function mouseClick() { |
+ mouseEvent.type = "click"; |
+ chrome.experimental.offscreenTabs. |
+ sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) { |
+ |
+ waitToNavigate(tabMouse.id, pass(function(tab) { |
+ assertEq(inTabs[1].url, tab.url); |
+ |
+ tabMouse.id = tab.id; |
Ken Russell (switch to Gerrit)
2011/08/26 00:57:48
You should never reassign the IDs within the offsc
alexbost
2011/08/26 22:07:23
Done.
|
+ |
+ mouseWheel(); |
+ })); |
+ }) |
+ ); |
+} |
+ |
+function mouseWheel() { |
+ mouseEvent.type = "mousewheel"; |
+ mouseEvent.wheelDeltaX = 0; |
+ mouseEvent.wheelDeltaY = -100; |
+ chrome.experimental.offscreenTabs. |
+ sendMouseEvent(tabMouse.id, mouseEvent, 0, 0, pass(function(tab) { |
+ |
+ waitToNavigate(tabMouse.id, pass(function(tab) { |
+ assertEq(inTabs[1].url, tab.url); |
+ |
+ mouseDownUp(); |
+ })); |
+ }) |
+ ); |
+} |
+ |
+function mouseDownUp() { |
+ mouseEvent.type = "mousedown"; |
+ chrome.experimental.offscreenTabs. |
+ sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) { |
+ }) |
+ ); |
+ |
+ mouseEvent.type = "mouseup"; |
+ chrome.experimental.offscreenTabs. |
+ sendMouseEvent(tabMouse.id, mouseEvent, x, y, pass(function(tab) { |
+ |
+ waitToNavigate(tabMouse.id, pass(function(tab) { |
+ assertEq(inTabs[2].url, tab.url); |
+ |
+ removeMouse(); |
+ })); |
+ }) |
+ ); |
+} |
+ |
+function removeMouse() { |
+ chrome.experimental.offscreenTabs.remove(tabMouse.id); |
+} |
+ |
+// Keyboard -------------------------------------------------------------------- |
+ |
+function startKeyboardEvents() { |
+ chrome.experimental.offscreenTabs.create( |
+ { "url":inTabs[0].url, |
+ "width":inTabs[0].width, |
+ "height":inTabs[0].height |
+ }, |
+ pass(function(tab) { |
+ waitToNavigate(tab.id, pass(function(tab) { |
+ tabKeyboard = tab; |
+ |
+ keyPress(); |
+ })); |
+ }) |
+ ); |
+} |
+ |
+function keyPress() { |
+ var keyboardEvent = { |
+ "type":"keypress", |
+ "charCode":113, // q |
+ "keyCode":113, |
+ "altKey":false, |
+ "ctrlKey":false, |
+ "shiftKey":false |
+ }; |
+ |
+ chrome.experimental.offscreenTabs. |
+ sendKeyboardEvent(tabKeyboard.id, keyboardEvent, pass(function(tab) { |
+ waitToNavigate(tabKeyboard.id, pass(function(tab) { |
+ assertEq(inTabs[1].url, tab.url); |
+ |
+ removeKeyboard(); |
+ })); |
+ }) |
+ ); |
+} |
+ |
+function removeKeyboard() { |
+ chrome.experimental.offscreenTabs.remove(tabKeyboard.id); |
+} |
+ |
+// toDataUrl ------------------------------------------------------------------- |
+ |
+// The two tabs should have the same size. We want to make sure we dont get |
+// empty images back so this way we can compare two images that are supposed |
+// to be different |
+function startToDataUrl() { |
+ var nCallbacksNotDone = nTabsCaptured; |
+ |
+ for (var i=0; i<nTabsCaptured; i++) { |
+ chrome.experimental.offscreenTabs.create( |
+ { "url":inTabs[i].url, |
+ "width":inTabs[i].width, |
+ "height":inTabs[i].height |
+ }, |
+ pass(function() { |
+ var j = i; |
+ return function(tab) { |
+ tabsCaptured[j] = tab; |
+ |
+ nCallbacksNotDone--; |
+ |
+ if (nCallbacksNotDone == 0) |
+ toDataUrl(); |
+ } |
+ }()) |
+ ); |
+ } |
+} |
+ |
+function toDataUrl() { |
+ var nCallbacksNotDone = nTabsCaptured; |
+ |
+ for (var i=0; i<nTabsCaptured; i++) { |
+ chrome.experimental.offscreenTabs.toDataUrl( |
+ tabsCaptured[i].id, |
+ {"format":"png"}, |
+ pass(function(dataUrl) { |
+ var j = i; |
+ return function(dataUrl) { |
+ assertEq('string', typeof(dataUrl)); |
+ assertEq('data:image/png;base64,', dataUrl.substr(0,22)); |
+ |
+ tabsCaptured[j].dataUrl = dataUrl; |
+ |
+ nCallbacksNotDone--; |
+ |
+ if (nCallbacksNotDone == 0) { |
+ // compare the dataUrls |
+ assertTrue(tabsCaptured[0].dataUrl != tabsCaptured[1].dataUrl); |
+ |
+ removeToDataUrl(); |
+ } |
+ } |
+ }()) |
+ ); |
+ } |
+} |
+ |
+function removeToDataUrl() { |
+ for (var i=0; i<nTabsCaptured; i++) |
+ chrome.experimental.offscreenTabs.remove(tabsCaptured[i].id); |
+} |
+ |
+// Run tests ------------------------------------------------------------------ |
+ |
+function run() { |
+chrome.test.runTests([ |
+ function tabManagement() { |
+ startTabManagement(); |
+ }, |
+ |
+ function mouseEvents() { |
+ startMouseEvents(); |
+ }, |
+ |
+ function keyboardEvents() { |
+ startKeyboardEvents(); |
+ }, |
+ |
+ function toDataUrl() { |
+ startToDataUrl(); |
+ } |
+]); |
+} |
+ |
+run(); |
Property changes on: chrome/test/data/extensions/api_test/offscreen_tabs/test.js |
___________________________________________________________________ |
Added: svn:eol-style |
+ LF |