summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/navigation-api/navigation-methods/return-value/resources/helpers.js
blob: 63d706ed285c66740496ac8ae1bed3eb50bce883 (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
window.assertReturnValue = (result, w = window) => {
  assert_equals(Object.getPrototypeOf(result), w.Object.prototype, "result object must be from the right realm");
  assert_array_equals(Reflect.ownKeys(result), ["committed", "finished"]);
  assert_true(result.committed instanceof w.Promise);
  assert_true(result.finished instanceof w.Promise);
  assert_not_equals(result.committed, result.finished);
};

window.assertNeverSettles = (t, result, w = window) => {
  assertReturnValue(result, w);
  result.committed.then(
    t.unreached_func("committed must not fulfill"),
    t.unreached_func("committed must not reject")
  );

  result.finished.then(
    t.unreached_func("finished must not fulfill"),
    t.unreached_func("finished must not reject")
  );
};

window.assertBothFulfillEntryNotAvailable = async (t, result, w = window) => {
  assertReturnValue(result, w);

  // Don't use await here so that we can catch out-of-order settlements.
  let committedValue;
  result.committed.then(
    t.step_func(v => { committedValue = v;}),
    t.unreached_func("committed must not reject")
  );

  const finishedValue = await result.finished;

  assert_not_equals(committedValue, undefined, "committed must fulfill before finished");
  assert_equals(finishedValue, committedValue, "committed and finished must fulfill with the same value");
  assert_true(finishedValue instanceof w.NavigationHistoryEntry, "fulfillment value must be a NavigationHistoryEntry");
};

window.assertBothFulfill = async (t, result, expected, w = window) => {
  assertReturnValue(result, w);

  // Don't use await here so that we can catch out-of-order settlements.
  let committedValue;
  result.committed.then(
    t.step_func(v => { committedValue = v; }),
    t.unreached_func("committed must not reject")
  );

  const finishedValue = await result.finished;

  assert_not_equals(committedValue, undefined, "committed must fulfill before finished");
  assert_equals(finishedValue, committedValue, "committed and finished must fulfill with the same value");
  assert_true(finishedValue instanceof w.NavigationHistoryEntry, "fulfillment value must be a NavigationHistoryEntry");
  assert_equals(finishedValue, expected);
};

window.assertCommittedFulfillsFinishedRejectsExactly = async (t, result, expectedEntry, expectedRejection, w = window) => {
  assertReturnValue(result, w);

  // Don't use await here so that we can catch out-of-order settlements.
  let committedValue;
  result.committed.then(
    t.step_func(v => { committedValue = v; }),
    t.unreached_func("committed must not reject")
  );

  await promise_rejects_exactly(t, expectedRejection, result.finished);

  assert_not_equals(committedValue, undefined, "committed must fulfill before finished rejects");
  assert_true(committedValue instanceof w.NavigationHistoryEntry, "fulfillment value must be a NavigationHistoryEntry");
  assert_equals(committedValue, expectedEntry);
};

window.assertCommittedFulfillsFinishedRejectsDOM = async (t, result, expectedEntry, expectedDOMExceptionCode, w = window, domExceptionConstructor = w.DOMException, navigationHistoryEntryConstuctor = w.NavigationHistoryEntry) => {
  assertReturnValue(result, w);

  let committedValue;
  result.committed.then(
    t.step_func(v => { committedValue = v; }),
    t.unreached_func("committed must not reject")
  );

  await promise_rejects_dom(t, expectedDOMExceptionCode, domExceptionConstructor, result.finished);

  assert_not_equals(committedValue, undefined, "committed must fulfill before finished rejects");
  assert_true(committedValue instanceof navigationHistoryEntryConstuctor, "fulfillment value must be an NavigationHistoryEntry");
  assert_equals(committedValue, expectedEntry);
};

window.assertBothRejectExactly = async (t, result, expectedRejection, w = window) => {
  assertReturnValue(result, w);

  let committedReason, finishedReason;
  await Promise.all([
    result.committed.then(
      t.unreached_func("committed must not fulfill"),
      t.step_func(r => { committedReason = r; })
    ),
    result.finished.then(
      t.unreached_func("finished must not fulfill"),
      t.step_func(r => { finishedReason = r; })
    )
  ]);

  assert_equals(committedReason, finishedReason, "committed and finished must reject with the same value");
  assert_equals(expectedRejection, committedReason);
};

window.assertBothRejectDOM = async (t, result, expectedDOMExceptionCode, w = window, domExceptionConstructor = w.DOMException) => {
  assertReturnValue(result, w);

  // Don't use await here so that we can catch out-of-order settlements.
  let committedReason, finishedReason;
  await Promise.all([
    result.committed.then(
      t.unreached_func("committed must not fulfill"),
      t.step_func(r => { committedReason = r; })
    ),
    result.finished.then(
      t.unreached_func("finished must not fulfill"),
      t.step_func(r => { finishedReason = r; })
    )
  ]);

  assert_equals(committedReason, finishedReason, "committed and finished must reject with the same value");
  assert_throws_dom(expectedDOMExceptionCode, domExceptionConstructor, () => { throw committedReason; });
};