This source file includes following definitions.
- CreateDeviceListener
- DestroyDeviceListener
- ListenerIsValid
- SimulateDefaultOutputDeviceChange
- TEST_F
#include <CoreAudio/AudioHardware.h>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "media/audio/mac/audio_device_listener_mac.h"
#include "media/base/bind_to_current_loop.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
class AudioDeviceListenerMacTest : public testing::Test {
public:
AudioDeviceListenerMacTest() {
message_loop_.PostTask(FROM_HERE, base::Bind(
&AudioDeviceListenerMacTest::CreateDeviceListener,
base::Unretained(this)));
message_loop_.RunUntilIdle();
}
virtual ~AudioDeviceListenerMacTest() {
message_loop_.PostTask(FROM_HERE, base::Bind(
&AudioDeviceListenerMacTest::DestroyDeviceListener,
base::Unretained(this)));
message_loop_.RunUntilIdle();
}
void CreateDeviceListener() {
output_device_listener_.reset(new AudioDeviceListenerMac(BindToCurrentLoop(
base::Bind(&AudioDeviceListenerMacTest::OnDeviceChange,
base::Unretained(this)))));
}
void DestroyDeviceListener() {
output_device_listener_.reset();
}
bool ListenerIsValid() {
return !output_device_listener_->listener_cb_.is_null();
}
bool SimulateDefaultOutputDeviceChange() {
const AudioObjectPropertyAddress addresses[] = {
AudioDeviceListenerMac::kDeviceChangePropertyAddress,
{ kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster }
};
return noErr == output_device_listener_->OnDefaultDeviceChanged(
kAudioObjectSystemObject, 1, addresses, output_device_listener_.get());
}
MOCK_METHOD0(OnDeviceChange, void());
protected:
base::MessageLoop message_loop_;
scoped_ptr<AudioDeviceListenerMac> output_device_listener_;
DISALLOW_COPY_AND_ASSIGN(AudioDeviceListenerMacTest);
};
TEST_F(AudioDeviceListenerMacTest, OutputDeviceChange) {
ASSERT_TRUE(ListenerIsValid());
EXPECT_CALL(*this, OnDeviceChange()).Times(1);
ASSERT_TRUE(SimulateDefaultOutputDeviceChange());
message_loop_.RunUntilIdle();
}
}