This source file includes following definitions.
- input_gain_changed_count_
- active_output_node_changed_count
- active_input_node_changed_count
- audio_nodes_changed_count
- output_mute_changed_count
- input_mute_changed_count
- output_volume_changed_count
- input_gain_changed_count
- OnActiveOutputNodeChanged
- OnActiveInputNodeChanged
- OnAudioNodesChanged
- OnOutputMuteChanged
- OnInputMuteChanged
- OnOutputVolumeChanged
- OnInputGainChanged
- cras_audio_client_stub_
- SetUp
- TearDown
- SetUpCrasAudioHandler
- ChangeAudioNodes
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chromeos/audio/cras_audio_handler.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/values.h"
#include "chromeos/audio/audio_devices_pref_handler_stub.h"
#include "chromeos/dbus/audio_node.h"
#include "chromeos/dbus/cras_audio_client_stub_impl.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
const uint64 kInternalSpeakerId = 10001;
const uint64 kHeadphoneId = 10002;
const uint64 kInternalMicId = 10003;
const uint64 kUSBMicId = 10004;
const uint64 kBluetoothHeadsetId = 10005;
const uint64 kHDMIOutputId = 10006;
const uint64 kUSBHeadphoneId1 = 10007;
const uint64 kUSBHeadphoneId2 = 10008;
const uint64 kMicJackId = 10009;
const uint64 kOtherTypeOutputId = 90001;
const uint64 kOtherTypeInputId = 90002;
const AudioNode kInternalSpeaker(
false,
kInternalSpeakerId,
"Fake Speaker",
"INTERNAL_SPEAKER",
"Speaker",
false,
0
);
const AudioNode kHeadphone(
false,
kHeadphoneId,
"Fake Headphone",
"HEADPHONE",
"Headphone",
false,
0
);
const AudioNode kInternalMic(
true,
kInternalMicId,
"Fake Mic",
"INTERNAL_MIC",
"Internal Mic",
false,
0
);
const AudioNode kMicJack(
true,
kMicJackId,
"Fake Mic Jack",
"MIC",
"Mic Jack",
false,
0
);
const AudioNode kUSBMic(
true,
kUSBMicId,
"Fake USB Mic",
"USB",
"USB Microphone",
false,
0
);
const AudioNode kOtherTypeOutput(
false,
kOtherTypeOutputId,
"Output Device",
"SOME_OTHER_TYPE",
"Other Type Output Device",
false,
0
);
const AudioNode kOtherTypeInput(
true,
kOtherTypeInputId,
"Input Device",
"SOME_OTHER_TYPE",
"Other Type Input Device",
false,
0
);
const AudioNode kBluetoothHeadset (
false,
kBluetoothHeadsetId,
"Bluetooth Headset",
"BLUETOOTH",
"Bluetooth Headset 1",
false,
0
);
const AudioNode kHDMIOutput (
false,
kHDMIOutputId,
"HDMI output",
"HDMI",
"HDMI output",
false,
0
);
const AudioNode kUSBHeadphone1 (
false,
kUSBHeadphoneId1,
"USB Headphone",
"USB",
"USB Headphone 1",
false,
0
);
const AudioNode kUSBHeadphone2 (
false,
kUSBHeadphoneId2,
"USB Headphone",
"USB",
"USB Headphone 1",
false,
0
);
class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
public:
TestObserver() : active_output_node_changed_count_(0),
active_input_node_changed_count_(0),
audio_nodes_changed_count_(0),
output_mute_changed_count_(0),
input_mute_changed_count_(0),
output_volume_changed_count_(0),
input_gain_changed_count_(0) {
}
int active_output_node_changed_count() const {
return active_output_node_changed_count_;
}
int active_input_node_changed_count() const {
return active_input_node_changed_count_;
}
int audio_nodes_changed_count() const {
return audio_nodes_changed_count_;
}
int output_mute_changed_count() const {
return output_mute_changed_count_;
}
int input_mute_changed_count() const {
return input_mute_changed_count_;
}
int output_volume_changed_count() const {
return output_volume_changed_count_;
}
int input_gain_changed_count() const {
return input_gain_changed_count_;
}
virtual ~TestObserver() {}
protected:
virtual void OnActiveOutputNodeChanged() OVERRIDE {
++active_output_node_changed_count_;
}
virtual void OnActiveInputNodeChanged() OVERRIDE {
++active_input_node_changed_count_;
}
virtual void OnAudioNodesChanged() OVERRIDE {
++audio_nodes_changed_count_;
}
virtual void OnOutputMuteChanged() OVERRIDE {
++output_mute_changed_count_;
}
virtual void OnInputMuteChanged() OVERRIDE {
++input_mute_changed_count_;
}
virtual void OnOutputVolumeChanged() OVERRIDE {
++output_volume_changed_count_;
}
virtual void OnInputGainChanged() OVERRIDE {
++input_gain_changed_count_;
}
private:
int active_output_node_changed_count_;
int active_input_node_changed_count_;
int audio_nodes_changed_count_;
int output_mute_changed_count_;
int input_mute_changed_count_;
int output_volume_changed_count_;
int input_gain_changed_count_;
DISALLOW_COPY_AND_ASSIGN(TestObserver);
};
class CrasAudioHandlerTest : public testing::Test {
public:
CrasAudioHandlerTest() : cras_audio_handler_(NULL),
cras_audio_client_stub_(NULL) {
}
virtual ~CrasAudioHandlerTest() {}
virtual void SetUp() OVERRIDE {
}
virtual void TearDown() OVERRIDE {
cras_audio_handler_->RemoveAudioObserver(test_observer_.get());
test_observer_.reset();
CrasAudioHandler::Shutdown();
audio_pref_handler_ = NULL;
DBusThreadManager::Shutdown();
}
void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) {
DBusThreadManager::InitializeWithStub();
cras_audio_client_stub_ = static_cast<CrasAudioClientStubImpl*>(
DBusThreadManager::Get()->GetCrasAudioClient());
cras_audio_client_stub_->SetAudioDevices(audio_nodes);
audio_pref_handler_ = new AudioDevicesPrefHandlerStub();
CrasAudioHandler::Initialize(audio_pref_handler_);
cras_audio_handler_ = CrasAudioHandler::Get();
test_observer_.reset(new TestObserver);
cras_audio_handler_->AddAudioObserver(test_observer_.get());
message_loop_.RunUntilIdle();
}
void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
cras_audio_client_stub_->ChangeAudioNodes(audio_nodes);
message_loop_.RunUntilIdle();
}
protected:
base::MessageLoopForUI message_loop_;
CrasAudioHandler* cras_audio_handler_;
CrasAudioClientStubImpl* cras_audio_client_stub_;
scoped_ptr<TestObserver> test_observer_;
scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_;
private:
DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest);
};
TEST_F(CrasAudioHandlerTest, InitializeWithOnlyDefaultAudioDevices) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
AudioDevice active_input;
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
}
TEST_F(CrasAudioHandlerTest, InitializeWithAlternativeAudioDevices) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
audio_nodes.push_back(kInternalMic);
audio_nodes.push_back(kUSBMic);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
AudioDevice active_input;
EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
}
TEST_F(CrasAudioHandlerTest, SwitchActiveOutputDevice) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
AudioDevice internal_speaker(kInternalSpeaker);
cras_audio_handler_->SwitchToDevice(internal_speaker);
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
}
TEST_F(CrasAudioHandlerTest, SwitchActiveInputDevice) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalMic);
audio_nodes.push_back(kUSBMic);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
AudioDevice internal_mic(kInternalMic);
cras_audio_handler_->SwitchToDevice(internal_mic);
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
}
TEST_F(CrasAudioHandlerTest, PlugHeadphone) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
AudioNode internal_speaker(kInternalSpeaker);
internal_speaker.active = true;
audio_nodes.push_back(internal_speaker);
audio_nodes.push_back(kHeadphone);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, UnplugHeadphone) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, InitializeWithBluetoothHeadset) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kBluetoothHeadset);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectBluetoothHeadset) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
AudioNode headphone(kHeadphone);
headphone.plugged_time = 80000000;
headphone.active = true;
audio_nodes.push_back(headphone);
AudioNode bluetooth_headset(kBluetoothHeadset);
bluetooth_headset.plugged_time = 90000000;
audio_nodes.push_back(bluetooth_headset);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
headphone.active = false;
audio_nodes.push_back(headphone);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, InitializeWithHDMIOutput) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHDMIOutput);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHDMIOutput.id, active_output.id);
EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, ConnectAndDisconnectHDMIOutput) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
AudioNode internal_speaker(kInternalSpeaker);
internal_speaker.active = true;
internal_speaker.plugged_time = 80000000;
audio_nodes.push_back(internal_speaker);
AudioNode hdmi(kHDMIOutput);
hdmi.plugged_time = 90000000;
audio_nodes.push_back(hdmi);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHDMIOutput.id, active_output.id);
EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, HandleHeadphoneAndHDMIOutput) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
audio_nodes.push_back(kHDMIOutput);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHDMIOutput);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHDMIOutput.id, active_output.id);
EXPECT_EQ(kHDMIOutput.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, InitializeWithUSBHeadphone) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kUSBHeadphone1);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(audio_nodes.size(), audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, PlugAndUnplugUSBHeadphone) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
AudioNode internal_speaker(kInternalSpeaker);
internal_speaker.active = true;
internal_speaker.plugged_time = 80000000;
audio_nodes.push_back(internal_speaker);
AudioNode usb_headphone(kUSBHeadphone1);
usb_headphone.plugged_time = 90000000;
audio_nodes.push_back(usb_headphone);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, HandleMultipleUSBHeadphones) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kUSBHeadphone1);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
AudioNode usb_headphone_1(kUSBHeadphone1);
usb_headphone_1.active = true;
usb_headphone_1.plugged_time = 80000000;
audio_nodes.push_back(usb_headphone_1);
AudioNode usb_headphone_2(kUSBHeadphone2);
usb_headphone_2.plugged_time = 90000000;
audio_nodes.push_back(usb_headphone_2);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kUSBHeadphone2.id, active_output.id);
EXPECT_EQ(kUSBHeadphone2.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kUSBHeadphone1);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, UnplugUSBHeadphonesWithActiveSpeaker) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kUSBHeadphone1);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kUSBHeadphone1.id, active_output.id);
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
AudioNode usb_headphone_1(kUSBHeadphone1);
usb_headphone_1.active = true;
usb_headphone_1.plugged_time = 80000000;
audio_nodes.push_back(usb_headphone_1);
AudioNode headphone_jack(kHeadphone);
headphone_jack.plugged_time = 90000000;
audio_nodes.push_back(headphone_jack);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
AudioDevice internal_speaker(kInternalSpeaker);
cras_audio_handler_->SwitchToDevice(internal_speaker);
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
audio_nodes.clear();
AudioNode internal_speaker_node(kInternalSpeaker);
internal_speaker_node.active = true;
internal_speaker_node.plugged_time = 70000000;
audio_nodes.push_back(internal_speaker_node);
headphone_jack.active = false;
audio_nodes.push_back(headphone_jack);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
}
TEST_F(CrasAudioHandlerTest, OneActiveAudioOutputAfterLoginNewUserSession) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
AudioNode bluetooth_headphone(kBluetoothHeadset);
bluetooth_headphone.active = true;
bluetooth_headphone.plugged_time = 70000000;
audio_nodes.push_back(bluetooth_headphone);
AudioNode headphone_jack(kHeadphone);
headphone_jack.plugged_time = 80000000;
audio_nodes.push_back(headphone_jack);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kHeadphone.id, active_output.id);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
for (size_t i = 0; i < audio_devices.size(); ++i) {
if (audio_devices[i].id != kHeadphone.id)
EXPECT_FALSE(audio_devices[i].active);
}
}
TEST_F(CrasAudioHandlerTest, BluetoothSpeakerIdChangedOnFly) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kBluetoothHeadset);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kBluetoothHeadset.id, active_output.id);
EXPECT_EQ(kBluetoothHeadset.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
AudioNode internal_speaker(kInternalSpeaker);
internal_speaker.active = false;
audio_nodes.push_back(internal_speaker);
AudioNode bluetooth_headphone(kBluetoothHeadset);
bluetooth_headphone.id = kBluetoothHeadsetId + 20000;
bluetooth_headphone.active = false;
audio_nodes.push_back(bluetooth_headphone);
ChangeAudioNodes(audio_nodes);
audio_devices.clear();
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(bluetooth_headphone.id, active_output.id);
}
TEST_F(CrasAudioHandlerTest, PlugUSBMic) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
audio_nodes.clear();
AudioNode internal_mic(kInternalMic);
internal_mic.active = true;
audio_nodes.push_back(internal_mic);
audio_nodes.push_back(kUSBMic);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
}
TEST_F(CrasAudioHandlerTest, UnplugUSBMic) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalMic);
audio_nodes.push_back(kUSBMic);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
audio_nodes.clear();
audio_nodes.push_back(kInternalMic);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size - 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
}
TEST_F(CrasAudioHandlerTest, PlugUSBMicNotAffectActiveOutput) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kHeadphoneId, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
AudioDevice internal_speaker(kInternalSpeaker);
cras_audio_handler_->SwitchToDevice(internal_speaker);
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
audio_nodes.clear();
AudioNode internal_speaker_node(kInternalSpeaker);
internal_speaker_node.active = true;
audio_nodes.push_back(internal_speaker_node);
audio_nodes.push_back(kHeadphone);
AudioNode internal_mic(kInternalMic);
internal_mic.active = true;
audio_nodes.push_back(internal_mic);
audio_nodes.push_back(kUSBMic);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
}
TEST_F(CrasAudioHandlerTest, PlugHeadphoneAutoUnplugSpeakerWithActiveUSB) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kUSBHeadphone1);
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMicId, cras_audio_handler_->GetActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_input());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kUSBHeadphoneId1, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
AudioNode usb_headphone_node(kUSBHeadphone1);
usb_headphone_node.active = true;
audio_nodes.push_back(usb_headphone_node);
AudioNode headphone_node(kHeadphone);
headphone_node.plugged_time = 1000;
audio_nodes.push_back(headphone_node);
AudioNode internal_mic(kInternalMic);
internal_mic.active = true;
audio_nodes.push_back(internal_mic);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kUSBHeadphone1);
AudioNode internal_speaker_node(kInternalSpeaker);
internal_speaker_node.plugged_time = 2000;
audio_nodes.push_back(internal_speaker_node);
audio_nodes.push_back(internal_mic);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
}
TEST_F(CrasAudioHandlerTest, PlugMicAutoUnplugInternalMicWithActiveUSB) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kUSBHeadphone1);
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kUSBMic);
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMicId, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kUSBHeadphoneId1, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
AudioNode usb_headphone_node(kUSBHeadphone1);
usb_headphone_node.active = true;
audio_nodes.push_back(usb_headphone_node);
AudioNode headphone_node(kHeadphone);
headphone_node.plugged_time = 1000;
audio_nodes.push_back(headphone_node);
AudioNode usb_mic(kUSBMic);
usb_mic.active = true;
audio_nodes.push_back(usb_mic);
AudioNode mic_jack(kMicJack);
mic_jack.plugged_time = 1000;
audio_nodes.push_back(mic_jack);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(1, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kMicJack.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
audio_nodes.clear();
audio_nodes.push_back(kUSBHeadphone1);
AudioNode internal_speaker_node(kInternalSpeaker);
internal_speaker_node.plugged_time = 2000;
audio_nodes.push_back(internal_speaker_node);
audio_nodes.push_back(kUSBMic);
AudioNode internal_mic(kInternalMic);
internal_mic.plugged_time = 2000;
audio_nodes.push_back(internal_mic);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(2, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kUSBHeadphone1.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
EXPECT_EQ(2, test_observer_->active_input_node_changed_count());
EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
}
TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInHeadphone) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kBluetoothHeadset);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kBluetoothHeadsetId, cras_audio_handler_->GetActiveOutputNode());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
audio_nodes.clear();
audio_nodes.push_back(kInternalSpeaker);
AudioNode bluetooth_headset(kBluetoothHeadset);
bluetooth_headset.plugged_time = 1000;
bluetooth_headset.active = true;
audio_nodes.push_back(bluetooth_headset);
AudioNode headphone(kHeadphone);
headphone.active = false;
headphone.plugged_time = 2000;
audio_nodes.push_back(headphone);
ChangeAudioNodes(audio_nodes);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_EQ(headphone.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(headphone.id, active_output.id);
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
for (size_t i = 0; i < audio_devices.size(); ++i) {
if (audio_devices[i].id == kInternalSpeaker.id)
EXPECT_FALSE(audio_devices[i].active);
else if (audio_devices[i].id == bluetooth_headset.id)
EXPECT_FALSE(audio_devices[i].active);
else if (audio_devices[i].id == headphone.id)
EXPECT_TRUE(audio_devices[i].active);
else
NOTREACHED();
}
}
TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnPlugInUSBMic) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
const size_t init_nodes_size = audio_nodes.size();
EXPECT_EQ(0, test_observer_->audio_nodes_changed_count());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
EXPECT_EQ(0, test_observer_->active_output_node_changed_count());
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_FALSE(cras_audio_handler_->has_alternative_output());
EXPECT_TRUE(audio_devices[0].active);
audio_nodes.clear();
AudioNode internal_mic(kInternalMic);
internal_mic.active = true;
internal_mic.plugged_time = 1000;
audio_nodes.push_back(internal_mic);
AudioNode usb_mic(kUSBMic);
usb_mic.active = false;
usb_mic.plugged_time = 2000;
audio_nodes.push_back(usb_mic);
ChangeAudioNodes(audio_nodes);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(1, test_observer_->active_input_node_changed_count());
EXPECT_EQ(usb_mic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size + 1, audio_devices.size());
for (size_t i = 0; i < audio_devices.size(); ++i) {
if (audio_devices[i].id == kInternalMic.id)
EXPECT_FALSE(audio_devices[i].active);
else if (audio_devices[i].id == usb_mic.id)
EXPECT_TRUE(audio_devices[i].active);
else
NOTREACHED();
}
}
TEST_F(CrasAudioHandlerTest, MultipleNodesChangedSignalsOnSystemBoot) {
AudioNodeList audio_nodes;
SetUpCrasAudioHandler(audio_nodes);
AudioNode internal_speaker(kInternalSpeaker);
internal_speaker.active = false;
AudioNode headphone(kHeadphone);
headphone.active = false;
AudioNode internal_mic(kInternalMic);
internal_mic.active = false;
audio_nodes.push_back(internal_speaker);
audio_nodes.push_back(headphone);
audio_nodes.push_back(internal_mic);
const size_t init_nodes_size = audio_nodes.size();
ChangeAudioNodes(audio_nodes);
ChangeAudioNodes(audio_nodes);
EXPECT_EQ(2, test_observer_->audio_nodes_changed_count());
EXPECT_EQ(1, test_observer_->active_output_node_changed_count());
EXPECT_EQ(headphone.id, cras_audio_handler_->GetActiveOutputNode());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(headphone.id, active_output.id);
EXPECT_EQ(internal_mic.id, cras_audio_handler_->GetActiveInputNode());
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(init_nodes_size, audio_devices.size());
for (size_t i = 0; i < audio_devices.size(); ++i) {
if (audio_devices[i].id == internal_speaker.id)
EXPECT_FALSE(audio_devices[i].active);
else if (audio_devices[i].id == headphone.id)
EXPECT_TRUE(audio_devices[i].active);
else if (audio_devices[i].id == internal_mic.id)
EXPECT_TRUE(audio_devices[i].active);
else
NOTREACHED();
}
}
TEST_F(CrasAudioHandlerTest, SetOutputMute) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
SetUpCrasAudioHandler(audio_nodes);
EXPECT_EQ(0, test_observer_->output_mute_changed_count());
cras_audio_handler_->SetOutputMute(true);
EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
EXPECT_EQ(1, test_observer_->output_mute_changed_count());
AudioDevice speaker(kInternalSpeaker);
EXPECT_TRUE(audio_pref_handler_->GetMuteValue(speaker));
cras_audio_handler_->SetOutputMute(false);
EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
EXPECT_EQ(2, test_observer_->output_mute_changed_count());
EXPECT_FALSE(audio_pref_handler_->GetMuteValue(speaker));
}
TEST_F(CrasAudioHandlerTest, SetInputMute) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
EXPECT_EQ(0, test_observer_->input_mute_changed_count());
cras_audio_handler_->SetInputMute(true);
EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
EXPECT_EQ(1, test_observer_->input_mute_changed_count());
AudioDevice internal_mic(kInternalMic);
EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic));
cras_audio_handler_->SetInputMute(false);
EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
EXPECT_EQ(2, test_observer_->input_mute_changed_count());
EXPECT_FALSE(audio_pref_handler_->GetMuteValue(internal_mic));
}
TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
SetUpCrasAudioHandler(audio_nodes);
EXPECT_EQ(0, test_observer_->output_volume_changed_count());
cras_audio_handler_->SetOutputVolumePercent(60);
const int kVolume = 60;
EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
EXPECT_EQ(1, test_observer_->output_volume_changed_count());
AudioDevice device;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&device));
EXPECT_EQ(device.id, kInternalSpeaker.id);
EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
}
TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalMic);
SetUpCrasAudioHandler(audio_nodes);
EXPECT_EQ(0, test_observer_->input_gain_changed_count());
cras_audio_handler_->SetInputGainPercent(60);
const int kGain = 60;
EXPECT_EQ(kGain, cras_audio_handler_->GetInputGainPercent());
EXPECT_EQ(1, test_observer_->input_gain_changed_count());
AudioDevice internal_mic(kInternalMic);
EXPECT_EQ(kGain, audio_pref_handler_->GetInputGainValue(&internal_mic));
}
TEST_F(CrasAudioHandlerTest, SetMuteForDevice) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
audio_nodes.push_back(kInternalMic);
audio_nodes.push_back(kUSBMic);
SetUpCrasAudioHandler(audio_nodes);
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
cras_audio_handler_->SetMuteForDevice(kHeadphone.id, true);
EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kHeadphone.id));
AudioDevice headphone(kHeadphone);
EXPECT_TRUE(audio_pref_handler_->GetMuteValue(headphone));
cras_audio_handler_->SetMuteForDevice(kInternalSpeaker.id, true);
EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalSpeaker.id));
AudioDevice internal_speaker(kInternalSpeaker);
EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_speaker));
EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
cras_audio_handler_->SetMuteForDevice(kUSBMic.id, true);
EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kUSBMic.id));
AudioDevice usb_mic(kUSBMic);
EXPECT_TRUE(audio_pref_handler_->GetMuteValue(usb_mic));
cras_audio_handler_->SetMuteForDevice(kInternalMic.id, true);
EXPECT_TRUE(cras_audio_handler_->IsOutputMutedForDevice(kInternalMic.id));
AudioDevice internal_mic(kInternalMic);
EXPECT_TRUE(audio_pref_handler_->GetMuteValue(internal_mic));
}
TEST_F(CrasAudioHandlerTest, SetVolumeGainPercentForDevice) {
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kHeadphone);
audio_nodes.push_back(kInternalMic);
audio_nodes.push_back(kUSBMic);
SetUpCrasAudioHandler(audio_nodes);
const int kHeadphoneVolume = 30;
EXPECT_EQ(kHeadphone.id, cras_audio_handler_->GetActiveOutputNode());
cras_audio_handler_->SetVolumeGainPercentForDevice(kHeadphone.id,
kHeadphoneVolume);
EXPECT_EQ(kHeadphoneVolume,
cras_audio_handler_->GetOutputVolumePercentForDevice(
kHeadphone.id));
AudioDevice headphone(kHeadphone);
EXPECT_EQ(kHeadphoneVolume,
audio_pref_handler_->GetOutputVolumeValue(&headphone));
const int kSpeakerVolume = 60;
cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalSpeaker.id,
kSpeakerVolume);
EXPECT_EQ(kSpeakerVolume,
cras_audio_handler_->GetOutputVolumePercentForDevice(
kInternalSpeaker.id));
AudioDevice speaker(kInternalSpeaker);
EXPECT_EQ(kSpeakerVolume,
audio_pref_handler_->GetOutputVolumeValue(&speaker));
const int kUSBMicGain = 30;
EXPECT_EQ(kUSBMic.id, cras_audio_handler_->GetActiveInputNode());
cras_audio_handler_->SetVolumeGainPercentForDevice(kUSBMic.id,
kUSBMicGain);
EXPECT_EQ(kUSBMicGain,
cras_audio_handler_->GetOutputVolumePercentForDevice(kUSBMic.id));
AudioDevice usb_mic(kHeadphone);
EXPECT_EQ(kUSBMicGain,
audio_pref_handler_->GetInputGainValue(&usb_mic));
const int kInternalMicGain = 60;
cras_audio_handler_->SetVolumeGainPercentForDevice(kInternalMic.id,
kInternalMicGain);
EXPECT_EQ(kInternalMicGain,
cras_audio_handler_->GetOutputVolumePercentForDevice(
kInternalMic.id));
AudioDevice internal_mic(kInternalMic);
EXPECT_EQ(kInternalMicGain,
audio_pref_handler_->GetInputGainValue(&internal_mic));
}
TEST_F(CrasAudioHandlerTest, HandleOtherDeviceType) {
const size_t kNumValidAudioDevices = 4;
AudioNodeList audio_nodes;
audio_nodes.push_back(kInternalSpeaker);
audio_nodes.push_back(kOtherTypeOutput);
audio_nodes.push_back(kInternalMic);
audio_nodes.push_back(kOtherTypeInput);
SetUpCrasAudioHandler(audio_nodes);
AudioDeviceList audio_devices;
cras_audio_handler_->GetAudioDevices(&audio_devices);
EXPECT_EQ(kNumValidAudioDevices, audio_devices.size());
AudioDevice active_output;
EXPECT_TRUE(cras_audio_handler_->GetActiveOutputDevice(&active_output));
EXPECT_EQ(kInternalSpeaker.id, active_output.id);
EXPECT_EQ(kInternalSpeaker.id, cras_audio_handler_->GetActiveOutputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
AudioDevice active_input;
EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetActiveInputNode());
EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
}
}