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

"use strict";

/**
 * Ensure that history entries that should not be persisted are restored in the
 * same state.
 */
add_task(async function check_history_not_persisted() {
  // Create an about:blank tab
  let tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Retrieve the tab state.
  await TabStateFlusher.flush(browser);
  let state = JSON.parse(ss.getTabState(tab));
  ok(!state.entries[0].persist, "Should have collected the persistence state");
  BrowserTestUtils.removeTab(tab);
  browser = null;

  // Open a new tab to restore into.
  tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  browser = tab.linkedBrowser;
  await promiseTabState(tab, state);

  if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
    await SpecialPowers.spawn(browser, [], function () {
      let sessionHistory =
        docShell.browsingContext.childSessionHistory.legacySHistory;

      is(sessionHistory.count, 1, "Should be a single history entry");
      is(
        sessionHistory.getEntryAtIndex(0).URI.spec,
        "about:blank",
        "Should be the right URL"
      );
    });
  } else {
    let sessionHistory = browser.browsingContext.sessionHistory;

    is(sessionHistory.count, 1, "Should be a single history entry");
    is(
      sessionHistory.getEntryAtIndex(0).URI.spec,
      "about:blank",
      "Should be the right URL"
    );
  }

  // Load a new URL into the tab, it should replace the about:blank history entry
  BrowserTestUtils.loadURIString(browser, "about:robots");
  await promiseBrowserLoaded(browser, false, "about:robots");
  if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
    await SpecialPowers.spawn(browser, [], function () {
      let sessionHistory =
        docShell.browsingContext.childSessionHistory.legacySHistory;

      is(sessionHistory.count, 1, "Should be a single history entry");
      is(
        sessionHistory.getEntryAtIndex(0).URI.spec,
        "about:robots",
        "Should be the right URL"
      );
    });
  } else {
    let sessionHistory = browser.browsingContext.sessionHistory;

    is(sessionHistory.count, 1, "Should be a single history entry");
    is(
      sessionHistory.getEntryAtIndex(0).URI.spec,
      "about:robots",
      "Should be the right URL"
    );
  }

  // Cleanup.
  BrowserTestUtils.removeTab(tab);
});

/**
 * Check that entries default to being persisted when the attribute doesn't
 * exist
 */
add_task(async function check_history_default_persisted() {
  // Create an about:blank tab
  let tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Retrieve the tab state.
  await TabStateFlusher.flush(browser);
  let state = JSON.parse(ss.getTabState(tab));
  delete state.entries[0].persist;
  BrowserTestUtils.removeTab(tab);
  browser = null;

  // Open a new tab to restore into.
  tab = BrowserTestUtils.addTab(gBrowser, "about:blank");
  browser = tab.linkedBrowser;
  await promiseTabState(tab, state);
  if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
    await SpecialPowers.spawn(browser, [], function () {
      let sessionHistory =
        docShell.browsingContext.childSessionHistory.legacySHistory;

      is(sessionHistory.count, 1, "Should be a single history entry");
      is(
        sessionHistory.getEntryAtIndex(0).URI.spec,
        "about:blank",
        "Should be the right URL"
      );
    });
  } else {
    let sessionHistory = browser.browsingContext.sessionHistory;

    is(sessionHistory.count, 1, "Should be a single history entry");
    is(
      sessionHistory.getEntryAtIndex(0).URI.spec,
      "about:blank",
      "Should be the right URL"
    );
  }

  // Load a new URL into the tab, it should replace the about:blank history entry
  BrowserTestUtils.loadURIString(browser, "about:robots");
  await promiseBrowserLoaded(browser, false, "about:robots");
  if (!SpecialPowers.Services.appinfo.sessionHistoryInParent) {
    await SpecialPowers.spawn(browser, [], function () {
      let sessionHistory =
        docShell.browsingContext.childSessionHistory.legacySHistory;

      is(sessionHistory.count, 2, "Should be two history entries");
      is(
        sessionHistory.getEntryAtIndex(0).URI.spec,
        "about:blank",
        "Should be the right URL"
      );
      is(
        sessionHistory.getEntryAtIndex(1).URI.spec,
        "about:robots",
        "Should be the right URL"
      );
    });
  } else {
    let sessionHistory = browser.browsingContext.sessionHistory;

    is(sessionHistory.count, 2, "Should be two history entries");
    is(
      sessionHistory.getEntryAtIndex(0).URI.spec,
      "about:blank",
      "Should be the right URL"
    );
    is(
      sessionHistory.getEntryAtIndex(1).URI.spec,
      "about:robots",
      "Should be the right URL"
    );
  }

  // Cleanup.
  BrowserTestUtils.removeTab(tab);
});