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> |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 } | 414 } |
| 415 | 415 |
| 416 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) { | 416 void SetHDMIRediscoverGracePeriodDuration(int duration_in_ms) { |
| 417 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms); | 417 cras_audio_handler_->SetHDMIRediscoverGracePeriodForTesting(duration_in_ms); |
| 418 } | 418 } |
| 419 | 419 |
| 420 bool IsDuringHDMIRediscoverGracePeriod() { | 420 bool IsDuringHDMIRediscoverGracePeriod() { |
| 421 return cras_audio_handler_->hdmi_rediscovering(); | 421 return cras_audio_handler_->hdmi_rediscovering(); |
| 422 } | 422 } |
| 423 | 423 |
| 424 void RestartAudioClient() { | |
| 425 cras_audio_handler_->AudioClientRestarted(); | |
| 426 message_loop_.RunUntilIdle(); | |
| 427 } | |
| 428 | |
| 424 protected: | 429 protected: |
| 425 base::MessageLoopForUI message_loop_; | 430 base::MessageLoopForUI message_loop_; |
| 426 CrasAudioHandler* cras_audio_handler_; // Not owned. | 431 CrasAudioHandler* cras_audio_handler_; // Not owned. |
| 427 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. | 432 FakeCrasAudioClient* fake_cras_audio_client_; // Not owned. |
| 428 std::unique_ptr<TestObserver> test_observer_; | 433 std::unique_ptr<TestObserver> test_observer_; |
| 429 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; | 434 scoped_refptr<AudioDevicesPrefHandlerStub> audio_pref_handler_; |
| 430 | 435 |
| 431 private: | 436 private: |
| 432 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); | 437 DISALLOW_COPY_AND_ASSIGN(CrasAudioHandlerTest); |
| 433 }; | 438 }; |
| (...skipping 1531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1965 EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); | 1970 EXPECT_FALSE(cras_audio_handler_->IsInputMuted()); |
| 1966 EXPECT_EQ(2, test_observer_->input_mute_changed_count()); | 1971 EXPECT_EQ(2, test_observer_->input_mute_changed_count()); |
| 1967 } | 1972 } |
| 1968 | 1973 |
| 1969 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) { | 1974 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercent) { |
| 1970 AudioNodeList audio_nodes; | 1975 AudioNodeList audio_nodes; |
| 1971 audio_nodes.push_back(kInternalSpeaker); | 1976 audio_nodes.push_back(kInternalSpeaker); |
| 1972 SetUpCrasAudioHandler(audio_nodes); | 1977 SetUpCrasAudioHandler(audio_nodes); |
| 1973 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | 1978 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 1974 | 1979 |
| 1975 cras_audio_handler_->SetOutputVolumePercent(60); | 1980 const int kVolume = 60; |
| 1981 cras_audio_handler_->SetOutputVolumePercent(kVolume); | |
| 1976 | 1982 |
| 1977 // Verify the output volume is changed to the designated value, | 1983 // Verify the output volume is changed to the designated value, |
| 1978 // OnOutputNodeVolumeChanged event is fired, and the device volume value | 1984 // OnOutputNodeVolumeChanged event is fired, and the device volume value |
| 1979 // is saved the preferences. | 1985 // is saved in the preferences. |
| 1980 const int kVolume = 60; | |
| 1981 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); | 1986 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 1982 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | 1987 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); |
| 1983 AudioDevice device; | 1988 AudioDevice device; |
| 1984 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | 1989 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); |
| 1985 EXPECT_EQ(device.id, kInternalSpeaker.id); | 1990 EXPECT_EQ(device.id, kInternalSpeaker.id); |
| 1986 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); | 1991 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); |
| 1987 } | 1992 } |
| 1988 | 1993 |
| 1994 TEST_F(CrasAudioHandlerTest, SetOutputVolumePercentWithoutNotifyingObservers) { | |
| 1995 AudioNodeList audio_nodes; | |
| 1996 audio_nodes.push_back(kInternalSpeaker); | |
| 1997 SetUpCrasAudioHandler(audio_nodes); | |
| 1998 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 1999 | |
| 2000 const int kVolume1 = 60; | |
| 2001 const int kVolume2 = 80; | |
| 2002 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( | |
| 2003 kVolume1, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | |
| 2004 // Verify the output volume is changed to the designated value, | |
| 2005 // OnOutputNodeVolumeChanged event is not fired, and the device volume value | |
| 2006 // is saved in the preferences. | |
| 2007 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2008 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2009 AudioDevice device; | |
| 2010 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | |
| 2011 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 2012 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device)); | |
| 2013 | |
| 2014 // Make another SetOutputVolumePercentWithoutNotifyingObservers call to make | |
| 2015 // sure everything is right. | |
| 2016 cras_audio_handler_->SetOutputVolumePercentWithoutNotifyingObservers( | |
| 2017 kVolume2, CrasAudioHandler::VOLUME_CHANGE_MAXIMIZE_MODE_SCREENSHOT); | |
| 2018 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2019 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2020 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | |
| 2021 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 2022 EXPECT_EQ(kVolume2, audio_pref_handler_->GetOutputVolumeValue(&device)); | |
| 2023 | |
| 2024 // Make a final SetOutputVolumePercent call to check if | |
| 2025 // SetOutputVolumePercentWithoutNotifyingObservers may block subsequent | |
| 2026 // notifying observers. | |
| 2027 cras_audio_handler_->SetOutputVolumePercent(kVolume1); | |
| 2028 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2029 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | |
| 2030 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); | |
| 2031 EXPECT_EQ(device.id, kInternalSpeaker.id); | |
| 2032 EXPECT_EQ(kVolume1, audio_pref_handler_->GetOutputVolumeValue(&device)); | |
| 2033 } | |
| 2034 | |
| 2035 TEST_F(CrasAudioHandlerTest, RestartAudioClientWithCrasReady) { | |
| 2036 AudioNodeList audio_nodes; | |
| 2037 audio_nodes.push_back(kInternalSpeaker); | |
| 2038 SetUpCrasAudioHandler(audio_nodes); | |
| 2039 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2040 | |
| 2041 const int kDefaultVolume = 75; | |
|
Daniel Erat
2016/08/05 16:00:53
nit: this test doesn't need to test the default vo
Qiang(Joe) Xu
2016/08/05 17:03:30
Done.
| |
| 2042 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2043 | |
| 2044 // Disable the auto OutputNodeVolumeChanged signal. | |
| 2045 fake_cras_audio_client_->set_notify_volume_change_with_delay(true); | |
| 2046 | |
| 2047 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); | |
| 2048 RestartAudioClient(); | |
| 2049 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2050 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2051 | |
| 2052 // The correct initialization OutputNodeVolumeChanged event is fired. We | |
| 2053 // should avoid notify observers. | |
|
Daniel Erat
2016/08/05 16:00:53
nit: s/notify/notifying/
Qiang(Joe) Xu
2016/08/05 17:03:30
Done.
| |
| 2054 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( | |
| 2055 kInternalSpeaker.id, kDefaultVolume); | |
| 2056 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2057 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2058 | |
| 2059 // The later OutputNodeVolumeChanged event after initialization should notify | |
| 2060 // observers. | |
| 2061 const int kVolume = 60; | |
| 2062 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( | |
| 2063 kInternalSpeaker.id, kVolume); | |
| 2064 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | |
| 2065 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2066 } | |
| 2067 | |
| 2068 TEST_F(CrasAudioHandlerTest, RestartAudioClientWithCrasDropRequest) { | |
| 2069 AudioNodeList audio_nodes; | |
| 2070 audio_nodes.push_back(kInternalSpeaker); | |
| 2071 SetUpCrasAudioHandler(audio_nodes); | |
| 2072 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2073 | |
| 2074 const int kDefaultVolume = 75; | |
| 2075 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
|
Daniel Erat
2016/08/05 16:00:53
nit: same conversation about removing this expecta
Qiang(Joe) Xu
2016/08/05 17:03:30
Done.
| |
| 2076 | |
| 2077 // Disable the auto OutputNodeVolumeChanged signal. | |
| 2078 fake_cras_audio_client_->set_notify_volume_change_with_delay(true); | |
| 2079 | |
| 2080 fake_cras_audio_client_->SetAudioNodesForTesting(audio_nodes); | |
| 2081 RestartAudioClient(); | |
| 2082 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | |
| 2083 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2084 | |
| 2085 // A wrong initialization OutputNodeVolumeChanged event is fired. This may | |
| 2086 // happen when Cras is not ready and drops request. The approach we use is | |
| 2087 // to log warning message, clear the penging automated volume change reasons, | |
|
Daniel Erat
2016/08/05 16:00:53
nit: s/penging/pending/
Qiang(Joe) Xu
2016/08/05 17:03:30
Done.
| |
| 2088 // and notify observers about this change. | |
| 2089 const int kVolume1 = 30; | |
| 2090 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( | |
| 2091 kInternalSpeaker.id, kVolume1); | |
| 2092 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); | |
| 2093 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2094 | |
| 2095 // The later OutputNodeVolumeChanged event should notify observers. | |
| 2096 const int kVolume2 = 60; | |
| 2097 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting( | |
| 2098 kInternalSpeaker.id, kVolume2); | |
| 2099 EXPECT_EQ(2, test_observer_->output_volume_changed_count()); | |
| 2100 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent()); | |
| 2101 } | |
| 2102 | |
| 1989 TEST_F(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) { | 2103 TEST_F(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) { |
| 1990 AudioNodeList audio_nodes; | 2104 AudioNodeList audio_nodes; |
| 1991 audio_nodes.push_back(kInternalSpeaker); | 2105 audio_nodes.push_back(kInternalSpeaker); |
| 1992 SetUpCrasAudioHandler(audio_nodes); | 2106 SetUpCrasAudioHandler(audio_nodes); |
| 1993 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); | 2107 EXPECT_EQ(0, test_observer_->output_volume_changed_count()); |
| 1994 | 2108 |
| 1995 const int kDefaultVolume = 75; | 2109 const int kDefaultVolume = 75; |
| 1996 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); | 2110 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent()); |
| 1997 | 2111 |
| 1998 // Disable the auto OutputNodeVolumeChanged signal. | 2112 // 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)); | 3407 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); |
| 3294 EXPECT_EQ(kInternalSpeaker.id, active_output.id); | 3408 EXPECT_EQ(kInternalSpeaker.id, active_output.id); |
| 3295 EXPECT_EQ(kInternalSpeaker.id, | 3409 EXPECT_EQ(kInternalSpeaker.id, |
| 3296 cras_audio_handler_->GetPrimaryActiveOutputNode()); | 3410 cras_audio_handler_->GetPrimaryActiveOutputNode()); |
| 3297 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); | 3411 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); |
| 3298 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); | 3412 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); |
| 3299 EXPECT_TRUE(test_observer_->output_mute_by_system()); | 3413 EXPECT_TRUE(test_observer_->output_mute_by_system()); |
| 3300 } | 3414 } |
| 3301 | 3415 |
| 3302 } // namespace chromeos | 3416 } // namespace chromeos |
| OLD | NEW |