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
fuchsia_web / webengine / browser / navigation_policy_browsertest.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <fuchsia/web/cpp/fidl.h>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/test/browser_test.h"
#include "fuchsia_web/common/test/frame_for_test.h"
#include "fuchsia_web/common/test/frame_test_util.h"
#include "fuchsia_web/common/test/test_navigation_listener.h"
#include "fuchsia_web/webengine/browser/context_impl.h"
#include "fuchsia_web/webengine/browser/fake_navigation_policy_provider.h"
#include "fuchsia_web/webengine/browser/frame_impl.h"
#include "fuchsia_web/webengine/browser/navigation_policy_handler.h"
#include "fuchsia_web/webengine/test/test_data.h"
#include "fuchsia_web/webengine/test/web_engine_browser_test.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kPagePath[] = "/title1.html";
const char kPageTitle[] = "title 1";
} // namespace
class NavigationPolicyTest : public WebEngineBrowserTest {
public:
NavigationPolicyTest() : policy_provider_binding_(&policy_provider_) {
WebEngineBrowserTest::set_test_server_root(base::FilePath(kTestServerRoot));
}
~NavigationPolicyTest() override = default;
NavigationPolicyTest(const NavigationPolicyTest&) = delete;
NavigationPolicyTest& operator=(const NavigationPolicyTest&) = delete;
void SetUp() override { WebEngineBrowserTest::SetUp(); }
void SetUpOnMainThread() override {
frame_ = FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
// Spin the loop to allow the Create() request to be handled.
base::RunLoop().RunUntilIdle();
frame_impl_ = context_impl()->GetFrameImplForTest(&frame_.ptr());
ASSERT_TRUE(embedded_test_server()->Start());
// Register a NavigationPolicyProvider to the frame.
fuchsia::web::NavigationPolicyProviderParams params;
*params.mutable_main_frame_phases() = fuchsia::web::NavigationPhase::START;
*params.mutable_subframe_phases() = fuchsia::web::NavigationPhase::REDIRECT;
frame_->SetNavigationPolicyProvider(std::move(params),
policy_provider_binding_.NewBinding());
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(
frame_impl_->navigation_policy_handler()->is_provider_connected());
}
void TearDownOnMainThread() override {
frame_ = {};
WebEngineBrowserTest::TearDownOnMainThread();
}
protected:
FrameForTest frame_;
raw_ptr<FrameImpl> frame_impl_ = nullptr;
fidl::Binding<fuchsia::web::NavigationPolicyProvider>
policy_provider_binding_;
FakeNavigationPolicyProvider policy_provider_;
};
IN_PROC_BROWSER_TEST_F(NavigationPolicyTest, Proceed) {
policy_provider_.set_should_abort_navigation(false);
GURL page_url(embedded_test_server()->GetURL(std::string(kPagePath)));
ASSERT_TRUE(LoadUrlAndExpectResponse(frame_.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
page_url.spec()));
frame_.navigation_listener().RunUntilUrlAndTitleEquals(page_url, kPageTitle);
EXPECT_EQ(page_url.spec(), policy_provider_.requested_navigation()->url());
}
IN_PROC_BROWSER_TEST_F(NavigationPolicyTest, Deferred) {
policy_provider_.set_should_abort_navigation(true);
GURL page_url(embedded_test_server()->GetURL(std::string(kPagePath)));
// Make sure the page has had time to load, but has not actually loaded, since
// we cannot check for the absence of page data.
ASSERT_TRUE(LoadUrlAndExpectResponse(frame_.GetNavigationController(),
fuchsia::web::LoadUrlParams(),
page_url.spec()));
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(200));
run_loop.Run();
// Make sure an up to date NavigationState is used.
fuchsia::web::NavigationState state;
state.set_url(page_url.spec());
frame_.navigation_listener().RunUntilNavigationStateMatches(state);
auto* current_state = frame_.navigation_listener().current_state();
EXPECT_TRUE(current_state->has_is_main_document_loaded());
EXPECT_FALSE(current_state->is_main_document_loaded());
EXPECT_EQ(page_url.spec(), policy_provider_.requested_navigation()->url());
}