popup_browsertest.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright 2021 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 "content/public/test/browser_test.h"
  5. #include "fuchsia_web/common/test/frame_test_util.h"
  6. #include "fuchsia_web/common/test/test_navigation_listener.h"
  7. #include "fuchsia_web/webengine/browser/frame_impl_browser_test_base.h"
  8. #include "fuchsia_web/webengine/test/frame_for_test.h"
  9. namespace {
  10. constexpr char kPage1Path[] = "/title1.html";
  11. constexpr char kPage2Path[] = "/title2.html";
  12. constexpr char kPage1Title[] = "title 1";
  13. constexpr char kPopupParentPath[] = "/popup_parent.html";
  14. constexpr char kPopupRedirectPath[] = "/popup_child.html";
  15. constexpr char kPopupMultiplePath[] = "/popup_multiple.html";
  16. constexpr char kChildQueryParamName[] = "child_url";
  17. constexpr char kPopupChildFile[] = "popup_child.html";
  18. constexpr char kAutoplayFileAndQuery[] =
  19. "play_video.html?autoplay=1&codecs=vp8";
  20. constexpr char kAutoPlayBlockedTitle[] = "blocked";
  21. constexpr char kAutoPlaySuccessTitle[] = "playing";
  22. class TestPopupListener : public fuchsia::web::PopupFrameCreationListener {
  23. public:
  24. TestPopupListener() = default;
  25. ~TestPopupListener() override = default;
  26. TestPopupListener(const TestPopupListener&) = delete;
  27. TestPopupListener& operator=(const TestPopupListener&) = delete;
  28. void GetAndAckNextPopup(fuchsia::web::FramePtr* frame,
  29. fuchsia::web::PopupFrameCreationInfo* creation_info) {
  30. if (!frame_) {
  31. base::RunLoop run_loop;
  32. received_popup_callback_ = run_loop.QuitClosure();
  33. run_loop.Run();
  34. }
  35. *frame = frame_.Bind();
  36. *creation_info = std::move(creation_info_);
  37. popup_ack_callback_();
  38. popup_ack_callback_ = {};
  39. }
  40. private:
  41. void OnPopupFrameCreated(fidl::InterfaceHandle<fuchsia::web::Frame> frame,
  42. fuchsia::web::PopupFrameCreationInfo creation_info,
  43. OnPopupFrameCreatedCallback callback) override {
  44. creation_info_ = std::move(creation_info);
  45. frame_ = std::move(frame);
  46. popup_ack_callback_ = std::move(callback);
  47. if (received_popup_callback_)
  48. std::move(received_popup_callback_).Run();
  49. }
  50. fidl::InterfaceHandle<fuchsia::web::Frame> frame_;
  51. fuchsia::web::PopupFrameCreationInfo creation_info_;
  52. base::OnceClosure received_popup_callback_;
  53. OnPopupFrameCreatedCallback popup_ack_callback_;
  54. };
  55. class PopupTest : public FrameImplTestBaseWithServer {
  56. public:
  57. PopupTest()
  58. : popup_listener_binding_(&popup_listener_),
  59. popup_nav_listener_binding_(&popup_nav_listener_) {}
  60. ~PopupTest() override = default;
  61. PopupTest(const PopupTest&) = delete;
  62. PopupTest& operator=(const PopupTest&) = delete;
  63. protected:
  64. // Builds a URL for the kPopupParentPath page to pop up a Frame with
  65. // |child_file_and_query|. |child_file_and_query| may optionally include a
  66. // query string.
  67. GURL GetParentPageTestServerUrl(const char* child) const {
  68. const std::string url = base::StringPrintf("%s?%s=%s", kPopupParentPath,
  69. kChildQueryParamName, child);
  70. return embedded_test_server()->GetURL(url);
  71. }
  72. // Loads a page that autoplays video in a popup, populates the popup_*
  73. // members, and returns its URL.
  74. GURL LoadAutoPlayingPageInPopup(
  75. fuchsia::web::CreateFrameParams parent_frame_params) {
  76. GURL popup_parent_url = GetParentPageTestServerUrl(kAutoplayFileAndQuery);
  77. GURL popup_child_url = embedded_test_server()->GetURL(
  78. base::StringPrintf("/%s", kAutoplayFileAndQuery));
  79. auto parent_frame =
  80. FrameForTest::Create(context(), std::move(parent_frame_params));
  81. parent_frame->SetPopupFrameCreationListener(
  82. popup_listener_binding_.NewBinding());
  83. EXPECT_TRUE(LoadUrlAndExpectResponse(parent_frame.GetNavigationController(),
  84. {}, popup_parent_url.spec()));
  85. fuchsia::web::PopupFrameCreationInfo popup_info;
  86. popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
  87. EXPECT_EQ(popup_info.initial_url(), popup_child_url);
  88. popup_frame_->SetNavigationEventListener2(
  89. popup_nav_listener_binding_.NewBinding(), /*flags=*/{});
  90. return popup_child_url;
  91. }
  92. fuchsia::web::FramePtr popup_frame_;
  93. TestPopupListener popup_listener_;
  94. fidl::Binding<fuchsia::web::PopupFrameCreationListener>
  95. popup_listener_binding_;
  96. TestNavigationListener popup_nav_listener_;
  97. fidl::Binding<fuchsia::web::NavigationEventListener>
  98. popup_nav_listener_binding_;
  99. };
  100. IN_PROC_BROWSER_TEST_F(PopupTest, PopupWindowRedirect) {
  101. GURL popup_parent_url = GetParentPageTestServerUrl(kPopupChildFile);
  102. GURL popup_child_url(embedded_test_server()->GetURL(kPopupRedirectPath));
  103. GURL title1_url(embedded_test_server()->GetURL(kPage1Path));
  104. auto frame = FrameForTest::Create(context(), {});
  105. frame->SetPopupFrameCreationListener(popup_listener_binding_.NewBinding());
  106. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  107. popup_parent_url.spec()));
  108. // Verify the popup's initial URL, "popup_child.html".
  109. fuchsia::web::PopupFrameCreationInfo popup_info;
  110. popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
  111. EXPECT_EQ(popup_info.initial_url(), popup_child_url);
  112. // Verify that the popup eventually redirects to "title1.html".
  113. popup_frame_->SetNavigationEventListener2(
  114. popup_nav_listener_binding_.NewBinding(), /*flags=*/{});
  115. popup_nav_listener_.RunUntilUrlAndTitleEquals(title1_url, kPage1Title);
  116. }
  117. IN_PROC_BROWSER_TEST_F(PopupTest, MultiplePopups) {
  118. GURL popup_parent_url(embedded_test_server()->GetURL(kPopupMultiplePath));
  119. GURL title1_url(embedded_test_server()->GetURL(kPage1Path));
  120. GURL title2_url(embedded_test_server()->GetURL(kPage2Path));
  121. auto frame = FrameForTest::Create(context(), {});
  122. frame->SetPopupFrameCreationListener(popup_listener_binding_.NewBinding());
  123. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  124. popup_parent_url.spec()));
  125. fuchsia::web::PopupFrameCreationInfo popup_info;
  126. popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
  127. EXPECT_EQ(popup_info.initial_url(), title1_url);
  128. popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
  129. EXPECT_EQ(popup_info.initial_url(), title2_url);
  130. }
  131. // Verifies that the child popup Frame has the same default CreateFrameParams as
  132. // the parent Frame by verifying that autoplay is blocked in the child. This
  133. // mostly verifies that AutoPlaySucceedsis actually modifies behavior.
  134. IN_PROC_BROWSER_TEST_F(PopupTest,
  135. PopupFrameHasSameCreateFrameParams_AutoplayBlocked) {
  136. // The default autoplay_policy is REQUIRE_USER_ACTIVATION.
  137. fuchsia::web::CreateFrameParams parent_frame_params;
  138. // Load the page and wait for the popup Frame to be created.
  139. GURL popup_child_url =
  140. LoadAutoPlayingPageInPopup(std::move(parent_frame_params));
  141. // Verify that the child does not autoplay media.
  142. popup_nav_listener_.RunUntilUrlAndTitleEquals(popup_child_url,
  143. kAutoPlayBlockedTitle);
  144. }
  145. // Verifies that the child popup Frame has the same CreateFrameParams as the
  146. // parent Frame by allowing autoplay in the parent's params and verifying that
  147. // autoplay succeeds in the child.
  148. IN_PROC_BROWSER_TEST_F(PopupTest,
  149. PopupFrameHasSameCreateFrameParams_AutoplaySucceeds) {
  150. // Set autoplay to always be allowed in the parent frame.
  151. fuchsia::web::CreateFrameParams parent_frame_params;
  152. parent_frame_params.set_autoplay_policy(fuchsia::web::AutoplayPolicy::ALLOW);
  153. // Load the page and wait for the popup Frame to be created.
  154. GURL popup_child_url =
  155. LoadAutoPlayingPageInPopup(std::move(parent_frame_params));
  156. // Verify that the child autoplays media.
  157. popup_nav_listener_.RunUntilUrlAndTitleEquals(popup_child_url,
  158. kAutoPlaySuccessTitle);
  159. }
  160. } // namespace