Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3572)

Unified Diff: chrome/test/data/extensions/api_test/offscreen_tabs/test.js

Issue 7720002: Chrome Extensions chrome.experimental.offscreenTabs.* API implementation, docs, and test. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,371 @@
+// 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.
+
+// OffscreenTabs API test
+// browser_tests.exe --gtest_filter=ExtensionApiTest.OffscreenTabs
jstritar 2011/09/16 16:03:50 ExperimentalApiTest
alexbost 2011/09/16 20:18:14 Done.
+
+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
+ },
jstritar 2011/09/16 16:03:50 style nits: spaces after the ':' { "url": exten
alexbost 2011/09/16 20:18:14 Done.
+ { "url":extensionPath + "b.html",
+ "width":200,
+ "height":200
+ },
+ { "url":extensionPath + "c.html",
+ "width":1000,
+ "height":800
+ }
+];
+
+// Tab management
jstritar 2011/09/16 16:03:50 can you make the comment more useful? or remove it
alexbost 2011/09/16 20:18:14 Done.
+var tabs = [];
+
+// Mouse
+
jstritar 2011/09/16 16:03:50 remove new line
alexbost 2011/09/16 20:18:14 Done.
+var tabMouse = new Object();
+
+var mouseEvent = {
+ "button":0,
jstritar 2011/09/16 16:03:50 "button": 0, etc..
alexbost 2011/09/16 20:18:14 Done.
+ "altKey":false,
+ "ctrlKey":false,
+ "shiftKey":false
+};
+
+var x = 11;
+var y = 11;
+
+// Keyboard
+
jstritar 2011/09/16 16:03:50 ditto on these comments. they don't add much right
alexbost 2011/09/16 20:18:14 Done.
+var tabKeyboard = new Object();
+
+// ToDataUrl
+
+var tabsCaptured = [];
+
+var nTabsCaptured = 2;
+
+// Util
+
+function compareTabs(tabA, tabB) {
+ assertEq(tabA.id, tabB.id);
+ assertEq(tabA.url, tabB.url);
+ assertEq(tabA.width, tabB.width);
+ assertEq(tabA.height, tabB.height);
+}
+
+function sortTab(tabA, tabB) {
+ return tabA.id - tabB.id;
+}
+
+function verifyTabDoesNotExist(tabId) {
+ chrome.experimental.offscreenTabs.
+ get(tabId, fail("No offscreen tab with id: " + tabId + "."));
+}
+
+// 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
+ },
+ function() {
+ var j = i;
jstritar 2011/09/16 16:03:50 indent the function body 2 spaces less
alexbost 2011/09/16 20:18:14 Done.
+ return function(tab) {
+ tabs[j] = tab;
+
+ nCallbacksNotDone--;
+
+ if (nCallbacksNotDone == 0)
+ getAll();
+ }
+ }()
+ );
+ }
+}
+
+function getAll() {
+ chrome.experimental.offscreenTabs.getAll(function(tabsResult) {
+ assertEq(tabs.length, tabsResult.length);
jstritar 2011/09/16 16:03:50 indent 2 spaces less
alexbost 2011/09/16 20:18:14 Done.
+
+ tabs.sort(sortTab);
+ tabsResult.sort(sortTab);
+
+ for (var i=0; i<tabs.length; i++)
+ compareTabs(tabs[i], tabsResult[i]);
+
+ get();
+ });
+}
+
+function get() {
+ var comparedTab = tabs[0];
+
+ chrome.experimental.offscreenTabs.get(comparedTab.id, function(tab) {
+ compareTabs(comparedTab, tab);
+
+ update();
+ });
+}
+
+function update() {
+ var replicatedTab = tabs[0];
+
+ chrome.experimental.offscreenTabs.update(tabs[1].id,
+ { "url":replicatedTab.url,
+ "width":replicatedTab.width,
+ "height":replicatedTab.height
+ },
+ function(tab) {
+ assertEq(replicatedTab.url, tab.url);
+ assertEq(replicatedTab.width, tab.width);
+ assertEq(replicatedTab.height, tab.height);
+
+ remove();
+ }
+ );
+}
+
+function remove() {
+ for (var i=0; i<inTabs.length; i++) {
+ chrome.experimental.offscreenTabs.remove(tabs[i].id);
+
+ verifyTabDoesNotExist(tabs[i].id);
+ }
+}
+
+// Mouse -----------------------------------------------------------------------
+
+function startMouseEvents() {
+ chrome.experimental.offscreenTabs.onUpdated.addListener(listener =
jstritar 2011/09/16 16:03:50 We have a helper method, chrome.test.listenOnce, t
alexbost 2011/09/16 20:18:14 Bingo! (Not completely sure if it will work in my
+ function (tabId, changeInfo, tab) {
+ chrome.experimental.offscreenTabs.onUpdated.removeListener(listener);
+
+ assertEq(inTabs[0].url, changeInfo.url);
+ assertEq(inTabs[0].url, tab.url);
+ assertEq(inTabs[0].width, tab.width);
+ assertEq(inTabs[0].height, tab.height);
+
+ tabMouse = tab;
+
+ mouseClick();
+ });
+
+ chrome.experimental.offscreenTabs.create(
+ { "url":inTabs[0].url,
+ "width":inTabs[0].width,
+ "height":inTabs[0].height
+ }
+ );
+}
+
+function mouseClick() {
+ chrome.experimental.offscreenTabs.onUpdated.addListener(listener =
+ function(tabId, changeInfo, tab) {
+ chrome.experimental.offscreenTabs.onUpdated.removeListener(listener);
+
+ assertEq(tabMouse.id, tabId);
+ assertEq(tabMouse.id, tab.id);
+ assertEq(inTabs[1].url, changeInfo.url);
+ assertEq(inTabs[1].url, tab.url);
+ assertEq(tabMouse.width, tab.width);
+ assertEq(tabMouse.height, tab.height);
+
+ mouseWheel();
+ });
+
+ mouseEvent.type = "click";
+ chrome.experimental.offscreenTabs.
+ sendMouseEvent(tabMouse.id, mouseEvent, x, y);
+}
+
+function mouseWheel() {
+ mouseEvent.type = "mousewheel";
+ mouseEvent.wheelDeltaX = 0;
+ mouseEvent.wheelDeltaY = -100;
+ chrome.experimental.offscreenTabs.
+ sendMouseEvent(tabMouse.id, mouseEvent, 0, 0, function(tab) {
+ mouseDownUp();
+ }
+ );
+}
+
+function mouseDownUp() {
+ chrome.experimental.offscreenTabs.onUpdated.addListener(listener =
+ function(tabId, changeInfo, tab) {
+ chrome.experimental.offscreenTabs.onUpdated.removeListener(listener);
+
+ assertEq(inTabs[2].url, tab.url);
+
+ removeMouse();
+ });
+
+ mouseEvent.type = "mousedown";
+ chrome.experimental.offscreenTabs.
+ sendMouseEvent(tabMouse.id, mouseEvent, x, y);
+
+ mouseEvent.type = "mouseup";
+ chrome.experimental.offscreenTabs.
+ sendMouseEvent(tabMouse.id, mouseEvent, x, y);
+}
+
+function removeMouse() {
jstritar 2011/09/16 16:03:50 see comments about removeKeyboard
alexbost 2011/09/16 20:18:14 Done.
+ chrome.experimental.offscreenTabs.remove(tabMouse.id);
+
+ verifyTabDoesNotExist(tabMouse.id);
+}
+
+// Keyboard --------------------------------------------------------------------
+
+function startKeyboardEvents() {
+ chrome.experimental.offscreenTabs.onUpdated.addListener(listener =
jstritar 2011/09/16 16:03:50 chrome.test.listenOnce
jstritar 2011/09/16 16:03:50 Why do you wait for onUpdated here? Why not just c
alexbost 2011/09/16 20:18:14 Done.
alexbost 2011/09/16 20:18:14 Because create might return before the page has na
+ function(tabId, changeInfo, tab) {
+ chrome.experimental.offscreenTabs.onUpdated.removeListener(listener);
+
+ tabKeyboard = tab;
+
+ keyPress();
+ });
+
+ chrome.experimental.offscreenTabs.create(
+ { "url":inTabs[0].url,
+ "width":inTabs[0].width,
+ "height":inTabs[0].height
+ }
+ );
jstritar 2011/09/16 16:03:50 I'm not sure about this but it's probably safer to
alexbost 2011/09/16 20:18:14 I had some trouble using the pass function with th
+}
+
+function keyPress() {
+ chrome.experimental.offscreenTabs.onUpdated.addListener(listener =
jstritar 2011/09/16 16:03:50 ditto on chrome.test.listenOnce
alexbost 2011/09/16 20:18:14 Done.
+ function(tabId, changeInfo, tab) {
+ chrome.experimental.offscreenTabs.onUpdated.removeListener(listener);
+
+ assertEq(inTabs[1].url, tab.url);
+
+ removeKeyboard();
+ });
+
+ var keyboardEvent = {
+ "type":"keypress",
+ "charCode":113, // q
+ "keyCode":113,
+ "altKey":false,
+ "ctrlKey":false,
+ "shiftKey":false
+ };
+
+ chrome.experimental.offscreenTabs.
+ sendKeyboardEvent(tabKeyboard.id, keyboardEvent);
+}
+
+function removeKeyboard() {
jstritar 2011/09/16 16:03:50 I don't think this needs to be its own method sinc
alexbost 2011/09/16 20:18:14 Done.
+ chrome.experimental.offscreenTabs.remove(tabKeyboard.id);
+
+ verifyTabDoesNotExist(tabKeyboard.id);
jstritar 2011/09/16 16:03:50 There's a race condition here. The tab may not rea
alexbost 2011/09/16 20:18:14 Done. BTW, I think there is a bug in the tabs API
+}
+
+// toDataUrl -------------------------------------------------------------------
+
+// In order to test that we don't get empty images back we can compare two
+// images that are supposed to be different. We only need to make sure the two
+// offscreen tabs have the same size (i.e. inTabs[0] and inTabs[1])
+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
+ },
+ 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"},
+ 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);
+
+ verifyTabDoesNotExist(tabsCaptured[i].id);
+ }
+}
+
+// Run tests ------------------------------------------------------------------
+
+function run() {
jstritar 2011/09/16 16:03:50 You don't need the run() method-- the chrome.test.
alexbost 2011/09/16 20:18:14 Done.
+chrome.test.runTests([
+ function tabManagement() {
jstritar 2011/09/16 16:03:50 I think you have race conditions in these tests- t
alexbost 2011/09/16 20:18:14 Unfortunately, I haven't been able to use the pass
jstritar 2011/09/16 21:02:41 I do think you need them. The timing issue could s
alexbost 2011/09/16 21:46:53 This error is caused by create returning before th
jstritar 2011/09/16 22:14:22 Okay, as long as the tests are passing now, I can
+ startTabManagement();
jstritar 2011/09/16 16:03:50 I think these tests would be easier to follow if y
alexbost 2011/09/16 20:18:14 Yes, I tried to stay away from the multiple levels
+ }
+ ,
jstritar 2011/09/16 16:03:50 nit: comma right after the '}' like this },
alexbost 2011/09/16 20:18:14 Done.
+ function mouseEvents() {
jstritar 2011/09/16 16:03:50 Can you add comments to each of these to make clea
alexbost 2011/09/16 20:18:14 Done.
+ 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

Powered by Google App Engine
This is Rietveld 408576698