browser/devtools/scratchpad/test/browser_scratchpad_restore.js

branch
TOR_BUG_3246
changeset 6
8bccb770b82d
equal deleted inserted replaced
-1:000000000000 0:fcbbbf74ae5f
1 /* vim: set ts=2 et sw=2 tw=80: */
2 /* Any copyright is dedicated to the Public Domain.
3 http://creativecommons.org/publicdomain/zero/1.0/ */
4
5 var ScratchpadManager = Scratchpad.ScratchpadManager;
6
7 /* Call the iterator for each item in the list,
8 calling the final callback with all the results
9 after every iterator call has sent its result */
10 function asyncMap(items, iterator, callback)
11 {
12 let expected = items.length;
13 let results = [];
14
15 items.forEach(function(item) {
16 iterator(item, function(result) {
17 results.push(result);
18 if (results.length == expected) {
19 callback(results);
20 }
21 });
22 });
23 }
24
25 function test()
26 {
27 waitForExplicitFinish();
28 testRestore();
29 }
30
31 function testRestore()
32 {
33 let states = [
34 {
35 filename: "testfile",
36 text: "test1",
37 executionContext: 2
38 },
39 {
40 text: "text2",
41 executionContext: 1
42 },
43 {
44 text: "text3",
45 executionContext: 1
46 }
47 ];
48
49 asyncMap(states, function(state, done) {
50 // Open some scratchpad windows
51 openScratchpad(done, {state: state, noFocus: true});
52 }, function(wins) {
53 // Then save the windows to session store
54 ScratchpadManager.saveOpenWindows();
55
56 // Then get their states
57 let session = ScratchpadManager.getSessionState();
58
59 // Then close them
60 wins.forEach(function(win) {
61 win.close();
62 });
63
64 // Clear out session state for next tests
65 ScratchpadManager.saveOpenWindows();
66
67 // Then restore them
68 let restoredWins = ScratchpadManager.restoreSession(session);
69
70 is(restoredWins.length, 3, "Three scratchad windows restored");
71
72 asyncMap(restoredWins, function(restoredWin, done) {
73 openScratchpad(function(aWin) {
74 let state = aWin.Scratchpad.getState();
75 aWin.close();
76 done(state);
77 }, {window: restoredWin, noFocus: true});
78 }, function(restoredStates) {
79 // Then make sure they were restored with the right states
80 ok(statesMatch(restoredStates, states),
81 "All scratchpad window states restored correctly");
82
83 // Yay, we're done!
84 finish();
85 });
86 });
87 }
88
89 function statesMatch(restoredStates, states)
90 {
91 return states.every(function(state) {
92 return restoredStates.some(function(restoredState) {
93 return state.filename == restoredState.filename
94 && state.text == restoredState.text
95 && state.executionContext == restoredState.executionContext;
96 })
97 });
98 }

mercurial