1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201

fuchsia_web / webengine / browser / media_browsertest.cc [blame]

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <fuchsia/mediacodec/cpp/fidl_test_base.h>

#include <optional>

#include "base/files/file_path.h"
#include "base/fuchsia/scoped_service_binding.h"
#include "base/fuchsia/test_component_context_for_process.h"
#include "base/test/scoped_feature_list.h"
#include "content/public/test/browser_test.h"
#include "fuchsia_web/common/test/frame_for_test.h"
#include "fuchsia_web/common/test/frame_test_util.h"
#include "fuchsia_web/common/test/test_navigation_listener.h"
#include "fuchsia_web/webengine/features.h"
#include "fuchsia_web/webengine/test/test_data.h"
#include "fuchsia_web/webengine/test/web_engine_browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Currently, VP8 can only be decoded in software, and VP9 can be decoded in
// hardware and software. The tests rely on this.
constexpr char kLoadSoftwareOnlyCodecUrl[] = "/play_video.html?codecs=vp8";
constexpr char kLoadHardwareAndSoftwareCodecUrl[] =
    "/play_video.html?codecs=vp9";
constexpr char kCanPlaySoftwareOnlyCodecUrl[] = "/can_play_vp8.html";

}  // namespace

class MediaTest : public WebEngineBrowserTest {
 public:
  MediaTest() { set_test_server_root(base::FilePath(kTestServerRoot)); }
  ~MediaTest() override = default;

  MediaTest(const MediaTest&) = delete;
  MediaTest& operator=(const MediaTest&) = delete;

 protected:
  void SetUpOnMainThread() override {
    CHECK(embedded_test_server()->Start());
    WebEngineBrowserTest::SetUpOnMainThread();
  }
};

using SoftwareOnlyDecodersEnabledTest = MediaTest;

// MediaTest with kEnableSoftwareOnlyVideoCodecs disabled.
class SoftwareOnlyDecodersDisabledTest : public MediaTest {
 public:
  SoftwareOnlyDecodersDisabledTest() = default;
  ~SoftwareOnlyDecodersDisabledTest() override = default;

 protected:
  void SetUp() override {
    scoped_feature_list_.InitAndDisableFeature(
        features::kEnableSoftwareOnlyVideoCodecs);
    MediaTest::SetUp();
  }

  base::test::ScopedFeatureList scoped_feature_list_;
};

// SoftwareOnlyDecodersDisabledTest with fuchsia.mediacodec.CodecFactory
// disconnected.
class SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest
    : public SoftwareOnlyDecodersDisabledTest {
 public:
  SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest() = default;
  ~SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest() override =
      default;

 protected:
  // Removes the decoder service to cause calls to it to fail.
  void SetUpOnMainThread() override {
    component_context_.emplace(
        base::TestComponentContextForProcess::InitialState::kCloneAll);
    component_context_->additional_services()
        ->RemovePublicService<fuchsia::mediacodec::CodecFactory>();

    SoftwareOnlyDecodersDisabledTest::SetUpOnMainThread();
  }

  // Used to disconnect fuchsia.mediacodec.CodecFactory.
  std::optional<base::TestComponentContextForProcess> component_context_;
};

// Verify that a codec only supported by a software decoder is reported as
// playable if kEnableSoftwareOnlyVideoCodecs is enabled.
IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersEnabledTest,
                       CanPlayTypeSoftwareOnlyCodecIsTrue) {
  const GURL kUrl(embedded_test_server()->GetURL(kCanPlaySoftwareOnlyCodecUrl));

  // TODO(crbug.com/40761737): Refactor these tests to use FrameForTest and
  // possibly to simplify the calls below since some of the details are not
  // interesting to the individual tests. In particular, has_user_activation is
  // more relevant than specific LoadUrlParams.
  auto frame =
      FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());

  EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
                                       fuchsia::web::LoadUrlParams(),
                                       kUrl.spec()));
  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl,
                                                        "can play vp8: true");
}

// Verify that a codec only supported by a software decoder is reported as not
// playable if kEnableSoftwareOnlyVideoCodecs is disabled.
IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersDisabledTest,
                       CanPlayTypeSoftwareOnlyCodecIsFalse) {
  const GURL kUrl(embedded_test_server()->GetURL(kCanPlaySoftwareOnlyCodecUrl));

  auto frame =
      FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());

  EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
                                       fuchsia::web::LoadUrlParams(),
                                       kUrl.spec()));
  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl,
                                                        "can play vp8: false");
}

// Verify that a codec only supported by a software decoder is loaded if
// kEnableSoftwareOnlyVideoCodecs is enabled.
IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersEnabledTest,
                       PlaySoftwareOnlyCodecSucceeds) {
  const GURL kUrl(embedded_test_server()->GetURL(kLoadSoftwareOnlyCodecUrl));

  auto frame =
      FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());

  EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
                                       fuchsia::web::LoadUrlParams(),
                                       kUrl.spec()));
  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "loaded");
}

// Verify that a codec only supported by a software decoder is not loaded if
// kEnableSoftwareOnlyVideoCodecs is disabled.
IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersDisabledTest,
                       LoadSoftwareOnlyCodecFails) {
  const GURL kUrl(embedded_test_server()->GetURL(kLoadSoftwareOnlyCodecUrl));

  auto frame =
      FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());

  EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
                                       fuchsia::web::LoadUrlParams(),
                                       kUrl.spec()));
  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl,
                                                        "media element error");
}

// Verify that a codec supported by hardware and software decoders plays if
// kEnableSoftwareOnlyVideoCodecs is disabled.
// Unlike the software-only codec, this codec loads because it is supposed to be
// supported. It then plays because a hardware decoder is used (when on real
// hardware) or it falls back to the software decoder (i.e., on emulator).
IN_PROC_BROWSER_TEST_F(SoftwareOnlyDecodersDisabledTest,
                       PlayHardwareAndSoftwareCodecSucceeds) {
  const GURL kUrl(
      embedded_test_server()->GetURL(kLoadHardwareAndSoftwareCodecUrl));

  auto frame =
      FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());

  EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
                                       CreateLoadUrlParamsWithUserActivation(),
                                       kUrl.spec()));

  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "loaded");
  ExecuteJavaScript(frame.get(), "bear.play()");

  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
}

// Verify that a codec supported by hardware and software plays if
// kEnableSoftwareOnlyVideoCodecs is disabled and the hardware decoder fails.
// Unlike the software-only codec, this codec loads because it is supposed to be
// supported. It then plays because it falls back to the software decoder.
IN_PROC_BROWSER_TEST_F(
    SoftwareOnlyDecodersDisabledAndHardwareDecoderFailureTest,
    PlayHardwareAndSoftwareCodecFails) {
  const GURL kUrl(
      embedded_test_server()->GetURL(kLoadHardwareAndSoftwareCodecUrl));

  auto frame =
      FrameForTest::Create(context(), fuchsia::web::CreateFrameParams());

  EXPECT_TRUE(LoadUrlAndExpectResponse(frame.GetNavigationController(),
                                       CreateLoadUrlParamsWithUserActivation(),
                                       kUrl.spec()));

  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "loaded");
  ExecuteJavaScript(frame.get(), "bear.play()");

  frame.navigation_listener().RunUntilUrlAndTitleEquals(kUrl, "playing");
}