summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_906076_lazy_tabs.js
blob: 2fab82ca018195f78f5ee07c3ced4751448d9087 (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/ */

const TEST_STATE = {
  windows: [
    {
      tabs: [
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
        {
          entries: [{ url: "http://example.com", triggeringPrincipal_base64 }],
        },
      ],
    },
  ],
};

const TEST_STATE_2 = {
  windows: [
    {
      tabs: [
        { entries: [{ url: "about:robots", triggeringPrincipal_base64 }] },
        {
          entries: [],
          userTypedValue: "http://example.com",
          userTypedClear: 1,
        },
      ],
    },
  ],
};

function countNonLazyTabs(win) {
  win = win || window;
  let count = 0;
  for (let browser of win.gBrowser.browsers) {
    if (browser.isConnected) {
      count++;
    }
  }
  return count;
}

/**
 * Test that lazy browsers do not get prematurely inserted by
 * code accessing browser bound properties on the unbound browser.
 */

add_task(async function test() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.sessionstore.restore_on_demand", true],
      ["browser.sessionstore.restore_tabs_lazily", true],
    ],
  });

  let backupState = SessionStore.getBrowserState();

  await promiseBrowserState(TEST_STATE);

  info(
    "Check that no lazy browsers get unnecessarily inserted after session restore"
  );
  is(countNonLazyTabs(), 1, "Window has only 1 non-lazy tab");

  await TestUtils.topicObserved("sessionstore-state-write-complete");

  // When sessionstore write occurs, tabs are checked for state changes.
  // Make sure none of them insert their browsers when this happens.
  info("Check that no lazy browsers get inserted after sessionstore write");
  is(countNonLazyTabs(), 1, "Window has only 1 non-lazy tab");

  info("Check that lazy browser gets inserted properly");
  ok(
    !gBrowser.browsers[1].isConnected,
    "The browser that we're attempting to insert is indeed lazy"
  );
  gBrowser._insertBrowser(gBrowser.tabs[1]);
  is(countNonLazyTabs(), 2, "Window now has 2 non-lazy tabs");

  // Check if any lazy tabs got inserted when window closes.
  let newWindow = await promiseNewWindowLoaded();

  SessionStore.setWindowState(newWindow, JSON.stringify(TEST_STATE));

  await new Promise(resolve => {
    newWindow.addEventListener(
      "unload",
      () => {
        info("Check that no lazy browsers get inserted when window closes");
        is(countNonLazyTabs(newWindow), 1, "Window has only 1 non-lazy tab");

        info(
          "Check that it is not possible to insert a lazy browser after the window closed"
        );
        ok(
          !newWindow.gBrowser.browsers[1].isConnected,
          "The browser that we're attempting to insert is indeed lazy"
        );
        newWindow.gBrowser._insertBrowser(newWindow.gBrowser.tabs[1]);
        is(
          countNonLazyTabs(newWindow),
          1,
          "Window still has only 1 non-lazy tab"
        );

        resolve();
      },
      { once: true }
    );

    newWindow.close();
  });

  // Bug 1365933.
  info(
    "Check that session with tab having empty entries array gets restored properly"
  );
  await promiseBrowserState(TEST_STATE_2);

  is(gBrowser.tabs.length, 2, "Window has 2 tabs");
  is(
    gBrowser.selectedBrowser.currentURI.spec,
    "about:robots",
    "Tab has the expected URL"
  );

  gBrowser.selectedTab = gBrowser.tabs[1];
  await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
  is(
    gBrowser.selectedBrowser.currentURI.spec,
    "http://example.com/",
    "Tab has the expected URL"
  );

  // Cleanup.
  await promiseBrowserState(backupState);
});