summaryrefslogtreecommitdiffstats
path: root/docshell/test/browser/browser_bug670318.js
blob: 06c2a10f79f2a3e601e6ae777853e9a9037dc792 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Test for Bug 670318
 *
 * When LoadEntry() is called on a browser that has multiple duplicate history
 * entries, history.index can end up out of range (>= history.count).
 */

const URL =
  "http://mochi.test:8888/browser/docshell/test/browser/file_bug670318.html";

add_task(async function test() {
  await BrowserTestUtils.withNewTab(
    { gBrowser, url: "about:blank" },
    async function (browser) {
      if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
        await ContentTask.spawn(browser, URL, async function (URL) {
          let history = docShell.QueryInterface(
            Ci.nsIWebNavigation
          ).sessionHistory;
          let count = 0;

          let testDone = {};
          testDone.promise = new Promise(resolve => {
            testDone.resolve = resolve;
          });

          // Since listener implements nsISupportsWeakReference, we are
          // responsible for keeping it alive so that the GC doesn't clear
          // it before the test completes. We do this by anchoring the listener
          // to the message manager, and clearing it just before the test
          // completes.
          this._testListener = {
            owner: this,
            OnHistoryNewEntry(aNewURI) {
              info("OnHistoryNewEntry " + aNewURI.spec + ", " + count);
              if (aNewURI.spec == URL && 5 == ++count) {
                addEventListener(
                  "load",
                  function onLoad() {
                    Assert.ok(
                      history.index < history.count,
                      "history.index is valid"
                    );
                    testDone.resolve();
                  },
                  { capture: true, once: true }
                );

                history.legacySHistory.removeSHistoryListener(
                  this.owner._testListener
                );
                delete this.owner._testListener;
                this.owner = null;
                content.setTimeout(() => {
                  content.location.reload();
                }, 0);
              }
            },

            OnHistoryReload: () => true,
            OnHistoryGotoIndex: () => {},
            OnHistoryPurge: () => {},
            OnHistoryReplaceEntry: () => {
              // The initial load of about:blank causes a transient entry to be
              // created, so our first navigation to a real page is a replace
              // instead of a new entry.
              ++count;
            },

            QueryInterface: ChromeUtils.generateQI([
              "nsISHistoryListener",
              "nsISupportsWeakReference",
            ]),
          };

          history.legacySHistory.addSHistoryListener(this._testListener);
          content.location = URL;

          await testDone.promise;
        });

        return;
      }

      let history = browser.browsingContext.sessionHistory;
      let count = 0;

      let testDone = {};
      testDone.promise = new Promise(resolve => {
        testDone.resolve = resolve;
      });

      let listener = {
        async OnHistoryNewEntry(aNewURI) {
          if (aNewURI.spec == URL && 5 == ++count) {
            history.removeSHistoryListener(listener);
            await ContentTask.spawn(browser, null, () => {
              return new Promise(resolve => {
                addEventListener(
                  "load",
                  evt => {
                    let history = docShell.QueryInterface(
                      Ci.nsIWebNavigation
                    ).sessionHistory;
                    Assert.ok(
                      history.index < history.count,
                      "history.index is valid"
                    );
                    resolve();
                  },
                  { capture: true, once: true }
                );

                content.location.reload();
              });
            });
            testDone.resolve();
          }
        },

        OnHistoryReload: () => true,
        OnHistoryGotoIndex: () => {},
        OnHistoryPurge: () => {},
        OnHistoryReplaceEntry: () => {
          // The initial load of about:blank causes a transient entry to be
          // created, so our first navigation to a real page is a replace
          // instead of a new entry.
          ++count;
        },

        QueryInterface: ChromeUtils.generateQI([
          "nsISHistoryListener",
          "nsISupportsWeakReference",
        ]),
      };

      history.addSHistoryListener(listener);
      BrowserTestUtils.loadURIString(browser, URL);

      await testDone.promise;
    }
  );
});