Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: media/base/silent_sink_suspender_unittest.cc

Issue 2382473002: Merge M54: "Break out WebAudio suspension code into new class. Add tests." (Closed)
Patch Set: Fix conflicts. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/base/silent_sink_suspender.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include "base/run_loop.h"
6 #include "base/test/test_message_loop.h"
7 #include "media/base/fake_audio_render_callback.h"
8 #include "media/base/mock_audio_renderer_sink.h"
9 #include "media/base/silent_sink_suspender.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace media {
14
15 ACTION_P(RunClosure, closure) {
16 closure.Run();
17 }
18
19 class SilentSinkSuspenderTest : public testing::Test {
20 public:
21 SilentSinkSuspenderTest()
22 : params_(AudioParameters::AUDIO_FAKE,
23 CHANNEL_LAYOUT_MONO,
24 44100,
25 8,
26 128),
27 mock_sink_(new testing::StrictMock<MockAudioRendererSink>()),
28 fake_callback_(0.1),
29 temp_bus_(AudioBus::Create(params_)),
30 // Set a negative timeout so any silence will suspend immediately.
31 suspender_(&fake_callback_,
32 base::TimeDelta::FromSeconds(-1),
33 params_,
34 mock_sink_,
35 test_loop_.task_runner()) {}
36 ~SilentSinkSuspenderTest() override {}
37
38 protected:
39 base::TestMessageLoop test_loop_;
40 const AudioParameters params_;
41 scoped_refptr<testing::StrictMock<MockAudioRendererSink>> mock_sink_;
42 FakeAudioRenderCallback fake_callback_;
43 std::unique_ptr<AudioBus> temp_bus_;
44 SilentSinkSuspender suspender_;
45
46 private:
47 DISALLOW_COPY_AND_ASSIGN(SilentSinkSuspenderTest);
48 };
49
50 TEST_F(SilentSinkSuspenderTest, BasicPassthough) {
51 temp_bus_->Zero();
52 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
53 EXPECT_FALSE(temp_bus_->AreFramesZero());
54 }
55
56 TEST_F(SilentSinkSuspenderTest, SuspendResumeTriggered) {
57 // Verify a normal Render() doesn't invoke suspend.
58 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing());
59 temp_bus_->Zero();
60 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
61 EXPECT_FALSE(temp_bus_->AreFramesZero());
62 base::RunLoop().RunUntilIdle();
63 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing());
64
65 // Mute all audio generated by the callback, this should suspend immediately.
66 fake_callback_.set_volume(0);
67 temp_bus_->Zero();
68 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
69 EXPECT_TRUE(temp_bus_->AreFramesZero());
70 {
71 base::RunLoop run_loop;
72 EXPECT_CALL(*mock_sink_, Pause())
73 .WillOnce(RunClosure(run_loop.QuitClosure()));
74 run_loop.Run();
75 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing());
76 }
77
78 // Unmute the audio, the FakeWorker inside |suspender_| should be running now,
79 // so we don't need to manually invoke Render().
80 fake_callback_.reset();
81 fake_callback_.set_volume(1);
82 {
83 base::RunLoop run_loop;
84 EXPECT_CALL(*mock_sink_, Play())
85 .WillOnce(RunClosure(run_loop.QuitClosure()));
86 run_loop.Run();
87 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing());
88 }
89
90 // The first Render() after resume should return the first buffer which was
91 // not silent.
92 fake_callback_.reset();
93 std::unique_ptr<AudioBus> true_bus = AudioBus::Create(params_);
94 fake_callback_.Render(true_bus.get(), 0, 0);
95 EXPECT_FALSE(true_bus->AreFramesZero());
96 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
97 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus->channel(0),
98 temp_bus_->frames() * sizeof(float)),
99 0);
100 }
101
102 TEST_F(SilentSinkSuspenderTest, MultipleSuspend) {
103 // Mute all audio generated by the callback, this should suspend immediately.
104 fake_callback_.set_volume(0);
105 temp_bus_->Zero();
106 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
107 EXPECT_TRUE(temp_bus_->AreFramesZero());
108
109 // A second render should only result in a single Pause() call.
110 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
111
112 EXPECT_CALL(*mock_sink_, Pause());
113 base::RunLoop().RunUntilIdle();
114 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing());
115 }
116
117 TEST_F(SilentSinkSuspenderTest, MultipleResume) {
118 // Mute all audio generated by the callback, this should suspend immediately.
119 fake_callback_.set_volume(0);
120 temp_bus_->Zero();
121 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
122 EXPECT_TRUE(temp_bus_->AreFramesZero());
123 EXPECT_CALL(*mock_sink_, Pause());
124 base::RunLoop().RunUntilIdle();
125 EXPECT_TRUE(suspender_.is_using_fake_sink_for_testing());
126
127 // Unmute the data.
128 fake_callback_.set_volume(1);
129
130 // Prepare our equality testers.
131 fake_callback_.reset();
132 std::unique_ptr<AudioBus> true_bus1 = AudioBus::Create(params_);
133 fake_callback_.Render(true_bus1.get(), 0, 0);
134 std::unique_ptr<AudioBus> true_bus2 = AudioBus::Create(params_);
135 fake_callback_.Render(true_bus2.get(), 0, 0);
136 EXPECT_NE(memcmp(true_bus1->channel(0), true_bus2->channel(0),
137 true_bus1->frames() * sizeof(float)),
138 0);
139
140 // Reset the fake callback data generation and force two Render() calls before
141 // the sink can transition.
142 fake_callback_.reset();
143 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(nullptr, 0, 0));
144 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(nullptr, 0, 0));
145 EXPECT_CALL(*mock_sink_, Play());
146 base::RunLoop().RunUntilIdle();
147 EXPECT_FALSE(suspender_.is_using_fake_sink_for_testing());
148
149 // Each render after resuming should return one of the non-silent bus.
150 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
151 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus1->channel(0),
152 temp_bus_->frames() * sizeof(float)),
153 0);
154 EXPECT_EQ(temp_bus_->frames(), suspender_.Render(temp_bus_.get(), 0, 0));
155 EXPECT_EQ(memcmp(temp_bus_->channel(0), true_bus2->channel(0),
156 temp_bus_->frames() * sizeof(float)),
157 0);
158 }
159
160 } // namespace content
OLDNEW
« no previous file with comments | « media/base/silent_sink_suspender.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698