1 // Copyright (c) 2012 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.
9 function clickLink(id) {
10 var clickEvent = document.createEvent('MouseEvents');
11 clickEvent.initMouseEvent('click', true, true, window);
12 document.querySelector('#' + id).dispatchEvent(clickEvent);
15 chrome.test.runTests([
17 function setupTwoWindows() {
18 createWindow(["about:blank", "chrome://newtab/", pageUrl("a")], {},
19 pass(function(winId, tabIds) {
20 secondWindowId = winId;
21 testTabId = tabIds[2];
23 createWindow(["chrome://newtab/", pageUrl("b")], {},
24 pass(function(winId, tabIds) {
25 thirdWindowId = winId;
30 function getAllInWindow() {
31 chrome.tabs.getAllInWindow(secondWindowId,
33 assertEq(3, tabs.length);
34 for (var i = 0; i < tabs.length; i++) {
35 assertEq(secondWindowId, tabs[i].windowId);
36 assertEq(i, tabs[i].index);
38 // The first tab should be active
39 assertEq((i == 0), tabs[i].active && tabs[i].selected);
41 assertEq("about:blank", tabs[0].url);
42 assertEq("chrome://newtab/", tabs[1].url);
43 assertEq(pageUrl("a"), tabs[2].url);
46 chrome.tabs.getAllInWindow(thirdWindowId,
48 assertEq(2, tabs.length);
49 for (var i = 0; i < tabs.length; i++) {
50 assertEq(thirdWindowId, tabs[i].windowId);
51 assertEq(i, tabs[i].index);
53 assertEq("chrome://newtab/", tabs[0].url);
54 assertEq(pageUrl("b"), tabs[1].url);
58 function updateSelect() {
59 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) {
60 assertEq(true, tabs[0].active && tabs[0].selected);
61 assertEq(false, tabs[1].active || tabs[1].selected);
62 assertEq(false, tabs[2].active || tabs[2].selected);
65 chrome.tabs.update(tabs[1].id, {active: true},
67 // Check update of tab[1].
68 chrome.test.assertEq(true, tab1.active);
69 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) {
70 assertEq(true, tabs[1].active && tabs[1].selected);
71 assertEq(false, tabs[2].active || tabs[2].selected);
73 chrome.tabs.update(tabs[2].id,
76 // Check update of tab[2].
77 chrome.test.assertEq(true, tab2.active);
78 chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) {
79 assertEq(false, tabs[1].active || tabs[1].selected);
80 assertEq(true, tabs[2].active && tabs[2].selected);
89 chrome.tabs.get(testTabId, pass(function(tab) {
90 assertEq(pageUrl("a"), tab.url);
92 chrome.tabs.update(testTabId, {"url": pageUrl("c")},
94 chrome.test.assertEq(pageUrl("c"), tab.url);
96 chrome.tabs.get(testTabId, pass(function(tab) {
97 assertEq(pageUrl("c"), tab.url);
103 function openerTabId() {
104 chrome.test.listenOnce(
105 chrome.tabs.onCreated,
107 chrome.tabs.getCurrent(pass(function(thisTab) {
108 assertEq(thisTab.id, tab.openerTabId);
111 // Pretend to click a link (openers aren't tracked when using tabs.create).
112 clickLink("test_link");
115 // The window on chrome.tabs.create is ignored if it doesn't accept tabs.
116 function testRedirectingToAnotherWindow() {
117 chrome.windows.create(
118 {url: 'about:blank', type: 'popup'},
119 pass(function(window) {
120 assertFalse(window.tabs[0].id == chrome.tabs.TAB_ID_NONE);
122 {url: 'about:blank', windowId: window.id},
124 assertTrue(window.id != tab.windowId);
129 // Creation of a tab in an empty non-tabbed window should be allowed.
130 function testOpenWindowInEmptyPopup() {
131 chrome.windows.create(
133 pass(function(window) {
135 {url: 'about:blank', windowId: window.id},
137 assertEq(window.id, tab.windowId);
142 // An empty popup window does not contain any tabs and the number of tabs
143 // before and after creation should be the same.
144 function testOpenEmptyPopup() {
145 chrome.tabs.query({}, pass(function(tabs) {
146 var tabsCountBefore = tabs.length;
147 chrome.windows.create({type: 'popup'}, pass(function(window) {
148 assertEq(window.tabs.length, 0);
149 chrome.tabs.query({}, pass(function(tabs) {
150 assertEq(tabsCountBefore, tabs.length);
156 function testCreatePopupAndMoveTab() {
157 // An existing tab can be moved into a created empty popup.
158 chrome.tabs.create({url: 'about:blank'}, pass(function(tab) {
159 chrome.windows.create({type: 'popup', tabId: tab.id},
160 pass(function(window) {
161 assertEq(window.tabs.length, 1);
162 chrome.tabs.get(tab.id, pass(function(updatedTabInfo) {
163 assertEq(window.id, updatedTabInfo.windowId);
168 // An existing tab cannot be moved into a created non-empty popup.
169 chrome.tabs.create({url: 'about:blank'}, pass(function(tab) {
170 chrome.windows.create({type: 'popup', url: 'about:blank', tabId: tab.id},
171 pass(function(window) {
172 assertEq(window.tabs.length, 1);
173 chrome.tabs.get(tab.id, pass(function(updatedTabInfo) {
174 assertEq(tab.windowId, updatedTabInfo.windowId);
175 assertTrue(window.id != updatedTabInfo.windowId);