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

Side by Side Diff: components/audio_modem/audio_player_unittest.cc

Issue 1806313003: Pass task runners to AudioManager constructor. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: speculative hack to fix test timeouts Created 4 years, 8 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/audio_modem/audio_player.h" 5 #include "components/audio_modem/audio_player.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
12 #include "components/audio_modem/audio_player_impl.h" 13 #include "components/audio_modem/audio_player_impl.h"
13 #include "components/audio_modem/public/audio_modem_types.h" 14 #include "components/audio_modem/public/audio_modem_types.h"
14 #include "components/audio_modem/test/random_samples.h" 15 #include "components/audio_modem/test/random_samples.h"
15 #include "media/audio/audio_manager.h" 16 #include "media/audio/audio_manager.h"
16 #include "media/audio/audio_manager_base.h" 17 #include "media/audio/audio_manager_base.h"
17 #include "media/base/audio_bus.h" 18 #include "media/base/audio_bus.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 namespace { 21 namespace {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 }; 71 };
71 72
72 } // namespace 73 } // namespace
73 74
74 namespace audio_modem { 75 namespace audio_modem {
75 76
76 class AudioPlayerTest : public testing::Test, 77 class AudioPlayerTest : public testing::Test,
77 public base::SupportsWeakPtr<AudioPlayerTest> { 78 public base::SupportsWeakPtr<AudioPlayerTest> {
78 public: 79 public:
79 AudioPlayerTest() : buffer_index_(0), player_(nullptr) { 80 AudioPlayerTest() : buffer_index_(0), player_(nullptr) {
80 if (!media::AudioManager::Get()) 81 audio_manager_ =
81 media::AudioManager::CreateForTesting(); 82 media::AudioManager::CreateForTesting(message_loop_.task_runner());
82 } 83 }
83 84
84 ~AudioPlayerTest() override { DeletePlayer(); } 85 ~AudioPlayerTest() override { DeletePlayer(); }
85 86
86 void CreatePlayer() { 87 void CreatePlayer() {
87 DeletePlayer(); 88 DeletePlayer();
88 player_ = new AudioPlayerImpl(); 89 player_ = new AudioPlayerImpl();
89 player_->set_output_stream_for_testing(new TestAudioOutputStream( 90 player_->set_output_stream_for_testing(new TestAudioOutputStream(
90 kDefaultFrameCount, 91 kDefaultFrameCount,
91 kMaxFrameCount, 92 kMaxFrameCount,
92 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr()))); 93 base::Bind(&AudioPlayerTest::GatherSamples, AsWeakPtr())));
93 player_->Initialize(); 94 player_->Initialize();
94 } 95 }
95 96
96 void DeletePlayer() { 97 void DeletePlayer() {
97 if (!player_) 98 if (!player_)
98 return; 99 return;
99 player_->Finalize(); 100 player_->Finalize();
100 player_ = nullptr; 101 player_ = nullptr;
102 base::RunLoop().RunUntilIdle();
101 } 103 }
102 104
103 void PlayAndVerifySamples( 105 void PlayAndVerifySamples(
104 const scoped_refptr<media::AudioBusRefCounted>& samples) { 106 const scoped_refptr<media::AudioBusRefCounted>& samples) {
105 DCHECK_LT(samples->frames(), kMaxFrameCount); 107 DCHECK_LT(samples->frames(), kMaxFrameCount);
106 108
107 buffer_ = media::AudioBus::Create(1, kMaxFrameCount); 109 buffer_ = media::AudioBus::Create(1, kMaxFrameCount);
108 buffer_index_ = 0; 110 buffer_index_ = 0;
109 player_->Play(samples); 111 player_->Play(samples);
110 player_->FlushAudioLoopForTesting(); 112 base::RunLoop().RunUntilIdle();
111 player_->Stop(); 113 player_->Stop();
112 114
113 int differences = 0; 115 int differences = 0;
114 for (int i = 0; i < kMaxFrameCount; ++i) { 116 for (int i = 0; i < kMaxFrameCount; ++i) {
115 differences += (buffer_->channel(0)[i] != 117 differences += (buffer_->channel(0)[i] !=
116 samples->channel(0)[i % samples->frames()]); 118 samples->channel(0)[i % samples->frames()]);
117 } 119 }
118 ASSERT_EQ(0, differences); 120 ASSERT_EQ(0, differences);
119 121
120 buffer_.reset(); 122 buffer_.reset();
121 } 123 }
122 124
123 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) { 125 void GatherSamples(scoped_ptr<media::AudioBus> bus, int frames) {
124 if (!buffer_.get()) 126 if (!buffer_.get())
125 return; 127 return;
126 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get()); 128 bus->CopyPartialFramesTo(0, frames, buffer_index_, buffer_.get());
127 buffer_index_ += frames; 129 buffer_index_ += frames;
128 } 130 }
129 131
130 protected: 132 protected:
131 bool IsPlaying() { 133 bool IsPlaying() {
132 player_->FlushAudioLoopForTesting(); 134 base::RunLoop().RunUntilIdle();
133 return player_->is_playing_; 135 return player_->is_playing_;
134 } 136 }
135 137
136 static const int kDefaultFrameCount = 1024; 138 static const int kDefaultFrameCount = 1024;
137 static const int kMaxFrameCount = 1024 * 100; 139 static const int kMaxFrameCount = 1024 * 100;
138 140
141 base::MessageLoop message_loop_;
142 media::ScopedAudioManagerPtr audio_manager_;
139 scoped_ptr<media::AudioBus> buffer_; 143 scoped_ptr<media::AudioBus> buffer_;
140 int buffer_index_; 144 int buffer_index_;
141 145
142 // Deleted by calling Finalize() on the object. 146 // Deleted by calling Finalize() on the object.
143 AudioPlayerImpl* player_; 147 AudioPlayerImpl* player_;
144 base::MessageLoop message_loop_;
145 }; 148 };
146 149
147 TEST_F(AudioPlayerTest, BasicPlayAndStop) { 150 TEST_F(AudioPlayerTest, BasicPlayAndStop) {
148 CreatePlayer(); 151 CreatePlayer();
149 scoped_refptr<media::AudioBusRefCounted> samples = 152 scoped_refptr<media::AudioBusRefCounted> samples =
150 media::AudioBusRefCounted::Create(1, 7331); 153 media::AudioBusRefCounted::Create(1, 7331);
151 154
152 player_->Play(samples); 155 player_->Play(samples);
153 EXPECT_TRUE(IsPlaying()); 156 EXPECT_TRUE(IsPlaying());
154 player_->Stop(); 157 player_->Stop();
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 208
206 PlayAndVerifySamples( 209 PlayAndVerifySamples(
207 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123)); 210 CreateRandomAudioRefCounted(0x7331, 1, kNumSamples - 3123));
208 211
209 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2)); 212 PlayAndVerifySamples(CreateRandomAudioRefCounted(0xf00d, 1, kNumSamples * 2));
210 213
211 DeletePlayer(); 214 DeletePlayer();
212 } 215 }
213 216
214 } // namespace audio_modem 217 } // namespace audio_modem
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698