Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Side by Side Diff: chromeos/audio/cras_audio_handler_unittest.cc

Issue 2087803003: Sync volume state using cras signal OutputNodeVolumeChanged. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chromeos/audio/cras_audio_handler.cc ('k') | chromeos/dbus/fake_cras_audio_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; } 237 void reset_output_mute_changed_count() { input_mute_changed_count_ = 0; }
238 238
239 int input_mute_changed_count() const { 239 int input_mute_changed_count() const {
240 return input_mute_changed_count_; 240 return input_mute_changed_count_;
241 } 241 }
242 242
243 int output_volume_changed_count() const { 243 int output_volume_changed_count() const {
244 return output_volume_changed_count_; 244 return output_volume_changed_count_;
245 } 245 }
246 246
247 void reset_output_volume_changed_count() { output_volume_changed_count_ = 0; }
248
247 int input_gain_changed_count() const { 249 int input_gain_changed_count() const {
248 return input_gain_changed_count_; 250 return input_gain_changed_count_;
249 } 251 }
250 252
251 bool output_mute_by_system() const { return output_mute_by_system_; } 253 bool output_mute_by_system() const { return output_mute_by_system_; }
252 254
253 int output_channel_remixing_changed_count() const { 255 int output_channel_remixing_changed_count() const {
254 return output_channel_remixing_changed_count_; 256 return output_channel_remixing_changed_count_;
255 } 257 }
256 258
(...skipping 1720 matching lines...) Expand 10 before | Expand all | Expand 10 after
1977 // is saved the preferences. 1979 // is saved the preferences.
1978 const int kVolume = 60; 1980 const int kVolume = 60;
1979 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent()); 1981 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
1980 EXPECT_EQ(1, test_observer_->output_volume_changed_count()); 1982 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
1981 AudioDevice device; 1983 AudioDevice device;
1982 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device)); 1984 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
1983 EXPECT_EQ(device.id, kInternalSpeaker.id); 1985 EXPECT_EQ(device.id, kInternalSpeaker.id);
1984 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device)); 1986 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
1985 } 1987 }
1986 1988
1989 TEST_F(CrasAudioHandlerTest, SetOutputVolumeWithDelayedSignal) {
1990 AudioNodeList audio_nodes;
1991 audio_nodes.push_back(kInternalSpeaker);
1992 SetUpCrasAudioHandler(audio_nodes);
1993 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
1994
1995 const int kDefaultVolume = 75;
1996 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
1997
1998 // Disable the auto OutputNodeVolumeChanged signal.
1999 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2000
2001 // Verify the volume state is not changed before OutputNodeVolumeChanged
2002 // signal fires.
2003 const int kVolume = 60;
2004 cras_audio_handler_->SetOutputVolumePercent(kVolume);
2005 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2006 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2007
2008 // Verify the output volume is changed to the designated value after
2009 // OnOutputNodeVolumeChanged cras signal fires, and the volume change event
2010 // has been fired to notify the observers.
2011 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2012 kInternalSpeaker.id, kVolume);
2013 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2014 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2015 AudioDevice device;
2016 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2017 EXPECT_EQ(device.id, kInternalSpeaker.id);
2018 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2019 }
2020
2021 TEST_F(CrasAudioHandlerTest,
2022 ChangeOutputVolumesWithDelayedSignalForSingleActiveDevice) {
2023 AudioNodeList audio_nodes;
2024 audio_nodes.push_back(kInternalSpeaker);
2025 SetUpCrasAudioHandler(audio_nodes);
2026 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2027
2028 const int kDefaultVolume = 75;
2029 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2030
2031 // Disable the auto OutputNodeVolumeChanged signal.
2032 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2033
2034 // Verify the volume state is not changed before OutputNodeVolumeChanged
2035 // signal fires.
2036 const int kVolume1 = 50;
2037 const int kVolume2 = 60;
2038 cras_audio_handler_->SetOutputVolumePercent(kVolume1);
2039 cras_audio_handler_->SetOutputVolumePercent(kVolume2);
2040 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2041 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2042
2043 // Simulate OutputNodeVolumeChanged signal fired with big latency that
2044 // it lags behind the SetOutputNodeVolume requests. Chrome sets the volume
2045 // to 50 then 60, but the volume changed signal for 50 comes back after
2046 // chrome sets the volume to 60. Verify chrome will sync to the designated
2047 // volume level after all signals arrive.
2048 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2049 kInternalSpeaker.id, kVolume1);
2050 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2051 EXPECT_EQ(kVolume1, cras_audio_handler_->GetOutputVolumePercent());
2052
2053 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2054 kInternalSpeaker.id, kVolume2);
2055 EXPECT_EQ(2, test_observer_->output_volume_changed_count());
2056 EXPECT_EQ(kVolume2, cras_audio_handler_->GetOutputVolumePercent());
2057 }
2058
2059 TEST_F(CrasAudioHandlerTest,
2060 ChangeOutputVolumeFromNonChromeSourceSingleActiveDevice) {
2061 AudioNodeList audio_nodes;
2062 audio_nodes.push_back(kInternalSpeaker);
2063 SetUpCrasAudioHandler(audio_nodes);
2064 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2065
2066 const int kDefaultVolume = 75;
2067 EXPECT_EQ(0, test_observer_->output_volume_changed_count());
2068 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2069
2070 // Simulate OutputNodeVolumeChanged signal fired by a non-chrome source.
2071 // Verify chrome will sync its volume state to the volume from the signal,
2072 // and notify its observers for the volume change event.
2073 const int kVolume = 20;
2074 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2075 kInternalSpeaker.id, kVolume);
2076 EXPECT_EQ(1, test_observer_->output_volume_changed_count());
2077 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
2078 AudioDevice device;
2079 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
2080 EXPECT_EQ(device.id, kInternalSpeaker.id);
2081 EXPECT_EQ(kVolume, audio_pref_handler_->GetOutputVolumeValue(&device));
2082 }
2083
1987 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) { 2084 TEST_F(CrasAudioHandlerTest, SetInputGainPercent) {
1988 AudioNodeList audio_nodes; 2085 AudioNodeList audio_nodes;
1989 audio_nodes.push_back(kInternalMic); 2086 audio_nodes.push_back(kInternalMic);
1990 SetUpCrasAudioHandler(audio_nodes); 2087 SetUpCrasAudioHandler(audio_nodes);
1991 EXPECT_EQ(0, test_observer_->input_gain_changed_count()); 2088 EXPECT_EQ(0, test_observer_->input_gain_changed_count());
1992 2089
1993 cras_audio_handler_->SetInputGainPercent(60); 2090 cras_audio_handler_->SetInputGainPercent(60);
1994 2091
1995 // Verify the input gain changed to the designated value, 2092 // Verify the input gain changed to the designated value,
1996 // OnInputNodeGainChanged event is fired, and the device gain value 2093 // OnInputNodeGainChanged event is fired, and the device gain value
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after
2507 // Adjust the volume of output devices, verify all active nodes are set to 2604 // Adjust the volume of output devices, verify all active nodes are set to
2508 // the same volume. 2605 // the same volume.
2509 cras_audio_handler_->SetOutputVolumePercent(25); 2606 cras_audio_handler_->SetOutputVolumePercent(25);
2510 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent()); 2607 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercent());
2511 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice( 2608 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2512 kUSBJabraSpeakerOutput1.id)); 2609 kUSBJabraSpeakerOutput1.id));
2513 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice( 2610 EXPECT_EQ(25, cras_audio_handler_->GetOutputVolumePercentForDevice(
2514 kUSBJabraSpeakerOutput2.id)); 2611 kUSBJabraSpeakerOutput2.id));
2515 } 2612 }
2516 2613
2614 TEST_F(CrasAudioHandlerTest, ChangeVolumeHotrodDualSpeakersWithDelayedSignals) {
2615 AudioNodeList audio_nodes;
2616 audio_nodes.push_back(kHDMIOutput);
2617 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2618 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2619 SetUpCrasAudioHandler(audio_nodes);
2620
2621 // Verify the audio devices size.
2622 AudioDeviceList audio_devices;
2623 cras_audio_handler_->GetAudioDevices(&audio_devices);
2624 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2625
2626 // Set both jabra speakers nodes to active, this simulate
2627 // the call sent by hotrod initialization process.
2628 test_observer_->reset_active_output_node_changed_count();
2629 CrasAudioHandler::NodeIdList active_nodes;
2630 active_nodes.push_back(kUSBJabraSpeakerOutput1.id);
2631 active_nodes.push_back(kUSBJabraSpeakerOutput2.id);
2632 cras_audio_handler_->ChangeActiveNodes(active_nodes);
2633
2634 // Verify both jabra speakers are made active.
2635 EXPECT_EQ(2, GetActiveDeviceCount());
2636 const AudioDevice* active_output_1 =
2637 GetDeviceFromId(kUSBJabraSpeakerOutput1.id);
2638 EXPECT_TRUE(active_output_1->active);
2639 const AudioDevice* active_output_2 =
2640 GetDeviceFromId(kUSBJabraSpeakerOutput2.id);
2641 EXPECT_TRUE(active_output_2->active);
2642
2643 // Verify all active devices are the not muted and their volume values are
2644 // the same.
2645 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2646 EXPECT_FALSE(
2647 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput1.id));
2648 EXPECT_FALSE(
2649 cras_audio_handler_->IsOutputMutedForDevice(kUSBJabraSpeakerOutput2.id));
2650 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2651 cras_audio_handler_->GetOutputVolumePercentForDevice(
2652 kUSBJabraSpeakerOutput1.id));
2653 EXPECT_EQ(cras_audio_handler_->GetOutputVolumePercent(),
2654 cras_audio_handler_->GetOutputVolumePercentForDevice(
2655 kUSBJabraSpeakerOutput2.id));
2656 const int kDefaultVolume = 75;
2657 EXPECT_EQ(kDefaultVolume, cras_audio_handler_->GetOutputVolumePercent());
2658
2659 // Disable the auto OutputNodeVolumeChanged signal.
2660 fake_cras_audio_client_->set_notify_volume_change_with_delay(true);
2661 test_observer_->reset_output_volume_changed_count();
2662
2663 // Adjust the volume of output devices continuously.
2664 cras_audio_handler_->SetOutputVolumePercent(20);
2665 cras_audio_handler_->SetOutputVolumePercent(30);
2666
2667 // Sends delayed OutputNodeVolumeChanged signals.
2668 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2669 kUSBJabraSpeakerOutput2.id, 20);
2670 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2671 kUSBJabraSpeakerOutput1.id, 20);
2672 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2673 kUSBJabraSpeakerOutput2.id, 30);
2674 fake_cras_audio_client_->NotifyOutputNodeVolumeChangedForTesting(
2675 kUSBJabraSpeakerOutput1.id, 30);
2676
2677 // Verify that both speakers are set to the designated volume level after
2678 // receiving all delayed signals.
2679 EXPECT_EQ(4, test_observer_->output_volume_changed_count());
2680 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercent());
2681 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
2682 kUSBJabraSpeakerOutput1.id));
2683 EXPECT_EQ(30, cras_audio_handler_->GetOutputVolumePercentForDevice(
2684 kUSBJabraSpeakerOutput2.id));
2685 }
2686
2517 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) { 2687 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInitWithCameraInputActive) {
2518 AudioNodeList audio_nodes; 2688 AudioNodeList audio_nodes;
2519 audio_nodes.push_back(kHDMIOutput); 2689 audio_nodes.push_back(kHDMIOutput);
2520 audio_nodes.push_back(kUSBJabraSpeakerOutput1); 2690 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2521 audio_nodes.push_back(kUSBJabraSpeakerOutput2); 2691 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2522 audio_nodes.push_back(kUSBJabraSpeakerInput1); 2692 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2523 audio_nodes.push_back(kUSBJabraSpeakerInput2); 2693 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2524 // Make the camera input to be plugged in later than jabra's input. 2694 // Make the camera input to be plugged in later than jabra's input.
2525 AudioNode usb_camera(kUSBCameraInput); 2695 AudioNode usb_camera(kUSBCameraInput);
2526 usb_camera.plugged_time = 10000000; 2696 usb_camera.plugged_time = 10000000;
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after
3097 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); 3267 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
3098 EXPECT_EQ(kInternalSpeaker.id, active_output.id); 3268 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
3099 EXPECT_EQ(kInternalSpeaker.id, 3269 EXPECT_EQ(kInternalSpeaker.id,
3100 cras_audio_handler_->GetPrimaryActiveOutputNode()); 3270 cras_audio_handler_->GetPrimaryActiveOutputNode());
3101 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); 3271 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
3102 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); 3272 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
3103 EXPECT_TRUE(test_observer_->output_mute_by_system()); 3273 EXPECT_TRUE(test_observer_->output_mute_by_system());
3104 } 3274 }
3105 3275
3106 } // namespace chromeos 3276 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/audio/cras_audio_handler.cc ('k') | chromeos/dbus/fake_cras_audio_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698