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
) {
121 {url
: 'about:blank', windowId
: window
.id
},
123 assertTrue(window
.id
!= tab
.windowId
);
128 // Creation of a tab in an empty non-tabbed window should be allowed.
129 function testOpenWindowInEmptyPopup() {
130 chrome
.windows
.create(
132 pass(function(window
) {
134 {url
: 'about:blank', windowId
: window
.id
},
136 assertEq(window
.id
, tab
.windowId
);
141 // An empty popup window does not contain any tabs and the number of tabs
142 // before and after creation should be the same.
143 function testOpenEmptyPopup() {
144 chrome
.tabs
.query({}, pass(function(tabs
) {
145 var tabsCountBefore
= tabs
.length
;
146 chrome
.windows
.create({type
: 'popup'}, pass(function(window
) {
147 assertEq(window
.tabs
.length
, 0);
148 chrome
.tabs
.query({}, pass(function(tabs
) {
149 assertEq(tabsCountBefore
, tabs
.length
);
155 function testCreatePopupAndMoveTab() {
156 // An existing tab can be moved into a created empty popup.
157 chrome
.tabs
.create({url
: 'about:blank'}, pass(function(tab
) {
158 chrome
.windows
.create({type
: 'popup', tabId
: tab
.id
},
159 pass(function(window
) {
160 assertEq(window
.tabs
.length
, 1);
161 chrome
.tabs
.get(tab
.id
, pass(function(updatedTabInfo
) {
162 assertEq(window
.id
, updatedTabInfo
.windowId
);
167 // An existing tab cannot be moved into a created non-empty popup.
168 chrome
.tabs
.create({url
: 'about:blank'}, pass(function(tab
) {
169 chrome
.windows
.create({type
: 'popup', url
: 'about:blank', tabId
: tab
.id
},
170 pass(function(window
) {
171 assertEq(window
.tabs
.length
, 1);
172 chrome
.tabs
.get(tab
.id
, pass(function(updatedTabInfo
) {
173 assertEq(tab
.windowId
, updatedTabInfo
.windowId
);
174 assertTrue(window
.id
!= updatedTabInfo
.windowId
);