123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "content/public/test/browser_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/frame_impl_browser_test_base.h"
- #include "fuchsia_web/webengine/test/frame_for_test.h"
- namespace {
- constexpr char kPage1Path[] = "/title1.html";
- constexpr char kPage2Path[] = "/title2.html";
- constexpr char kPage1Title[] = "title 1";
- constexpr char kPopupParentPath[] = "/popup_parent.html";
- constexpr char kPopupRedirectPath[] = "/popup_child.html";
- constexpr char kPopupMultiplePath[] = "/popup_multiple.html";
- constexpr char kChildQueryParamName[] = "child_url";
- constexpr char kPopupChildFile[] = "popup_child.html";
- constexpr char kAutoplayFileAndQuery[] =
- "play_video.html?autoplay=1&codecs=vp8";
- constexpr char kAutoPlayBlockedTitle[] = "blocked";
- constexpr char kAutoPlaySuccessTitle[] = "playing";
- class TestPopupListener : public fuchsia::web::PopupFrameCreationListener {
- public:
- TestPopupListener() = default;
- ~TestPopupListener() override = default;
- TestPopupListener(const TestPopupListener&) = delete;
- TestPopupListener& operator=(const TestPopupListener&) = delete;
- void GetAndAckNextPopup(fuchsia::web::FramePtr* frame,
- fuchsia::web::PopupFrameCreationInfo* creation_info) {
- if (!frame_) {
- base::RunLoop run_loop;
- received_popup_callback_ = run_loop.QuitClosure();
- run_loop.Run();
- }
- *frame = frame_.Bind();
- *creation_info = std::move(creation_info_);
- popup_ack_callback_();
- popup_ack_callback_ = {};
- }
- private:
- void OnPopupFrameCreated(fidl::InterfaceHandle<fuchsia::web::Frame> frame,
- fuchsia::web::PopupFrameCreationInfo creation_info,
- OnPopupFrameCreatedCallback callback) override {
- creation_info_ = std::move(creation_info);
- frame_ = std::move(frame);
- popup_ack_callback_ = std::move(callback);
- if (received_popup_callback_)
- std::move(received_popup_callback_).Run();
- }
- fidl::InterfaceHandle<fuchsia::web::Frame> frame_;
- fuchsia::web::PopupFrameCreationInfo creation_info_;
- base::OnceClosure received_popup_callback_;
- OnPopupFrameCreatedCallback popup_ack_callback_;
- };
- class PopupTest : public FrameImplTestBaseWithServer {
- public:
- PopupTest()
- : popup_listener_binding_(&popup_listener_),
- popup_nav_listener_binding_(&popup_nav_listener_) {}
- ~PopupTest() override = default;
- PopupTest(const PopupTest&) = delete;
- PopupTest& operator=(const PopupTest&) = delete;
- protected:
- // Builds a URL for the kPopupParentPath page to pop up a Frame with
- // |child_file_and_query|. |child_file_and_query| may optionally include a
- // query string.
- GURL GetParentPageTestServerUrl(const char* child) const {
- const std::string url = base::StringPrintf("%s?%s=%s", kPopupParentPath,
- kChildQueryParamName, child);
- return embedded_test_server()->GetURL(url);
- }
- // Loads a page that autoplays video in a popup, populates the popup_*
- // members, and returns its URL.
- GURL LoadAutoPlayingPageInPopup(
- fuchsia::web::CreateFrameParams parent_frame_params) {
- GURL popup_parent_url = GetParentPageTestServerUrl(kAutoplayFileAndQuery);
- GURL popup_child_url = embedded_test_server()->GetURL(
- base::StringPrintf("/%s", kAutoplayFileAndQuery));
- auto parent_frame =
- FrameForTest::Create(context(), std::move(parent_frame_params));
- parent_frame->SetPopupFrameCreationListener(
- popup_listener_binding_.NewBinding());
- EXPECT_TRUE(LoadUrlAndExpectResponse(parent_frame.GetNavigationController(),
- {}, popup_parent_url.spec()));
- fuchsia::web::PopupFrameCreationInfo popup_info;
- popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
- EXPECT_EQ(popup_info.initial_url(), popup_child_url);
- popup_frame_->SetNavigationEventListener2(
- popup_nav_listener_binding_.NewBinding(), /*flags=*/{});
- return popup_child_url;
- }
- fuchsia::web::FramePtr popup_frame_;
- TestPopupListener popup_listener_;
- fidl::Binding<fuchsia::web::PopupFrameCreationListener>
- popup_listener_binding_;
- TestNavigationListener popup_nav_listener_;
- fidl::Binding<fuchsia::web::NavigationEventListener>
- popup_nav_listener_binding_;
- };
- IN_PROC_BROWSER_TEST_F(PopupTest, PopupWindowRedirect) {
- GURL popup_parent_url = GetParentPageTestServerUrl(kPopupChildFile);
- GURL popup_child_url(embedded_test_server()->GetURL(kPopupRedirectPath));
- GURL title1_url(embedded_test_server()->GetURL(kPage1Path));
- auto frame = FrameForTest::Create(context(), {});
- frame->SetPopupFrameCreationListener(popup_listener_binding_.NewBinding());
- EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
- popup_parent_url.spec()));
- // Verify the popup's initial URL, "popup_child.html".
- fuchsia::web::PopupFrameCreationInfo popup_info;
- popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
- EXPECT_EQ(popup_info.initial_url(), popup_child_url);
- // Verify that the popup eventually redirects to "title1.html".
- popup_frame_->SetNavigationEventListener2(
- popup_nav_listener_binding_.NewBinding(), /*flags=*/{});
- popup_nav_listener_.RunUntilUrlAndTitleEquals(title1_url, kPage1Title);
- }
- IN_PROC_BROWSER_TEST_F(PopupTest, MultiplePopups) {
- GURL popup_parent_url(embedded_test_server()->GetURL(kPopupMultiplePath));
- GURL title1_url(embedded_test_server()->GetURL(kPage1Path));
- GURL title2_url(embedded_test_server()->GetURL(kPage2Path));
- auto frame = FrameForTest::Create(context(), {});
- frame->SetPopupFrameCreationListener(popup_listener_binding_.NewBinding());
- EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
- popup_parent_url.spec()));
- fuchsia::web::PopupFrameCreationInfo popup_info;
- popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
- EXPECT_EQ(popup_info.initial_url(), title1_url);
- popup_listener_.GetAndAckNextPopup(&popup_frame_, &popup_info);
- EXPECT_EQ(popup_info.initial_url(), title2_url);
- }
- // Verifies that the child popup Frame has the same default CreateFrameParams as
- // the parent Frame by verifying that autoplay is blocked in the child. This
- // mostly verifies that AutoPlaySucceedsis actually modifies behavior.
- IN_PROC_BROWSER_TEST_F(PopupTest,
- PopupFrameHasSameCreateFrameParams_AutoplayBlocked) {
- // The default autoplay_policy is REQUIRE_USER_ACTIVATION.
- fuchsia::web::CreateFrameParams parent_frame_params;
- // Load the page and wait for the popup Frame to be created.
- GURL popup_child_url =
- LoadAutoPlayingPageInPopup(std::move(parent_frame_params));
- // Verify that the child does not autoplay media.
- popup_nav_listener_.RunUntilUrlAndTitleEquals(popup_child_url,
- kAutoPlayBlockedTitle);
- }
- // Verifies that the child popup Frame has the same CreateFrameParams as the
- // parent Frame by allowing autoplay in the parent's params and verifying that
- // autoplay succeeds in the child.
- IN_PROC_BROWSER_TEST_F(PopupTest,
- PopupFrameHasSameCreateFrameParams_AutoplaySucceeds) {
- // Set autoplay to always be allowed in the parent frame.
- fuchsia::web::CreateFrameParams parent_frame_params;
- parent_frame_params.set_autoplay_policy(fuchsia::web::AutoplayPolicy::ALLOW);
- // Load the page and wait for the popup Frame to be created.
- GURL popup_child_url =
- LoadAutoPlayingPageInPopup(std::move(parent_frame_params));
- // Verify that the child autoplays media.
- popup_nav_listener_.RunUntilUrlAndTitleEquals(popup_child_url,
- kAutoPlaySuccessTitle);
- }
- } // namespace
|