File: precommitHandler-helpers.js

package info (click to toggle)
firefox-esr 140.4.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,539,276 kB
  • sloc: cpp: 7,381,286; javascript: 6,388,710; ansic: 3,710,139; python: 1,393,780; xml: 628,165; asm: 426,918; java: 184,004; sh: 65,742; makefile: 19,302; objc: 13,059; perl: 12,912; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,226; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (93 lines) | stat: -rw-r--r-- 4,821 bytes parent folder | download | duplicates (9)
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
window.testDeferredCommit = async (t, navigationType, mode, destinationIndex = 0) => {
  let startHash = location.hash;
  let destinationHash;
  const err = new Error("boo!");

  let popstate_fired = false;
  window.addEventListener("popstate", () => popstate_fired = true, { once : true });
  let navigatesuccess_fired = false;
  navigation.addEventListener("navigatesuccess", () => navigatesuccess_fired = true, { once : true });
  let navigateerror_fired = false;
  navigation.addEventListener("navigateerror", () => navigateerror_fired = true, { once : true });

  // mode-specific logic for the navigate event handler
  let navigate_helpers = {
    rejectBeforeCommit : async (e) => { return Promise.reject("Should never run") },
    rejectAfterCommit : async (e) => {
      assert_equals(location.hash, destinationHash, "hash after commit");
      assert_equals(false, popstate_fired, "popstate before handler starts");
      await new Promise(resolve => t.step_timeout(resolve, 0));
      assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after handler async step");
      return Promise.reject(err);
    },
    success : async (e) => {
      assert_equals(location.hash, destinationHash, "hash after commit");
      assert_equals(false, popstate_fired, "popstate before handler starts");
      await new Promise(resolve => t.step_timeout(resolve, 0));
      assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after handler async step");
      return new Promise(resolve => t.step_timeout(resolve, 0));
    },
  }

  navigation.addEventListener("navigate", e => {
    e.intercept({ precommitHandler: t.step_func(async () => {
                    assert_equals(e.navigationType, navigationType);
                    assert_equals(location.hash, startHash, "start hash");
                    assert_false(popstate_fired, "popstate fired at handler start");

                    await new Promise(resolve => t.step_timeout(resolve, 0));
                    assert_equals(location.hash, startHash, "hash after first async step");
                    assert_false(popstate_fired, "popstate fired after first async step");

                    if (mode == "rejectBeforeCommit")
                      return Promise.reject(err);
                  }),
                  handler: t.step_func(navigate_helpers[mode])
                });
  }, { once: true });

  let startingIndex = navigation.currentEntry.index;
  let expectedIndexOnCommit;

  let promises;
  if (navigationType === "push" || navigationType === "replace") {
    destinationHash = (startHash === "" ? "#" : startHash) + "a";
    promises = navigation.navigate(destinationHash, { history: navigationType });
    expectedIndexOnCommit = (navigationType === "push") ? startingIndex + 1
                                                        : startingIndex;
  } else if (navigationType === "reload") {
    destinationHash = startHash;
    promises = navigation.reload();
    expectedIndexOnCommit = startingIndex;
  } else if (navigationType === "traverse") {
    let destinationEntry = navigation.entries()[destinationIndex];
    destinationHash = new URL(destinationEntry.url).hash;
    promises = navigation.traverseTo(destinationEntry.key);
    expectedIndexOnCommit = destinationIndex;
  }

  if (mode === "rejectBeforeCommit") {
    await assertBothRejectExactly(t, promises, err);
    assert_equals(location.hash, startHash, "hash after promise resolution");
    assert_false(popstate_fired, "popstate fired after promise resolution");
    assert_false(navigatesuccess_fired, "navigatesuccess fired");
    assert_true(navigateerror_fired, "navigateerror fired");
    assert_equals(navigation.currentEntry.index, startingIndex);
  } else if (mode === "rejectAfterCommit") {
    await promises.committed;
    await assertCommittedFulfillsFinishedRejectsExactly(t, promises, navigation.currentEntry, err);
    assert_equals(location.hash, destinationHash, "hash after promise resolution");
    assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after promise resolution");
    assert_false(navigatesuccess_fired, "navigatesuccess fired");
    assert_true(navigateerror_fired, "navigateerror fired");
    assert_equals(navigation.currentEntry.index, expectedIndexOnCommit);
  } else {
    await promises.committed;
    await assertBothFulfill(t, promises, navigation.currentEntry);
    assert_equals(location.hash, destinationHash, "hash after promise resolution");
    assert_equals(navigationType == "traverse", popstate_fired, "popstate fired after promise resolution");
    assert_true(navigatesuccess_fired, "navigatesuccess fired");
    assert_false(navigateerror_fired, "navigateerror fired");
    assert_equals(navigation.currentEntry.index, expectedIndexOnCommit);
  }
}