navigation_policy_browsertest.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/cpp/fidl.h>
  5. #include "content/public/test/browser_test.h"
  6. #include "fuchsia_web/common/test/frame_test_util.h"
  7. #include "fuchsia_web/common/test/test_navigation_listener.h"
  8. #include "fuchsia_web/webengine/browser/context_impl.h"
  9. #include "fuchsia_web/webengine/browser/fake_navigation_policy_provider.h"
  10. #include "fuchsia_web/webengine/browser/frame_impl.h"
  11. #include "fuchsia_web/webengine/browser/navigation_policy_handler.h"
  12. #include "fuchsia_web/webengine/test/frame_for_test.h"
  13. #include "fuchsia_web/webengine/test/test_data.h"
  14. #include "fuchsia_web/webengine/test/web_engine_browser_test.h"
  15. #include "net/test/embedded_test_server/embedded_test_server.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace {
  18. const char kPagePath[] = "/title1.html";
  19. const char kPageTitle[] = "title 1";
  20. } // namespace
  21. class NavigationPolicyTest : public WebEngineBrowserTest {
  22. public:
  23. NavigationPolicyTest() : policy_provider_binding_(&policy_provider_) {
  24. WebEngineBrowserTest::set_test_server_root(base::FilePath(kTestServerRoot));
  25. }
  26. ~NavigationPolicyTest() override = default;
  27. NavigationPolicyTest(const NavigationPolicyTest&) = delete;
  28. NavigationPolicyTest& operator=(const NavigationPolicyTest&) = delete;
  29. void SetUp() override { WebEngineBrowserTest::SetUp(); }
  30. void SetUpOnMainThread() override {
  31. frame_ = FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  32. // Spin the loop to allow the Create() request to be handled.
  33. base::RunLoop().RunUntilIdle();
  34. frame_impl_ = context_impl()->GetFrameImplForTest(&frame_.ptr());
  35. ASSERT_TRUE(embedded_test_server()->Start());
  36. // Register a NavigationPolicyProvider to the frame.
  37. fuchsia::web::NavigationPolicyProviderParams params;
  38. *params.mutable_main_frame_phases() = fuchsia::web::NavigationPhase::START;
  39. *params.mutable_subframe_phases() = fuchsia::web::NavigationPhase::REDIRECT;
  40. frame_->SetNavigationPolicyProvider(std::move(params),
  41. policy_provider_binding_.NewBinding());
  42. base::RunLoop().RunUntilIdle();
  43. ASSERT_TRUE(
  44. frame_impl_->navigation_policy_handler()->is_provider_connected());
  45. }
  46. protected:
  47. FrameForTest frame_;
  48. FrameImpl* frame_impl_ = nullptr;
  49. fidl::Binding<fuchsia::web::NavigationPolicyProvider>
  50. policy_provider_binding_;
  51. FakeNavigationPolicyProvider policy_provider_;
  52. };
  53. IN_PROC_BROWSER_TEST_F(NavigationPolicyTest, Proceed) {
  54. policy_provider_.set_should_abort_navigation(false);
  55. GURL page_url(embedded_test_server()->GetURL(std::string(kPagePath)));
  56. ASSERT_TRUE(LoadUrlAndExpectResponse(frame_.GetNavigationController(),
  57. fuchsia::web::LoadUrlParams(),
  58. page_url.spec()));
  59. frame_.navigation_listener().RunUntilUrlAndTitleEquals(page_url, kPageTitle);
  60. EXPECT_EQ(page_url.spec(), policy_provider_.requested_navigation()->url());
  61. }
  62. IN_PROC_BROWSER_TEST_F(NavigationPolicyTest, Deferred) {
  63. policy_provider_.set_should_abort_navigation(true);
  64. GURL page_url(embedded_test_server()->GetURL(std::string(kPagePath)));
  65. // Make sure the page has had time to load, but has not actually loaded, since
  66. // we cannot check for the absence of page data.
  67. ASSERT_TRUE(LoadUrlAndExpectResponse(frame_.GetNavigationController(),
  68. fuchsia::web::LoadUrlParams(),
  69. page_url.spec()));
  70. base::RunLoop run_loop;
  71. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  72. FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(200));
  73. run_loop.Run();
  74. // Make sure an up to date NavigationState is used.
  75. fuchsia::web::NavigationState state;
  76. state.set_url(page_url.spec());
  77. frame_.navigation_listener().RunUntilNavigationStateMatches(state);
  78. auto* current_state = frame_.navigation_listener().current_state();
  79. EXPECT_TRUE(current_state->has_is_main_document_loaded());
  80. EXPECT_FALSE(current_state->is_main_document_loaded());
  81. EXPECT_EQ(page_url.spec(), policy_provider_.requested_navigation()->url());
  82. }