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..46a945977bebc9bab6bdd411efc1c97ca380b404 100644 |
--- a/chromeos/audio/cras_audio_handler_unittest.cc |
+++ b/chromeos/audio/cras_audio_handler_unittest.cc |
@@ -12,8 +12,12 @@ |
#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; |
+ |
namespace chromeos { |
const uint64 kInternalSpeakerId = 10001; |
@@ -21,11 +25,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 +122,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 +154,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 +316,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,12 +343,44 @@ 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_) |
+ SetUpNiceMockAudioManager(); |
+ } |
+ |
+ void SetUpNiceMockAudioManager() { |
+ nice_mock_audio_manager_ = new NiceMock<MockAudioManagerWrapper>(); |
+ CrasAudioHandler::SetUpAudioManagerWrapperForTesting( |
+ nice_mock_audio_manager_); |
+ } |
+ |
+ // Set the expected method and arguments on strict_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; |
+ SetUpStrictMockAudioManager(); |
+ EXPECT_CALL(*strict_mock_audio_manager_, |
+ SetHasInputDevices(has_input_devices)); |
+ SetUpCrasAudioHandler(audio_nodes); |
+ } |
+ |
+ void SetUpStrictMockAudioManager() { |
+ strict_mock_audio_manager_ = new StrictMock<MockAudioManagerWrapper>(); |
+ CrasAudioHandler::SetUpAudioManagerWrapperForTesting( |
+ strict_mock_audio_manager_); |
+ } |
+ |
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(); |
+ SetUpDefaultNiceMockAudioManagerIfNeeded(); |
CrasAudioHandler::Initialize(audio_pref_handler_); |
cras_audio_handler_ = CrasAudioHandler::Get(); |
test_observer_.reset(new TestObserver); |
@@ -315,6 +397,7 @@ 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(); |
+ SetUpDefaultNiceMockAudioManagerIfNeeded(); |
CrasAudioHandler::Initialize(audio_pref_handler_); |
cras_audio_handler_ = CrasAudioHandler::Get(); |
test_observer_.reset(new TestObserver); |
@@ -349,6 +432,9 @@ class CrasAudioHandlerTest : public testing::Test { |
FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. |
scoped_ptr<TestObserver> test_observer_; |
scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; |
+ NiceMock<MockAudioManagerWrapper>* nice_mock_audio_manager_; |
+ StrictMock<MockAudioManagerWrapper>* strict_mock_audio_manager_; |
+ bool use_nice_mock_audio_manager_; |
private: |
DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); |
@@ -2493,4 +2579,73 @@ 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) { |
+ // Set up audio handler with output nodes and input nodes for loopback. |
+ AudioNodeList audio_nodes; |
+ audio_nodes.push_back(kInternalSpeaker); |
+ audio_nodes.push_back(kPostMixLoopback); |
+ audio_nodes.push_back(kPostDSPLoopback); |
+ |
+ // SetHasInputAudioDevices should be called with argument false. |
+ use_nice_mock_audio_manager_ = false; |
+ SetUpStrictMockAudioManager(); |
+ EXPECT_CALL(*strict_mock_audio_manager_, SetHasInputDevices(false)); |
+ SetUpCrasAudioHandler(audio_nodes); |
+ |
+ // Plug the Mic Jack. SetHasInputAudioDevices should get called with argument |
+ // true. |
+ audio_nodes.push_back(kMicJack); |
+ EXPECT_CALL(*strict_mock_audio_manager_, SetHasInputDevices(true)); |
+ 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 = {kHeadphone, |
Daniel Erat
2015/06/23 14:15:49
i don't think that this initialization style is al
cychiang
2015/06/24 08:11:29
Done.
|
+ kUSBHeadphone1, |
+ kBluetoothHeadset, |
+ kHDMIOutput, |
+ kInternalSpeaker, |
+ kKeyboardMic, |
+ kAOKR, |
+ kPostMixLoopback, |
+ kPostDSPLoopback, |
+ kOtherTypeOutput, |
+ 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 = {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 = {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 = {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 = {kBluetoothHeadsetMic}; |
+ CheckSetHasInputDevices(audio_nodes, true); |
+} |
+ |
} // namespace chromeos |