Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: chrome/test/data/extensions/api_test/session_restore/session_restore.js

Issue 21022018: Sessions API - previously Session Restore API. Supports restoring currently open foreign windows an… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: delete test. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 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.
4
5 var pages = [pageUrl('a'), pageUrl('b'), pageUrl('c')];
6 var firstWindowTabIds = [];
7 var recentlyClosedSecondWindowTabIds = [];
8 var recentlyClosedTabIds = [];
9 var recentlyClosedWindowIds = [];
10 var windowIds = [];
11
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;
17
18 function pageUrl(letter) {
19 return chrome.extension.getURL(letter + ".html");
20 }
21
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) {
27 var newTabIds = [];
28 win.tabs.forEach(function(tab) {
29 newTabIds.push(tab.id);
30 });
31 callback(win.id, newTabIds);
32 });
33 }
34
35 function callForEach(fn, calls, eachCallback, doneCallback) {
36 if (!calls.length) {
37 doneCallback();
38 return;
39 }
40 fn.call(null, calls[0], callbackPass(function() {
41 eachCallback.apply(null, arguments);
42 callForEach(fn, calls.slice(1), eachCallback, doneCallback);
43 }));
44 }
45
46 function checkEntries(expectedEntries, actualEntries) {
47 assertEq(expectedEntries.length, actualEntries.length);
48 expectedEntries.forEach(function(expected, i) {
49 var actual = actualEntries[i];
50 if (expected.tab) {
51 assertTrue(actual.hasOwnProperty('tab'));
52 assertFalse(actual.hasOwnProperty('window'));
53 assertEq(expected.tab.url, actual.tab.url);
54 } else {
55 assertTrue(actual.hasOwnProperty('window'));
56 assertFalse(actual.hasOwnProperty('tab'));
57 assertEq(expected.window.tabsLength, actual.window.tabs.length);
58 }
59 });
60 }
61
62 chrome.test.runTests([
63 // After setupWindows
64 //
65 // Window1: a,b,c
66 // Window2: a,b
67 // Window3: a,b
68 //
69 // After retriveClosedTabs:
70 //
71 // Window1: c
72 // Window2: a,b
73 // Window3: a,b
74 // ClosedList: a,b
75 //
76 // After retriveClosedWindows:
77 //
78 // Window1: c
79 // ClosedList: Window2,Window3,a,b
80 function setupWindows() {
81 var callArgs = [
82 pages,
83 pages.slice(0, 2),
84 pages.slice(0, 2)
85 ];
86 callForEach(
87 createWindow,
88 callArgs,
89 function each(winId, tabIds) {
90 windowIds.push(winId);
91 },
92 function done() {
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);
97 });
98 }));
99 chrome.windows.getAll({"populate": true},
100 callbackPass(function(win) {
101 assertEq(callArgs.length + 1, win.length);
102 })
103 );
104 }
105 );
106 },
107
108 function retrieveClosedTabs() {
109 // Check that the recently closed list contains what we expect
110 // after removing tabs.
111 callForEach(
112 chrome.tabs.remove,
113 firstWindowTabIds.slice(0, 2).reverse(),
114 function each() {
115 },
116 function done() {
117 chrome.sessionRestore.getRecentlyClosed(
118 {maxResults: 2, entryType: "tab"},
119 callbackPass(function(entries) {
120 var expectedEntries = [
121 { tab: { url: pages[0] } },
122 { tab: { url: pages[1] } }
123 ];
124 checkEntries(expectedEntries, entries);
125 entries.forEach(function(entry) {
126 recentlyClosedTabIds.push(entry.id);
127 });
128 })
129 );
130 }
131 );
132 },
133
134 function retrieveClosedWindows() {
135 // Check that the recently closed list contains what we expect
136 // after removing windows.
137 callForEach(
138 chrome.windows.remove,
139 windowIds.slice(1, 3).reverse(),
140 function each() {
141 },
142 function done() {
143 chrome.sessionRestore.getRecentlyClosed(
144 {maxResults: 2, entryType: "window"},
145 callbackPass(function(entries) {
146 var expectedEntries = [
147 { window: { tabsLength: 2 } },
148 { window: { tabsLength: 2 } }
149 ];
150 checkEntries(expectedEntries, entries);
151 entries[0].window.tabs.forEach(function(tab) {
152 recentlyClosedSecondWindowTabIds.push(tab.id);
153 });
154 entries.forEach(function(entry) {
155 recentlyClosedWindowIds.push(entry.id);
156 });
157 })
158 );
159 }
160 );
161 },
162
163 function retrieveClosedEntries() {
164 // Check that the recently closed list contains what we expect
165 // after removing tabs and windows.
166 chrome.sessionRestore.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] } }
173 ];
174 checkEntries(expectedEntries, entries);
175 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
176 entries.length);
177 })
178 );
179 },
180
181 function retrieveMaxEntries() {
182 // Check that the recently closed list contains what we expect
183 // after removing tabs and windows.
184 chrome.sessionRestore.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] } }
191 ];
192 checkEntries(expectedEntries, entries);
193 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
194 entries.length);
195 })
196 );
197 },
198
199 function restoreClosedTabs() {
200 chrome.windows.get(windowIds[0], {"populate": true},
201 callbackPass(function(win) {
202 var tabCountBeforeRestore = win.tabs.length;
203 callForEach(
204 chrome.sessionRestore.restore,
205 recentlyClosedTabIds.slice(0, 2),
206 function each() {
207 },
208 function done() {
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);
214 });
215 })
216 );
217 }
218 );
219 })
220 );
221 },
222
223 function restoreTabInClosedWindow() {
224 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
225 var windowCountBeforeRestore = win.length;
226 chrome.sessionRestore.restore(recentlyClosedSecondWindowTabIds[0],
227 callbackPass(function() {
228 chrome.windows.getAll({"populate": true},
229 callbackPass(function(win) {
230 assertEq(windowCountBeforeRestore + 1, win.length);
231 assertEq(1, win[win.length - 1].tabs.length);
232 assertEq(pages[0], win[win.length - 1].tabs[0].url);
233 })
234 );
235 })
236 );
237 }));
238 },
239
240 function restoreClosedWindows() {
241 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
242 var windowCountBeforeRestore = win.length;
243 callForEach(
244 chrome.sessionRestore.restore,
245 recentlyClosedWindowIds.slice(0, 1),
246 function each() {
247 },
248 function done() {
249 chrome.windows.getAll({"populate": true},
250 callbackPass(function(win) {
251 assertEq(windowCountBeforeRestore + 1, win.length);
252 })
253 );
254 }
255 );
256 }));
257 },
258
259 function restoreSameEntryTwice() {
260 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
261 var windowCountBeforeRestore = win.length;
262 chrome.sessionRestore.restore(recentlyClosedWindowIds[0],
263 callbackFail("Invalid session id.", function() {
264 chrome.windows.getAll({"populate": true},
265 callbackPass(function(win) {
266 assertEq(windowCountBeforeRestore, win.length);
267 })
268 );
269 })
270 );
271 }));
272 },
273
274 function restoreInvalidEntries() {
275 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
276 var windowCountBeforeRestore = win.length;
277 chrome.sessionRestore.restore(-1,
278 callbackFail("Invalid session id.", function() {
279 chrome.windows.getAll({"populate": true},
280 callbackPass(function(win) {
281 assertEq(windowCountBeforeRestore, win.length);
282 })
283 );
284 })
285 );
286 }));
287 },
288
289 function restoreMostRecentEntry() {
290 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
291 var windowCountBeforeRestore = win.length;
292 chrome.sessionRestore.restore(callbackPass(function() {
293 chrome.windows.getAll({"populate": true},
294 callbackPass(function(win) {
295 assertEq(windowCountBeforeRestore + 1, win.length);
296 })
297 );
298 }));
299 }));
300 },
301
302 function checkRecentlyClosedListEmpty() {
303 chrome.windows.getAll({"populate": true}, callbackPass(function(win) {
304 var windowCountBeforeRestore = win.length;
305 chrome.sessionRestore.restore(
306 callbackFail("There are no recently closed sessions.", function() {
307 chrome.windows.getAll({"populate": true},
308 callbackPass(function(win) {
309 assertEq(windowCountBeforeRestore, win.length);
310 })
311 );
312 })
313 );
314 }));
315 }
316
317 ]);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698