Upstream version 11.40.277.0
[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 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;
66
67   var callbackCount = 0;
68   var done = chrome.test.listenForever(chrome.sessions.onChanged, function() {
69       callbackCount++;
70     }
71   );
72
73   return function() {
74     var retry = 0;
75     var checkEvent = function() {
76       if (callbackCount < expectedCallbackCount) {
77         retry++;
78         if (retry % 10 == 0)
79           console.log("Waiting for " +
80                       (expectedCallbackCount - callbackCount) +
81                       " more onChanged events");
82         window.setTimeout(checkEvent, retryPeriod);
83       } else {
84         assertEq(callbackCount, expectedCallbackCount);
85         done();
86       }
87     };
88     window.setTimeout(checkEvent, retryPeriod);
89   };
90 }
91
92 chrome.test.runTests([
93   // After setupWindows
94   //
95   //  Window1: a,b,c
96   //  Window2: a,b
97   //  Window3: a,b
98   //
99   // After retrieveClosedTabs:
100   //
101   //  Window1: c
102   //  Window2: a,b
103   //  Window3: a,b
104   //  ClosedList: a,b
105   //
106   // After retrieveClosedWindows:
107   //
108   //  Window1: c
109   //  ClosedList: Window2,Window3,a,b
110   function setupWindows() {
111     var callArgs = [
112       pages,
113       pages.slice(0, 2),
114       pages.slice(0, 2)
115     ];
116     callForEach(
117       createWindow,
118       callArgs,
119       function each(winId, tabIds) {
120         windowIds.push(winId);
121       },
122       function done() {
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);
127           });
128         }));
129         chrome.windows.getAll({"populate": true},
130           callbackPass(function(win) {
131             assertEq(callArgs.length + 1, win.length);
132           })
133         );
134       }
135     );
136   },
137
138   function retrieveClosedTabs() {
139     // Check that the recently closed list contains what we expect
140     // after removing tabs.
141     var checkEvent = checkOnChangedEvent(2);
142
143     callForEach(
144       chrome.tabs.remove,
145       firstWindowTabIds.slice(0, 2).reverse(),
146       function each() {
147       },
148       function done() {
149         chrome.sessions.getRecentlyClosed(
150           {maxResults: 2},
151           callbackPass(function(entries) {
152             var expectedEntries = [
153               { tab: { url: pages[0] } },
154               { tab: { url: pages[1] } }
155             ];
156             checkEntries(expectedEntries, entries);
157             entries.forEach(function(entry) {
158               recentlyClosedTabIds.push(entry.tab.sessionId);
159             });
160             checkEvent();
161           })
162         );
163       }
164     );
165   },
166
167   function retrieveClosedWindows() {
168     // Check that the recently closed list contains what we expect
169     // after removing windows.
170     var checkEvent = checkOnChangedEvent(2);
171
172     callForEach(
173       chrome.windows.remove,
174       windowIds.slice(1, 3).reverse(),
175       function each() {
176       },
177       function done() {
178         chrome.sessions.getRecentlyClosed(
179           {maxResults: 2},
180           callbackPass(function(entries) {
181             var expectedEntries = [
182               { window: { tabsLength: 2 } },
183               { window: { tabsLength: 2 } }
184             ];
185             checkEntries(expectedEntries, entries);
186             entries[0].window.tabs.forEach(function(tab) {
187               recentlyClosedSecondWindowTabIds.push(tab.sessionId);
188             });
189             entries.forEach(function(entry) {
190               recentlyClosedWindowIds.push(entry.window.sessionId);
191             });
192             checkEvent();
193           })
194         );
195       }
196     );
197   },
198
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] } }
209         ];
210         checkEntries(expectedEntries, entries);
211         assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
212           entries.length);
213       })
214     );
215   },
216
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] } }
227         ];
228         checkEntries(expectedEntries, entries);
229         assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length,
230           entries.length);
231       })
232     );
233   },
234
235   function restoreClosedTabs() {
236     var checkEvent = checkOnChangedEvent(2);
237
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);
243         });
244         chrome.sessions.restore(recentlyClosedTabIds[1], function(tab_session) {
245           assertEq(pages[1], tab_session.tab.url);
246         });
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);
252             });
253             checkEvent();
254           })
255         );
256       })
257     );
258   },
259
260   function restoreTabInClosedWindow() {
261     var checkEvent = checkOnChangedEvent(1);
262
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);
273               checkEvent();
274             })
275           );
276         })
277       );
278     }));
279   },
280
281   function restoreClosedWindows() {
282     var checkEvent = checkOnChangedEvent(1);
283
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);
289             checkEvent();
290           });
291       function done() {
292         chrome.windows.getAll({"populate": true},
293           callbackPass(function(win) {
294             assertEq(windowCountBeforeRestore + 1, win.length);
295           })
296         );
297       }
298     }));
299   },
300
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);
310             })
311           );
312         })
313       );
314     }));
315   },
316
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);
325             })
326           );
327         })
328       );
329     }));
330   },
331
332   function restoreMostRecentEntry() {
333     var checkEvent = checkOnChangedEvent(1);
334
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);
342             checkEvent();
343           })
344         );
345       }));
346     }));
347   },
348
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);
357             })
358           );
359         })
360       );
361     }));
362   }
363 ]);