|
| 1 | +<!doctype html> |
| 2 | +<script src="/resources/testharness.js"></script> |
| 3 | +<script src="/resources/testharnessreport.js"></script> |
| 4 | +<body> |
| 5 | +<script> |
| 6 | +promise_test(async t => { |
| 7 | + // Wait for after the load event so that the navigation doesn't get converted |
| 8 | + // into a replace navigation. |
| 9 | + await new Promise(resolve => window.onload = () => t.step_timeout(resolve, 0)); |
| 10 | + |
| 11 | + let start_length = navigation.entries().length; |
| 12 | + let start_hash = location.hash; |
| 13 | + |
| 14 | + let navInfo0 = { nav : "info0" }; |
| 15 | + let navState0 = { statevar: "state0" }; |
| 16 | + |
| 17 | + let navInfo1 = { nav : "info1" }; |
| 18 | + let navState1 = { statevar: "state1" }; |
| 19 | + |
| 20 | + let navInfo2 = { nav : "info2" }; |
| 21 | + let navState2 = { statevar: "state2" }; |
| 22 | + |
| 23 | + navigation.onnavigate = t.step_func(e => { |
| 24 | + e.intercept({ |
| 25 | + precommitHandler: t.step_func(controller => { |
| 26 | + assert_equals(location.hash, start_hash); |
| 27 | + assert_equals(new URL(e.destination.url).hash, "#push"); |
| 28 | + assert_equals(e.info, navInfo0); |
| 29 | + assert_not_equals(e.destination.getState(), navState0); |
| 30 | + assert_equals(e.destination.getState().statevar, navState0.statevar); |
| 31 | + |
| 32 | + // Redirect without options should not overwrite the original options. |
| 33 | + controller.redirect("#redirect0"); |
| 34 | + assert_equals(location.hash, start_hash); |
| 35 | + assert_equals(new URL(e.destination.url).hash, "#redirect0"); |
| 36 | + assert_equals(e.info, navInfo0); |
| 37 | + assert_not_equals(e.destination.getState(), navState0); |
| 38 | + assert_equals(e.destination.getState().statevar, navState0.statevar); |
| 39 | + |
| 40 | + // Update both info and state. |
| 41 | + controller.redirect("#redirect1", { info: navInfo1, state: navState1}); |
| 42 | + assert_equals(location.hash, start_hash); |
| 43 | + assert_equals(new URL(e.destination.url).hash, "#redirect1"); |
| 44 | + assert_equals(e.info, navInfo1); |
| 45 | + assert_not_equals(e.destination.getState(), navState1); |
| 46 | + assert_equals(e.destination.getState().statevar, navState1.statevar); |
| 47 | + |
| 48 | + // Update just info |
| 49 | + controller.redirect("#redirect2", { info: navInfo2 }); |
| 50 | + assert_equals(location.hash, start_hash); |
| 51 | + assert_equals(new URL(e.destination.url).hash, "#redirect2"); |
| 52 | + assert_equals(e.info, navInfo2); |
| 53 | + assert_not_equals(e.destination.getState(), navState1); |
| 54 | + assert_equals(e.destination.getState().statevar, navState1.statevar); |
| 55 | + |
| 56 | + // Update just state - this also tests the case where the url does |
| 57 | + // not change. |
| 58 | + controller.redirect("#redirect2", { state: navState2 }); |
| 59 | + assert_equals(location.hash, start_hash); |
| 60 | + assert_equals(new URL(e.destination.url).hash, "#redirect2"); |
| 61 | + assert_equals(e.info, navInfo2); |
| 62 | + assert_not_equals(e.destination.getState(), navState2); |
| 63 | + assert_equals(e.destination.getState().statevar, navState2.statevar); |
| 64 | + |
| 65 | + // Explicit undefined is treated as absent. |
| 66 | + controller.redirect("#redirect2", { info: undefined, state: undefined }); |
| 67 | + assert_equals(location.hash, start_hash); |
| 68 | + assert_equals(new URL(e.destination.url).hash, "#redirect2"); |
| 69 | + assert_equals(e.info, navInfo2); |
| 70 | + assert_not_equals(e.destination.getState(), navState2); |
| 71 | + assert_equals(e.destination.getState().statevar, navState2.statevar); |
| 72 | + }), |
| 73 | + }); |
| 74 | + }); |
| 75 | + await navigation.navigate("#push", { info: navInfo0, state: navState0 }).committed; |
| 76 | + assert_equals(location.hash, "#redirect2"); |
| 77 | + assert_equals(navigation.entries().length, start_length + 1); |
| 78 | + assert_equals(navigation.currentEntry.getState().statevar, navState2.statevar); |
| 79 | +}, "precommitHandler redirect() with options dictionary"); |
| 80 | +</script> |
| 81 | +</body> |
0 commit comments