Skip to content

Commit

Permalink
App history: fix event.formData to be POST-only, plus tests
Browse files Browse the repository at this point in the history
* event.formData is only supposed to be populated for POST form submissions. Fix that and update tests accordingly.

* A number of tests were using e.preventDefault() after all the asserts. This was subpar because if the asserts failed the event would not be prevented, i.e. we would leave the test page. Put the e.preventDefault() first.

* Test that event.formData does not appear for "traverse" or "reload" navigations even if the session history entry contains form data

Fixed: 1233710
Bug: 1183545
Change-Id: Ibc6307a9a8629df769c2eeae19b2d332af77843b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3076282
Commit-Queue: Domenic Denicola <[email protected]>
Reviewed-by: Nate Chapin <[email protected]>
Reviewed-by: Mason Freed <[email protected]>
Cr-Commit-Position: refs/heads/master@{#909507}
  • Loading branch information
domenic authored and chromium-wpt-export-bot committed Aug 6, 2021
1 parent 59d525c commit bd68e11
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 12 deletions.
26 changes: 26 additions & 0 deletions app-history/navigate-event/navigate-form-get.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<form id="form" action=""></form>
<script>
async_test(t => {
appHistory.onnavigate = t.step_func_done(e => {
e.preventDefault();

assert_equals(e.navigationType, "replace");
assert_true(e.cancelable);
assert_true(e.canRespond);
assert_false(e.userInitiated);
assert_false(e.hashChange);
assert_equals(e.destination.url, location.href + "?");
assert_false(e.destination.sameDocument);
assert_equals(e.destination.key, null);
assert_equals(e.destination.id, null);
assert_equals(e.destination.index, -1);

// Because it's a GET, not a POST
assert_equals(e.formData, null);
});
window.onload = t.step_func(() => form.submit());
}, "<form> submission with GET method fires navigate event but with formData null");
</script>
28 changes: 28 additions & 0 deletions app-history/navigate-event/navigate-form-reload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="iframe" name="i" src="/common/blank.html"></iframe>
<form id="form" action="/common/blank.html?1" method="post" target="i"></form>
<script>
async_test(t => {
window.onload = t.step_func(() => {
appHistory.onnavigate = t.step_func_done(() => {
assert_unreached("onnavigate should not have fired in source window");
});
iframe.contentWindow.appHistory.onnavigate = t.step_func(e => {
assert_equals(e.navigationType, "push");
assert_not_equals(e.formData, null);

iframe.onload = t.step_func(() => {
iframe.contentWindow.appHistory.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "reload");
assert_equals(e.formData, null);
});

iframe.contentWindow.location.reload();
});
});
form.submit();
});
}, "reloading a page created from form submission results in formData of null, not the original form data");
</script>
44 changes: 44 additions & 0 deletions app-history/navigate-event/navigate-form-traverse.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="iframe" name="i" src="/common/blank.html"></iframe>
<form id="form" action="/common/blank.html?1" method="post" target="i"></form>
<script>
async_test(t => {
window.onload = t.step_func(() => {
appHistory.onnavigate = t.step_func_done(() => {
assert_unreached("onnavigate should not have fired in source window");
});
iframe.contentWindow.appHistory.onnavigate = t.step_func(e => {
assert_equals(e.navigationType, "push");
assert_not_equals(e.formData, null);

iframe.onload = t.step_func(() => {
// Avoid the replace behavior that occurs if you navigate during the load handler
t.step_timeout(() => {
iframe.contentWindow.appHistory.onnavigate = t.step_func(e => {
assert_equals(e.navigationType, "push");
assert_equals(e.formData, null);
});

iframe.contentWindow.onhashchange = t.step_func(() => {
iframe.contentWindow.appHistory.onnavigate = t.step_func_done(e => {
assert_equals(e.navigationType, "traverse");
assert_equals(e.formData, null);
});

// 3: go back
iframe.contentWindow.history.back();
});

// 2: navigate from /common/blank.html?1-with-form-data to /common/blank.html?1#1-with-form-data
iframe.contentWindow.location.hash = "#1";
}, 0);
});
});

// 1: submit the form, navigating from /common/blank.html to /common/blank.html?1-with-form-data
form.submit();
});
}, "reloading a page created from form submission results in formData of null, not the original form data");
</script>
7 changes: 4 additions & 3 deletions app-history/navigate-event/navigate-form-userInitiated.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<form id="form" action="">
<form id="form" method="post" action="">
<input id="submit" type="submit" value="Submit">
</form>
<script>
async_test(t => {
appHistory.onnavigate = t.step_func_done(e => {
e.preventDefault();

assert_equals(e.navigationType, "push");
assert_true(e.cancelable);
assert_true(e.canRespond);
assert_true(e.userInitiated);
assert_false(e.hashChange);
assert_equals(e.destination.url, location.href + "?");
assert_equals(e.destination.url, location.href);
assert_false(e.destination.sameDocument);
assert_equals(e.destination.key, null);
assert_equals(e.destination.id, null);
assert_equals(e.destination.index, -1);
assert_not_equals(e.formData, null);
e.preventDefault();
});
window.onload = t.step_func(() => test_driver.click(submit));
}, "<form> submission fires navigate event");
Expand Down
9 changes: 3 additions & 6 deletions app-history/navigate-event/navigate-form-with-target.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<iframe id="iframe" name="i" src="/common/blank.html"></iframe>
<form id="form" action="foo.html" target="i"></form>
<form id="form" method="post" action="foo.html" target="i"></form>
<script>
async_test(t => {
window.onload = t.step_func(() => {
appHistory.onnavigate = t.step_func_done(() => {
assert_unreached("onnavigate should not have fired in source window");
});
appHistory.onnavigate = t.unreached_func("onnavigate should not have fired in source window");

iframe.contentWindow.appHistory.onnavigate = t.step_func_done(e => {
e.preventDefault();
assert_equals(e.navigationType, "push");
assert_true(e.cancelable);
assert_true(e.canRespond);
Expand All @@ -23,7 +21,6 @@
assert_equals(e.destination.id, null);
assert_equals(e.destination.index, -1);
assert_not_equals(e.formData, null);
e.preventDefault();
});
form.submit();
});
Expand Down
7 changes: 4 additions & 3 deletions app-history/navigate-event/navigate-form.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
<!doctype html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<form id="form" action=""></form>
<form id="form" method="post" action=""></form>
<script>
async_test(t => {
appHistory.onnavigate = t.step_func_done(e => {
e.preventDefault();

assert_equals(e.navigationType, "replace");
assert_true(e.cancelable);
assert_true(e.canRespond);
assert_false(e.userInitiated);
assert_false(e.hashChange);
assert_equals(e.destination.url, location.href + "?");
assert_equals(e.destination.url, location.href);
assert_false(e.destination.sameDocument);
assert_equals(e.destination.key, null);
assert_equals(e.destination.id, null);
assert_equals(e.destination.index, -1);
assert_not_equals(e.formData, null);
e.preventDefault();
});
window.onload = t.step_func(() => form.submit());
}, "<form> submission fires navigate event");
Expand Down

0 comments on commit bd68e11

Please sign in to comment.