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

"use strict";

const BUILDER = "http://mochi.test:8888/document-builder.sjs?html=";
const PAGE_1 = BUILDER + encodeURIComponent(`<html><body>Page 1</body></html>`);
const PAGE_2 = BUILDER + encodeURIComponent(`<html><body>Page 2</body></html>`);

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["browser.history.collectWireframes", true]],
  });
});

/**
 * Test that capturing wireframes on nsISHEntriy's in the parent process
 * happens at the right times.
 */
add_task(async function () {
  await BrowserTestUtils.withNewTab(PAGE_1, async browser => {
    let sh = browser.browsingContext.sessionHistory;
    Assert.equal(
      sh.count,
      1,
      "Got the right SessionHistory entry count after initial tab load."
    );
    Assert.ok(
      !sh.getEntryAtIndex(0).wireframe,
      "No wireframe for the loaded entry after initial tab load."
    );

    let loaded = BrowserTestUtils.browserLoaded(browser, false, PAGE_2);
    BrowserTestUtils.loadURIString(browser, PAGE_2);
    await loaded;

    Assert.equal(
      sh.count,
      2,
      "Got the right SessionHistory entry count after loading page 2."
    );
    Assert.ok(
      sh.getEntryAtIndex(0).wireframe,
      "A wireframe was captured for the first entry after loading page 2."
    );
    Assert.ok(
      !sh.getEntryAtIndex(1).wireframe,
      "No wireframe for the loaded entry after loading page 2."
    );

    // Now go back
    loaded = BrowserTestUtils.waitForContentEvent(browser, "pageshow");
    browser.goBack();
    await loaded;
    Assert.ok(
      sh.getEntryAtIndex(1).wireframe,
      "A wireframe was captured for the second entry after going back."
    );
    Assert.ok(
      !sh.getEntryAtIndex(0).wireframe,
      "No wireframe for the loaded entry after going back."
    );

    // Now forward again
    loaded = BrowserTestUtils.waitForContentEvent(browser, "pageshow");
    browser.goForward();
    await loaded;

    Assert.equal(
      sh.count,
      2,
      "Got the right SessionHistory entry count after going forward again."
    );
    Assert.ok(
      sh.getEntryAtIndex(0).wireframe,
      "A wireframe was captured for the first entry after going forward again."
    );
    Assert.ok(
      !sh.getEntryAtIndex(1).wireframe,
      "No wireframe for the loaded entry after going forward again."
    );

    // And using pushState
    await SpecialPowers.spawn(browser, [], async () => {
      content.history.pushState({}, "", "nothing-1.html");
      content.history.pushState({}, "", "nothing-2.html");
    });

    Assert.equal(
      sh.count,
      4,
      "Got the right SessionHistory entry count after using pushState."
    );
    Assert.ok(
      sh.getEntryAtIndex(0).wireframe,
      "A wireframe was captured for the first entry after using pushState."
    );
    Assert.ok(
      sh.getEntryAtIndex(1).wireframe,
      "A wireframe was captured for the second entry after using pushState."
    );
    Assert.ok(
      sh.getEntryAtIndex(2).wireframe,
      "A wireframe was captured for the third entry after using pushState."
    );
    Assert.ok(
      !sh.getEntryAtIndex(3).wireframe,
      "No wireframe for the loaded entry after using pushState."
    );

    // Now check that wireframes can be written to in case we're restoring
    // an nsISHEntry from serialization.
    let wireframe = sh.getEntryAtIndex(2).wireframe;
    sh.getEntryAtIndex(2).wireframe = null;
    Assert.equal(
      sh.getEntryAtIndex(2).wireframe,
      null,
      "Successfully cleared wireframe."
    );

    sh.getEntryAtIndex(3).wireframe = wireframe;
    Assert.deepEqual(
      sh.getEntryAtIndex(3).wireframe,
      wireframe,
      "Successfully wrote a wireframe to an nsISHEntry."
    );
  });
});