Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/audio/cras_audio_handler.h" | 5 #include "chromeos/audio/cras_audio_handler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chromeos/audio/audio_devices_pref_handler.h" | 19 #include "chromeos/audio/audio_devices_pref_handler.h" |
| 20 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 20 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
| 21 #include "chromeos/dbus/audio_node.h" | 21 #include "chromeos/dbus/audio_node.h" |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | 22 #include "chromeos/dbus/dbus_thread_manager.h" |
| 23 #include "chromeos/dbus/fake_cras_audio_client.h" | 23 #include "chromeos/dbus/fake_cras_audio_client.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 namespace chromeos { | 27 namespace chromeos { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const uint64_t kInternalSpeakerId = 10001; | 30 const uint64_t kInternalSpeakerId = 10001; |
| 30 const uint64_t kHeadphoneId = 10002; | 31 const uint64_t kHeadphoneId = 10002; |
| 31 const uint64_t kInternalMicId = 10003; | 32 const uint64_t kInternalMicId = 10003; |
| 32 const uint64_t kUSBMicId = 10004; | 33 const uint64_t kUSBMicId = 10004; |
| 33 const uint64_t kBluetoothHeadsetId = 10005; | 34 const uint64_t kBluetoothHeadsetId = 10005; |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 int output_mute_changed_count_; | 300 int output_mute_changed_count_; |
| 300 int input_mute_changed_count_; | 301 int input_mute_changed_count_; |
| 301 int output_volume_changed_count_; | 302 int output_volume_changed_count_; |
| 302 int input_gain_changed_count_; | 303 int input_gain_changed_count_; |
| 303 bool output_mute_by_system_; // output mute state adjusted by system. | 304 bool output_mute_by_system_; // output mute state adjusted by system. |
| 304 int output_channel_remixing_changed_count_; | 305 int output_channel_remixing_changed_count_; |
| 305 | 306 |
| 306 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 307 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 307 }; | 308 }; |
| 308 | 309 |
| 310 // A Mock CrasAudioClientObserver to detect if Cras side dbus signals are | |
| 311 // correctly received. | |
| 312 class MockAudioClientObserver : public CrasAudioClient::Observer { | |
| 313 public: | |
| 314 MockAudioClientObserver() {} | |
| 315 ~MockAudioClientObserver() override {} | |
| 316 | |
| 317 MOCK_METHOD2(OutputNodeVolumeChanged, void(uint64_t node_id, int volume)); | |
| 318 }; | |
| 319 | |
| 309 } // namespace | 320 } // namespace |
| 310 | 321 |
| 311 class CrasAudioHandlerTest : public testing::Test { | 322 class CrasAudioHandlerTest : public testing::Test { |
| 312 public: | 323 public: |
| 313 CrasAudioHandlerTest() : cras_audio_handler_(NULL), | 324 CrasAudioHandlerTest() : cras_audio_handler_(NULL), |
| 314 fake_cras_audio_client_(NULL) { | 325 fake_cras_audio_client_(NULL) { |
| 315 } | 326 } |
| 316 ~CrasAudioHandlerTest() override {} | 327 ~CrasAudioHandlerTest() override {} |
| 317 | 328 |
| 318 void SetUp() override {} | 329 void SetUp() override {} |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 } | 425 } |
| 415 | 426 |
| 416 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) { | 427 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) { |
| 417 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms); | 428 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms); |
| 418 } | 429 } |
| 419 | 430 |
| 420 bool IsDuringHDMIRediscoverGracePeriod() { | 431 bool IsDuringHDMIRediscoverGracePeriod() { |
| 421 return cras_audio_handler_->hdmi_rediscovering(); | 432 return cras_audio_handler_->hdmi_rediscovering(); |
| 422 } | 433 } |
| 423 | 434 |
| 435 void RestartAudioClient(const AudioNodeList& audio_nodes) { | |
|
jennyz
2016/08/04 16:07:33
It is better not to pass audio_nodes in RestartAud
Qiang(Joe) Xu
2016/08/04 22:51:58
done by deleting this method
| |
| 436 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); | |
| 437 fake_cras_audio_client_->NotifyAudioClientRestartedForTesting(); | |
| 438 message_loop_.RunUntilIdle(); | |
| 439 } | |
| 440 | |
| 424 protected: | 441 protected: |
| 425 base::MessageLoopForUI message_loop_; | 442 base::MessageLoopForUI message_loop_; |
| 426 CrasAudioHandler* cras_audio_handler_; // Not owned. | 443 CrasAudioHandler* cras_audio_handler_; // Not owned. |
| 427 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. | 444 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. |
| 428 std::unique_ptr<TestObserver> test_observer_; | 445 std::unique_ptr<TestObserver> test_observer_; |
| 429 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; | 446 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; |
| 430 | 447 |
| 431 private: | 448 private: |
| 432 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); | 449 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); |
| 433 }; | 450 }; |
| (...skipping 1531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1965 EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); | 1982 EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); |
| 1966 EXPECT_EQ(2, test_observer_->input_mute_changed_count()); | 1983 EXPECT_EQ(2, test_observer_->input_mute_changed_count()); |
| 1967 } | 1984 } |
| 1968 | 1985 |
| 1969 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) { | 1986 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) { |
| 1970 AudioNodeList audio_nodes; | 1987 AudioNodeList audio_nodes; |
| 1971 audio_nodes.push_back(kInternalSpeaker); | 1988 audio_nodes.push_back(kInternalSpeaker); |
| 1972 SetUpCrasAudioHandler(audio_nodes); | 1989 SetUpCrasAudioHandler(audio_nodes); |
| 1973 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | 1990 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 1974 | 1991 |
| 1975 cras_audio_handler_->SetOutputVolumePercent(60); | 1992 const int kVolume = 60; |
| 1993 cras_audio_handler_->SetOutputVolumePercent(kVolume); | |
| 1976 | 1994 |
| 1977 // Verify the output volume is changed to the designated value, | 1995 // Verify the output volume is changed to the designated value, |
| 1978 // OnOutputNodeVolumeChanged event is fired, and the device volume value | 1996 // OnOutputNodeVolumeChanged event is fired, and the device volume value |
| 1979 // is saved the preferences. | 1997 // is saved in the preferences. |
| 1980 const int kVolume = 60; | |
| 1981 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); | 1998 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 1982 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | 1999 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 1983 AudioDevice device; | 2000 AudioDevice device; |
| 1984 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | 2001 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
| 1985 EXPECT_EQ(device.id, kInternalSpeaker.id); | 2002 EXPECT_EQ(device.id, kInternalSpeaker.id); |
| 1986 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); | 2003 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 1987 } | 2004 } |
| 1988 | 2005 |
| 2006 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercentWithoutNotifyingObservers) { | |
| 2007 AudioNodeList audio_nodes; | |
| 2008 audio_nodes.push_back(kInternalSpeaker); | |
| 2009 SetUpCrasAudioHandler(audio_nodes); | |
| 2010 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2011 | |
| 2012 const int kVolume1 = 60; | |
| 2013 const int kVolume2 = 80; | |
| 2014 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( | |
| 2015 kVolume1, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | |
| 2016 // Verify the output volume is changed to the designated value, | |
| 2017 // OnOutputNodeVolumeChanged event is not fired, and the device volume value | |
| 2018 // is saved in the preferences. | |
| 2019 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2020 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2021 AudioDevice device; | |
| 2022 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | |
| 2023 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 2024 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device)); | |
| 2025 | |
| 2026 // Make another SetOutputVolumePercentWithoutNotifyingObservers call to make | |
| 2027 // sure everything is right. | |
| 2028 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( | |
| 2029 kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | |
| 2030 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2031 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2032 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | |
| 2033 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 2034 EXPECT_EQ(kVolume2, audio_pref_handler_->GetOutputVolumeValue(&device)); | |
| 2035 | |
| 2036 // Make a final SetOutputVolumePercent call to check if | |
| 2037 // SetOutputVolumePercentWithoutNotifyingObservers may block subsequent | |
| 2038 // notifying observers. | |
| 2039 cras_audio_handler_->SetOutputVolumePercent(kVolume1); | |
| 2040 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2041 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | |
| 2042 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | |
| 2043 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 2044 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device)); | |
| 2045 } | |
| 2046 | |
| 2047 // OutputNodeVolumeChanged can be asynchronously fired by several reaons, | |
| 2048 // such as restarting audio client, SetOutputVolumePercent and | |
| 2049 // SetOutputVolumePercentWithoutNotifyingObservers. They should be correctly | |
| 2050 // received. | |
|
jennyz
2016/08/04 16:07:33
It is not very clear what is tested here. OutputNo
Qiang(Joe) Xu
2016/08/04 22:51:58
Done.
| |
| 2051 TEST_F(CrasAudioHandlerTest, OutputNodeVolumeChangedTest) { | |
| 2052 AudioNodeList audio_nodes; | |
| 2053 audio_nodes.push_back(kInternalSpeaker); | |
| 2054 SetUpCrasAudioHandler(audio_nodes); | |
| 2055 MockAudioClientObserver audio_client_observer; | |
| 2056 fake_cras_audio_client_->AddObserver(&audio_client_observer); | |
| 2057 | |
| 2058 // Set Cras OutputNodeVolumeChanged signal expectations. They are sequentially | |
| 2059 // received according to the CrasAudioHandler's calls. | |
| 2060 const int kVolume1 = 30; | |
| 2061 const int kVolume2 = 40; | |
| 2062 const int kVolume3 = 50; | |
| 2063 const int kVolume4 = 60; | |
| 2064 EXPECT_CALL(audio_client_observer, | |
| 2065 OutputNodeVolumeChanged(kInternalSpeaker.id, kVolume1)) | |
| 2066 .Times(3); | |
|
Daniel Erat
2016/08/04 16:25:51
i don't understand what this MockAudioClientObserv
Qiang(Joe) Xu
2016/08/04 22:51:58
The updated unittest should work in this way. Than
| |
| 2067 EXPECT_CALL(audio_client_observer, | |
| 2068 OutputNodeVolumeChanged(kInternalSpeaker.id, kVolume2)) | |
| 2069 .Times(1); | |
| 2070 EXPECT_CALL(audio_client_observer, | |
| 2071 OutputNodeVolumeChanged(kInternalSpeaker.id, kVolume3)) | |
| 2072 .Times(1); | |
| 2073 EXPECT_CALL(audio_client_observer, | |
| 2074 OutputNodeVolumeChanged(kInternalSpeaker.id, kVolume4)) | |
| 2075 .Times(1); | |
| 2076 | |
| 2077 // Sequenced calls which will raise Cras side OutputNodeVolumeChanged signals. | |
| 2078 cras_audio_handler_->SetOutputVolumePercent(kVolume1); | |
| 2079 // RestartAudioClient causes InitializingAudioState, which should not notify | |
| 2080 // AudioObserver::OnOutputNodeVolumeChanged. | |
| 2081 RestartAudioClient(audio_nodes); | |
| 2082 RestartAudioClient(audio_nodes); | |
| 2083 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( | |
| 2084 kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | |
| 2085 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( | |
| 2086 kVolume3, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | |
| 2087 cras_audio_handler_->SetOutputVolumePercent(kVolume4); | |
| 2088 | |
| 2089 // Only the two SetOutputVolumePercent calls notifying observers. | |
| 2090 EXPECT_EQ(2, test_observer_->output_volume_changed_count()); | |
| 2091 | |
| 2092 fake_cras_audio_client_->RemoveObserver(&audio_client_observer); | |
| 2093 message_loop_.RunUntilIdle(); | |
| 2094 } | |
| 2095 | |
| 1989 TEST_F(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) { | 2096 TEST_F(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) { |
| 1990 AudioNodeList audio_nodes; | 2097 AudioNodeList audio_nodes; |
| 1991 audio_nodes.push_back(kInternalSpeaker); | 2098 audio_nodes.push_back(kInternalSpeaker); |
| 1992 SetUpCrasAudioHandler(audio_nodes); | 2099 SetUpCrasAudioHandler(audio_nodes); |
| 1993 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | 2100 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 1994 | 2101 |
| 1995 const int kDefaultVolume = 75; | 2102 const int kDefaultVolume = 75; |
| 1996 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | 2103 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 1997 | 2104 |
| 1998 // Disable the auto OutputNodeVolumeChanged signal. | 2105 // Disable the auto OutputNodeVolumeChanged signal. |
| (...skipping 1294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3293 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); | 3400 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 3294 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 3401 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
| 3295 EXPECT_EQ(kInternalSpeaker.id, | 3402 EXPECT_EQ(kInternalSpeaker.id, |
| 3296 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 3403 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3297 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); | 3404 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 3298 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); | 3405 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); |
| 3299 EXPECT_TRUE(test_observer_->output_mute_by_system()); | 3406 EXPECT_TRUE(test_observer_->output_mute_by_system()); |
| 3300 } | 3407 } |
| 3301 | 3408 |
| 3302 } // namespace chromeos | 3409 } // namespace chromeos |
| OLD | NEW |