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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/audio/output_controller.h"
#include <stdint.h>
#include <memory>
#include <vector>
#include "base/barrier_closure.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback_helpers.h"
#include "base/check.h"
#include "base/environment.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/notreached.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_piece.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/test_message_loop.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "base/unguessable_token.h"
#include "media/audio/audio_device_description.h"
#include "media/audio/fake_audio_log_factory.h"
#include "media/audio/fake_audio_manager.h"
#include "media/audio/test_audio_thread.h"
#include "media/base/audio_bus.h"
#include "media/base/audio_parameters.h"
#include "services/audio/loopback_group_member.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::StrictMock;
using media::AudioBus;
using media::AudioManager;
using media::AudioOutputStream;
using media::AudioParameters;
using base::test::RunClosure;
using base::test::RunOnceClosure;
namespace audio {
namespace {
constexpr int kSampleRate = AudioParameters::kAudioCDSampleRate;
constexpr media::ChannelLayout kChannelLayout = media::CHANNEL_LAYOUT_STEREO;
constexpr int kSamplesPerPacket = kSampleRate / 1000;
constexpr double kTestVolume = 0.25;
constexpr float kBufferNonZeroData = 1.0f;
AudioParameters GetTestParams() {
// AudioManagerForControllerTest only creates FakeAudioOutputStreams
// behind-the-scenes. So, the use of PCM_LOW_LATENCY won't actually result in
// any real system audio output during these tests.
return AudioParameters(AudioParameters::AUDIO_PCM_LOW_LATENCY, kChannelLayout,
kSampleRate, kSamplesPerPacket);
}
class MockOutputControllerEventHandler : public OutputController::EventHandler {
public:
MockOutputControllerEventHandler() = default;
MOCK_METHOD0(OnControllerPlaying, void());
MOCK_METHOD0(OnControllerPaused, void());
MOCK_METHOD0(OnControllerError, void());
void OnLog(base::StringPiece) override {}
private:
DISALLOW_COPY_AND_ASSIGN(MockOutputControllerEventHandler);
};
class MockOutputControllerSyncReader : public OutputController::SyncReader {
public:
MockOutputControllerSyncReader() = default;
MOCK_METHOD3(RequestMoreData,
void(base::TimeDelta delay,
base::TimeTicks delay_timestamp,
int prior_frames_skipped));
MOCK_METHOD1(Read, void(AudioBus* dest));
MOCK_METHOD0(Close, void());
private:
DISALLOW_COPY_AND_ASSIGN(MockOutputControllerSyncReader);
};
// Wraps an AudioOutputStream instance, calling DidXYZ() mock methods for test
// verification of controller behavior. If a null AudioOutputStream pointer is
// provided to the constructor, a "data pump" thread will be run between the
// Start() and Stop() calls to simulate an AudioOutputStream not owned by the
// AudioManager.
class MockAudioOutputStream : public AudioOutputStream,
public AudioOutputStream::AudioSourceCallback {
public:
MockAudioOutputStream(AudioOutputStream* impl, AudioParameters::Format format)
: impl_(impl), format_(format) {}
AudioParameters::Format format() const { return format_; }
void set_close_callback(base::OnceClosure callback) {
close_callback_ = std::move(callback);
}
// We forward to a fake stream to get automatic OnMoreData callbacks,
// required by some tests.
MOCK_METHOD0(DidOpen, void());
MOCK_METHOD0(DidStart, void());
MOCK_METHOD0(DidStop, void());
MOCK_METHOD0(DidClose, void());
MOCK_METHOD1(DidSetVolume, void(double));
MOCK_METHOD0(DidFlush, void());
bool Open() override {
if (impl_)
impl_->Open();
DidOpen();
return true;
}
void Start(AudioOutputStream::AudioSourceCallback* cb) override {
EXPECT_EQ(nullptr, callback_);
callback_ = cb;
if (impl_) {
impl_->Start(this);
} else {
data_thread_ = std::make_unique<base::Thread>("AudioDataThread");
CHECK(data_thread_->StartAndWaitForTesting());
data_thread_->task_runner()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&MockAudioOutputStream::RunDataLoop,
base::Unretained(this), data_thread_->task_runner()),
GetTestParams().GetBufferDuration());
}
DidStart();
}
void Stop() override {
if (impl_) {
impl_->Stop();
} else {
data_thread_ = nullptr; // Joins/Stops the thread cleanly.
}
callback_ = nullptr;
DidStop();
}
void Close() override {
if (impl_) {
impl_->Close();
impl_ = nullptr;
}
DidClose();
if (close_callback_)
std::move(close_callback_).Run();
delete this;
}
void SetVolume(double volume) override {
volume_ = volume;
if (impl_)
impl_->SetVolume(volume);
DidSetVolume(volume);
}
void GetVolume(double* volume) override { *volume = volume_; }
void Flush() override {
if (impl_)
impl_->Flush();
DidFlush();
}
protected:
~MockAudioOutputStream() override = default;
private:
// Calls OnMoreData() and then posts a delayed task to call itself again soon.
void RunDataLoop(scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
auto bus = AudioBus::Create(GetTestParams());
OnMoreData(base::TimeDelta(), base::TimeTicks::Now(), 0, bus.get());
task_runner->PostDelayedTask(
FROM_HERE,
base::BindOnce(&MockAudioOutputStream::RunDataLoop,
base::Unretained(this), task_runner),
GetTestParams().GetBufferDuration());
}
int OnMoreData(base::TimeDelta delay,
base::TimeTicks delay_timestamp,
int prior_frames_skipped,
AudioBus* dest) override {
int res = callback_->OnMoreData(delay, delay_timestamp,
prior_frames_skipped, dest);
EXPECT_EQ(dest->channel(0)[0], kBufferNonZeroData);
return res;
}
void OnError(ErrorType type) override {
// Fake stream doesn't send errors.
NOTREACHED();
}
AudioOutputStream* impl_;
const AudioParameters::Format format_;
base::OnceClosure close_callback_;
AudioOutputStream::AudioSourceCallback* callback_ = nullptr;
double volume_ = 1.0;
std::unique_ptr<base::Thread> data_thread_;
DISALLOW_COPY_AND_ASSIGN(MockAudioOutputStream);
};
class MockSnooper : public Snoopable::Snooper {
public:
MockSnooper() = default;
~MockSnooper() override = default;
MOCK_METHOD0(DidProvideData, void());
void OnData(const media::AudioBus& audio_bus,
base::TimeTicks reference_time,
double volume) final {
// Is the AudioBus populated?
EXPECT_EQ(kBufferNonZeroData, audio_bus.channel(0)[0]);
// Are reference timestamps monotonically increasing?
if (!last_reference_time_.is_null()) {
EXPECT_LT(last_reference_time_, reference_time);
}
last_reference_time_ = reference_time;
// Is the correct volume being provided?
EXPECT_EQ(kTestVolume, volume);
DidProvideData();
}
private:
base::TimeTicks last_reference_time_;
DISALLOW_COPY_AND_ASSIGN(MockSnooper);
};
// A FakeAudioManager that produces MockAudioOutputStreams, and tracks the last
// stream that was created and the last stream that was closed.
class AudioManagerForControllerTest : public media::FakeAudioManager {
public:
AudioManagerForControllerTest()
: media::FakeAudioManager(std::make_unique<media::TestAudioThread>(false),
&fake_audio_log_factory_) {}
~AudioManagerForControllerTest() final = default;
MockAudioOutputStream* last_created_stream() const {
return last_created_stream_;
}
MockAudioOutputStream* last_closed_stream() const {
return last_closed_stream_;
}
AudioOutputStream* MakeAudioOutputStream(const AudioParameters& params,
const std::string& device_id,
const LogCallback& cb) final {
last_created_stream_ = new NiceMock<MockAudioOutputStream>(
media::FakeAudioManager::MakeAudioOutputStream(params, device_id, cb),
params.format());
last_created_stream_->set_close_callback(
base::BindOnce(&AudioManagerForControllerTest::SetLastClosedStream,
base::Unretained(this), last_created_stream_));
return last_created_stream_;
}
AudioOutputStream* MakeAudioOutputStreamProxy(
const AudioParameters& params,
const std::string& device_id) final {
last_created_stream_ = new NiceMock<MockAudioOutputStream>(
media::FakeAudioManager::MakeAudioOutputStream(params, device_id,
base::DoNothing()),
params.format());
last_created_stream_->set_close_callback(
base::BindOnce(&AudioManagerForControllerTest::SetLastClosedStream,
base::Unretained(this), last_created_stream_));
return last_created_stream_;
}
private:
void SetLastClosedStream(MockAudioOutputStream* stream) {
last_closed_stream_ = stream;
}
media::FakeAudioLogFactory fake_audio_log_factory_;
MockAudioOutputStream* last_created_stream_ = nullptr;
MockAudioOutputStream* last_closed_stream_ = nullptr;
};
ACTION(PopulateBuffer) {
arg0->Zero();
// Note: To confirm the buffer will be populated in these tests, it's
// sufficient that only the first float in channel 0 is set to the value.
arg0->channel(0)[0] = kBufferNonZeroData;
}
class OutputControllerTest : public ::testing::Test {
public:
OutputControllerTest() : group_id_(base::UnguessableToken::Create()) {}
~OutputControllerTest() override { audio_manager_.Shutdown(); }
void SetUp() override {
controller_.emplace(&audio_manager_, &mock_event_handler_, GetTestParams(),
std::string(), &mock_sync_reader_);
controller_->SetVolume(kTestVolume);
}
void TearDown() override { controller_ = base::nullopt; }
protected:
// Returns the last-created or last-closed AudioOuptutStream.
MockAudioOutputStream* last_created_stream() const {
return audio_manager_.last_created_stream();
}
MockAudioOutputStream* last_closed_stream() const {
return audio_manager_.last_closed_stream();
}
void Create() {
controller_->CreateStream();
controller_->SetVolume(kTestVolume);
}
void Play() {
base::RunLoop loop;
// The barrier is used to wait for all of the expectations to be fulfilled.
base::RepeatingClosure barrier =
base::BarrierClosure(3, loop.QuitClosure());
EXPECT_CALL(mock_event_handler_, OnControllerPlaying())
.WillOnce(RunClosure(barrier));
EXPECT_CALL(mock_sync_reader_, RequestMoreData(_, _, _))
.WillOnce(RunClosure(barrier))
.WillRepeatedly(Return());
EXPECT_CALL(mock_sync_reader_, Read(_))
.WillOnce(Invoke([barrier](AudioBus* data) {
data->Zero();
data->channel(0)[0] = kBufferNonZeroData;
barrier.Run();
}))
.WillRepeatedly(PopulateBuffer());
controller_->Play();
Mock::VerifyAndClearExpectations(&mock_event_handler_);
// Waits for all gmock expectations to be satisfied.
loop.Run();
}
void Pause() {
base::RunLoop loop;
EXPECT_CALL(mock_event_handler_, OnControllerPaused())
.WillOnce(RunOnceClosure(loop.QuitClosure()));
controller_->Pause();
loop.Run();
Mock::VerifyAndClearExpectations(&mock_event_handler_);
}
void ChangeDevice() {
// Expect the event handler to receive one OnControllerPaying() call and no
// OnControllerPaused() call.
EXPECT_CALL(mock_event_handler_, OnControllerPlaying());
EXPECT_CALL(mock_event_handler_, OnControllerPaused()).Times(0);
// Simulate a device change event to OutputController from the AudioManager.
audio_manager_.GetTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(&OutputController::OnDeviceChange,
base::Unretained(&(*controller_))));
// Wait for device change to take effect.
base::RunLoop loop;
audio_manager_.GetTaskRunner()->PostTask(FROM_HERE, loop.QuitClosure());
loop.Run();
Mock::VerifyAndClearExpectations(&mock_event_handler_);
}
void StartMutingBeforePlaying() { controller_->StartMuting(); }
void StartMutingWhilePlaying() {
EXPECT_CALL(mock_event_handler_, OnControllerPlaying());
controller_->StartMuting();
Mock::VerifyAndClearExpectations(&mock_event_handler_);
}
void StopMuting() {
EXPECT_CALL(mock_event_handler_, OnControllerPlaying());
controller_->StopMuting();
Mock::VerifyAndClearExpectations(&mock_event_handler_);
}
void StartSnooping(MockSnooper* snooper) {
controller_->StartSnooping(snooper);
}
void WaitForSnoopedData(MockSnooper* snooper) {
base::RunLoop loop;
EXPECT_CALL(*snooper, DidProvideData())
.WillOnce(RunOnceClosure(loop.QuitClosure()))
.WillRepeatedly(Return());
loop.Run();
Mock::VerifyAndClearExpectations(snooper);
}
void StopSnooping(MockSnooper* snooper) {
controller_->StopSnooping(snooper);
}
Snoopable* GetSnoopable() { return &(*controller_); }
void Close() {
EXPECT_CALL(mock_sync_reader_, Close());
controller_->Close();
// Flush any pending tasks (that should have been canceled!).
base::RunLoop loop;
audio_manager_.GetTaskRunner()->PostTask(FROM_HERE, loop.QuitClosure());
loop.Run();
}
void Flush() { controller_->Flush(); }
void SimulateErrorThenDeviceChange() {
audio_manager_.GetTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&OutputControllerTest::TriggerErrorThenDeviceChange,
base::Unretained(this)));
base::RunLoop loop;
audio_manager_.GetTaskRunner()->PostTask(FROM_HERE, loop.QuitClosure());
loop.Run();
}
void TriggerErrorThenDeviceChange() {
DCHECK(audio_manager_.GetTaskRunner()->BelongsToCurrentThread());
// Errors should be deferred; the device change should ensure it's dropped.
EXPECT_CALL(mock_event_handler_, OnControllerError()).Times(0);
controller_->OnError(
media::AudioOutputStream::AudioSourceCallback::ErrorType::kUnknown);
EXPECT_CALL(mock_event_handler_, OnControllerPlaying());
EXPECT_CALL(mock_event_handler_, OnControllerPaused()).Times(0);
controller_->OnDeviceChange();
Mock::VerifyAndClearExpectations(&mock_event_handler_);
}
StrictMock<MockOutputControllerEventHandler> mock_event_handler_;
private:
base::TestMessageLoop message_loop_;
AudioManagerForControllerTest audio_manager_;
base::UnguessableToken group_id_;
StrictMock<MockOutputControllerSyncReader> mock_sync_reader_;
base::Optional<OutputController> controller_;
DISALLOW_COPY_AND_ASSIGN(OutputControllerTest);
};
TEST_F(OutputControllerTest, CreateAndClose) {
Create();
Close();
}
TEST_F(OutputControllerTest, PlayAndClose) {
Create();
Play();
Close();
}
TEST_F(OutputControllerTest, PlayPauseClose) {
Create();
Play();
Pause();
Close();
}
TEST_F(OutputControllerTest, PlayPausePlayClose) {
Create();
Play();
Pause();
Play();
Close();
}
TEST_F(OutputControllerTest, PlayDeviceChangeClose) {
Create();
Play();
ChangeDevice();
Close();
}
TEST_F(OutputControllerTest, PlayDeviceChangeError) {
Create();
Play();
SimulateErrorThenDeviceChange();
Close();
}
// Syntactic convenience.
double GetStreamVolume(AudioOutputStream* stream) {
double result = NAN;
stream->GetVolume(&result);
return result;
}
// Tests that muting before the stream is created will result in only the
// "muting stream" being created, and not any local playout streams (that might
// possibly cause an audible blip).
TEST_F(OutputControllerTest, MuteCreatePlayClose) {
StartMutingBeforePlaying();
EXPECT_EQ(nullptr, last_created_stream()); // No stream yet.
EXPECT_EQ(nullptr, last_closed_stream()); // No stream yet.
Create();
MockAudioOutputStream* const mute_stream = last_created_stream();
ASSERT_TRUE(mute_stream);
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
Play();
ASSERT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
Close();
EXPECT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(mute_stream, last_closed_stream());
}
// Tests that a local playout stream is shut-down and replaced with a "muting
// stream" if StartMuting() is called after playback begins.
TEST_F(OutputControllerTest, CreatePlayMuteClose) {
Create();
MockAudioOutputStream* const playout_stream = last_created_stream();
ASSERT_TRUE(playout_stream);
EXPECT_EQ(nullptr, last_closed_stream());
Play();
ASSERT_EQ(playout_stream, last_created_stream());
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(GetTestParams().format(), playout_stream->format());
EXPECT_EQ(kTestVolume, GetStreamVolume(playout_stream));
StartMutingWhilePlaying();
MockAudioOutputStream* const mute_stream = last_created_stream();
ASSERT_TRUE(mute_stream);
EXPECT_EQ(playout_stream, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
Close();
EXPECT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(mute_stream, last_closed_stream());
}
// Tests that the "muting stream" is shut down and replaced with the normal
// playout stream after StopMuting() is called.
TEST_F(OutputControllerTest, PlayMuteUnmuteClose) {
StartMutingBeforePlaying();
Create();
Play();
MockAudioOutputStream* const mute_stream = last_created_stream();
ASSERT_TRUE(mute_stream);
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
StopMuting();
MockAudioOutputStream* const playout_stream = last_created_stream();
ASSERT_TRUE(playout_stream);
EXPECT_EQ(mute_stream, last_closed_stream());
EXPECT_EQ(GetTestParams().format(), playout_stream->format());
EXPECT_EQ(kTestVolume, GetStreamVolume(playout_stream));
Close();
EXPECT_EQ(playout_stream, last_created_stream());
EXPECT_EQ(playout_stream, last_closed_stream());
}
TEST_F(OutputControllerTest, SnoopCreatePlayStopClose) {
NiceMock<MockSnooper> snooper;
StartSnooping(&snooper);
Create();
Play();
WaitForSnoopedData(&snooper);
StopSnooping(&snooper);
Close();
}
TEST_F(OutputControllerTest, CreatePlaySnoopStopClose) {
NiceMock<MockSnooper> snooper;
Create();
Play();
StartSnooping(&snooper);
WaitForSnoopedData(&snooper);
StopSnooping(&snooper);
Close();
}
TEST_F(OutputControllerTest, CreatePlaySnoopCloseStop) {
NiceMock<MockSnooper> snooper;
Create();
Play();
StartSnooping(&snooper);
WaitForSnoopedData(&snooper);
Close();
StopSnooping(&snooper);
}
TEST_F(OutputControllerTest, TwoSnoopers_StartAtDifferentTimes) {
NiceMock<MockSnooper> snooper1;
NiceMock<MockSnooper> snooper2;
StartSnooping(&snooper1);
Create();
Play();
WaitForSnoopedData(&snooper1);
StartSnooping(&snooper2);
WaitForSnoopedData(&snooper2);
WaitForSnoopedData(&snooper1);
WaitForSnoopedData(&snooper2);
Close();
StopSnooping(&snooper1);
StopSnooping(&snooper2);
}
TEST_F(OutputControllerTest, TwoSnoopers_StopAtDifferentTimes) {
NiceMock<MockSnooper> snooper1;
NiceMock<MockSnooper> snooper2;
Create();
Play();
StartSnooping(&snooper1);
WaitForSnoopedData(&snooper1);
StartSnooping(&snooper2);
WaitForSnoopedData(&snooper2);
StopSnooping(&snooper1);
WaitForSnoopedData(&snooper2);
Close();
StopSnooping(&snooper2);
}
TEST_F(OutputControllerTest, SnoopWhileMuting) {
NiceMock<MockSnooper> snooper;
StartMutingBeforePlaying();
EXPECT_EQ(nullptr, last_created_stream()); // No stream yet.
EXPECT_EQ(nullptr, last_closed_stream()); // No stream yet.
Create();
MockAudioOutputStream* const mute_stream = last_created_stream();
ASSERT_TRUE(mute_stream);
EXPECT_EQ(nullptr, last_closed_stream());
Play();
ASSERT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
StartSnooping(&snooper);
ASSERT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
WaitForSnoopedData(&snooper);
StopSnooping(&snooper);
ASSERT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(nullptr, last_closed_stream());
EXPECT_EQ(AudioParameters::AUDIO_FAKE, mute_stream->format());
Close();
EXPECT_EQ(mute_stream, last_created_stream());
EXPECT_EQ(mute_stream, last_closed_stream());
}
TEST_F(OutputControllerTest, FlushWhenStreamIsPlayingTriggersError) {
Create();
Play();
MockAudioOutputStream* const mock_stream = last_created_stream();
EXPECT_CALL(*mock_stream, DidFlush()).Times(0);
EXPECT_CALL(mock_event_handler_, OnControllerError()).Times(1);
Flush();
Close();
}
TEST_F(OutputControllerTest, FlushesWhenStreamIsNotPlaying) {
Create();
Play();
Pause();
MockAudioOutputStream* const mock_stream = last_created_stream();
EXPECT_CALL(*mock_stream, DidFlush()).Times(1);
Flush();
Close();
}
} // namespace
} // namespace audio
|