Commit 0faa5e49 authored by ckitagawa's avatar ckitagawa Committed by Commit Bot

Fix NavigationControllerTest PostThenReplaceStateThenReload test

This test is passing, but doesn't actually do anything for a couple of
reasons:

1. The first POST commit used a deprecated style of navigation without a
   NavigationEntry this fails automatically with bad message 216 since
   cross-document navigations require a NavigationEntry in
   RenderFrameHostImpl::DidCommitNavigationInternal(). This is fixed
   by using NavigationSimulator.
2. In NavigationControllerImpl g_check_for_repost is set to false for
   all tests. Even if a repost happened in this test the repost dialog
   would never show or increment the counter. This is fixed by adding a
   Scoped*ForTesting class similar to those for thread restrictions to
   permit showing a repost dialog in a test.

I'm surprised the bad message didn't trigger a test failure...

Bug: 1095727
Change-Id: I32906017fe2b931d459544b27c437691b92e08a1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2247079Reviewed-by: default avatarCamille Lamy <clamy@chromium.org>
Commit-Queue: Calder Kitagawa <ckitagawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#779387}
parent f3c8e9b8
......@@ -533,6 +533,17 @@ base::Time NavigationControllerImpl::TimeSmoother::GetSmoothedTime(
return t;
}
NavigationControllerImpl::ScopedShowRepostDialogForTesting::
ScopedShowRepostDialogForTesting()
: was_disallowed_(g_check_for_repost) {
g_check_for_repost = true;
}
NavigationControllerImpl::ScopedShowRepostDialogForTesting::
~ScopedShowRepostDialogForTesting() {
g_check_for_repost = was_disallowed_;
}
NavigationControllerImpl::NavigationControllerImpl(
NavigationControllerDelegate* delegate,
BrowserContext* browser_context)
......
......@@ -335,6 +335,8 @@ class CONTENT_EXPORT NavigationControllerImpl : public NavigationController {
FRIEND_TEST_ALL_PREFIXES(TimeSmoother, SingleDuplicate);
FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ManyDuplicates);
FRIEND_TEST_ALL_PREFIXES(TimeSmoother, ClockBackwardsJump);
FRIEND_TEST_ALL_PREFIXES(NavigationControllerTest,
PostThenReplaceStateThenReload);
// Defines possible actions that are returned by
// DetermineActionForHistoryNavigation().
......@@ -359,6 +361,23 @@ class CONTENT_EXPORT NavigationControllerImpl : public NavigationController {
base::Time high_water_mark_;
};
// The repost dialog is suppressed during testing. However, it should be shown
// in some tests. This allows a test to elect to allow the repost dialog to
// show for a scoped duration.
class CONTENT_EXPORT ScopedShowRepostDialogForTesting {
public:
ScopedShowRepostDialogForTesting();
~ScopedShowRepostDialogForTesting();
ScopedShowRepostDialogForTesting(const ScopedShowRepostDialogForTesting&) =
delete;
ScopedShowRepostDialogForTesting& operator=(
const ScopedShowRepostDialogForTesting&) = delete;
private:
const bool was_disallowed_;
};
// Navigates in session history to the given index. If
// |sandbox_frame_tree_node_id| is valid, then this request came
// from a sandboxed iframe with top level navigation disallowed. This
......
......@@ -4079,38 +4079,36 @@ TEST_F(NavigationControllerTest, PostThenReplaceStateThenReload) {
contents()->SetDelegate(delegate.get());
// Submit a form.
GURL url("http://foo");
FrameHostMsg_DidCommitProvisionalLoad_Params params;
params.nav_entry_id = 0;
params.did_create_new_entry = true;
params.url = url;
params.origin = url::Origin::Create(url);
params.transition = ui::PAGE_TRANSITION_FORM_SUBMIT;
params.gesture = NavigationGestureUser;
params.page_state = PageState::CreateFromURL(url);
params.method = "POST";
params.post_id = 2;
main_test_rfh()->SendRendererInitiatedNavigationRequest(url, false);
main_test_rfh()->PrepareForCommit();
contents()->GetMainFrame()->SendNavigateWithParams(&params, false);
auto simulator = NavigationSimulatorImpl::CreateRendererInitiated(
GURL("http://foo"), main_test_rfh());
simulator->SetIsFormSubmission(true);
simulator->SetIsPostWithId(123);
simulator->Commit();
// Now reload. We should show repost warning dialog.
{
NavigationControllerImpl::ScopedShowRepostDialogForTesting show_repost;
controller_impl().Reload(ReloadType::NORMAL, true);
}
const int expected_repost_form_warning_count = 1;
EXPECT_EQ(expected_repost_form_warning_count,
delegate->repost_form_warning_count());
// history.replaceState() is called.
GURL replace_url("http://foo#foo");
params.nav_entry_id = 0;
params.did_create_new_entry = false;
params.url = replace_url;
params.origin = url::Origin::Create(replace_url);
params.transition = ui::PAGE_TRANSITION_LINK;
params.gesture = NavigationGestureUser;
params.page_state = PageState::CreateFromURL(replace_url);
params.method = "GET";
params.post_id = -1;
contents()->GetMainFrame()->SendNavigateWithParams(&params, true);
simulator = NavigationSimulatorImpl::CreateRendererInitiated(
GURL("http://foo#foo"), main_test_rfh());
simulator->set_did_create_new_entry(false);
simulator->Commit();
// Now reload. replaceState overrides the POST, so we should not show a
// repost warning dialog.
controller_impl().Reload(ReloadType::NORMAL, true);
EXPECT_EQ(0, delegate->repost_form_warning_count());
{
NavigationControllerImpl::ScopedShowRepostDialogForTesting show_repost;
controller_impl().Reload(ReloadType::NORMAL, true);
}
EXPECT_EQ(expected_repost_form_warning_count,
delegate->repost_form_warning_count());
}
TEST_F(NavigationControllerTest, UnreachableURLGivesErrorPage) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment