chromium/third_party/blink/renderer/modules/media_capabilities/media_capabilities_test.cc

// 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 "third_party/blink/renderer/modules/media_capabilities/media_capabilities.h"

#include <math.h>

#include <algorithm>

#include "base/memory/raw_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "media/base/media_switches.h"
#include "media/base/video_codecs.h"
#include "media/learning/common/media_learning_tasks.h"
#include "media/learning/common/target_histogram.h"
#include "media/learning/mojo/public/mojom/learning_task_controller.mojom-blink.h"
#include "media/mojo/clients/mojo_video_encoder_metrics_provider.h"
#include "media/mojo/mojom/media_metrics_provider.mojom-blink.h"
#include "media/mojo/mojom/media_types.mojom-blink.h"
#include "media/mojo/mojom/video_decode_perf_history.mojom-blink.h"
#include "media/mojo/mojom/watch_time_recorder.mojom-blink.h"
#include "media/mojo/mojom/webrtc_video_perf.mojom-blink.h"
#include "media/video/mock_gpu_video_accelerator_factories.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_tester.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_audio_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_capabilities_info.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_decoding_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_media_encoding_configuration.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_video_configuration.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/peerconnection/rtc_video_encoder_factory.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/text/string_view.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"
#include "third_party/googletest/src/googlemock/include/gmock/gmock-actions.h"
#include "ui/gfx/geometry/size.h"

FeatureValue;
ObservationCompletion;
TargetValue;
_;
InSequence;
Invoke;
Return;
Unused;

