1 // Copyright (c) 2015 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.
8 #include "base/location.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "content/browser/renderer_host/media/audio_output_device_enumerator.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "media/audio/audio_manager_base.h"
17 #include "media/audio/fake_audio_log_factory.h"
18 #include "media/audio/fake_audio_manager.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
27 class MockAudioManager
: public media::FakeAudioManager
{
29 MockAudioManager() : FakeAudioManager(&fake_audio_log_factory_
) {}
30 ~MockAudioManager() override
{}
31 MOCK_METHOD1(GetAudioOutputDeviceNames
, void(media::AudioDeviceNames
*));
34 media::FakeAudioLogFactory fake_audio_log_factory_
;
35 DISALLOW_COPY_AND_ASSIGN(MockAudioManager
);
39 class AudioOutputDeviceEnumeratorTest
: public ::testing::Test
{
41 AudioOutputDeviceEnumeratorTest()
42 : thread_bundle_(), task_runner_(base::ThreadTaskRunnerHandle::Get()) {
43 audio_manager_
.reset(new MockAudioManager());
46 ~AudioOutputDeviceEnumeratorTest() override
{}
48 MOCK_METHOD1(MockCallback
, void(const AudioOutputDeviceEnumeration
&));
50 // Performs n calls to MockCallback (one by QuitCallback)
51 // and n-1 calls to Enumerate
52 void EnumerateCallback(AudioOutputDeviceEnumerator
* enumerator
,
54 const AudioOutputDeviceEnumeration
& result
) {
57 enumerator
->Enumerate(
58 base::Bind(&AudioOutputDeviceEnumeratorTest::EnumerateCallback
,
59 base::Unretained(this), enumerator
, n
- 1));
61 enumerator
->Enumerate(
62 base::Bind(&AudioOutputDeviceEnumeratorTest::QuitCallback
,
63 base::Unretained(this)));
67 void QuitCallback(const AudioOutputDeviceEnumeration
& result
) {
69 task_runner_
->PostTask(FROM_HERE
, run_loop_
.QuitClosure());
73 scoped_ptr
<MockAudioManager
> audio_manager_
;
74 TestBrowserThreadBundle thread_bundle_
;
75 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
76 base::RunLoop run_loop_
;
79 DISALLOW_COPY_AND_ASSIGN(AudioOutputDeviceEnumeratorTest
);
82 TEST_F(AudioOutputDeviceEnumeratorTest
, EnumerateWithCache
) {
83 const int num_calls
= 10;
84 EXPECT_CALL(*audio_manager_
, GetAudioOutputDeviceNames(_
)).Times(1);
85 EXPECT_CALL(*this, MockCallback(_
)).Times(num_calls
);
86 AudioOutputDeviceEnumerator
enumerator(
88 AudioOutputDeviceEnumerator::CACHE_POLICY_MANUAL_INVALIDATION
);
90 base::Bind(&AudioOutputDeviceEnumeratorTest::EnumerateCallback
,
91 base::Unretained(this), &enumerator
, num_calls
- 1));
95 TEST_F(AudioOutputDeviceEnumeratorTest
, EnumerateWithNoCache
) {
96 const int num_calls
= 10;
97 EXPECT_CALL(*audio_manager_
, GetAudioOutputDeviceNames(_
)).Times(num_calls
);
98 EXPECT_CALL(*this, MockCallback(_
)).Times(num_calls
);
99 AudioOutputDeviceEnumerator
enumerator(
100 audio_manager_
.get(),
101 AudioOutputDeviceEnumerator::CACHE_POLICY_NO_CACHING
);
102 enumerator
.Enumerate(
103 base::Bind(&AudioOutputDeviceEnumeratorTest::EnumerateCallback
,
104 base::Unretained(this), &enumerator
, num_calls
- 1));
108 } // namespace content