1 // Copyright (c) 2012 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/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/win/scoped_com_initializer.h"
12 #include "media/audio/audio_manager.h"
13 #include "media/audio/win/audio_device_listener_win.h"
14 #include "media/audio/win/core_audio_util_win.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::win::ScopedCOMInitializer;
22 static const char kNoDevice[] = "";
23 static const char kFirstTestDevice[] = "test_device_0";
24 static const char kSecondTestDevice[] = "test_device_1";
26 class AudioDeviceListenerWinTest : public testing::Test {
28 AudioDeviceListenerWinTest()
29 : com_init_(ScopedCOMInitializer::kMTA) {
32 virtual void SetUp() {
33 if (!CoreAudioUtil::IsSupported())
36 output_device_listener_.reset(new AudioDeviceListenerWin(base::Bind(
37 &AudioDeviceListenerWinTest::OnDeviceChange, base::Unretained(this))));
40 // Simulate a device change where no output devices are available.
41 bool SimulateNullDefaultOutputDeviceChange() {
42 return output_device_listener_->OnDefaultDeviceChanged(
43 static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender),
47 bool SimulateDefaultOutputDeviceChange(const char* new_device_id) {
48 return output_device_listener_->OnDefaultDeviceChanged(
49 static_cast<EDataFlow>(eConsole), static_cast<ERole>(eRender),
50 ASCIIToWide(new_device_id).c_str()) == S_OK;
53 void SetOutputDeviceId(std::string new_device_id) {
54 output_device_listener_->default_render_device_id_ = new_device_id;
57 MOCK_METHOD0(OnDeviceChange, void());
60 ScopedCOMInitializer com_init_;
61 scoped_ptr<AudioDeviceListenerWin> output_device_listener_;
63 DISALLOW_COPY_AND_ASSIGN(AudioDeviceListenerWinTest);
66 // Simulate a device change events and ensure we get the right callbacks.
67 TEST_F(AudioDeviceListenerWinTest, OutputDeviceChange) {
68 if (!CoreAudioUtil::IsSupported())
71 SetOutputDeviceId(kNoDevice);
72 EXPECT_CALL(*this, OnDeviceChange()).Times(1);
73 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice));
75 testing::Mock::VerifyAndClear(this);
76 EXPECT_CALL(*this, OnDeviceChange()).Times(1);
77 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice));
79 // The second device event should be ignored since the device id has not
81 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kSecondTestDevice));
84 // Ensure that null output device changes don't crash. Simulates the situation
85 // where we have no output devices.
86 TEST_F(AudioDeviceListenerWinTest, NullOutputDeviceChange) {
87 if (!CoreAudioUtil::IsSupported())
90 SetOutputDeviceId(kNoDevice);
91 EXPECT_CALL(*this, OnDeviceChange()).Times(0);
92 ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange());
94 testing::Mock::VerifyAndClear(this);
95 EXPECT_CALL(*this, OnDeviceChange()).Times(1);
96 ASSERT_TRUE(SimulateDefaultOutputDeviceChange(kFirstTestDevice));
98 testing::Mock::VerifyAndClear(this);
99 EXPECT_CALL(*this, OnDeviceChange()).Times(1);
100 ASSERT_TRUE(SimulateNullDefaultOutputDeviceChange());