diff options
author | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-28 11:41:39 +0000 |
---|---|---|
committer | jochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-28 11:41:39 +0000 |
commit | 3655a6dec098ca14d8d5ccf001a8d801b005e0a5 (patch) | |
tree | 5165552c76b8d30a5b48cdc290acbc0dd1187fb7 | |
parent | 9a3cc7af453ba2cfc5a938801bf95073a8808cd8 (diff) | |
download | chromium_src-3655a6dec098ca14d8d5ccf001a8d801b005e0a5.zip chromium_src-3655a6dec098ca14d8d5ccf001a8d801b005e0a5.tar.gz chromium_src-3655a6dec098ca14d8d5ccf001a8d801b005e0a5.tar.bz2 |
Revert "Revert "Revert "Split ExtensionApiTest.Tabs into several smaller tests and re-enable."""
This reverts commit 230aa348ea1b2530b57e8c19451e386128239e16.
TBR=aa@chromium.org
BUG=none
TEST=none
Review URL: http://codereview.chromium.org/4213002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64231 0039d316-1c4b-4281-b951-d872f2087c98
10 files changed, 524 insertions, 547 deletions
diff --git a/chrome/browser/extensions/extension_tabs_apitest.cc b/chrome/browser/extensions/extension_tabs_apitest.cc index 043e23b..a81c78b 100644 --- a/chrome/browser/extensions/extension_tabs_apitest.cc +++ b/chrome/browser/extensions/extension_tabs_apitest.cc @@ -9,6 +9,14 @@ #include "chrome/browser/profile.h" #include "chrome/common/pref_names.h" +#if defined(OS_WIN) +// This test times out on win. +// http://crbug.com/58269 +#define MAYBE_Tabs DISABLED_Tabs +#else +#define MAYBE_Tabs Tabs +#endif + // Possible race in ChromeURLDataManager. http://crbug.com/59198 #if defined(OS_MACOSX) || defined(OS_LINUX) #define MAYBE_TabOnRemoved DISABLED_TabOnRemoved @@ -16,7 +24,7 @@ #define MAYBE_TabOnRemoved TabOnRemoved #endif -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Tabs) { +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_Tabs) { ASSERT_TRUE(test_server()->Start()); // The test creates a tab and checks that the URL of the new tab @@ -25,23 +33,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Tabs) { browser()->profile()->GetPrefs()->SetBoolean( prefs::kHomePageIsNewTabPage, true); - ASSERT_TRUE(RunExtensionSubtest("tabs/basics", "crud.html")) << message_; -} - -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabMove) { - ASSERT_TRUE(test_server()->Start()); - ASSERT_TRUE(RunExtensionSubtest("tabs/basics", "move.html")) << message_; -} - -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabEvents) { - ASSERT_TRUE(test_server()->Start()); - ASSERT_TRUE(RunExtensionSubtest("tabs/basics", "events.html")) << message_; -} - -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabRelativeURLs) { - ASSERT_TRUE(test_server()->Start()); - ASSERT_TRUE(RunExtensionSubtest("tabs/basics", "relative_urls.html")) - << message_; + ASSERT_TRUE(RunExtensionTest("tabs/basics")) << message_; } IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabGetCurrent) { diff --git a/chrome/test/data/extensions/api_test/tabs/basics/crud.html b/chrome/test/data/extensions/api_test/tabs/basics/crud.html deleted file mode 100644 index 0b7bac2..0000000 --- a/chrome/test/data/extensions/api_test/tabs/basics/crud.html +++ /dev/null @@ -1,203 +0,0 @@ -<script src="tabs_util.js"></script> - -<script> -var firstWindowId; -var secondWindowId; -var testTabId; - -chrome.test.runTests([ - function getSelected() { - chrome.tabs.getSelected(null, pass(function(tab) { - assertEq(location.href, tab.url); - assertEq(location.href, tab.title); - firstWindowId = tab.windowId; - })); - }, - - function create() { - chrome.tabs.create({"windowId" : firstWindowId, "selected" : false}, - pass(function(tab){ - assertEq(1, tab.index); - assertEq(firstWindowId, tab.windowId); - assertEq(false, tab.selected); - assertEq("chrome://newtab/", tab.url); - })); - }, - - function createInOtherWindow() { - chrome.windows.create({}, pass(function(win) { - // Create a tab in the older window. - chrome.tabs.create({"windowId" : firstWindowId, "selected" : false}, - pass(function(tab) { - assertEq(firstWindowId, tab.windowId); - })); - // Create a tab in this new window. - chrome.tabs.create({"windowId" : win.id}, pass(function(tab) { - assertEq(win.id, tab.windowId); - })); - })); - }, - - function createAtIndex() { - chrome.tabs.create({"windowId" : firstWindowId, "index" : 1}, - pass(function(tab) { - assertEq(1, tab.index); - })); - }, - - function createSelected() { - chrome.tabs.create({"windowId" : firstWindowId, "selected" : true}, - pass(function(tab) { - assertTrue(tab.selected); - chrome.tabs.getSelected(firstWindowId, pass(function(selectedTab) { - assertEq(tab.id, selectedTab.id); - })); - })); - }, - - function createWindowWithDefaultTab() { - var verify_default = function() { - return pass(function(win) { - assertEq(1, win.tabs.length); - assertEq("chrome://newtab/", win.tabs[0].url); - }); - }; - - // Make sure the window always has the NTP when no URL is supplied. - chrome.windows.create({}, verify_default()); - chrome.windows.create({url:[]}, verify_default()); - }, - - function setupTwoWindows() { - createWindow(["about:blank", "chrome://newtab/", pageUrl("a")], {}, - pass(function(winId, tabIds) { - firstWindowId = winId; - testTabId = tabIds[2]; - - createWindow(["chrome://newtab/", pageUrl("b")], {}, - pass(function(winId, tabIds) { - secondWindowId = winId; - })); - })); - }, - - function getAllInWindow() { - chrome.tabs.getAllInWindow(firstWindowId, - pass(function(tabs) { - assertEq(3, tabs.length); - for (var i = 0; i < tabs.length; i++) { - assertEq(firstWindowId, tabs[i].windowId); - assertEq(i, tabs[i].index); - - // The first tab should be selected - assertEq((i == 0), tabs[i].selected); - } - assertEq("about:blank", tabs[0].url); - assertEq("chrome://newtab/", tabs[1].url); - assertEq(pageUrl("a"), tabs[2].url); - })); - - chrome.tabs.getAllInWindow(secondWindowId, - pass(function(tabs) { - assertEq(2, tabs.length); - for (var i = 0; i < tabs.length; i++) { - assertEq(secondWindowId, tabs[i].windowId); - assertEq(i, tabs[i].index); - } - assertEq("chrome://newtab/", tabs[0].url); - assertEq(pageUrl("b"), tabs[1].url); - })); - }, - - /* - :: sigh :: This test used to be disabled for unrelated reasons, but while it - was, the underlying functionality was broken. crbug.com/60795 - function getAllInWindowNullArg() { - chrome.tabs.getAllInWindow(null, pass(function(tabs) { - assertEq(2, tabs.length); - assertEq(secondWindowId, tabs[0].windowId); - })); - }, - */ - - function update() { - chrome.tabs.get(testTabId, pass(function(tab) { - assertEq(pageUrl("a"), tab.url); - // Update url. - chrome.tabs.update(testTabId, {"url": pageUrl("c")}, - pass(function(tab){ - chrome.test.assertEq(pageUrl("c"), tab.url); - // Check url. - chrome.tabs.get(testTabId, pass(function(tab) { - assertEq(pageUrl("c"), tab.url); - })); - })); - })); - }, - - function updateSelect() { - chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { - assertEq(true, tabs[0].selected); - assertEq(false, tabs[1].selected); - assertEq(false, tabs[2].selected); - // Select tab[1]. - chrome.tabs.update(tabs[1].id, {selected: true}, - pass(function(tab1){ - // Check update of tab[1]. - chrome.test.assertEq(true, tab1.selected); - chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { - assertEq(true, tabs[1].selected); - assertEq(false, tabs[2].selected); - // Select tab[2]. - chrome.tabs.update(tabs[2].id, - {selected: true}, - pass(function(tab2){ - // Check update of tab[2]. - chrome.test.assertEq(true, tab2.selected); - chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { - assertEq(false, tabs[1].selected); - assertEq(true, tabs[2].selected); - })); - })); - })); - })); - })); - }, - - /* Sadly, this has also regressed. crbug.com/60802. - function detectLanguage() { - chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { - chrome.tabs.detectLanguage(tabs[0].id, pass(function(lang) { - assertEq("en", lang); - })); - })); - }, - */ - - function windowCreate() { - chrome.windows.create({type: "popup"}, pass(function(window) { - assertEq("popup", window.type); - assertTrue(!window.incognito); - })); - chrome.windows.create({incognito: true}, pass(function(window) { - // This extension is not incognito-enabled, so it shouldn't be able to - // see the incognito window. - assertEq(null, window); - })); - }, - - /* Disabled -- see http://bugs.chromium.org/58229. - function windowSetFocused() { - chrome.windows.getCurrent(function(oldWin) { - chrome.windows.create({}, function(newWin) { - assertTrue(newWin.focused); - chrome.windows.update(oldWin.id, {focused:true}); - chrome.windows.get(oldWin.id, pass(function(oldWin2) { - assertTrue(oldWin2.focused); - })); - }); - }); - }, - */ -]); -</script> diff --git a/chrome/test/data/extensions/api_test/tabs/basics/events.html b/chrome/test/data/extensions/api_test/tabs/basics/events.html deleted file mode 100644 index 7cfaec7..0000000 --- a/chrome/test/data/extensions/api_test/tabs/basics/events.html +++ /dev/null @@ -1,179 +0,0 @@ -<script src="tabs_util.js"></script> - -<script> -var testTabId; -var otherTabId; -var firstWindowId; -var secondWindowId; - -chrome.test.runTests([ - function init() { - chrome.tabs.getSelected(null, pass(function(tab) { - testTabId = tab.id; - firstWindowId = tab.windowId; - })); - }, - - function tabsOnCreated() { - chrome.test.listenOnce(chrome.tabs.onCreated, function(tab) { - assertEq(pageUrl("f"), tab.url); - otherTabId = tab.id; - // TODO(jstritar): http://crbug.com/59194 tab.selected is always false - // when passed into chrome.tabs.onCreated listener - //assertEq(true, tab.selected); - }); - - chrome.tabs.create({"windowId": firstWindowId, "url": pageUrl("f"), - "selected": true}, pass(function(tab) {})); - }, - - function tabsOnUpdatedIgnoreTabArg() { - // A third argument was added to the onUpdated event callback. - // Test that an event handler which ignores this argument works. - var onUpdatedCompleted = chrome.test.listenForever(chrome.tabs.onUpdated, - function(tabid, changeInfo) { - if (tabid == otherTabId && changeInfo.status == "complete") { - onUpdatedCompleted(); - } - } - ); - - chrome.tabs.update(otherTabId, {"url": pageUrl("f")}, pass()); - }, - - function tabsOnUpdated() { - var onUpdatedCompleted = chrome.test.listenForever( - chrome.tabs.onUpdated, - function(tabid, changeInfo, tab) { - // |tab| contains the id of the tab it describes. - // Test that |tabid| matches this id. - assertEq(tabid, tab.id); - - // If |changeInfo| has a status property, than - // it should match the status of the tab in |tab|. - if (changeInfo.status) { - assertEq(changeInfo.status, tab.status); - } - - if (tabid == otherTabId && changeInfo.status == "complete") { - onUpdatedCompleted(); - } - } - ); - - chrome.tabs.update(otherTabId, {"url": pageUrl("f")}, pass()); - }, - - function tabsOnMoved() { - chrome.test.listenOnce(chrome.tabs.onMoved, function(tabid, info) { - assertEq(otherTabId, tabid); - }); - - chrome.tabs.move(otherTabId, {"index": 0}, pass()); - }, - - function tabsOnSelectionChanged() { - chrome.test.listenOnce(chrome.tabs.onSelectionChanged, - function(tabid, info) { - assertEq(testTabId, tabid); - } - ); - - chrome.tabs.update(testTabId, {"selected": true}, pass()); - }, - - function setupTabsOnAttachDetach() { - createWindow([""], {}, pass(function(winId, tabIds) { - secondWindowId = winId; - })); - }, - - function tabsOnAttached() { - function moveAndListen(tabId, properties, callback) { - chrome.test.listenOnce(chrome.tabs.onAttached, - function(testTabId, info) { - // Ensure notification is correct. - assertEq(testTabId, tabId); - assertEq(properties.windowId, info.newWindowId); - assertEq(properties.index, info.newPosition); - if (callback) - callback(); - }); - chrome.tabs.move(tabId, properties); - }; - - // Move tab to second window, then back to first. - // The original tab/window configuration should be restored. - // tabsOnDetached() depends on it. - moveAndListen(testTabId, {"windowId": secondWindowId, "index": 0}, - pass(function() { - moveAndListen(testTabId, {"windowId": firstWindowId, "index": 1}); - })); - }, - - function tabsOnDetached() { - function moveAndListen(tabId, oldWindowId, oldIndex, properties, - callback) { - chrome.test.listenOnce(chrome.tabs.onDetached, - function(detachedTabId, info) { - // Ensure notification is correct. - assertEq(detachedTabId, tabId); - assertEq(oldWindowId, info.oldWindowId); - assertEq(oldIndex, info.oldPosition); - if (callback) - callback(); - }); - chrome.tabs.move(tabId, properties); - }; - - // Move tab to second window, then back to first. - moveAndListen(testTabId, firstWindowId, 1, - {"windowId": secondWindowId, "index": 0}, pass(function() { - moveAndListen(testTabId, secondWindowId, 0, - {"windowId": firstWindowId, "index": 1}); - })); - }, - - function windowsOnCreated() { - chrome.test.listenOnce(chrome.windows.onCreated, function(window) { - assertTrue(window.width > 0); - assertTrue(window.height > 0); - assertEq("normal", window.type); - assertTrue(!window.incognito); - windowEventsWindow = window; - chrome.tabs.getAllInWindow(window.id, pass(function(tabs) { - assertEq(pageUrl("a"), tabs[0].url); - })); - }); - - chrome.windows.create({"url": pageUrl("a")}, pass(function(tab) {})); - }, - - function windowsOnFocusChanged() { - // This test doesn't work on mac because the Chromium app never gets - // brought to the front. See: crbug.com/60963. - // - // It also doesn't work on Chrome OS for unknown reasons. - // See: crbug.com/61035. - if (navigator.platform.toLowerCase().indexOf("mac") == 0 || - /\bCrOS\b/.test(navigator.userAgent)) { - chrome.test.succeed(); - return; - } - - chrome.windows.getCurrent(pass(function(windowA) { - chrome.windows.create({}, pass(function(windowB) { - chrome.windows.update(windowA.id, {focused: true}, pass(function() { - chrome.windows.update(windowB.id, {focused: true}, pass(function() { - chrome.test.listenOnce(chrome.windows.onFocusChanged, - function(changedWindowId) { - assertEq(windowEventsWindow.id, changedWindowId); - }); - chrome.windows.remove(windowB.id); - })); - })); - })); - })); - } -]); -</script> diff --git a/chrome/test/data/extensions/api_test/tabs/basics/manifest.json b/chrome/test/data/extensions/api_test/tabs/basics/manifest.json index 59ed72e..0ffa68f 100644 --- a/chrome/test/data/extensions/api_test/tabs/basics/manifest.json +++ b/chrome/test/data/extensions/api_test/tabs/basics/manifest.json @@ -2,5 +2,6 @@ "name": "chrome.tabs", "version": "0.1", "description": "end-to-end browser test for chrome.tabs API", + "background_page": "test.html", "permissions": ["tabs"] } diff --git a/chrome/test/data/extensions/api_test/tabs/basics/move.html b/chrome/test/data/extensions/api_test/tabs/basics/move.html deleted file mode 100644 index 9df94f4..0000000 --- a/chrome/test/data/extensions/api_test/tabs/basics/move.html +++ /dev/null @@ -1,85 +0,0 @@ -<script src="tabs_util.js"></script> - -<script> -var firstWindowId; -var secondWindowId; -var moveTabIds = {}; - -chrome.test.runTests([ - // Do a series of moves so that we get the following - // - // Before: - // Window1: (newtab),a,b,c,d,e - // Window2: (newtab) - // - // After: - // Window1: (newtab),a,e,c - // Window2: b,(newtab),d - function setupLetterPages() { - var pages = ["chrome://newtab/", pageUrl('a'), pageUrl('b'), - pageUrl('c'), pageUrl('d'), pageUrl('e')]; - createWindow(pages, {}, pass(function(winId, tabIds) { - firstWindowId = winId; - moveTabIds['a'] = tabIds[1]; - moveTabIds['b'] = tabIds[2]; - moveTabIds['c'] = tabIds[3]; - moveTabIds['d'] = tabIds[4]; - moveTabIds['e'] = tabIds[5]; - createWindow(["chrome://newtab/"], {}, pass(function(winId, tabIds) { - secondWindowId = winId; - })); - chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { - assertEq(pages.length, tabs.length); - for (var i in tabs) { - assertEq(pages[i], tabs[i].url); - } - })); - })); - }, - - function move() { - // Check that the tab/window state is what we expect after doing moves. - function checkMoveResults() { - chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { - assertEq(4, tabs.length); - assertEq("chrome://newtab/", tabs[0].url); - assertEq(pageUrl("a"), tabs[1].url); - assertEq(pageUrl("e"), tabs[2].url); - assertEq(pageUrl("c"), tabs[3].url); - - chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) { - assertEq(3, tabs.length); - assertEq(pageUrl("b"), tabs[0].url); - assertEq("chrome://newtab/", tabs[1].url); - assertEq(pageUrl("d"), tabs[2].url); - })); - })); - } - - chrome.tabs.move(moveTabIds['b'], {"windowId": secondWindowId, "index": 0}, - pass(function(tabB) { - chrome.test.assertEq(0, tabB.index); - chrome.tabs.move(moveTabIds['e'], {"index": 2}, - pass(function(tabE) { - chrome.test.assertEq(2, tabE.index); - chrome.tabs.move(moveTabIds['d'], {"windowId": secondWindowId, - "index": 2}, pass(function(tabD) { - chrome.test.assertEq(2, tabD.index); - checkMoveResults(); - })); - })); - })); - }, - - function remove() { - chrome.tabs.remove(moveTabIds["d"], pass(function() { - chrome.tabs.getAllInWindow(secondWindowId, - pass(function(tabs) { - assertEq(2, tabs.length); - assertEq(pageUrl("b"), tabs[0].url); - assertEq("chrome://newtab/", tabs[1].url); - })); - })); - } -]); -</script> diff --git a/chrome/test/data/extensions/api_test/tabs/basics/relative.html b/chrome/test/data/extensions/api_test/tabs/basics/relative.html index 30c4a2d..5a9e6ee8 100644 --- a/chrome/test/data/extensions/api_test/tabs/basics/relative.html +++ b/chrome/test/data/extensions/api_test/tabs/basics/relative.html @@ -2,13 +2,8 @@ <head> <script> window.onload = function() { - chrome.extension.getViews({type:"tab"}).forEach(function(view) { - if (view.relativePageLoaded) { - view.relativePageLoaded(); - break; - } - }); + chrome.extension.getBackgroundPage().relativePageLoaded(); } </script> </head> -</html> +</html>
\ No newline at end of file diff --git a/chrome/test/data/extensions/api_test/tabs/basics/relative_urls.html b/chrome/test/data/extensions/api_test/tabs/basics/relative_urls.html deleted file mode 100644 index f554399..0000000 --- a/chrome/test/data/extensions/api_test/tabs/basics/relative_urls.html +++ /dev/null @@ -1,46 +0,0 @@ -<script src="tabs_util.js"></script> - -<script> -var firstWindowId; -var relativePageLoaded; - -chrome.test.runTests([ - function setupRelativeUrlTests() { - createWindow(["about:blank"], {}, pass(function(winId, tabIds) { - firstWindowId = winId; - })); - }, - - function relativeUrlTestsTabsCreate() { - // Will be called from relative.html - relativePageLoaded = chrome.test.callbackAdded(); - var createCompleted = chrome.test.callbackAdded(); - - chrome.tabs.create({windowId: firstWindowId, url: 'relative.html'}, - function(tab){ - testTabId = tab.id; - createCompleted(); - } - ); - }, - - function relativeUrlTestsTabsUpdate() { - // Will be called from relative.html - relativePageLoaded = chrome.test.callbackAdded(); - - chrome.tabs.update(testTabId, {url: pageUrl("a")}, function(tab) { - chrome.test.assertEq(pageUrl("a"), tab.url); - chrome.tabs.update(tab.id, {url: "relative.html"}, function(tab) { - }); - }); - }, - - function relativeUrlTestsWindowCreate() { - // Will be called from relative.html - relativePageLoaded = chrome.test.callbackAdded(); - - chrome.windows.create({url: "relative.html"}); - } - -]); -</script> diff --git a/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js b/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js index 9b1a9ba..9f13dd9 100644 --- a/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js +++ b/chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js @@ -1,13 +1,26 @@ -// Copyright (c) 2010 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. +// Utility functions to help with tabs/windows testing. -var pass = chrome.test.callbackPass; -var assertEq = chrome.test.assertEq; -var assertTrue = chrome.test.assertTrue; - -function pageUrl(letter) { - return chrome.extension.getURL(letter + ".html"); +// Removes current windows and creates one window with tabs set to +// the urls in the array |tabUrls|. At least one url must be specified. +// The |callback| should look like function(windowId, tabIds) {...}. +function setupWindow(tabUrls, callback) { + createWindow(tabUrls, {}, function(winId, tabIds) { + // Remove all other windows. + var removedCount = 0; + chrome.windows.getAll({}, function(windows) { + for (var i in windows) { + if (windows[i].id != winId) { + chrome.windows.remove(windows[i].id, function() { + removedCount++; + if (removedCount == windows.length - 1) + callback(winId, tabIds); + }); + } + } + if (windows.length == 1) + callback(winId, tabIds); + }); + }); } // Creates one window with tabs set to the urls in the array |tabUrls|. @@ -54,3 +67,4 @@ function waitForAllTabs(callback) { } waitForTabs(); } + diff --git a/chrome/test/data/extensions/api_test/tabs/basics/test.html b/chrome/test/data/extensions/api_test/tabs/basics/test.html new file mode 100644 index 0000000..54f4ab7 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/basics/test.html @@ -0,0 +1,2 @@ +<script src="tabs_util.js"></script> +<script src="test.js"></script> diff --git a/chrome/test/data/extensions/api_test/tabs/basics/test.js b/chrome/test/data/extensions/api_test/tabs/basics/test.js new file mode 100644 index 0000000..3297bdf --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/basics/test.js @@ -0,0 +1,486 @@ +// tabs api test +// browser_tests.exe --gtest_filter=ExtensionApiTest.Tabs + +// We have a bunch of places where we need to remember some state from one +// test (or setup code) to subsequent tests. +var firstWindowId = null; +var secondWindowId = null; +var firstTabIndex = null; +var testTabId = null; + +var windowEventsWindow = null; +var moveTabIds = {}; + +var pass = chrome.test.callbackPass; +var assertEq = chrome.test.assertEq; +var assertTrue = chrome.test.assertTrue; + +function pageUrl(letter) { + return chrome.extension.getURL(letter + ".html"); +} + +chrome.test.runTests([ + function getSelected() { + chrome.tabs.getSelected(null, pass(function(tab) { + assertEq("about:blank", tab.url); + assertEq("about:blank", tab.title); + firstWindowId = tab.windowId; + firstTabIndex = tab.index; + })); + }, + + function create() { + chrome.tabs.create({"windowId" : firstWindowId, "selected" : false}, + pass(function(tab){ + assertTrue(tab.index > firstTabIndex); + assertEq(firstWindowId, tab.windowId); + assertEq(false, tab.selected); + assertEq("chrome://newtab/", tab.url); + })); + }, + + function createInOtherWindow() { + chrome.windows.create({}, pass(function(win) { + // The newly created window is now the currentWindow. + // Create a tab in the older window. + chrome.tabs.create({"windowId" : firstWindowId, "selected" : false}, + pass(function(tab) { + assertEq(firstWindowId, tab.windowId); + })); + // Create a tab in this new window. + chrome.tabs.create({"windowId" : win.id}, pass(function(tab) { + assertEq(win.id, tab.windowId); + })); + })); + }, + + function createAtIndex() { + chrome.tabs.create({"windowId" : firstWindowId, "index" : 1}, + pass(function(tab) { + assertEq(1, tab.index); + })); + }, + + function createSelected() { + chrome.tabs.create({"windowId" : firstWindowId, "selected" : true}, + pass(function(tab) { + assertTrue(tab.selected); + chrome.tabs.getSelected(firstWindowId, pass(function(selectedTab) { + assertEq(tab.id, selectedTab.id); + })); + })); + }, + + function createWindowWithDefaultTab() { + var verify_default = function() { + return pass(function(win) { + assertEq(1, win.tabs.length); + assertEq("chrome://newtab/", win.tabs[0].url); + }); + }; + + // Make sure the window always has the NTP when no URL is supplied. + chrome.windows.create({}, verify_default()); + chrome.windows.create({url:[]}, verify_default()); + }, + + function setupTwoWindows() { + setupWindow(["about:blank", "chrome://newtab/", pageUrl("a")], + pass(function(winId, tabIds) { + firstWindowId = winId; + testTabId = tabIds[2]; + + createWindow(["chrome://newtab/", pageUrl("b")], {}, + pass(function(winId, tabIds) { + secondWindowId = winId; + })); + })); + }, + + function getAllInWindow() { + chrome.tabs.getAllInWindow(firstWindowId, + pass(function(tabs) { + assertEq(3, tabs.length); + for (var i = 0; i < tabs.length; i++) { + assertEq(firstWindowId, tabs[i].windowId); + assertEq(i, tabs[i].index); + + // The first tab should be selected + assertEq((i == 0), tabs[i].selected); + } + assertEq("about:blank", tabs[0].url); + assertEq("chrome://newtab/", tabs[1].url); + assertEq(pageUrl("a"), tabs[2].url); + })); + + chrome.tabs.getAllInWindow(secondWindowId, + pass(function(tabs) { + assertEq(2, tabs.length); + for (var i = 0; i < tabs.length; i++) { + assertEq(secondWindowId, tabs[i].windowId); + assertEq(i, tabs[i].index); + } + assertEq("chrome://newtab/", tabs[0].url); + assertEq(pageUrl("b"), tabs[1].url); + })); + }, + + /* TODO: Enable this test when crbug.com/28055 is fixed. This bug causes a + newly created window not to be set as the current window, if + Chrome was not the foreground window when the create call was made. + function getAllInWindowNullArg() { + chrome.tabs.getAllInWindow(null, pass(function(tabs) { + assertEq(2, tabs.length); + assertEq(secondWindowId, tabs[0].windowId); + })); + }, */ + + function update() { + chrome.tabs.get(testTabId, pass(function(tab) { + assertEq(pageUrl("a"), tab.url); + // Update url. + chrome.tabs.update(testTabId, {"url": pageUrl("c")}, + pass(function(tab){ + chrome.test.assertEq(pageUrl("c"), tab.url); + // Check url. + chrome.tabs.get(testTabId, pass(function(tab) { + assertEq(pageUrl("c"), tab.url); + })); + })); + })); + }, + + function updateSelect() { + chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { + assertEq(true, tabs[0].selected); + assertEq(false, tabs[1].selected); + assertEq(false, tabs[2].selected); + // Select tab[1]. + chrome.tabs.update(tabs[1].id, {selected: true}, + pass(function(tab1){ + // Check update of tab[1]. + chrome.test.assertEq(true, tab1.selected); + chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { + assertEq(true, tabs[1].selected); + assertEq(false, tabs[2].selected); + // Select tab[2]. + chrome.tabs.update(tabs[2].id, + {selected: true}, + pass(function(tab2){ + // Check update of tab[2]. + chrome.test.assertEq(true, tab2.selected); + chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { + assertEq(false, tabs[1].selected); + assertEq(true, tabs[2].selected); + })); + })); + })); + })); + })); + }, + + // Do a series of moves so that we get the following + // + // Before: + // Window1: (newtab),a,b,c,d,e + // Window2: (newtab) + // + // After: + // Window1: (newtab),a,e,c + // Window2: b,(newtab),d + function setupLetterPages() { + var pages = ["chrome://newtab/", pageUrl('a'), pageUrl('b'), + pageUrl('c'), pageUrl('d'), pageUrl('e')]; + setupWindow(pages, pass(function(winId, tabIds) { + firstWindowId = winId; + moveTabIds['a'] = tabIds[1]; + moveTabIds['b'] = tabIds[2]; + moveTabIds['c'] = tabIds[3]; + moveTabIds['d'] = tabIds[4]; + moveTabIds['e'] = tabIds[5]; + createWindow(["chrome://newtab/"], {}, pass(function(winId, tabIds) { + secondWindowId = winId; + })); + chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { + assertEq(pages.length, tabs.length); + for (var i in tabs) { + assertEq(pages[i], tabs[i].url); + } + })); + })); + }, + + function move() { + // Check that the tab/window state is what we expect after doing moves. + function checkMoveResults() + { + chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { + assertEq(4, tabs.length); + assertEq("chrome://newtab/", tabs[0].url); + assertEq(pageUrl("a"), tabs[1].url); + assertEq(pageUrl("e"), tabs[2].url); + assertEq(pageUrl("c"), tabs[3].url); + + chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) { + assertEq(3, tabs.length); + assertEq(pageUrl("b"), tabs[0].url); + assertEq("chrome://newtab/", tabs[1].url); + assertEq(pageUrl("d"), tabs[2].url); + })); + })); + } + + chrome.tabs.move(moveTabIds['b'], {"windowId": secondWindowId, "index": 0}, + pass(function(tabB) { + chrome.test.assertEq(0, tabB.index); + chrome.tabs.move(moveTabIds['e'], {"index": 2}, + pass(function(tabE) { + chrome.test.assertEq(2, tabE.index); + chrome.tabs.move(moveTabIds['d'], {"windowId": secondWindowId, + "index": 2}, pass(function(tabD) { + chrome.test.assertEq(2, tabD.index); + checkMoveResults(); + })); + })); + })); + }, + + function remove() { + chrome.tabs.remove(moveTabIds["d"], pass(function() { + chrome.tabs.getAllInWindow(secondWindowId, + pass(function(tabs) { + assertEq(2, tabs.length); + assertEq(pageUrl("b"), tabs[0].url); + assertEq("chrome://newtab/", tabs[1].url); + })); + })); + }, + + /* + // TODO(jcampan): http://crbug.com/30662 the detection language library + // crashes on some sites and has been temporarily disabled. + function detectLanguage() { + chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) { + chrome.tabs.detectLanguage(tabs[0].id, pass(function(lang) { + assertEq("en", lang); + })); + })); + }, + */ + + function tabsOnCreated() { + chrome.test.listenOnce(chrome.tabs.onCreated, function(tab) { + assertEq(pageUrl("f"), tab.url); + // TODO(jstritar): http://crbug.com/59194 tab.selected is always false + // when passed into chrome.tabs.onCreated listener + //assertEq(true, tab.selected); + }); + + chrome.tabs.create({"windowId": firstWindowId, "url": pageUrl("f"), + "selected": true}, pass(function(tab) {})); + }, + + function tabsOnUpdatedIgnoreTabArg() { + // A third argument was added to the onUpdated event callback. + // Test that an event handler which ignores this argument works. + var onUpdatedCompleted = chrome.test.listenForever(chrome.tabs.onUpdated, + function(tabid, changeInfo) { + if (tabid == moveTabIds['a'] && changeInfo.status == "complete") { + onUpdatedCompleted(); + } + } + ); + + chrome.tabs.update(moveTabIds['a'], {"url": pageUrl("f")}, pass()); + }, + + function tabsOnUpdated() { + var onUpdatedCompleted = chrome.test.listenForever( + chrome.tabs.onUpdated, + function(tabid, changeInfo, tab) { + // |tab| contains the id of the tab it describes. + // Test that |tabid| matches this id. + assertEq(tabid, tab.id); + + // If |changeInfo| has a status property, than + // it should match the status of the tab in |tab|. + if (changeInfo.status) { + assertEq(changeInfo.status, tab.status); + } + + if (tabid == moveTabIds['a'] && changeInfo.status == "complete") { + onUpdatedCompleted(); + } + } + ); + + chrome.tabs.update(moveTabIds['a'], {"url": pageUrl("f")}, pass()); + }, + + function tabsOnMoved() { + chrome.test.listenOnce(chrome.tabs.onMoved, function(tabid, info) { + assertEq(moveTabIds['a'], tabid); + }); + + chrome.tabs.move(moveTabIds['a'], {"index": 0}, pass()); + }, + + function tabsOnSelectionChanged() { + chrome.test.listenOnce(chrome.tabs.onSelectionChanged, + function(tabid, info) { + assertEq(moveTabIds['c'], tabid); + } + ); + + chrome.tabs.update(moveTabIds['c'], {"selected": true}, + pass()); + }, + + function setupTabsOnAttachDetach() + { + setupWindow(["", ""], pass(function(winId, tabIds) { + firstWindowId = winId; + testTabId = tabIds[1]; + createWindow([""], {}, pass(function(winId, tabIds) { + secondWindowId = winId; + })); + })); + }, + + function tabsOnAttached() { + function moveAndListen(tabId, properties, callback) { + chrome.test.listenOnce(chrome.tabs.onAttached, + function(testTabId, info) { + // Ensure notification is correct. + assertEq(testTabId, tabId); + assertEq(properties.windowId, info.newWindowId); + assertEq(properties.index, info.newPosition); + if (callback) + callback(); + }); + chrome.tabs.move(tabId, properties); + }; + + // Move tab to second window, then back to first. + // The original tab/window configuration should be restored. + // tabsOnDetached() depends on it. + moveAndListen(testTabId, {"windowId": secondWindowId, "index": 0}, + pass(function() { + moveAndListen(testTabId, {"windowId": firstWindowId, "index": 1}); + })); + }, + + function tabsOnDetached() { + function moveAndListen(tabId, oldWindowId, oldIndex, properties, + callback) { + chrome.test.listenOnce(chrome.tabs.onDetached, + function(detachedTabId, info) { + // Ensure notification is correct. + assertEq(detachedTabId, tabId); + assertEq(oldWindowId, info.oldWindowId); + assertEq(oldIndex, info.oldPosition); + if (callback) + callback(); + }); + chrome.tabs.move(tabId, properties); + }; + + // Move tab to second window, then back to first. + moveAndListen(testTabId, firstWindowId, 1, + {"windowId": secondWindowId, "index": 0}, pass(function() { + moveAndListen(testTabId, secondWindowId, 0, + {"windowId": firstWindowId, "index": 1}); + })); + }, + + function setupRelativeUrlTests() { + setupWindow(["about:blank"], pass(function(winId, tabIds) { + firstWindowId = winId; + })); + }, + + function relativeUrlTestsTabsCreate() { + // Will be called from relative.html + window.relativePageLoaded = chrome.test.callbackAdded(); + var createCompleted = chrome.test.callbackAdded(); + + chrome.tabs.create({windowId: firstWindowId, url: 'relative.html'}, + function(tab){ + testTabId = tab.id; + createCompleted(); + } + ); + }, + + function relativeUrlTestsTabsUpdate() { + // Will be called from relative.html + window.relativePageLoaded = chrome.test.callbackAdded(); + + chrome.tabs.update(testTabId, {url: pageUrl("a")}, function(tab) { + chrome.test.assertEq(pageUrl("a"), tab.url); + chrome.tabs.update(tab.id, {url: "relative.html"}, function(tab) { + }); + }); + }, + + function relativeUrlTestsWindowCreate() { + // Will be called from relative.html + window.relativePageLoaded = chrome.test.callbackAdded(); + + chrome.windows.create({url: "relative.html"}); + }, + + function windowCreate() { + chrome.windows.create({type: "popup"}, pass(function(window) { + assertEq("popup", window.type); + assertTrue(!window.incognito); + })); + chrome.windows.create({incognito: true}, pass(function(window) { + // This extension is not incognito-enabled, so it shouldn't be able to + // see the incognito window. + assertEq(null, window); + })); + }, + + function windowsOnCreated() { + chrome.test.listenOnce(chrome.windows.onCreated, function(window) { + assertTrue(window.width > 0); + assertTrue(window.height > 0); + assertEq("normal", window.type); + assertTrue(!window.incognito); + windowEventsWindow = window; + chrome.tabs.getAllInWindow(window.id, pass(function(tabs) { + assertEq(pageUrl("a"), tabs[0].url); + })); + }); + + chrome.windows.create({"url": pageUrl("a")}, pass(function(tab) {})); + }, + + /* Disabled -- see http://bugs.chromium.org/58229. + function windowSetFocused() { + chrome.windows.getCurrent(function(oldWin) { + chrome.windows.create({}, function(newWin) { + assertTrue(newWin.focused); + chrome.windows.update(oldWin.id, {focused:true}); + chrome.windows.get(oldWin.id, pass(function(oldWin2) { + assertTrue(oldWin2.focused); + })); + }); + }); + }, + */ + + /* TODO: Enable this test when crbug.com/28055 is fixed. This bug causes a + newly created window not to be set as the current window, if + Chrome was not the foreground window when the create call was made. + function windowsOnFocusChanged() { + chrome.windows.create({}, pass(function(window) { + chrome.test.listenOnce(chrome.windows.onFocusChanged, + function(windowId) { + assertEq(windowEventsWindow.id, windowId); + }); + chrome.windows.remove(window.id); + })); + } */ +]); |