media_browsertest.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <fuchsia/mediacodec/cpp/fidl_test_base.h>
  6. #include "base/files/file_path.h"
  7. #include "base/fuchsia/scoped_service_binding.h"
  8. #include "base/fuchsia/test_component_context_for_process.h"
  9. #include "base/test/scoped_feature_list.h"
  10. #include "content/public/test/browser_test.h"
  11. #include "fuchsia_web/common/test/frame_test_util.h"
  12. #include "fuchsia_web/common/test/test_navigation_listener.h"
  13. #include "fuchsia_web/webengine/features.h"
  14. #include "fuchsia_web/webengine/test/frame_for_test.h"
  15. #include "fuchsia_web/webengine/test/test_data.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #include "third_party/abseil-cpp/absl/types/optional.h"
  18. namespace {
  19. // Currently, VP8 can only be decoded in software, and VP9 can be decoded in
  20. // hardware and software. The tests rely on this.
  21. constexpr char kLoadSoftwareOnlyCodecUrl[] = "/play_video.html?codecs=vp8";
  22. constexpr char kLoadHardwareAndSoftwareCodecUrl[] =
  23. "/play_video.html?codecs=vp9";
  24. constexpr char kCanPlaySoftwareOnlyCodecUrl[] = "/can_play_vp8.html";
  25. } // namespace
  26. class MediaTest : public WebEngineBrowserTest {
  27. public:
  28. MediaTest() { set_test_server_root(base::FilePath(kTestServerRoot)); }
  29. ~MediaTest() override = default;
  30. MediaTest(const MediaTest&) = delete;
  31. MediaTest& operator=(const MediaTest&) = delete;
  32. protected:
  33. void SetUpOnMainThread() override {
  34. CHECK(embedded_test_server()->Start());
  35. WebEngineBrowserTest::SetUpOnMainThread();
  36. }
  37. };
  38. using SoftwareOnlyDecodersEnabledTest = MediaTest;
  39. // MediaTest with kEnableSoftwareOnlyVideoCodecs disabled.
  40. class SoftwareOnlyDecodersDisabledTest : public MediaTest {
  41. public:
  42. SoftwareOnlyDecodersDisabledTest() = default;
  43. ~SoftwareOnlyDecodersDisabledTest() override = default;
  44. protected:
  45. void SetUp() override {
  46. scoped_feature_list_.InitAndDisableFeature(
  47. features::kEnableSoftwareOnlyVideoCodecs);
  48. MediaTest::SetUp();
  49. }
  50. base::test::ScopedFeatureList scoped_feature_list_;
  51. };
  52. // SoftwareOnlyDecodersDisabledTest with fuchsia.mediacodec.CodecFactory
  53. // disconnected.
  54. class SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest
  55. : public SoftwareOnlyDecodersDisabledTest {
  56. public:
  57. SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest() = default;
  58. ~SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest() override =
  59. default;
  60. protected:
  61. // Removes the decoder service to cause calls to it to fail.
  62. void SetUpOnMainThread() override {
  63. component_context_.emplace(
  64. base::TestComponentContextForProcess::InitialState::kCloneAll);
  65. component_context_->additional_services()
  66. ->RemovePublicService<fuchsia::mediacodec::CodecFactory>();
  67. SoftwareOnlyDecodersDisabledTest::SetUpOnMainThread();
  68. }
  69. // Used to disconnect fuchsia.mediacodec.CodecFactory.
  70. absl::optional<base::TestComponentContextForProcess> component_context_;
  71. };
  72. // Verify that a codec only supported by a software decoder is reported as
  73. // playable if kEnableSoftwareOnlyVideoCodecs is enabled.
  74. IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersEnabledTest,
  75. CanPlayTypeSoftwareOnlyCodecIsTrue) {
  76. const GURL kUrl(embedded_test_server()->GetURL(kCanPlaySoftwareOnlyCodecUrl));
  77. // TODO(crbug.com/1200314): Refactor these tests to use FrameForTest and
  78. // possibly to simplify the calls below since some of the details are not
  79. // interesting to the individual tests. In particular, has_user_activation is
  80. // more relevant than specific LoadUrlParams.
  81. auto frame =
  82. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  83. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  84. fuchsia::web::LoadUrlParams(),
  85. kUrl.spec()));
  86. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl,
  87. "can play vp8: true");
  88. }
  89. // Verify that a codec only supported by a software decoder is reported as not
  90. // playable if kEnableSoftwareOnlyVideoCodecs is disabled.
  91. IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersDisabledTest,
  92. CanPlayTypeSoftwareOnlyCodecIsFalse) {
  93. const GURL kUrl(embedded_test_server()->GetURL(kCanPlaySoftwareOnlyCodecUrl));
  94. auto frame =
  95. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  96. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  97. fuchsia::web::LoadUrlParams(),
  98. kUrl.spec()));
  99. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl,
  100. "can play vp8: false");
  101. }
  102. // Verify that a codec only supported by a software decoder is loaded if
  103. // kEnableSoftwareOnlyVideoCodecs is enabled.
  104. IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersEnabledTest,
  105. PlaySoftwareOnlyCodecSucceeds) {
  106. const GURL kUrl(embedded_test_server()->GetURL(kLoadSoftwareOnlyCodecUrl));
  107. auto frame =
  108. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  109. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  110. fuchsia::web::LoadUrlParams(),
  111. kUrl.spec()));
  112. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "loaded");
  113. }
  114. // Verify that a codec only supported by a software decoder is not loaded if
  115. // kEnableSoftwareOnlyVideoCodecs is disabled.
  116. IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersDisabledTest,
  117. LoadSoftwareOnlyCodecFails) {
  118. const GURL kUrl(embedded_test_server()->GetURL(kLoadSoftwareOnlyCodecUrl));
  119. auto frame =
  120. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  121. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  122. fuchsia::web::LoadUrlParams(),
  123. kUrl.spec()));
  124. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl,
  125. "media element error");
  126. }
  127. // Verify that a codec supported by hardware and software decoders plays if
  128. // kEnableSoftwareOnlyVideoCodecs is disabled.
  129. // Unlike the software-only codec, this codec loads because it is supposed to be
  130. // supported. It then plays because a hardware decoder is used (when on real
  131. // hardware) or it falls back to the software decoder (i.e., on emulator).
  132. IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersDisabledTest,
  133. PlayHardwareAndSoftwareCodecSucceeds) {
  134. const GURL kUrl(
  135. embedded_test_server()->GetURL(kLoadHardwareAndSoftwareCodecUrl));
  136. auto frame =
  137. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  138. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  139. CreateLoadUrlParamsWithUserActivation(),
  140. kUrl.spec()));
  141. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "loaded");
  142. ExecuteJavaScript(frame.get(), "bear.play()");
  143. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
  144. }
  145. // Verify that a codec supported by hardware and software plays if
  146. // kEnableSoftwareOnlyVideoCodecs is disabled and the hardware decoder fails.
  147. // Unlike the software-only codec, this codec loads because it is supposed to be
  148. // supported. It then plays because it falls back to the software decoder.
  149. IN_PROC_BROWSER_TEST_F(
  150. SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest,
  151. PlayHardwareAndSoftwareCodecFails) {
  152. const GURL kUrl(
  153. embedded_test_server()->GetURL(kLoadHardwareAndSoftwareCodecUrl));
  154. auto frame =
  155. FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());
  156. EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
  157. CreateLoadUrlParamsWithUserActivation(),
  158. kUrl.spec()));
  159. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "loaded");
  160. ExecuteJavaScript(frame.get(), "bear.play()");
  161. frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
  162. }