diff options
author | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-01 01:44:03 +0000 |
---|---|---|
committer | asargent@chromium.org <asargent@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-01 01:44:03 +0000 |
commit | 9b6d31d8b6c5f47c7a425e1b07f6477323a669f4 (patch) | |
tree | 8577815a605b6cf370ca4442ad30913c6288c94e /chrome/test | |
parent | 7b6dd0cd5176e27364fa8c5338792b64bbd18073 (diff) | |
download | chromium_src-9b6d31d8b6c5f47c7a425e1b07f6477323a669f4.zip chromium_src-9b6d31d8b6c5f47c7a425e1b07f6477323a669f4.tar.gz chromium_src-9b6d31d8b6c5f47c7a425e1b07f6477323a669f4.tar.bz2 |
Add browser tests for extensions tab API.
BUG=18442
TEST=none
Review URL: http://codereview.chromium.org/174633
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25006 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/test')
5 files changed, 306 insertions, 15 deletions
diff --git a/chrome/test/data/extensions/api_test/bookmarks/test.js b/chrome/test/data/extensions/api_test/bookmarks/test.js index 7189296..4325e18 100644 --- a/chrome/test/data/extensions/api_test/bookmarks/test.js +++ b/chrome/test/data/extensions/api_test/bookmarks/test.js @@ -10,6 +10,8 @@ var expected = [ } ]; +var testCallback = chrome.test.testCallback; + function compareNode(left, right) { //console.log(JSON.stringify(left)); //console.log(JSON.stringify(right)); @@ -52,7 +54,7 @@ function compareTrees(left, right) { chrome.test.runTests([ function getTree() { - chrome.bookmarks.getTree(chrome.test.testFunction(function(results) { + chrome.bookmarks.getTree(testCallback(true, function(results) { chrome.test.assertTrue(compareTrees(results, expected), "getTree() result != expected"); expected = results; @@ -60,14 +62,13 @@ chrome.test.runTests([ }, function get() { - chrome.bookmarks.get("1", chrome.test.testFunction(function(results) { + chrome.bookmarks.get("1", testCallback(true, function(results) { chrome.test.assertTrue(compareNode(results[0], expected[0].children[0])); })); }, function getArray() { - chrome.bookmarks.get(["1", "2"], - chrome.test.testFunction(function(results) { + chrome.bookmarks.get(["1", "2"], testCallback(true, function(results) { chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), "get() result != expected"); chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), @@ -76,8 +77,7 @@ chrome.test.runTests([ }, function getChildren() { - chrome.bookmarks.getChildren("0", - chrome.test.testFunction(function(results) { + chrome.bookmarks.getChildren("0", testCallback(true, function(results) { chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), "getChildren() result != expected"); chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), @@ -87,7 +87,7 @@ chrome.test.runTests([ function create() { var node = {parentId: "1", title:"google", url:"http://www.google.com/"}; - chrome.bookmarks.create(node, chrome.test.testFunction(function(results) { + chrome.bookmarks.create(node, testCallback(true, function(results) { node.id = results.id; // since we couldn't know this going in node.index = 0; chrome.test.assertTrue(compareNode(node, results), diff --git a/chrome/test/data/extensions/api_test/override1/test.js b/chrome/test/data/extensions/api_test/override1/test.js index f70d85da..4f94e08 100644 --- a/chrome/test/data/extensions/api_test/override1/test.js +++ b/chrome/test/data/extensions/api_test/override1/test.js @@ -1,8 +1,8 @@ -chrome.test.runTests([
- function newtab() {
- chrome.tabs.create({"url": "chrome://newtab/"},
- chrome.test.testFunction(function(response) {
- }));
- }
-]);
-
+chrome.test.runTests([ + function newtab() { + chrome.tabs.create({"url": "chrome://newtab/"}, + chrome.test.testCallback(function(response) { + })); + } +]); + diff --git a/chrome/test/data/extensions/api_test/tabs/manifest.json b/chrome/test/data/extensions/api_test/tabs/manifest.json new file mode 100644 index 0000000..0ffa68f --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/manifest.json @@ -0,0 +1,7 @@ +{ + "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/test.html b/chrome/test/data/extensions/api_test/tabs/test.html new file mode 100644 index 0000000..46f4d74 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/test.html @@ -0,0 +1 @@ +<script src="test.js"></script> diff --git a/chrome/test/data/extensions/api_test/tabs/test.js b/chrome/test/data/extensions/api_test/tabs/test.js new file mode 100644 index 0000000..280d1d8 --- /dev/null +++ b/chrome/test/data/extensions/api_test/tabs/test.js @@ -0,0 +1,283 @@ +// 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 moveWindow1 = null; +var moveWindow2 = null; +var moveTabIds = {}; + +var testCallback = chrome.test.testCallback; +var assertEq = chrome.test.assertEq; +var assertTrue = chrome.test.assertTrue; + +chrome.test.runTests([ + function getSelected() { + chrome.tabs.getSelected(null, testCallback(true, function(tab) { + assertEq("about:blank", tab.url); + assertEq("about:blank", tab.title); + firstWindowId = tab.windowId; + firstTabIndex = tab.index; + })); + }, + + function openNewTab() { + // TODO(asargent) Add more tests for the following cases: + // 1) two windows open. create tab in "other" window. + // 2) don't pass windowId. confirm created in "this" window. + // 3) pass index. confirm placed at correct index position. + // 4) pass selected. confirm is selected. + chrome.tabs.create({"windowId" : firstWindowId, "selected" : false}, + testCallback(true, function(tab){ + assertTrue(tab.index > firstTabIndex); + assertEq(firstWindowId, tab.windowId); + assertEq(false, tab.selected); + assertEq("chrome://newtab/", tab.url); + })); + }, + + // Setup a new window for later tests, and open some tabs in the + // first and second windows. + function createWindow() { + // TODO(asargent) Add more tests for: + // 1) window sizing/positioning. + // 2) passed url (relative & absolute) + chrome.windows.create({}, testCallback(false, function(win) { + assertTrue(win.id > 0); + secondWindowId = win.id; + chrome.tabs.create({"windowId" : firstWindowId, "url" : "chrome://a"}, + testCallback(false, null)); + chrome.tabs.create({"windowId" : secondWindowId, "url" : "chrome://b"}, + testCallback(true, null)); + })); + }, + + function getAllFirstWindow() { + // TODO(asargent) Add test for passing null for windowId - this should + // default to the "current" window. + chrome.tabs.getAllInWindow(firstWindowId, + testCallback(true, 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 most recent tab should be selected + assertEq((i == 2), tabs[i].selected); + } + assertEq("about:blank", tabs[0].url); + assertEq("chrome://newtab/", tabs[1].url); + assertEq("chrome://a/", tabs[2].url); + testTabId = tabs[2].id; + })); + }, + + function getAllSecondWindow() { + chrome.tabs.getAllInWindow(secondWindowId, + testCallback(true, 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("chrome://b/", tabs[1].url); + })); + }, + + function update() { + chrome.tabs.update(testTabId, {"selected":true, "url": "chrome://c"}, + testCallback(false, function(){ + chrome.tabs.getSelected(firstWindowId, testCallback(true, function(tab) { + assertEq("chrome://c/", tab.url); + assertEq(true, tab.selected); + })); + })); + }, + + // Create 2 new windows, close existing windows. + function moveTabsSetup1() { + chrome.windows.create({}, testCallback(false, function(win) { + moveWindow1 = win.id; + })); + chrome.windows.create({}, testCallback(false, function(win) { + moveWindow2 = win.id; + })); + chrome.windows.remove(firstWindowId, testCallback(false, null)); + chrome.windows.remove(secondWindowId, testCallback(true, null)); + }, + + // Create a bunch of tabs and record the resulting ids. + function moveTabsSetup2() { + var letters = ['a', 'b', 'c', 'd', 'e']; + for (var i in letters) { + chrome.tabs.create({"windowId": moveWindow1, + "url": "chrome://" + letters[i]}, + testCallback(false, function(tab) { + var letter = tab.url[tab.url.length-2]; + moveTabIds[letter] = tab.id; + if (tab.url == "chrome://e/") { + chrome.test.succeed(); + } + })); + } + }, + + // 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 moveTabs() { + chrome.tabs.move(moveTabIds['b'], {"windowId": moveWindow2, "index": 0}, + testCallback(false, null)); + chrome.tabs.move(moveTabIds['e'], {"index": 2}, + testCallback(false, null)); + chrome.tabs.move(moveTabIds['d'], {"windowId": moveWindow2, "index": 2}, + testCallback(true, null)); + }, + + // Check that the tab/window state is what we expect after doing moves. + function moveTabsCheck() { + chrome.tabs.getAllInWindow(moveWindow1, testCallback(false, function(tabs) { + assertEq(4, tabs.length); + assertEq("chrome://newtab/", tabs[0].url); + assertEq("chrome://a/", tabs[1].url); + assertEq("chrome://e/", tabs[2].url); + assertEq("chrome://c/", tabs[3].url); + })); + chrome.tabs.getAllInWindow(moveWindow2, testCallback(true, function(tabs) { + assertEq(3, tabs.length); + assertEq("chrome://b/", tabs[0].url); + assertEq("chrome://newtab/", tabs[1].url); + assertEq("chrome://d/", tabs[2].url); + })); + }, + + function remove() { + chrome.tabs.remove(moveTabIds["d"], testCallback(false, function() { + chrome.tabs.getAllInWindow(moveWindow2, + testCallback(true, function(tabs) { + assertEq(2, tabs.length); + assertEq("chrome://b/", tabs[0].url); + assertEq("chrome://newtab/", tabs[1].url); + })); + })); + }, + + function detectLanguage() { + chrome.tabs.getAllInWindow(moveWindow1, testCallback(false, function(tabs) { + chrome.tabs.detectLanguage(tabs[0].id, testCallback(true, function(lang) { + assertEq("en", lang); + })); + })); + }, + + function captureVisibleTab() { + // Grab an image for each of our two windows. + var firstImage; + var secondImage; + chrome.tabs.captureVisibleTab(moveWindow1, + testCallback(false, function(url) { + assertEq("string", typeof(url)); + assertTrue(url.length > 0); + firstImage = url; + })); + chrome.tabs.captureVisibleTab(moveWindow2, + testCallback(false, function(url) { + assertEq("string", typeof(url)); + assertTrue(url.length > 0); + secondImage = url; + assertTrue(firstImage != secondImage); + })); + + // Now pass null for windowId - it should come back with something + // equal to either the first or second window. This is nondeterministic + // depending on whether you let chrome stay focused, or click + // focus away (or are running on the try/build servers). + chrome.tabs.captureVisibleTab(null, testCallback(true, function(url) { + assertEq("string", typeof(url)); + assertTrue(url.length > 0); + assertTrue(url == firstImage || url == secondImage); + })); + }, + + function onCreated() { + var callbackSuccess = false; + var listener = function(tab) { + assertEq("chrome://f/", tab.url); + callbackSuccess = true; + }; + chrome.tabs.onCreated.addListener(listener); + chrome.tabs.create({"windowId": moveWindow1, "url": "chrome://f", + "selected": true}, testCallback(true, function(tab) { + chrome.tabs.onCreated.removeListener(listener); + assertTrue(callbackSuccess); + })); + }, + + function onUpdated() { + var listener = function(tabid, info) { + if (tabid != moveTabIds['a']) { + return; // ignore updates for tabs we weren't interested in + } + if (info.status == "complete") { + chrome.tabs.onUpdated.removeListener(listener); + chrome.test.succeed(); + } + }; + chrome.tabs.onUpdated.addListener(listener); + chrome.tabs.update(moveTabIds['a'], {"url": "chrome://aa"}, + testCallback(false, null)); + }, + + function onMoved() { + var listener = function(tabid, info) { + chrome.tabs.onMoved.removeListener(listener); + assertEq(moveTabIds['a'], tabid); + chrome.test.succeed(); + }; + chrome.tabs.onMoved.addListener(listener); + chrome.tabs.move(moveTabIds['a'], {"index": 0}, testCallback(false, null)); + }, + + function onSelectionChanged() { + var listener = function(tabid, info) { + chrome.tabs.onSelectionChanged.removeListener(listener); + assertEq(moveTabIds['c'], tabid); + chrome.test.succeed(); + }; + chrome.tabs.onSelectionChanged.addListener(listener); + chrome.tabs.update(moveTabIds['c'], {"selected": true}, + testCallback(false, null)); + }, + + function onRemoved() { + var listener = function(tabid) { + chrome.tabs.onRemoved.removeListener(listener); + assertEq(moveTabIds['c'], tabid); + chrome.test.succeed(); + }; + chrome.tabs.onRemoved.addListener(listener); + chrome.tabs.remove(moveTabIds['c'], testCallback(false, null)); + } + + // TODO(asargent) We still need to add tests for the following: + // Methods: + // -chrome.tabs.connect + // Events: + // -chrome.tabs.onAttached + // -chrome.tabs.onDetched + // + // Also, it would be an improvement to check the captureVisibleTab results + // against a known-good result. +]); |