Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_output_device_enumerator_unittest.cc
blobd5091534e807cb7084a82732bce4e5ebd10c4362
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.
5 #include <string>
7 #include "base/bind.h"
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"
22 using testing::_;
24 namespace content {
26 namespace {
27 class MockAudioManager : public media::FakeAudioManager {
28 public:
29 MockAudioManager() : FakeAudioManager(&fake_audio_log_factory_) {}
30 ~MockAudioManager() override {}
31 MOCK_METHOD1(GetAudioOutputDeviceNames, void(media::AudioDeviceNames*));
33 private:
34 media::FakeAudioLogFactory fake_audio_log_factory_;
35 DISALLOW_COPY_AND_ASSIGN(MockAudioManager);
37 } // namespace
39 class AudioOutputDeviceEnumeratorTest : public ::testing::Test {
40 public:
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,
53 int n,
54 const AudioOutputDeviceEnumeration& result) {
55 MockCallback(result);
56 if (n > 1) {
57 enumerator->Enumerate(
58 base::Bind(&AudioOutputDeviceEnumeratorTest::EnumerateCallback,
59 base::Unretained(this), enumerator, n - 1));
60 } else {
61 enumerator->Enumerate(
62 base::Bind(&AudioOutputDeviceEnumeratorTest::QuitCallback,
63 base::Unretained(this)));
67 void QuitCallback(const AudioOutputDeviceEnumeration& result) {
68 MockCallback(result);
69 task_runner_->PostTask(FROM_HERE, run_loop_.QuitClosure());
72 protected:
73 scoped_ptr<MockAudioManager> audio_manager_;
74 TestBrowserThreadBundle thread_bundle_;
75 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
76 base::RunLoop run_loop_;
78 private:
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(
87 audio_manager_.get(),
88 AudioOutputDeviceEnumerator::CACHE_POLICY_MANUAL_INVALIDATION);
89 enumerator.Enumerate(
90 base::Bind(&AudioOutputDeviceEnumeratorTest::EnumerateCallback,
91 base::Unretained(this), &enumerator, num_calls - 1));
92 run_loop_.Run();
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));
105 run_loop_.Run();
108 } // namespace content