- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / sessions / sessions.js
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.
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 retrieveClosedTabs:
70   //
71   //  Window1: c
72   //  Window2: a,b
73   //  Window3: a,b
74   //  ClosedList: a,b
75   //
76   // After retrieveClosedWindows:
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.sessions.getRecentlyClosed(
118           {maxResults: 2},
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.tab.sessionId);
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.sessions.getRecentlyClosed(
144           {maxResults: 2},
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.sessionId);
153             });
154             entries.forEach(function(entry) {
155               recentlyClosedWindowIds.push(entry.window.sessionId);
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.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] } }
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.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] } }
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         chrome.sessions.restore(recentlyClosedTabIds[0], function(tab_session) {
204           assertEq(pages[0], tab_session.tab.url);
205         });
206         chrome.sessions.restore(recentlyClosedTabIds[1], function(tab_session) {
207           assertEq(pages[1], tab_session.tab.url);
208         });
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   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);
232             })
233           );
234         })
235       );
236     }));
237   },
238
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);
245           });
246       function done() {
247         chrome.windows.getAll({"populate": true},
248           callbackPass(function(win) {
249             assertEq(windowCountBeforeRestore + 1, win.length);
250           })
251         );
252       }
253     }));
254   },
255
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);
265             })
266           );
267         })
268       );
269     }));
270   },
271
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);
280             })
281           );
282         })
283       );
284     }));
285   },
286
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);
295           })
296         );
297       }));
298     }));
299   },
300
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);
309             })
310           );
311         })
312       );
313     }));
314   }
315 ]);