summaryrefslogtreecommitdiffstats
path: root/devtools/client/scratchpad/test/browser_scratchpad_restore.js
blob: 5890e954f356df2c27ea730f7775465609d639eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/* Call the iterator for each item in the list,
   calling the final callback with all the results
   after every iterator call has sent its result */
function asyncMap(items, iterator, callback)
{
  let expected = items.length;
  let results = [];

  items.forEach(function (item) {
    iterator(item, function (result) {
      results.push(result);
      if (results.length == expected) {
        callback(results);
      }
    });
  });
}

function test()
{
  waitForExplicitFinish();
  testRestore();
}

function testRestore()
{
  let states = [
    {
      filename: "testfile",
      text: "test1",
      executionContext: 2
    },
    {
      text: "text2",
      executionContext: 1
    },
    {
      text: "text3",
      executionContext: 1
    }
  ];

  asyncMap(states, function (state, done) {
    // Open some scratchpad windows
    openScratchpad(done, {state: state, noFocus: true});
  }, function (wins) {
    // Then save the windows to session store
    ScratchpadManager.saveOpenWindows();

    // Then get their states
    let session = ScratchpadManager.getSessionState();

    // Then close them
    wins.forEach(function (win) {
      win.close();
    });

    // Clear out session state for next tests
    ScratchpadManager.saveOpenWindows();

    // Then restore them
    let restoredWins = ScratchpadManager.restoreSession(session);

    is(restoredWins.length, 3, "Three scratchad windows restored");

    asyncMap(restoredWins, function (restoredWin, done) {
      openScratchpad(function (aWin) {
        let state = aWin.Scratchpad.getState();
        aWin.close();
        done(state);
      }, {window: restoredWin, noFocus: true});
    }, function (restoredStates) {
      // Then make sure they were restored with the right states
      ok(statesMatch(restoredStates, states),
        "All scratchpad window states restored correctly");

      // Yay, we're done!
      finish();
    });
  });
}

function statesMatch(restoredStates, states)
{
  return states.every(function (state) {
    return restoredStates.some(function (restoredState) {
      return state.filename == restoredState.filename
        && state.text == restoredState.text
        && state.executionContext == restoredState.executionContext;
    });
  });
}