OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var pages = [pageUrl('a'), pageUrl('b'), pageUrl('c')]; | 5 var pages = [pageUrl('a'), pageUrl('b'), pageUrl('c')]; |
6 var firstWindowTabIds = []; | 6 var firstWindowTabIds = []; |
7 var recentlyClosedSecondWindowTabIds = []; | 7 var recentlyClosedSecondWindowTabIds = []; |
8 var recentlyClosedTabIds = []; | 8 var recentlyClosedTabIds = []; |
9 var recentlyClosedWindowIds = []; | 9 var recentlyClosedWindowIds = []; |
10 var windowIds = []; | 10 var windowIds = []; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 assertFalse(actual.hasOwnProperty('window')); | 52 assertFalse(actual.hasOwnProperty('window')); |
53 assertEq(expected.tab.url, actual.tab.url); | 53 assertEq(expected.tab.url, actual.tab.url); |
54 } else { | 54 } else { |
55 assertTrue(actual.hasOwnProperty('window')); | 55 assertTrue(actual.hasOwnProperty('window')); |
56 assertFalse(actual.hasOwnProperty('tab')); | 56 assertFalse(actual.hasOwnProperty('tab')); |
57 assertEq(expected.window.tabsLength, actual.window.tabs.length); | 57 assertEq(expected.window.tabsLength, actual.window.tabs.length); |
58 } | 58 } |
59 }); | 59 }); |
60 } | 60 } |
61 | 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 |
62 chrome.test.runTests([ | 92 chrome.test.runTests([ |
63 // After setupWindows | 93 // After setupWindows |
64 // | 94 // |
65 // Window1: a,b,c | 95 // Window1: a,b,c |
66 // Window2: a,b | 96 // Window2: a,b |
67 // Window3: a,b | 97 // Window3: a,b |
68 // | 98 // |
69 // After retrieveClosedTabs: | 99 // After retrieveClosedTabs: |
70 // | 100 // |
71 // Window1: c | 101 // Window1: c |
(...skipping 29 matching lines...) Expand all Loading... |
101 assertEq(callArgs.length + 1, win.length); | 131 assertEq(callArgs.length + 1, win.length); |
102 }) | 132 }) |
103 ); | 133 ); |
104 } | 134 } |
105 ); | 135 ); |
106 }, | 136 }, |
107 | 137 |
108 function retrieveClosedTabs() { | 138 function retrieveClosedTabs() { |
109 // Check that the recently closed list contains what we expect | 139 // Check that the recently closed list contains what we expect |
110 // after removing tabs. | 140 // after removing tabs. |
| 141 var checkEvent = checkOnChangedEvent(2); |
| 142 |
111 callForEach( | 143 callForEach( |
112 chrome.tabs.remove, | 144 chrome.tabs.remove, |
113 firstWindowTabIds.slice(0, 2).reverse(), | 145 firstWindowTabIds.slice(0, 2).reverse(), |
114 function each() { | 146 function each() { |
115 }, | 147 }, |
116 function done() { | 148 function done() { |
117 chrome.sessions.getRecentlyClosed( | 149 chrome.sessions.getRecentlyClosed( |
118 {maxResults: 2}, | 150 {maxResults: 2}, |
119 callbackPass(function(entries) { | 151 callbackPass(function(entries) { |
120 var expectedEntries = [ | 152 var expectedEntries = [ |
121 { tab: { url: pages[0] } }, | 153 { tab: { url: pages[0] } }, |
122 { tab: { url: pages[1] } } | 154 { tab: { url: pages[1] } } |
123 ]; | 155 ]; |
124 checkEntries(expectedEntries, entries); | 156 checkEntries(expectedEntries, entries); |
125 entries.forEach(function(entry) { | 157 entries.forEach(function(entry) { |
126 recentlyClosedTabIds.push(entry.tab.sessionId); | 158 recentlyClosedTabIds.push(entry.tab.sessionId); |
127 }); | 159 }); |
| 160 checkEvent(); |
128 }) | 161 }) |
129 ); | 162 ); |
130 } | 163 } |
131 ); | 164 ); |
132 }, | 165 }, |
133 | 166 |
134 function retrieveClosedWindows() { | 167 function retrieveClosedWindows() { |
135 // Check that the recently closed list contains what we expect | 168 // Check that the recently closed list contains what we expect |
136 // after removing windows. | 169 // after removing windows. |
| 170 var checkEvent = checkOnChangedEvent(2); |
| 171 |
137 callForEach( | 172 callForEach( |
138 chrome.windows.remove, | 173 chrome.windows.remove, |
139 windowIds.slice(1, 3).reverse(), | 174 windowIds.slice(1, 3).reverse(), |
140 function each() { | 175 function each() { |
141 }, | 176 }, |
142 function done() { | 177 function done() { |
143 chrome.sessions.getRecentlyClosed( | 178 chrome.sessions.getRecentlyClosed( |
144 {maxResults: 2}, | 179 {maxResults: 2}, |
145 callbackPass(function(entries) { | 180 callbackPass(function(entries) { |
146 var expectedEntries = [ | 181 var expectedEntries = [ |
147 { window: { tabsLength: 2 } }, | 182 { window: { tabsLength: 2 } }, |
148 { window: { tabsLength: 2 } } | 183 { window: { tabsLength: 2 } } |
149 ]; | 184 ]; |
150 checkEntries(expectedEntries, entries); | 185 checkEntries(expectedEntries, entries); |
151 entries[0].window.tabs.forEach(function(tab) { | 186 entries[0].window.tabs.forEach(function(tab) { |
152 recentlyClosedSecondWindowTabIds.push(tab.sessionId); | 187 recentlyClosedSecondWindowTabIds.push(tab.sessionId); |
153 }); | 188 }); |
154 entries.forEach(function(entry) { | 189 entries.forEach(function(entry) { |
155 recentlyClosedWindowIds.push(entry.window.sessionId); | 190 recentlyClosedWindowIds.push(entry.window.sessionId); |
156 }); | 191 }); |
| 192 checkEvent(); |
157 }) | 193 }) |
158 ); | 194 ); |
159 } | 195 } |
160 ); | 196 ); |
161 }, | 197 }, |
162 | 198 |
163 function retrieveClosedEntries() { | 199 function retrieveClosedEntries() { |
164 // Check that the recently closed list contains what we expect | 200 // Check that the recently closed list contains what we expect |
165 // after removing tabs and windows. | 201 // after removing tabs and windows. |
166 chrome.sessions.getRecentlyClosed( | 202 chrome.sessions.getRecentlyClosed( |
(...skipping 23 matching lines...) Expand all Loading... |
190 { tab: { url: pages[1] } } | 226 { tab: { url: pages[1] } } |
191 ]; | 227 ]; |
192 checkEntries(expectedEntries, entries); | 228 checkEntries(expectedEntries, entries); |
193 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length, | 229 assertEq(recentlyClosedTabIds.length + recentlyClosedWindowIds.length, |
194 entries.length); | 230 entries.length); |
195 }) | 231 }) |
196 ); | 232 ); |
197 }, | 233 }, |
198 | 234 |
199 function restoreClosedTabs() { | 235 function restoreClosedTabs() { |
| 236 var checkEvent = checkOnChangedEvent(2); |
| 237 |
200 chrome.windows.get(windowIds[0], {"populate": true}, | 238 chrome.windows.get(windowIds[0], {"populate": true}, |
201 callbackPass(function(win) { | 239 callbackPass(function(win) { |
202 var tabCountBeforeRestore = win.tabs.length; | 240 var tabCountBeforeRestore = win.tabs.length; |
203 chrome.sessions.restore(recentlyClosedTabIds[0], function(tab_session) { | 241 chrome.sessions.restore(recentlyClosedTabIds[0], function(tab_session) { |
204 assertEq(pages[0], tab_session.tab.url); | 242 assertEq(pages[0], tab_session.tab.url); |
205 }); | 243 }); |
206 chrome.sessions.restore(recentlyClosedTabIds[1], function(tab_session) { | 244 chrome.sessions.restore(recentlyClosedTabIds[1], function(tab_session) { |
207 assertEq(pages[1], tab_session.tab.url); | 245 assertEq(pages[1], tab_session.tab.url); |
208 }); | 246 }); |
209 chrome.windows.get(windowIds[0], {"populate": true}, | 247 chrome.windows.get(windowIds[0], {"populate": true}, |
210 callbackPass(function(win){ | 248 callbackPass(function(win){ |
211 assertEq(tabCountBeforeRestore + 2, win.tabs.length); | 249 assertEq(tabCountBeforeRestore + 2, win.tabs.length); |
212 win.tabs.forEach(function(tab, i) { | 250 win.tabs.forEach(function(tab, i) { |
213 assertEq(pages[i++], tab.url); | 251 assertEq(pages[i++], tab.url); |
214 }); | 252 }); |
| 253 checkEvent(); |
215 }) | 254 }) |
216 ); | 255 ); |
217 }) | 256 }) |
218 ); | 257 ); |
219 }, | 258 }, |
220 | 259 |
221 function restoreTabInClosedWindow() { | 260 function restoreTabInClosedWindow() { |
| 261 var checkEvent = checkOnChangedEvent(1); |
| 262 |
222 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { | 263 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { |
223 var windowCountBeforeRestore = win.length; | 264 var windowCountBeforeRestore = win.length; |
224 chrome.sessions.restore(recentlyClosedSecondWindowTabIds[0], | 265 chrome.sessions.restore(recentlyClosedSecondWindowTabIds[0], |
225 callbackPass(function(tab_session) { | 266 callbackPass(function(tab_session) { |
226 assertEq(pages[0], tab_session.tab.url); | 267 assertEq(pages[0], tab_session.tab.url); |
227 chrome.windows.getAll({"populate": true}, | 268 chrome.windows.getAll({"populate": true}, |
228 callbackPass(function(win) { | 269 callbackPass(function(win) { |
229 assertEq(windowCountBeforeRestore + 1, win.length); | 270 assertEq(windowCountBeforeRestore + 1, win.length); |
230 assertEq(1, win[win.length - 1].tabs.length); | 271 assertEq(1, win[win.length - 1].tabs.length); |
231 assertEq(pages[0], win[win.length - 1].tabs[0].url); | 272 assertEq(pages[0], win[win.length - 1].tabs[0].url); |
| 273 checkEvent(); |
232 }) | 274 }) |
233 ); | 275 ); |
234 }) | 276 }) |
235 ); | 277 ); |
236 })); | 278 })); |
237 }, | 279 }, |
238 | 280 |
239 function restoreClosedWindows() { | 281 function restoreClosedWindows() { |
| 282 var checkEvent = checkOnChangedEvent(1); |
| 283 |
240 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { | 284 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { |
241 var windowCountBeforeRestore = win.length; | 285 var windowCountBeforeRestore = win.length; |
242 chrome.sessions.restore(recentlyClosedWindowIds[0], | 286 chrome.sessions.restore(recentlyClosedWindowIds[0], |
243 function(win_session) { | 287 function(win_session) { |
244 assertEq(1, win_session.window.tabs.length); | 288 assertEq(1, win_session.window.tabs.length); |
| 289 checkEvent(); |
245 }); | 290 }); |
246 function done() { | 291 function done() { |
247 chrome.windows.getAll({"populate": true}, | 292 chrome.windows.getAll({"populate": true}, |
248 callbackPass(function(win) { | 293 callbackPass(function(win) { |
249 assertEq(windowCountBeforeRestore + 1, win.length); | 294 assertEq(windowCountBeforeRestore + 1, win.length); |
250 }) | 295 }) |
251 ); | 296 ); |
252 } | 297 } |
253 })); | 298 })); |
254 }, | 299 }, |
(...skipping 23 matching lines...) Expand all Loading... |
278 callbackPass(function(win) { | 323 callbackPass(function(win) { |
279 assertEq(windowCountBeforeRestore, win.length); | 324 assertEq(windowCountBeforeRestore, win.length); |
280 }) | 325 }) |
281 ); | 326 ); |
282 }) | 327 }) |
283 ); | 328 ); |
284 })); | 329 })); |
285 }, | 330 }, |
286 | 331 |
287 function restoreMostRecentEntry() { | 332 function restoreMostRecentEntry() { |
| 333 var checkEvent = checkOnChangedEvent(1); |
| 334 |
288 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { | 335 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { |
289 var windowCountBeforeRestore = win.length; | 336 var windowCountBeforeRestore = win.length; |
290 chrome.sessions.restore(callbackPass(function(win_session) { | 337 chrome.sessions.restore(callbackPass(function(win_session) { |
291 assertEq(2, win_session.window.tabs.length); | 338 assertEq(2, win_session.window.tabs.length); |
292 chrome.windows.getAll({"populate": true}, | 339 chrome.windows.getAll({"populate": true}, |
293 callbackPass(function(win) { | 340 callbackPass(function(win) { |
294 assertEq(windowCountBeforeRestore + 1, win.length); | 341 assertEq(windowCountBeforeRestore + 1, win.length); |
| 342 checkEvent(); |
295 }) | 343 }) |
296 ); | 344 ); |
297 })); | 345 })); |
298 })); | 346 })); |
299 }, | 347 }, |
300 | 348 |
301 function checkRecentlyClosedListEmpty() { | 349 function checkRecentlyClosedListEmpty() { |
302 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { | 350 chrome.windows.getAll({"populate": true}, callbackPass(function(win) { |
303 var windowCountBeforeRestore = win.length; | 351 var windowCountBeforeRestore = win.length; |
304 chrome.sessions.restore( | 352 chrome.sessions.restore( |
305 callbackFail("There are no recently closed sessions.", function() { | 353 callbackFail("There are no recently closed sessions.", function() { |
306 chrome.windows.getAll({"populate": true}, | 354 chrome.windows.getAll({"populate": true}, |
307 callbackPass(function(win) { | 355 callbackPass(function(win) { |
308 assertEq(windowCountBeforeRestore, win.length); | 356 assertEq(windowCountBeforeRestore, win.length); |
309 }) | 357 }) |
310 ); | 358 ); |
311 }) | 359 }) |
312 ); | 360 ); |
313 })); | 361 })); |
314 } | 362 } |
315 ]); | 363 ]); |
OLD | NEW |