summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_async_flushes.js
blob: ad3b27ed4e330d7da31c2ffbf229968ccf4040d9 (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
"use strict";

const PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content/",
  "http://example.com/"
);
const URL = PATH + "file_async_flushes.html";

add_task(async function test_flush() {
  // Create new tab.
  let tab = BrowserTestUtils.addTab(gBrowser, URL);
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Flush to empty any queued update messages.
  await TabStateFlusher.flush(browser);

  // There should be one history entry.
  let { entries } = JSON.parse(ss.getTabState(tab));
  is(entries.length, 1, "there is a single history entry");

  // Click the link to navigate, this will add second shistory entry.
  await SpecialPowers.spawn(browser, [], async function() {
    return new Promise(resolve => {
      docShell.chromeEventHandler.addEventListener(
        "hashchange",
        () => resolve(),
        { once: true, capture: true }
      );

      // Click the link.
      content.document.querySelector("a").click();
    });
  });

  // Flush to empty any queued update messages.
  await TabStateFlusher.flush(browser);

  // There should be two history entries now.
  ({ entries } = JSON.parse(ss.getTabState(tab)));
  is(entries.length, 2, "there are two shistory entries");

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

add_task(async function test_crash() {
  // Create new tab.
  let tab = BrowserTestUtils.addTab(gBrowser, URL);
  gBrowser.selectedTab = tab;
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Flush to empty any queued update messages.
  await TabStateFlusher.flush(browser);

  // There should be one history entry.
  let { entries } = JSON.parse(ss.getTabState(tab));
  is(entries.length, 1, "there is a single history entry");

  // Click the link to navigate.
  await SpecialPowers.spawn(browser, [], async function() {
    return new Promise(resolve => {
      docShell.chromeEventHandler.addEventListener(
        "hashchange",
        () => resolve(),
        { once: true, capture: true }
      );

      // Click the link.
      content.document.querySelector("a").click();
    });
  });

  // Crash the browser and flush. Both messages are async and will be sent to
  // the content process. The "crash" message makes it first so that we don't
  // get a chance to process the flush. The TabStateFlusher however should be
  // notified so that the flush still completes.
  let promise1 = BrowserTestUtils.crashFrame(browser);
  let promise2 = TabStateFlusher.flush(browser);
  await Promise.all([promise1, promise2]);

  // The pending update should be lost.
  ({ entries } = JSON.parse(ss.getTabState(tab)));
  is(entries.length, 1, "still only one history entry");

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

add_task(async function test_remove() {
  // Create new tab.
  let tab = BrowserTestUtils.addTab(gBrowser, URL);
  let browser = tab.linkedBrowser;
  await promiseBrowserLoaded(browser);

  // Flush to empty any queued update messages.
  await TabStateFlusher.flush(browser);

  // There should be one history entry.
  let { entries } = JSON.parse(ss.getTabState(tab));
  is(entries.length, 1, "there is a single history entry");

  // Click the link to navigate.
  await SpecialPowers.spawn(browser, [], async function() {
    return new Promise(resolve => {
      docShell.chromeEventHandler.addEventListener(
        "hashchange",
        () => resolve(),
        { once: true, capture: true }
      );

      // Click the link.
      content.document.querySelector("a").click();
    });
  });

  // Request a flush and remove the tab. The flush should still complete.
  await Promise.all([
    TabStateFlusher.flush(browser),
    promiseRemoveTabAndSessionState(tab),
  ]);
});