Chromium Code Reviews| Index: chromeos/audio/cras_audio_handler_unittest.cc |
| diff --git a/chromeos/audio/cras_audio_handler_unittest.cc b/chromeos/audio/cras_audio_handler_unittest.cc |
| index fa5dbc35ba60a5c266c49f4c1517455b8022c1ae..03d2604d24d19e69eb7803ebce31e175b14623a9 100644 |
| --- a/chromeos/audio/cras_audio_handler_unittest.cc |
| +++ b/chromeos/audio/cras_audio_handler_unittest.cc |
| @@ -12,8 +12,13 @@ |
| #include "chromeos/dbus/audio_node.h" |
| #include "chromeos/dbus/dbus_thread_manager.h" |
| #include "chromeos/dbus/fake_cras_audio_client.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +using testing::NiceMock; |
| +using testing::StrictMock; |
| +using testing::InSequence; |
|
Daniel Erat
2015/06/24 13:37:45
nit: move this above NiceMock to keep the list alp
cychiang
2015/06/25 05:45:25
InSequence is not used after using mock_audio_mana
|
| + |
| namespace chromeos { |
| const uint64 kInternalSpeakerId = 10001; |
| @@ -21,11 +26,15 @@ 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 kKeyboardMicId = 10010; |
| +const uint64 kBluetoothHeadsetMicId = 10006; |
| +const uint64 kHDMIOutputId = 10007; |
| +const uint64 kUSBHeadphoneId1 = 10008; |
| +const uint64 kUSBHeadphoneId2 = 10009; |
| +const uint64 kMicJackId = 10010; |
| +const uint64 kKeyboardMicId = 10011; |
| +const uint64 kPostMixLoopbackId = 10012; |
| +const uint64 kPostDSPLoopbackId = 10013; |
| +const uint64 kAOKRId = 10014; |
| const uint64 kOtherTypeOutputId = 90001; |
| const uint64 kOtherTypeInputId = 90002; |
| const uint64 kUSBJabraSpeakerOutputId1 = 90003; |
| @@ -114,6 +123,30 @@ const AudioNode kOtherTypeInput( |
| 0 |
| ); |
| +const AudioNode kPostMixLoopback(false, |
| + kPostMixLoopbackId, |
| + "Fake Post Mix Loopback", |
| + "POST_MIX_LOOPBACK", |
| + "Post Mix Loopback", |
| + false, |
| + 0); |
| + |
| +const AudioNode kPostDSPLoopback(false, |
| + kPostDSPLoopbackId, |
| + "Fake Post DSP Loopback", |
| + "POST_DSP_LOOPBACK", |
| + "Post DSP Loopback", |
| + false, |
| + 0); |
| + |
| +const AudioNode kAOKR(true, |
| + kAOKRId, |
| + "Fake AOKR Mic", |
| + "AOKR", |
| + "AOKR Mic", |
| + false, |
| + 0); |
| + |
| const AudioNode kBluetoothHeadset(false, |
| kBluetoothHeadsetId, |
| "Bluetooth Headset", |
| @@ -122,6 +155,14 @@ const AudioNode kBluetoothHeadset(false, |
| false, |
| 0); |
| +const AudioNode kBluetoothHeadsetMic(true, |
| + kBluetoothHeadsetMicId, |
| + "Bluetooth Headset", |
| + "BLUETOOTH", |
| + "Bluetooth Headset 1", |
| + false, |
| + 0); |
| + |
| const AudioNode kHDMIOutput(false, |
| kHDMIOutputId, |
| "HDMI output", |
| @@ -276,11 +317,21 @@ class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { |
| DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| }; |
| +// AudioManagerWrapper is mocked in unittest because unittest is run in an |
| +// environment without AudioManagerCras. |
| +class MockAudioManagerWrapper : public CrasAudioHandler::AudioManagerWrapper { |
| + public: |
| + MockAudioManagerWrapper() {} |
| + ~MockAudioManagerWrapper() override {} |
| + MOCK_METHOD1(SetHasInputDevices, void(bool has_input_devices)); |
| +}; |
| + |
| class CrasAudioHandlerTest : public testing::Test { |
| public: |
| - CrasAudioHandlerTest() : cras_audio_handler_(NULL), |
| - fake_cras_audio_client_(NULL) { |
| - } |
| + CrasAudioHandlerTest() |
| + : cras_audio_handler_(NULL), |
| + fake_cras_audio_client_(NULL), |
| + use_nice_mock_audio_manager_(true) {} |
| ~CrasAudioHandlerTest() override {} |
| void SetUp() override {} |
| @@ -293,13 +344,34 @@ class CrasAudioHandlerTest : public testing::Test { |
| DBusThreadManager::Shutdown(); |
| } |
| + // This is for the cases where the test does not care |
| + // audio manager behavior. |
| + void SetUpDefaultNiceMockAudioManagerIfNeeded() { |
| + if (use_nice_mock_audio_manager_) |
| + mock_audio_manager_.reset(new NiceMock<MockAudioManagerWrapper>()); |
| + } |
| + |
| + // Set the expected method and arguments on mock_audio_manager_ to |
| + // check that SetHasInputDevices is called with correct argument when a |
| + // list of audio nodes present in CrasAudioHandler. |
| + void CheckSetHasInputDevices(AudioNodeList audio_nodes, |
| + bool has_input_devices) { |
| + use_nice_mock_audio_manager_ = false; |
| + mock_audio_manager_.reset(new StrictMock<MockAudioManagerWrapper>()); |
|
Daniel Erat
2015/06/24 13:37:45
it might be better to make mock_audio_manager_ be
cychiang
2015/06/25 05:45:24
Done.
I see! make_scoped_ptr is very useful.
This
|
| + EXPECT_CALL(*mock_audio_manager_.get(), |
| + SetHasInputDevices(has_input_devices)); |
| + SetUpCrasAudioHandler(audio_nodes); |
| + } |
| + |
| void SetUpCrasAudioHandler(const AudioNodeList& audio_nodes) { |
| DBusThreadManager::Initialize(); |
| fake_cras_audio_client_ = static_cast<FakeCrasAudioClient*>( |
| DBusThreadManager::Get()->GetCrasAudioClient()); |
| fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
| audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
| - CrasAudioHandler::Initialize(audio_pref_handler_); |
| + SetUpDefaultNiceMockAudioManagerIfNeeded(); |
| + CrasAudioHandler::Initialize(audio_pref_handler_, |
| + mock_audio_manager_.Pass()); |
| cras_audio_handler_ = CrasAudioHandler::Get(); |
| test_observer_.reset(new TestObserver); |
| cras_audio_handler_->AddAudioObserver(test_observer_.get()); |
| @@ -315,7 +387,9 @@ class CrasAudioHandlerTest : public testing::Test { |
| fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); |
| fake_cras_audio_client_->SetActiveOutputNode(primary_active_node.id), |
| audio_pref_handler_ = new AudioDevicesPrefHandlerStub(); |
| - CrasAudioHandler::Initialize(audio_pref_handler_); |
| + SetUpDefaultNiceMockAudioManagerIfNeeded(); |
| + CrasAudioHandler::Initialize(audio_pref_handler_, |
| + mock_audio_manager_.Pass()); |
| cras_audio_handler_ = CrasAudioHandler::Get(); |
| test_observer_.reset(new TestObserver); |
| cras_audio_handler_->AddAudioObserver(test_observer_.get()); |
| @@ -349,6 +423,8 @@ class CrasAudioHandlerTest : public testing::Test { |
| FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. |
| scoped_ptr<TestObserver> test_observer_; |
| scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; |
| + scoped_ptr<MockAudioManagerWrapper> mock_audio_manager_; |
| + bool use_nice_mock_audio_manager_; |
| private: |
| DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); |
| @@ -2493,4 +2569,80 @@ TEST_F(CrasAudioHandlerTest, ActiveNodeLostDuringLoginSession) { |
| EXPECT_TRUE(headphone_resumed->active); |
| } |
| +// Test the case where there is no input device in the beginning. |
| +// SetHasInputDevices on AudioManagerWrapper is called with argument false. |
| +// After a mic jack is plugged, CrasAudioHandler calls SetHasInputDevices on |
| +// AudioManagerWrapper with argument true. |
| +TEST_F(CrasAudioHandlerTest, PlugMicJackAndUpdateAudioManager) { |
| + use_nice_mock_audio_manager_ = false; |
| + mock_audio_manager_.reset(new StrictMock<MockAudioManagerWrapper>()); |
| + |
| + InSequence s; |
| + EXPECT_CALL(*mock_audio_manager_.get(), SetHasInputDevices(false)); |
| + EXPECT_CALL(*mock_audio_manager_.get(), SetHasInputDevices(true)); |
| + |
| + // Set up audio handler with output nodes and input nodes for loopback. |
| + // SetHasInputAudioDevices should be called with argument false. |
| + AudioNodeList audio_nodes; |
| + audio_nodes.push_back(kInternalSpeaker); |
| + audio_nodes.push_back(kPostMixLoopback); |
| + audio_nodes.push_back(kPostDSPLoopback); |
| + SetUpCrasAudioHandler(audio_nodes); |
| + |
| + // Plug the Mic Jack. SetHasInputAudioDevices should get called with argument |
| + // true. |
| + audio_nodes.push_back(kMicJack); |
| + ChangeAudioNodes(audio_nodes); |
| +} |
| + |
| +// Test the case where there is no input device for simple usage. |
| +// SetHasInputDevices on AudioManagerWrapper is called with argument false. |
| +TEST_F(CrasAudioHandlerTest, SetHasInputDeviceToFalse) { |
| + AudioNodeList audio_nodes; |
| + audio_nodes.push_back(kHeadphone); |
| + audio_nodes.push_back(kUSBHeadphone1); |
| + audio_nodes.push_back(kBluetoothHeadset); |
| + audio_nodes.push_back(kHDMIOutput); |
| + audio_nodes.push_back(kInternalSpeaker); |
| + audio_nodes.push_back(kKeyboardMic); |
| + audio_nodes.push_back(kAOKR); |
| + audio_nodes.push_back(kPostMixLoopback); |
| + audio_nodes.push_back(kPostDSPLoopback); |
| + audio_nodes.push_back(kOtherTypeOutput); |
| + audio_nodes.push_back(kOtherTypeInput); |
| + CheckSetHasInputDevices(audio_nodes, false); |
| +} |
| + |
| +// Test the case where there is an internal mic. |
| +// SetHasInputDevices on AudioManagerWrapper is called with argument true. |
| +TEST_F(CrasAudioHandlerTest, SetHasInputDeviceToTrueForInternalMic) { |
| + AudioNodeList audio_nodes; |
| + audio_nodes.push_back(kInternalMic); |
| + CheckSetHasInputDevices(audio_nodes, true); |
| +} |
| + |
| +// Test the case where there is an external mic. |
| +// SetHasInputDevices on AudioManagerWrapper is called with argument true. |
| +TEST_F(CrasAudioHandlerTest, SetHasInputDeviceToTrueForMicJack) { |
| + AudioNodeList audio_nodes; |
| + audio_nodes.push_back(kMicJack); |
| + CheckSetHasInputDevices(audio_nodes, true); |
| +} |
| + |
| +// Test the case where there is a USB mic. |
| +// SetHasInputDevices on AudioManagerWrapper is called with argument true. |
| +TEST_F(CrasAudioHandlerTest, SetHasInputDeviceToTrueForUSBMic) { |
| + AudioNodeList audio_nodes; |
| + audio_nodes.push_back(kUSBMic); |
| + CheckSetHasInputDevices(audio_nodes, true); |
| +} |
| + |
| +// Test the case where there is a bluetooth mic. |
| +// SetHasInputDevices on AudioManagerWrapper is called with argument true. |
| +TEST_F(CrasAudioHandlerTest, SetHasInputDeviceToTrueForBluetoothMic) { |
| + AudioNodeList audio_nodes; |
| + audio_nodes.push_back(kBluetoothHeadsetMic); |
| + CheckSetHasInputDevices(audio_nodes, true); |
| +} |
| + |
| } // namespace chromeos |