summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_248970_b_perwindowpb.js
blob: f5775cd5b3b2c7f5a4bcb97ae8eaf2473a11f8a0 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

function test() {
  /** Test (B) for Bug 248970 **/
  waitForExplicitFinish();

  let windowsToClose = [];
  let file = Services.dirsvc.get("TmpD", Ci.nsIFile);
  let filePath = file.path;
  let fieldList = {
    "//input[@name='input']":     Date.now().toString(),
    "//input[@name='spaced 1']":  Math.random().toString(),
    "//input[3]":                 "three",
    "//input[@type='checkbox']":  true,
    "//input[@name='uncheck']":   false,
    "//input[@type='radio'][1]":  false,
    "//input[@type='radio'][2]":  true,
    "//input[@type='radio'][3]":  false,
    "//select":                   2,
    "//select[@multiple]":        [1, 3],
    "//textarea[1]":              "",
    "//textarea[2]":              "Some text... " + Math.random(),
    "//textarea[3]":              "Some more text\n" + new Date(),
    "//input[@type='file']":      filePath
  };

  registerCleanupFunction(function* () {
    for (let win of windowsToClose) {
      yield BrowserTestUtils.closeWindow(win);
    }
  });

  function test(aLambda) {
    try {
      return aLambda() || true;
    } catch(ex) { }
    return false;
  }

  function getElementByXPath(aTab, aQuery) {
    let doc = aTab.linkedBrowser.contentDocument;
    let xptype = Ci.nsIDOMXPathResult.FIRST_ORDERED_NODE_TYPE;
    return doc.evaluate(aQuery, doc, null, xptype, null).singleNodeValue;
  }

  function setFormValue(aTab, aQuery, aValue) {
    let node = getElementByXPath(aTab, aQuery);
    if (typeof aValue == "string")
      node.value = aValue;
    else if (typeof aValue == "boolean")
      node.checked = aValue;
    else if (typeof aValue == "number")
      node.selectedIndex = aValue;
    else
      Array.forEach(node.options, (aOpt, aIx) =>
        (aOpt.selected = aValue.indexOf(aIx) > -1));
  }

  function compareFormValue(aTab, aQuery, aValue) {
    let node = getElementByXPath(aTab, aQuery);
    if (!node)
      return false;
    if (node instanceof Ci.nsIDOMHTMLInputElement)
      return aValue == (node.type == "checkbox" || node.type == "radio" ?
                       node.checked : node.value);
    if (node instanceof Ci.nsIDOMHTMLTextAreaElement)
      return aValue == node.value;
    if (!node.multiple)
      return aValue == node.selectedIndex;
    return Array.every(node.options, (aOpt, aIx) =>
            (aValue.indexOf(aIx) > -1) == aOpt.selected);
  }

  //////////////////////////////////////////////////////////////////
  // Test (B) : Session data restoration between windows          //
  //////////////////////////////////////////////////////////////////

  let rootDir = getRootDirectory(gTestPath);
  const testURL = rootDir + "browser_248970_b_sample.html";
  const testURL2 = "http://mochi.test:8888/browser/" +
    "browser/components/sessionstore/test/browser_248970_b_sample.html";

  whenNewWindowLoaded({ private: false }, function(aWin) {
    windowsToClose.push(aWin);

    // get closed tab count
    let count = ss.getClosedTabCount(aWin);
    let max_tabs_undo =
      Services.prefs.getIntPref("browser.sessionstore.max_tabs_undo");
    ok(0 <= count && count <= max_tabs_undo,
      "getClosedTabCount should return zero or at most max_tabs_undo");

    // setup a state for tab (A) so we can check later that is restored
    let key = "key";
    let value = "Value " + Math.random();
    let state = { entries: [{ url: testURL }], extData: { key: value } };

    // public session, add new tab: (A)
    let tab_A = aWin.gBrowser.addTab(testURL);
    ss.setTabState(tab_A, JSON.stringify(state));
    promiseBrowserLoaded(tab_A.linkedBrowser).then(() => {
      // make sure that the next closed tab will increase getClosedTabCount
      Services.prefs.setIntPref(
        "browser.sessionstore.max_tabs_undo", max_tabs_undo + 1)

      // populate tab_A with form data
      for (let i in fieldList)
        setFormValue(tab_A, i, fieldList[i]);

      // public session, close tab: (A)
      aWin.gBrowser.removeTab(tab_A);

      // verify that closedTabCount increased
      ok(ss.getClosedTabCount(aWin) > count,
         "getClosedTabCount has increased after closing a tab");

      // verify tab: (A), in undo list
      let tab_A_restored = test(() => ss.undoCloseTab(aWin, 0));
      ok(tab_A_restored, "a tab is in undo list");
      promiseTabRestored(tab_A_restored).then(() => {
        is(testURL, tab_A_restored.linkedBrowser.currentURI.spec,
           "it's the same tab that we expect");
        aWin.gBrowser.removeTab(tab_A_restored);

        whenNewWindowLoaded({ private: true }, function(aWin) {
          windowsToClose.push(aWin);

          // setup a state for tab (B) so we can check that its duplicated
          // properly
          let key1 = "key1";
          let value1 = "Value " + Math.random();
          let state1 = {
            entries: [{ url: testURL2 }], extData: { key1: value1 }
          };

          let tab_B = aWin.gBrowser.addTab(testURL2);
          promiseTabState(tab_B, state1).then(() => {
            // populate tab: (B) with different form data
            for (let item in fieldList)
              setFormValue(tab_B, item, fieldList[item]);

            // duplicate tab: (B)
            let tab_C = aWin.gBrowser.duplicateTab(tab_B);
            promiseTabRestored(tab_C).then(() => {
              // verify the correctness of the duplicated tab
              is(ss.getTabValue(tab_C, key1), value1,
                "tab successfully duplicated - correct state");

              for (let item in fieldList)
                ok(compareFormValue(tab_C, item, fieldList[item]),
                  "The value for \"" + item + "\" was correctly duplicated");

              // private browsing session, close tab: (C) and (B)
              aWin.gBrowser.removeTab(tab_C);
              aWin.gBrowser.removeTab(tab_B);

              finish();
            });
          });
        });
      });
    });
  });
}