Commit 13dbde4f authored by Xiaohan Wang's avatar Xiaohan Wang Committed by Commit Bot

media: Use NiceMocks in WebMediaPlayerImplTest

Currently we are using StrictMocks in several places which force us to
set several expectations unrelated to what is being tested, making the
code harder to read. This CL updates these cases to use NiceMocks to
solve this issue. Sepcific tests that do care about these mocks can
still use EXPECT_CALL to setup expectations.

This is also consistent with googlemock guidelines [1]:
"""
Our general recommendation is to use nice mocks (not yet the default)
most of the time, use naggy mocks (the current default) when developing
or debugging tests, and use strict mocks only as the last resort.
"""

[1] https://github.com/google/googletest/blob/master/googlemock/docs/cook_book.md#the-nice-the-strict-and-the-naggy-nicestrictnaggy

Bug: 1018372
Test: Test cleanup only
Change-Id: Ie639f2706f02c9701cb2f4ab2d2ed2658006fb32
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1879685Reviewed-by: default avatarMounir Lamouri <mlamouri@chromium.org>
Commit-Queue: Xiaohan Wang <xhwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#710857}
parent 24e516d8
......@@ -322,12 +322,24 @@ class WebMediaPlayerImplTest : public testing::Test {
media_thread_.StartAndWaitForTesting();
}
void InitializeWebMediaPlayerImpl() {
auto media_log = std::make_unique<NiceMock<MockMediaLog>>();
void InitializeSurfaceLayerBridge() {
surface_layer_bridge_ =
std::make_unique<StrictMock<MockSurfaceLayerBridge>>();
std::make_unique<NiceMock<MockSurfaceLayerBridge>>();
surface_layer_bridge_ptr_ = surface_layer_bridge_.get();
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
ON_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillByDefault(ReturnRef(surface_id_));
ON_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillByDefault(Return(base::TimeTicks()));
}
}
void InitializeWebMediaPlayerImpl() {
auto media_log = std::make_unique<NiceMock<MockMediaLog>>();
InitializeSurfaceLayerBridge();
// Retain a raw pointer to |media_log| for use by tests. Meanwhile, give its
// ownership to |wmpi_|. Reject attempts to reinitialize to prevent orphaned
// expectations on previous |media_log_|.
......@@ -381,7 +393,7 @@ class WebMediaPlayerImplTest : public testing::Test {
is_background_suspend_enabled_, is_background_video_playback_enabled_,
true);
auto compositor = std::make_unique<StrictMock<MockVideoFrameCompositor>>(
auto compositor = std::make_unique<NiceMock<MockVideoFrameCompositor>>(
params->video_frame_compositor_task_runner());
compositor_ = compositor.get();
......@@ -713,7 +725,7 @@ class WebMediaPlayerImplTest : public testing::Test {
blink::WebLocalFrame* web_local_frame_;
scoped_refptr<viz::TestContextProvider> context_provider_;
StrictMock<MockVideoFrameCompositor>* compositor_;
NiceMock<MockVideoFrameCompositor>* compositor_;
scoped_refptr<NiceMock<MockAudioRendererSink>> audio_sink_;
MockResourceFetchContext mock_resource_fetch_context_;
......@@ -737,8 +749,9 @@ class WebMediaPlayerImplTest : public testing::Test {
NiceMock<MockWebMediaPlayerDelegate> delegate_;
std::unique_ptr<StrictMock<MockSurfaceLayerBridge>> surface_layer_bridge_;
StrictMock<MockSurfaceLayerBridge>* surface_layer_bridge_ptr_ = nullptr;
// Use NiceMock since most tests do not care about this.
std::unique_ptr<NiceMock<MockSurfaceLayerBridge>> surface_layer_bridge_;
NiceMock<MockSurfaceLayerBridge>* surface_layer_bridge_ptr_ = nullptr;
// Only valid once set by InitializeWebMediaPlayerImpl(), this is for
// verifying a subset of potential media logs.
......@@ -946,17 +959,6 @@ TEST_F(WebMediaPlayerImplTest, LazyLoadPreloadMetadataSuspend) {
// Don't set poster, but ensure we still reach suspended state.
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
}
LoadAndWaitForReadyState(kVideoOnlyTestFile,
blink::WebMediaPlayer::kReadyStateHaveMetadata);
testing::Mock::VerifyAndClearExpectations(&client_);
......@@ -982,17 +984,6 @@ TEST_F(WebMediaPlayerImplTest, LoadPreloadMetadataSuspendNoVideoMemoryUsage) {
wmpi_->SetPreload(blink::WebMediaPlayer::kPreloadMetaData);
wmpi_->SetPoster(blink::WebURL(GURL("file://example.com/sample.jpg")));
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
}
LoadAndWaitForReadyState(kVideoOnlyTestFile,
blink::WebMediaPlayer::kReadyStateHaveMetadata);
testing::Mock::VerifyAndClearExpectations(&client_);
......@@ -1570,25 +1561,6 @@ TEST_F(WebMediaPlayerImplTest, Encrypted) {
TEST_F(WebMediaPlayerImplTest, Waiting_NoDecryptionKey) {
InitializeWebMediaPlayerImpl();
wmpi_->SetPreload(blink::WebMediaPlayer::kPreloadAuto);
scoped_refptr<cc::Layer> layer = cc::Layer::Create();
EXPECT_CALL(*surface_layer_bridge_ptr_, GetCcLayer())
.WillRepeatedly(Return(layer.get()));
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(true)).Times(1);
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false)).Times(1);
} else {
EXPECT_CALL(client_, SetCcLayer(NotNull()));
}
// Use non-encrypted file here since we don't have a CDM. Otherwise pipeline
// initialization will stall waiting for a CDM to be set.
......@@ -1598,8 +1570,6 @@ TEST_F(WebMediaPlayerImplTest, Waiting_NoDecryptionKey) {
EXPECT_CALL(encrypted_client_, DidResumePlaybackBlockedForKey());
OnWaiting(WaitingReason::kNoDecryptionKey);
EXPECT_CALL(*surface_layer_bridge_ptr_, ClearObserver());
}
TEST_F(WebMediaPlayerImplTest, VideoConfigChange) {
......@@ -1610,19 +1580,6 @@ TEST_F(WebMediaPlayerImplTest, VideoConfigChange) {
TestVideoConfig::NormalCodecProfile(kCodecVP9, VP9PROFILE_PROFILE0);
metadata.natural_size = gfx::Size(320, 240);
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
} else {
EXPECT_CALL(client_, SetCcLayer(NotNull()));
}
// Arrival of metadata should trigger creation of reporter with video config
// with profile matching test config.
OnMetadata(metadata);
......@@ -1666,19 +1623,6 @@ TEST_F(WebMediaPlayerImplTest, NaturalSizeChange) {
TestVideoConfig::NormalCodecProfile(kCodecVP8, VP8PROFILE_MIN);
metadata.natural_size = gfx::Size(320, 240);
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
} else {
EXPECT_CALL(client_, SetCcLayer(NotNull()));
}
OnMetadata(metadata);
ASSERT_EQ(blink::WebSize(320, 240), wmpi_->NaturalSize());
......@@ -1708,19 +1652,6 @@ TEST_F(WebMediaPlayerImplTest, NaturalSizeChange_Rotated) {
TestVideoConfig::NormalRotated(VIDEO_ROTATION_90);
metadata.natural_size = gfx::Size(320, 240);
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
} else {
EXPECT_CALL(client_, SetCcLayer(NotNull()));
}
OnMetadata(metadata);
ASSERT_EQ(blink::WebSize(320, 240), wmpi_->NaturalSize());
......@@ -1751,19 +1682,6 @@ TEST_F(WebMediaPlayerImplTest, VideoLockedWhenPausedWhenHidden) {
metadata.has_video = true;
metadata.video_decoder_config = TestVideoConfig::Normal();
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
} else {
EXPECT_CALL(client_, SetCcLayer(NotNull()));
}
OnMetadata(metadata);
EXPECT_FALSE(IsVideoLockedWhenPausedWhenHidden());
......@@ -1828,19 +1746,6 @@ TEST_F(WebMediaPlayerImplTest, InfiniteDuration) {
metadata.audio_decoder_config = TestAudioConfig::Normal();
metadata.natural_size = gfx::Size(400, 400);
if (base::FeatureList::IsEnabled(kUseSurfaceLayerForVideo)) {
EXPECT_CALL(client_, SetCcLayer(_)).Times(0);
EXPECT_CALL(*surface_layer_bridge_ptr_, CreateSurfaceLayer());
EXPECT_CALL(*surface_layer_bridge_ptr_, GetSurfaceId())
.WillOnce(ReturnRef(surface_id_));
EXPECT_CALL(*surface_layer_bridge_ptr_, GetLocalSurfaceIdAllocationTime())
.WillOnce(Return(base::TimeTicks()));
EXPECT_CALL(*compositor_, EnableSubmission(_, _, _, _));
EXPECT_CALL(*surface_layer_bridge_ptr_, SetContentsOpaque(false));
} else {
EXPECT_CALL(client_, SetCcLayer(NotNull()));
}
OnMetadata(metadata);
EXPECT_EQ(std::numeric_limits<double>::infinity(), wmpi_->Duration());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment