browser/devtools/scratchpad/test/browser_scratchpad_restore.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/devtools/scratchpad/test/browser_scratchpad_restore.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* vim: set ts=2 et sw=2 tw=80: */
     1.5 +/* Any copyright is dedicated to the Public Domain.
     1.6 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.7 +
     1.8 +var ScratchpadManager = Scratchpad.ScratchpadManager;
     1.9 +
    1.10 +/* Call the iterator for each item in the list,
    1.11 +   calling the final callback with all the results
    1.12 +   after every iterator call has sent its result */
    1.13 +function asyncMap(items, iterator, callback)
    1.14 +{
    1.15 +  let expected = items.length;
    1.16 +  let results = [];
    1.17 +
    1.18 +  items.forEach(function(item) {
    1.19 +    iterator(item, function(result) {
    1.20 +      results.push(result);
    1.21 +      if (results.length == expected) {
    1.22 +        callback(results);
    1.23 +      }
    1.24 +    });
    1.25 +  });
    1.26 +}
    1.27 +
    1.28 +function test()
    1.29 +{
    1.30 +  waitForExplicitFinish();
    1.31 +  testRestore();
    1.32 +}
    1.33 +
    1.34 +function testRestore()
    1.35 +{
    1.36 +  let states = [
    1.37 +    {
    1.38 +      filename: "testfile",
    1.39 +      text: "test1",
    1.40 +      executionContext: 2
    1.41 +    },
    1.42 +    {
    1.43 +      text: "text2",
    1.44 +      executionContext: 1
    1.45 +    },
    1.46 +    {
    1.47 +      text: "text3",
    1.48 +      executionContext: 1
    1.49 +    }
    1.50 +  ];
    1.51 +
    1.52 +  asyncMap(states, function(state, done) {
    1.53 +    // Open some scratchpad windows
    1.54 +    openScratchpad(done, {state: state, noFocus: true});
    1.55 +  }, function(wins) {
    1.56 +    // Then save the windows to session store
    1.57 +    ScratchpadManager.saveOpenWindows();
    1.58 +
    1.59 +    // Then get their states
    1.60 +    let session = ScratchpadManager.getSessionState();
    1.61 +
    1.62 +    // Then close them
    1.63 +    wins.forEach(function(win) {
    1.64 +      win.close();
    1.65 +    });
    1.66 +
    1.67 +    // Clear out session state for next tests
    1.68 +    ScratchpadManager.saveOpenWindows();
    1.69 +
    1.70 +    // Then restore them
    1.71 +    let restoredWins = ScratchpadManager.restoreSession(session);
    1.72 +
    1.73 +    is(restoredWins.length, 3, "Three scratchad windows restored");
    1.74 +
    1.75 +    asyncMap(restoredWins, function(restoredWin, done) {
    1.76 +      openScratchpad(function(aWin) {
    1.77 +        let state = aWin.Scratchpad.getState();
    1.78 +        aWin.close();
    1.79 +        done(state);
    1.80 +      }, {window: restoredWin, noFocus: true});
    1.81 +    }, function(restoredStates) {
    1.82 +      // Then make sure they were restored with the right states
    1.83 +      ok(statesMatch(restoredStates, states),
    1.84 +        "All scratchpad window states restored correctly");
    1.85 +
    1.86 +      // Yay, we're done!
    1.87 +      finish();
    1.88 +    });
    1.89 +  });
    1.90 +}
    1.91 +
    1.92 +function statesMatch(restoredStates, states)
    1.93 +{
    1.94 +  return states.every(function(state) {
    1.95 +    return restoredStates.some(function(restoredState) {
    1.96 +      return state.filename == restoredState.filename
    1.97 +        && state.text == restoredState.text
    1.98 +        && state.executionContext == restoredState.executionContext;
    1.99 +    })
   1.100 +  });
   1.101 +}

mercurial