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 function checkOnChangedEvent(expectedCallbackCount) {
63 // The frequency in ms between checking whether the right events have
64 // fired. Every 10 attempts progress is logged.
65 var retryPeriod = 100;
67 var callbackCount = 0;
68 var done = chrome.test.listenForever(chrome.sessions.onChanged, function() {
75 var checkEvent = function() {
76 if (callbackCount < expectedCallbackCount) {
79 console.log("Waiting for " +
80 (expectedCallbackCount - callbackCount) +
81 " more onChanged events");
82 window.setTimeout(checkEvent, retryPeriod);
84 assertEq(callbackCount, expectedCallbackCount);
88 window.setTimeout(checkEvent, retryPeriod);
92 chrome.test.runTests([
99 // After retrieveClosedTabs:
106 // After retrieveClosedWindows:
109 // ClosedList: Window2,Window3,a,b
110 function setupWindows() {
119 function each(winId, tabIds) {
120 windowIds.push(winId);
123 chrome.tabs.getAllInWindow(windowIds[0], callbackPass(function(tabs) {
124 assertEq(pages.length, tabs.length);
125 tabs.forEach(function(tab) {
126 firstWindowTabIds.push(tab.id);
129 chrome.windows.getAll({"populate": true},
130 callbackPass(function(win) {
131 assertEq(callArgs.length + 1, win.length);
138 function retrieveClosedTabs() {
139 // Check that the recently closed list contains what we expect
140 // after removing tabs.
141 var checkEvent = checkOnChangedEvent(2);
145 firstWindowTabIds.slice(0, 2).reverse(),
149 chrome.sessions.getRecentlyClosed(
151 callbackPass(function(entries) {
152 var expectedEntries = [
153 { tab: { url: pages[0] } },
154 { tab: { url: pages[1] } }
156 checkEntries(expectedEntries, entries);
157 entries.forEach(function(entry) {
158 recentlyClosedTabIds.push(entry.tab.sessionId);
167 function retrieveClosedWindows() {
168 // Check that the recently closed list contains what we expect
169 // after removing windows.
170 var checkEvent = checkOnChangedEvent(2);
173 chrome.windows.remove,
174 windowIds.slice(1, 3).reverse(),
178 chrome.sessions.getRecentlyClosed(
180 callbackPass(function(entries) {
181 var expectedEntries = [
182 { window: { tabsLength: 2 } },
183 { window: { tabsLength: 2 } }
185 checkEntries(expectedEntries, entries);
186 entries[0].window.tabs.forEach(function(tab) {
187 recentlyClosedSecondWindowTabIds.push(tab.sessionId);
189 entries.forEach(function(entry) {
190 recentlyClosedWindowIds.push(entry.window.sessionId);
199 function retrieveClosedEntries() {
200 // Check that the recently closed list contains what we expect
201 // after removing tabs and windows.
202 chrome.sessions.getRecentlyClosed(
203 callbackPass(function(entries) {
204 var expectedEntries = [
205 { window: { tabsLength: 2 } },
206 { window: { tabsLength: 2 } },
207 { tab: { url: pages[0] } },
208 { tab: { url: pages[1] } }
210 checkEntries(expectedEntries, entries);
211 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
217 function retrieveMaxEntries() {
218 // Check that the recently closed list contains what we expect
219 // after removing tabs and windows.
220 chrome.sessions.getRecentlyClosed({maxResults: 25},
221 callbackPass(function(entries) {
222 var expectedEntries = [
223 { window: { tabsLength: 2 } },
224 { window: { tabsLength: 2 } },
225 { tab: { url: pages[0] } },
226 { tab: { url: pages[1] } }
228 checkEntries(expectedEntries, entries);
229 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
235 function restoreClosedTabs() {
236 var checkEvent = checkOnChangedEvent(2);
238 chrome.windows.get(windowIds[0], {"populate": true},
239 callbackPass(function(win) {
240 var tabCountBeforeRestore = win.tabs.length;
241 chrome.sessions.restore(recentlyClosedTabIds[0], function(tab_session) {
242 assertEq(pages[0], tab_session.tab.url);
244 chrome.sessions.restore(recentlyClosedTabIds[1], function(tab_session) {
245 assertEq(pages[1], tab_session.tab.url);
247 chrome.windows.get(windowIds[0], {"populate": true},
248 callbackPass(function(win){
249 assertEq(tabCountBeforeRestore + 2, win.tabs.length);
250 win.tabs.forEach(function(tab, i) {
251 assertEq(pages[i++], tab.url);
260 function restoreTabInClosedWindow() {
261 var checkEvent = checkOnChangedEvent(1);
263 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
264 var windowCountBeforeRestore = win.length;
265 chrome.sessions.restore(recentlyClosedSecondWindowTabIds[0],
266 callbackPass(function(tab_session) {
267 assertEq(pages[0], tab_session.tab.url);
268 chrome.windows.getAll({"populate": true},
269 callbackPass(function(win) {
270 assertEq(windowCountBeforeRestore + 1, win.length);
271 assertEq(1, win[win.length - 1].tabs.length);
272 assertEq(pages[0], win[win.length - 1].tabs[0].url);
281 function restoreClosedWindows() {
282 var checkEvent = checkOnChangedEvent(1);
284 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
285 var windowCountBeforeRestore = win.length;
286 chrome.sessions.restore(recentlyClosedWindowIds[0],
287 function(win_session) {
288 assertEq(1, win_session.window.tabs.length);
292 chrome.windows.getAll({"populate": true},
293 callbackPass(function(win) {
294 assertEq(windowCountBeforeRestore + 1, win.length);
301 function restoreSameEntryTwice() {
302 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
303 var windowCountBeforeRestore = win.length;
304 var id = recentlyClosedWindowIds[0];
305 chrome.sessions.restore(id,
306 callbackFail("Invalid session id: \"" + id + "\".", function() {
307 chrome.windows.getAll({"populate": true},
308 callbackPass(function(win) {
309 assertEq(windowCountBeforeRestore, win.length);
317 function restoreInvalidEntries() {
318 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
319 var windowCountBeforeRestore = win.length;
320 chrome.sessions.restore("-1",
321 callbackFail("Invalid session id: \"-1\".", function() {
322 chrome.windows.getAll({"populate": true},
323 callbackPass(function(win) {
324 assertEq(windowCountBeforeRestore, win.length);
332 function restoreMostRecentEntry() {
333 var checkEvent = checkOnChangedEvent(1);
335 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
336 var windowCountBeforeRestore = win.length;
337 chrome.sessions.restore(callbackPass(function(win_session) {
338 assertEq(2, win_session.window.tabs.length);
339 chrome.windows.getAll({"populate": true},
340 callbackPass(function(win) {
341 assertEq(windowCountBeforeRestore + 1, win.length);
349 function checkRecentlyClosedListEmpty() {
350 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
351 var windowCountBeforeRestore = win.length;
352 chrome.sessions.restore(
353 callbackFail("There are no recently closed sessions.", function() {
354 chrome.windows.getAll({"populate": true},
355 callbackPass(function(win) {
356 assertEq(windowCountBeforeRestore, win.length);