1 // Copyright 2013 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.
5 var pages = [pageUrl('a'), pageUrl('b'), pageUrl('c')];
6 var firstWindowTabIds = [];
7 var recentlyClosedSecondWindowTabIds = [];
8 var recentlyClosedTabIds = [];
9 var recentlyClosedWindowIds = [];
12 var callbackPass = chrome.test.callbackPass;
13 var callbackFail = chrome.test.callbackFail;
14 var assertEq = chrome.test.assertEq;
15 var assertFalse = chrome.test.assertFalse;
16 var assertTrue = chrome.test.assertTrue;
18 function pageUrl(letter) {
19 return chrome.extension.getURL(letter + ".html");
22 // Creates one window with tabs set to the urls in the array |tabUrls|.
23 // At least one url must be specified.
24 // The |callback| should look like function(windowId, tabIds) {...}.
25 function createWindow(tabUrls, callback) {
26 chrome.windows.create({url: tabUrls}, function(win) {
28 win.tabs.forEach(function(tab) {
29 newTabIds.push(tab.id);
31 callback(win.id, newTabIds);
35 function callForEach(fn, calls, eachCallback, doneCallback) {
40 fn.call(null, calls[0], callbackPass(function() {
41 eachCallback.apply(null, arguments);
42 callForEach(fn, calls.slice(1), eachCallback, doneCallback);
46 function checkEntries(expectedEntries, actualEntries) {
47 assertEq(expectedEntries.length, actualEntries.length);
48 expectedEntries.forEach(function(expected, i) {
49 var actual = actualEntries[i];
51 assertTrue(actual.hasOwnProperty('tab'));
52 assertFalse(actual.hasOwnProperty('window'));
53 assertEq(expected.tab.url, actual.tab.url);
55 assertTrue(actual.hasOwnProperty('window'));
56 assertFalse(actual.hasOwnProperty('tab'));
57 assertEq(expected.window.tabsLength, actual.window.tabs.length);
62 chrome.test.runTests([
69 // After retrieveClosedTabs:
76 // After retrieveClosedWindows:
79 // ClosedList: Window2,Window3,a,b
80 function setupWindows() {
89 function each(winId, tabIds) {
90 windowIds.push(winId);
93 chrome.tabs.getAllInWindow(windowIds[0], callbackPass(function(tabs) {
94 assertEq(pages.length, tabs.length);
95 tabs.forEach(function(tab) {
96 firstWindowTabIds.push(tab.id);
99 chrome.windows.getAll({"populate": true},
100 callbackPass(function(win) {
101 assertEq(callArgs.length + 1, win.length);
108 function retrieveClosedTabs() {
109 // Check that the recently closed list contains what we expect
110 // after removing tabs.
113 firstWindowTabIds.slice(0, 2).reverse(),
117 chrome.sessions.getRecentlyClosed(
119 callbackPass(function(entries) {
120 var expectedEntries = [
121 { tab: { url: pages[0] } },
122 { tab: { url: pages[1] } }
124 checkEntries(expectedEntries, entries);
125 entries.forEach(function(entry) {
126 recentlyClosedTabIds.push(entry.tab.sessionId);
134 function retrieveClosedWindows() {
135 // Check that the recently closed list contains what we expect
136 // after removing windows.
138 chrome.windows.remove,
139 windowIds.slice(1, 3).reverse(),
143 chrome.sessions.getRecentlyClosed(
145 callbackPass(function(entries) {
146 var expectedEntries = [
147 { window: { tabsLength: 2 } },
148 { window: { tabsLength: 2 } }
150 checkEntries(expectedEntries, entries);
151 entries[0].window.tabs.forEach(function(tab) {
152 recentlyClosedSecondWindowTabIds.push(tab.sessionId);
154 entries.forEach(function(entry) {
155 recentlyClosedWindowIds.push(entry.window.sessionId);
163 function retrieveClosedEntries() {
164 // Check that the recently closed list contains what we expect
165 // after removing tabs and windows.
166 chrome.sessions.getRecentlyClosed(
167 callbackPass(function(entries) {
168 var expectedEntries = [
169 { window: { tabsLength: 2 } },
170 { window: { tabsLength: 2 } },
171 { tab: { url: pages[0] } },
172 { tab: { url: pages[1] } }
174 checkEntries(expectedEntries, entries);
175 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
181 function retrieveMaxEntries() {
182 // Check that the recently closed list contains what we expect
183 // after removing tabs and windows.
184 chrome.sessions.getRecentlyClosed({maxResults: 25},
185 callbackPass(function(entries) {
186 var expectedEntries = [
187 { window: { tabsLength: 2 } },
188 { window: { tabsLength: 2 } },
189 { tab: { url: pages[0] } },
190 { tab: { url: pages[1] } }
192 checkEntries(expectedEntries, entries);
193 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
199 function restoreClosedTabs() {
200 chrome.windows.get(windowIds[0], {"populate": true},
201 callbackPass(function(win) {
202 var tabCountBeforeRestore = win.tabs.length;
203 chrome.sessions.restore(recentlyClosedTabIds[0], function(tab_session) {
204 assertEq(pages[0], tab_session.tab.url);
206 chrome.sessions.restore(recentlyClosedTabIds[1], function(tab_session) {
207 assertEq(pages[1], tab_session.tab.url);
209 chrome.windows.get(windowIds[0], {"populate": true},
210 callbackPass(function(win){
211 assertEq(tabCountBeforeRestore + 2, win.tabs.length);
212 win.tabs.forEach(function(tab, i) {
213 assertEq(pages[i++], tab.url);
221 function restoreTabInClosedWindow() {
222 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
223 var windowCountBeforeRestore = win.length;
224 chrome.sessions.restore(recentlyClosedSecondWindowTabIds[0],
225 callbackPass(function(tab_session) {
226 assertEq(pages[0], tab_session.tab.url);
227 chrome.windows.getAll({"populate": true},
228 callbackPass(function(win) {
229 assertEq(windowCountBeforeRestore + 1, win.length);
230 assertEq(1, win[win.length - 1].tabs.length);
231 assertEq(pages[0], win[win.length - 1].tabs[0].url);
239 function restoreClosedWindows() {
240 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
241 var windowCountBeforeRestore = win.length;
242 chrome.sessions.restore(recentlyClosedWindowIds[0],
243 function(win_session) {
244 assertEq(1, win_session.window.tabs.length);
247 chrome.windows.getAll({"populate": true},
248 callbackPass(function(win) {
249 assertEq(windowCountBeforeRestore + 1, win.length);
256 function restoreSameEntryTwice() {
257 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
258 var windowCountBeforeRestore = win.length;
259 var id = recentlyClosedWindowIds[0];
260 chrome.sessions.restore(id,
261 callbackFail("Invalid session id: \"" + id + "\".", function() {
262 chrome.windows.getAll({"populate": true},
263 callbackPass(function(win) {
264 assertEq(windowCountBeforeRestore, win.length);
272 function restoreInvalidEntries() {
273 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
274 var windowCountBeforeRestore = win.length;
275 chrome.sessions.restore("-1",
276 callbackFail("Invalid session id: \"-1\".", function() {
277 chrome.windows.getAll({"populate": true},
278 callbackPass(function(win) {
279 assertEq(windowCountBeforeRestore, win.length);
287 function restoreMostRecentEntry() {
288 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
289 var windowCountBeforeRestore = win.length;
290 chrome.sessions.restore(callbackPass(function(win_session) {
291 assertEq(2, win_session.window.tabs.length);
292 chrome.windows.getAll({"populate": true},
293 callbackPass(function(win) {
294 assertEq(windowCountBeforeRestore + 1, win.length);
301 function checkRecentlyClosedListEmpty() {
302 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
303 var windowCountBeforeRestore = win.length;
304 chrome.sessions.restore(
305 callbackFail("There are no recently closed sessions.", function() {
306 chrome.windows.getAll({"populate": true},
307 callbackPass(function(win) {
308 assertEq(windowCountBeforeRestore, win.length);