autoplay_browsertest.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2019 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/test/web_engine_browser_test.h"
  5. #include "base/files/file_path.h"
  6. #include "content/public/test/browser_test.h"
  7. #include "fuchsia_web/common/test/frame_test_util.h"
  8. #include "fuchsia_web/common/test/test_navigation_listener.h"
  9. #include "fuchsia_web/webengine/browser/context_impl.h"
  10. #include "fuchsia_web/webengine/browser/frame_impl.h"
  11. #include "fuchsia_web/webengine/test/frame_for_test.h"
  12. #include "fuchsia_web/webengine/test/test_data.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. #include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom.h"
  15. namespace {
  16. constexpr char kAutoplayVp8Url[] = "/play_video.html?autoplay=1&codecs=vp8";
  17. } // namespace
  18. class AutoplayTest : public WebEngineBrowserTest {
  19. public:
  20. AutoplayTest() { set_test_server_root(base::FilePath(kTestServerRoot)); }
  21. ~AutoplayTest() override = default;
  22. AutoplayTest(const AutoplayTest&) = delete;
  23. AutoplayTest& operator=(const AutoplayTest&) = delete;
  24. void SetUpOnMainThread() override {
  25. EXPECT_TRUE(embedded_test_server()->Start());
  26. WebEngineBrowserTest::SetUpOnMainThread();
  27. }
  28. void SetUpCommandLine(base::CommandLine* command_line) final {
  29. SetHeadlessInCommandLine(command_line);
  30. WebEngineBrowserTest::SetUpCommandLine(command_line);
  31. }
  32. protected:
  33. // Creates a Frame with |navigation_listener_| attached and |policy|
  34. // applied.
  35. FrameForTest CreateFrame(fuchsia::web::AutoplayPolicy policy) {
  36. fuchsia::web::CreateFrameParams params;
  37. params.set_autoplay_policy(policy);
  38. auto frame = FrameForTest::Create(context(), std::move(params));
  39. return frame;
  40. }
  41. };
  42. IN_PROC_BROWSER_TEST_F(
  43. AutoplayTest,
  44. UserActivationPolicy_UserActivatedViaSimulatedInteraction) {
  45. const GURL kUrl(embedded_test_server()->GetURL(kAutoplayVp8Url));
  46. constexpr const char kPageLoadedTitle[] = "initial title";
  47. FrameForTest frame =
  48. CreateFrame(fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
  49. fuchsia::web::LoadUrlParams params;
  50. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(), {},
  51. kUrl.spec()));
  52. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, kPageLoadedTitle);
  53. context_impl()
  54. ->GetFrameImplForTest(&frame.ptr())
  55. ->web_contents_for_test()
  56. ->GetPrimaryMainFrame()
  57. ->NotifyUserActivation(
  58. blink::mojom::UserActivationNotificationType::kTest);
  59. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
  60. }
  61. IN_PROC_BROWSER_TEST_F(AutoplayTest,
  62. UserActivationPolicy_UserActivatedNavigation) {
  63. const GURL kUrl(embedded_test_server()->GetURL(kAutoplayVp8Url));
  64. FrameForTest frame =
  65. CreateFrame(fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
  66. fuchsia::web::LoadUrlParams params;
  67. params.set_was_user_activated(true);
  68. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  69. std::move(params), kUrl.spec()));
  70. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
  71. }
  72. IN_PROC_BROWSER_TEST_F(AutoplayTest, UserActivationPolicy_NoUserActivation) {
  73. const GURL kUrl(embedded_test_server()->GetURL(kAutoplayVp8Url));
  74. FrameForTest frame =
  75. CreateFrame(fuchsia::web::AutoplayPolicy::REQUIRE_USER_ACTIVATION);
  76. fuchsia::web::LoadUrlParams params;
  77. params.set_was_user_activated(false);
  78. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  79. std::move(params), kUrl.spec()));
  80. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "blocked");
  81. }
  82. IN_PROC_BROWSER_TEST_F(AutoplayTest,
  83. AllowAllPolicy_DefaultNotUserActivatedNavigation) {
  84. const GURL kUrl(embedded_test_server()->GetURL(kAutoplayVp8Url));
  85. FrameForTest frame = CreateFrame(fuchsia::web::AutoplayPolicy::ALLOW);
  86. // The page is deliberately not user activated.
  87. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  88. fuchsia::web::LoadUrlParams(),
  89. kUrl.spec()));
  90. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
  91. }