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.

     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/ */
     5 var ScratchpadManager = Scratchpad.ScratchpadManager;
     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 = [];
    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 }
    25 function test()
    26 {
    27   waitForExplicitFinish();
    28   testRestore();
    29 }
    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   ];
    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();
    56     // Then get their states
    57     let session = ScratchpadManager.getSessionState();
    59     // Then close them
    60     wins.forEach(function(win) {
    61       win.close();
    62     });
    64     // Clear out session state for next tests
    65     ScratchpadManager.saveOpenWindows();
    67     // Then restore them
    68     let restoredWins = ScratchpadManager.restoreSession(session);
    70     is(restoredWins.length, 3, "Three scratchad windows restored");
    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");
    83       // Yay, we're done!
    84       finish();
    85     });
    86   });
    87 }
    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