namespace blink {

namespace {

// Simulating the browser-side service.
class MockPerfHistoryService
    : public media::mojom::blink::VideoDecodePerfHistory {};

class MockWebrtcPerfHistoryService
    : public media::mojom::blink::WebrtcVideoPerfHistory {};

class MockLearningTaskControllerService
    : public media::learning::mojom::blink::LearningTaskController {};

class FakeMediaMetricsProvider
    : public media::mojom::blink::MediaMetricsProvider {};

// Simple helper for saving back-end callbacks for pending decodingInfo() calls.
// Callers can then manually fire the callbacks, gaining fine-grain control of
// the timing and order of their arrival.
class CallbackSaver {};

class MockPlatform : public TestingPlatformSupport {};

// This would typically be a test fixture, but we need it to be
// STACK_ALLOCATED() in order to use V8TestingScope, and we can't force that on
// whatever gtest class instantiates the fixture.
class MediaCapabilitiesTestContext {};

// |kVideoContentType|, |kCodec|, and |kCodecProfile| must match.
const char kVideoContentType[] =;
const char kAudioContentType[] =;
const media::VideoCodecProfile kCodecProfile =;
const media::VideoCodec kCodec =;
const double kFramerate =;
const int kWidth =;
const int kHeight =;
const int kBitrate =;
const char kWebrtcVideoContentType[] =;
const char kWebrtcAudioContentType[] =;

// Construct AudioConfig using the constants above.
template <class T>
T* CreateAudioConfig(const char content_type[], const char type[]) {}

// Construct media-source AudioConfig using the constants above.
MediaDecodingConfiguration* CreateAudioDecodingConfig() {}

// Construct webrtc decoding AudioConfig using the constants above.
MediaDecodingConfiguration* CreateWebrtcAudioDecodingConfig() {}

// Construct webrtc decoding AudioConfig using the constants above.
MediaEncodingConfiguration* CreateWebrtcAudioEncodingConfig() {}

// Construct VideoConfig using the constants above.
template <class T>
T* CreateVideoConfig(const char content_type[], const char type[]) {}

// Construct media-source VideoConfig using the constants above.
MediaDecodingConfiguration* CreateDecodingConfig() {}

// Construct webrtc decoding VideoConfig using the constants above.
MediaDecodingConfiguration* CreateWebrtcDecodingConfig() {}

// Construct webrtc encoding VideoConfig using the constants above.
MediaEncodingConfiguration* CreateWebrtcEncodingConfig() {}

// Construct PredicitonFeatures matching the CreateDecodingConfig, using the
// constants above.
media::mojom::blink::PredictionFeatures CreateFeatures() {}

Vector<media::learning::FeatureValue> CreateFeaturesML() {}

// Construct WebrtcPredicitonFeatures matching the CreateWebrtc{Decoding,
// Encoding}Config, using the constants above.
media::mojom::blink::WebrtcPredictionFeatures CreateWebrtcFeatures(
    bool is_decode) {}

// Types of smoothness predictions.
enum class PredictionType {};

// Makes a TargetHistogram with single count at |target_value|.
media::learning::TargetHistogram MakeHistogram(double target_value) {}

// Makes DB (PerfHistoryService) callback for use with gtest WillOnce().
// Callback will verify |features| matches |expected_features| and run with
// provided values for |is_smooth| and |is_power_efficient|.
testing::Action<void(media::mojom::blink::PredictionFeaturesPtr,
                     MockPerfHistoryService::GetPerfInfoCallback)>
DbCallback(const media::mojom::blink::PredictionFeatures& expected_features,
           bool is_smooth,
           bool is_power_efficient) {}

// Makes ML (LearningTaskControllerService) callback for use with gtest
// WillOnce(). Callback will verify |features| matches |expected_features| and
// run a TargetHistogram containing a single count for |histogram_target|.
testing::Action<void(
    const Vector<media::learning::FeatureValue>&,
    MockLearningTaskControllerService::PredictDistributionCallback predict_cb)>
MlCallback(const Vector<media::learning::FeatureValue>& expected_features,
           double histogram_target) {}

// Makes DB (WebrtcPerfHistoryService) callback for use with gtest WillOnce().
// Callback will verify |features| and |framerate| matches |expected_features|
// and |expected_framreate| and run with provided values for |is_smooth|.
testing::Action<void(media::mojom::blink::WebrtcPredictionFeaturesPtr,
                     int,
                     MockWebrtcPerfHistoryService::GetPerfInfoCallback)>
WebrtcDbCallback(
    const media::mojom::blink::WebrtcPredictionFeatures& expected_features,
    int expected_framerate,
    bool is_smooth) {}

testing::Action<void(base::OnceClosure)> GpuFactoriesNotifyCallback() {}

// Helper to constructs field trial params with given ML prediction thresholds.
base::FieldTrialParams MakeMlParams(double bad_window_threshold,
                                    double nnr_threshold) {}

// Wrapping decodingInfo() call for readability. Await resolution of the promise
// and return its info.
MediaCapabilitiesInfo* DecodingInfo(
    const MediaDecodingConfiguration* decoding_config,
    MediaCapabilitiesTestContext* context) {}

// Wrapping encodingInfo() call for readability. Await resolution of the promise
// and return its info.
MediaCapabilitiesInfo* EncodingInfo(
    const MediaEncodingConfiguration* encoding_config,
    MediaCapabilitiesTestContext* context) {}
}  // namespace

TEST(MediaCapabilitiesTests, BasicAudio) {}

// Other tests will assume these match. Test to be sure they stay in sync.
TEST(MediaCapabilitiesTests, ConfigMatchesFeatures) {}

// Test that non-integer framerate isn't truncated by IPC.
// https://crbug.com/1024399
TEST(MediaCapabilitiesTests, NonIntegerFramerate) {}

// Test smoothness predictions from DB (PerfHistoryService).
TEST(MediaCapabilitiesTests, PredictWithJustDB) {}

TEST(MediaCapabilitiesTests, PredictPowerEfficientWithGpuFactories) {}

// Test with smoothness predictions coming solely from "bad window" ML service.
TEST(MediaCapabilitiesTests, PredictWithBadWindowMLService) {}

// Test with smoothness predictions coming solely from "NNR" ML service.
TEST(MediaCapabilitiesTests, PredictWithNnrMLService) {}

// Test with combined smoothness predictions from both ML services.
TEST(MediaCapabilitiesTests, PredictWithBothMLServices) {}

// Simulate a call to DecodingInfo with smoothness predictions arriving in the
// specified |callback_order|. Ensure that promise resolves correctly only after
// all callbacks have arrived.
void RunCallbackPermutationTest(std::vector<PredictionType> callback_order) {}

// Test that decodingInfo() behaves correctly for all orderings/timings of the
// underlying prediction services.
TEST(MediaCapabilitiesTests, PredictionCallbackPermutations) {}

// WebRTC decodingInfo tests.
TEST(MediaCapabilitiesTests, WebrtcDecodingBasicAudio) {}

TEST(MediaCapabilitiesTests, WebrtcDecodingUnsupportedAudio) {}

// Other tests will assume these match. Test to be sure they stay in sync.
TEST(MediaCapabilitiesTests, WebrtcConfigMatchesFeatures) {}

// Test smoothness predictions from DB (WebrtcPerfHistoryService).
TEST(MediaCapabilitiesTests, WebrtcDecodingBasicVideo) {}

TEST(MediaCapabilitiesTests, WebrtcDecodingUnsupportedVideo) {}

TEST(MediaCapabilitiesTests, WebrtcDecodingSpatialScalability) {}

// WebRTC encodingInfo tests.
TEST(MediaCapabilitiesTests, WebrtcEncodingBasicAudio) {}

TEST(MediaCapabilitiesTests, WebrtcEncodingUnsupportedAudio) {}

// Test smoothness predictions from DB (WebrtcPerfHistoryService).
TEST(MediaCapabilitiesTests, WebrtcEncodingBasicVideo) {}

TEST(MediaCapabilitiesTests, WebrtcEncodingUnsupportedVideo) {}

TEST(MediaCapabilitiesTests, WebrtcEncodingScalabilityMode) {}

TEST(MediaCapabilitiesTests, WebrtcDecodePowerEfficientIsSmooth) {}

TEST(MediaCapabilitiesTests, WebrtcDecodeOverridePowerEfficientIsSmooth) {}

TEST(MediaCapabilitiesTests, WebrtcEncodePowerEfficientIsSmooth) {}

TEST(MediaCapabilitiesTests, WebrtcEncodeOverridePowerEfficientIsSmooth) {}

}  // namespace blink