|
| 1 | +window.assertReturnValue = (result, w = window) => { |
| 2 | + assert_equals(Object.getPrototypeOf(result), w.Object.prototype, "result object must be from the right realm"); |
| 3 | + assert_array_equals(Reflect.ownKeys(result), ["committed", "finished"]); |
| 4 | + assert_true(result.committed instanceof w.Promise); |
| 5 | + assert_true(result.finished instanceof w.Promise); |
| 6 | + assert_not_equals(result.committed, result.finished); |
| 7 | +}; |
| 8 | + |
| 9 | +window.assertNeverSettles = (t, result, w = window) => { |
| 10 | + assertReturnValue(result, w); |
| 11 | + result.committed.then( |
| 12 | + t.unreached_func("committed must not fulfill"), |
| 13 | + t.unreached_func("committed must not reject") |
| 14 | + ); |
| 15 | + |
| 16 | + result.finished.then( |
| 17 | + t.unreached_func("finished must not fulfill"), |
| 18 | + t.unreached_func("finished must not reject") |
| 19 | + ); |
| 20 | +}; |
| 21 | + |
| 22 | +window.assertBothFulfillEntryNotAvailable = async (t, result, w = window) => { |
| 23 | + assertReturnValue(result, w); |
| 24 | + |
| 25 | + // Don't use await here so that we can catch out-of-order settlements. |
| 26 | + let committedValue; |
| 27 | + result.committed.then( |
| 28 | + t.step_func(v => { committedValue = v;}), |
| 29 | + t.unreached_func("committed must not reject") |
| 30 | + ); |
| 31 | + |
| 32 | + const finishedValue = await result.finished; |
| 33 | + |
| 34 | + assert_not_equals(committedValue, undefined, "committed must fulfill before finished"); |
| 35 | + assert_equals(finishedValue, committedValue, "committed and finished must fulfill with the same value"); |
| 36 | + assert_true(finishedValue instanceof w.NavigationHistoryEntry, "fulfillment value must be a NavigationHistoryEntry"); |
| 37 | +}; |
| 38 | + |
| 39 | +window.assertBothFulfill = async (t, result, expected, w = window) => { |
| 40 | + assertReturnValue(result, w); |
| 41 | + |
| 42 | + // Don't use await here so that we can catch out-of-order settlements. |
| 43 | + let committedValue; |
| 44 | + result.committed.then( |
| 45 | + t.step_func(v => { committedValue = v; }), |
| 46 | + t.unreached_func("committed must not reject") |
| 47 | + ); |
| 48 | + |
| 49 | + const finishedValue = await result.finished; |
| 50 | + |
| 51 | + assert_not_equals(committedValue, undefined, "committed must fulfill before finished"); |
| 52 | + assert_equals(finishedValue, committedValue, "committed and finished must fulfill with the same value"); |
| 53 | + assert_true(finishedValue instanceof w.NavigationHistoryEntry, "fulfillment value must be a NavigationHistoryEntry"); |
| 54 | + assert_equals(finishedValue, expected); |
| 55 | +}; |
| 56 | + |
| 57 | +window.assertCommittedFulfillsFinishedRejectsExactly = async (t, result, expectedEntry, expectedRejection, w = window) => { |
| 58 | + assertReturnValue(result, w); |
| 59 | + |
| 60 | + // Don't use await here so that we can catch out-of-order settlements. |
| 61 | + let committedValue; |
| 62 | + result.committed.then( |
| 63 | + t.step_func(v => { committedValue = v; }), |
| 64 | + t.unreached_func("committed must not reject") |
| 65 | + ); |
| 66 | + |
| 67 | + await promise_rejects_exactly(t, expectedRejection, result.finished); |
| 68 | + |
| 69 | + assert_not_equals(committedValue, undefined, "committed must fulfill before finished rejects"); |
| 70 | + assert_true(committedValue instanceof w.NavigationHistoryEntry, "fulfillment value must be a NavigationHistoryEntry"); |
| 71 | + assert_equals(committedValue, expectedEntry); |
| 72 | +}; |
| 73 | + |
| 74 | +window.assertCommittedFulfillsFinishedRejectsDOM = async (t, result, expectedEntry, expectedDOMExceptionCode, w = window, domExceptionConstructor = w.DOMException, navigationHistoryEntryConstuctor = w.NavigationHistoryEntry) => { |
| 75 | + assertReturnValue(result, w); |
| 76 | + |
| 77 | + let committedValue; |
| 78 | + result.committed.then( |
| 79 | + t.step_func(v => { committedValue = v; }), |
| 80 | + t.unreached_func("committed must not reject") |
| 81 | + ); |
| 82 | + |
| 83 | + await promise_rejects_dom(t, expectedDOMExceptionCode, domExceptionConstructor, result.finished); |
| 84 | + |
| 85 | + assert_not_equals(committedValue, undefined, "committed must fulfill before finished rejects"); |
| 86 | + assert_true(committedValue instanceof navigationHistoryEntryConstuctor, "fulfillment value must be an NavigationHistoryEntry"); |
| 87 | + assert_equals(committedValue, expectedEntry); |
| 88 | +}; |
| 89 | + |
| 90 | +// We cannot use Promise.all() because the automatic coercion behavior when |
| 91 | +// promises from multiple realms are involved causes it to hang if one of the |
| 92 | +// promises is from a detached iframe's realm. See discussion at |
| 93 | +// https://github.com/whatwg/html/issues/11252#issuecomment-2984143855. |
| 94 | +window.waitForAllLenient = (iterable) => { |
| 95 | + const { promise: all, resolve, reject } = Promise.withResolvers(); |
| 96 | + let remaining = 0; |
| 97 | + let results = []; |
| 98 | + for (const promise of iterable) { |
| 99 | + let index = remaining++; |
| 100 | + promise.then(v => { |
| 101 | + results[index] = v; |
| 102 | + --remaining; |
| 103 | + if (!remaining) { |
| 104 | + resolve(results); |
| 105 | + } |
| 106 | + return v; |
| 107 | + }, v => reject(v)); |
| 108 | + } |
| 109 | + |
| 110 | + if (!remaining) { |
| 111 | + resolve(results); |
| 112 | + } |
| 113 | + |
| 114 | + return all; |
| 115 | +} |
| 116 | + |
| 117 | +window.assertBothRejectExactly = async (t, result, expectedRejection, w = window) => { |
| 118 | + assertReturnValue(result, w); |
| 119 | + |
| 120 | + let committedReason, finishedReason; |
| 121 | + await waitForAllLenient([ |
| 122 | + result.committed.then( |
| 123 | + t.unreached_func("committed must not fulfill"), |
| 124 | + t.step_func(r => { committedReason = r; }) |
| 125 | + ), |
| 126 | + result.finished.then( |
| 127 | + t.unreached_func("finished must not fulfill"), |
| 128 | + t.step_func(r => { finishedReason = r; }) |
| 129 | + ) |
| 130 | + ]); |
| 131 | + |
| 132 | + assert_equals(committedReason, finishedReason, "committed and finished must reject with the same value"); |
| 133 | + assert_equals(expectedRejection, committedReason); |
| 134 | +}; |
| 135 | + |
| 136 | +window.assertBothRejectDOM = async (t, result, expectedDOMExceptionCode, w = window, domExceptionConstructor = w.DOMException) => { |
| 137 | + assertReturnValue(result, w); |
| 138 | + |
| 139 | + // Don't use await here so that we can catch out-of-order settlements. |
| 140 | + let committedReason, finishedReason; |
| 141 | + await waitForAllLenient([ |
| 142 | + result.committed.then( |
| 143 | + t.unreached_func("committed must not fulfill"), |
| 144 | + t.step_func(r => { committedReason = r; }) |
| 145 | + ), |
| 146 | + result.finished.then( |
| 147 | + t.unreached_func("finished must not fulfill"), |
| 148 | + t.step_func(r => { finishedReason = r; }) |
| 149 | + ) |
| 150 | + ]); |
| 151 | + |
| 152 | + assert_equals(committedReason, finishedReason, "committed and finished must reject with the same value"); |
| 153 | + assert_throws_dom(expectedDOMExceptionCode, domExceptionConstructor, () => { throw committedReason; }); |
| 154 | +}; |
0 commit comments