navigation_policy_throttle_unittest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "fuchsia_web/webengine/browser/navigation_policy_throttle.h"
  5. #include <fuchsia/web/cpp/fidl.h>
  6. #include <lib/fidl/cpp/binding.h>
  7. #include "base/test/task_environment.h"
  8. #include "content/public/test/mock_navigation_handle.h"
  9. #include "fuchsia_web/webengine/browser/fake_navigation_policy_provider.h"
  10. #include "fuchsia_web/webengine/browser/navigation_policy_handler.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace {
  13. const char kUrl1[] = "http://test.net/";
  14. const char kUrl2[] = "http://page.net/";
  15. void CheckRequestedNavigationFieldsEqual(
  16. fuchsia::web::RequestedNavigation* requested_navigation,
  17. const std::string& url,
  18. bool is_same_document) {
  19. ASSERT_TRUE(requested_navigation->has_url() &&
  20. requested_navigation->has_is_same_document());
  21. EXPECT_EQ(requested_navigation->url(), url);
  22. EXPECT_EQ(requested_navigation->is_same_document(), is_same_document);
  23. }
  24. } // namespace
  25. class MockNavigationPolicyHandle : public content::MockNavigationHandle {
  26. public:
  27. explicit MockNavigationPolicyHandle(const GURL& url)
  28. : content::MockNavigationHandle(url, nullptr) {}
  29. ~MockNavigationPolicyHandle() override = default;
  30. MockNavigationPolicyHandle(const MockNavigationPolicyHandle&) = delete;
  31. MockNavigationPolicyHandle& operator=(const MockNavigationPolicyHandle&) =
  32. delete;
  33. void set_is_main_frame(bool is_main_frame) { is_main_frame_ = is_main_frame; }
  34. bool IsInMainFrame() const override { return is_main_frame_; }
  35. private:
  36. bool is_main_frame_ = true;
  37. };
  38. class NavigationPolicyThrottleTest : public testing::Test {
  39. public:
  40. NavigationPolicyThrottleTest()
  41. : policy_provider_binding_(&policy_provider_) {}
  42. ~NavigationPolicyThrottleTest() override = default;
  43. NavigationPolicyThrottleTest(const NavigationPolicyThrottleTest&) = delete;
  44. NavigationPolicyThrottleTest& operator=(const NavigationPolicyThrottleTest&) =
  45. delete;
  46. void SetUp() override {
  47. fuchsia::web::NavigationPolicyProviderParams params;
  48. *params.mutable_main_frame_phases() =
  49. fuchsia::web::NavigationPhase::START |
  50. fuchsia::web::NavigationPhase::PROCESS_RESPONSE;
  51. *params.mutable_subframe_phases() =
  52. fuchsia::web::NavigationPhase::REDIRECT |
  53. fuchsia::web::NavigationPhase::FAIL;
  54. policy_handler_ = std::make_unique<NavigationPolicyHandler>(
  55. std::move(params), policy_provider_binding_.NewBinding());
  56. }
  57. FakeNavigationPolicyProvider* policy_provider() { return &policy_provider_; }
  58. protected:
  59. base::test::SingleThreadTaskEnvironment task_environment_{
  60. base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
  61. std::unique_ptr<NavigationPolicyHandler> policy_handler_;
  62. fidl::Binding<fuchsia::web::NavigationPolicyProvider>
  63. policy_provider_binding_;
  64. FakeNavigationPolicyProvider policy_provider_;
  65. };
  66. // The navigation is expected to be evaluated, based on the params and
  67. // NavigationPhase. The navigation is set to be aborted.
  68. TEST_F(NavigationPolicyThrottleTest, WillStartRequest_MainFrame) {
  69. MockNavigationPolicyHandle navigation_handle((GURL(kUrl1)));
  70. navigation_handle.set_is_same_document(true);
  71. policy_provider()->set_should_abort_navigation(true);
  72. NavigationPolicyThrottle throttle(&navigation_handle, policy_handler_.get());
  73. auto result = throttle.WillStartRequest();
  74. EXPECT_EQ(content::NavigationThrottle::DEFER, result);
  75. base::RunLoop run_loop;
  76. throttle.set_cancel_deferred_navigation_callback_for_testing(
  77. base::BindRepeating(
  78. [](base::RunLoop* run_loop,
  79. content::NavigationThrottle::ThrottleCheckResult result) {
  80. EXPECT_EQ(content::NavigationThrottle::CANCEL, result);
  81. run_loop->Quit();
  82. },
  83. base::Unretained(&run_loop)));
  84. run_loop.Run();
  85. CheckRequestedNavigationFieldsEqual(policy_provider()->requested_navigation(),
  86. kUrl1, true);
  87. EXPECT_EQ(policy_provider()->num_evaluated_navigations(), 1);
  88. }
  89. // Based on the params, the client is not interested in WillStartRequests for
  90. // subframes. It will not be evaluated and the navigation is expected to
  91. // proceed, even if the NavigationPolicyProvider is set to abort the current
  92. // request.
  93. TEST_F(NavigationPolicyThrottleTest, WillStartRequest_SubFrame) {
  94. MockNavigationPolicyHandle navigation_handle((GURL(kUrl2)));
  95. navigation_handle.set_is_main_frame(false);
  96. navigation_handle.set_is_same_document(false);
  97. policy_provider()->set_should_abort_navigation(true);
  98. NavigationPolicyThrottle throttle(&navigation_handle, policy_handler_.get());
  99. auto result = throttle.WillStartRequest();
  100. EXPECT_EQ(content::NavigationThrottle::PROCEED, result);
  101. }
  102. // This is equivalent to WillStartRequest_SubFrame with a different
  103. // NavigationPhase.
  104. TEST_F(NavigationPolicyThrottleTest, WillRedirectRequest) {
  105. MockNavigationPolicyHandle navigation_handle((GURL(kUrl2)));
  106. navigation_handle.set_is_same_document(false);
  107. policy_provider()->set_should_abort_navigation(true);
  108. NavigationPolicyThrottle throttle(&navigation_handle, policy_handler_.get());
  109. auto result = throttle.WillRedirectRequest();
  110. EXPECT_EQ(content::NavigationThrottle::PROCEED, result);
  111. }
  112. // The navigation will be evaluated, and will proceed due to the value set in
  113. // |policy_provider_|.
  114. TEST_F(NavigationPolicyThrottleTest, WillFailRequest) {
  115. MockNavigationPolicyHandle navigation_handle((GURL(kUrl1)));
  116. navigation_handle.set_is_main_frame(false);
  117. navigation_handle.set_is_same_document(true);
  118. policy_provider()->set_should_abort_navigation(false);
  119. NavigationPolicyThrottle throttle(&navigation_handle, policy_handler_.get());
  120. auto result = throttle.WillFailRequest();
  121. EXPECT_EQ(content::NavigationThrottle::DEFER, result);
  122. base::RunLoop run_loop;
  123. throttle.set_resume_callback_for_testing(run_loop.QuitClosure());
  124. run_loop.Run();
  125. CheckRequestedNavigationFieldsEqual(policy_provider()->requested_navigation(),
  126. kUrl1, true);
  127. EXPECT_EQ(policy_provider()->num_evaluated_navigations(), 1);
  128. }
  129. // This navigation will be evaluated and will proceed.
  130. TEST_F(NavigationPolicyThrottleTest, WillProcessResponse) {
  131. MockNavigationPolicyHandle navigation_handle((GURL(kUrl2)));
  132. navigation_handle.set_is_same_document(true);
  133. policy_provider()->set_should_abort_navigation(false);
  134. NavigationPolicyThrottle throttle(&navigation_handle, policy_handler_.get());
  135. auto result = throttle.WillProcessResponse();
  136. EXPECT_EQ(content::NavigationThrottle::DEFER, result);
  137. base::RunLoop run_loop;
  138. throttle.set_resume_callback_for_testing(run_loop.QuitClosure());
  139. run_loop.Run();
  140. CheckRequestedNavigationFieldsEqual(policy_provider()->requested_navigation(),
  141. kUrl2, true);
  142. EXPECT_EQ(policy_provider()->num_evaluated_navigations(), 1);
  143. }