browser/devtools/scratchpad/test/browser_scratchpad_restore.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial