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

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

Issue 1380103003: Store audio device's active state in preference (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 23 matching lines...) Expand all
34 const uint64 kOtherTypeInputId = 90002; 34 const uint64 kOtherTypeInputId = 90002;
35 const uint64 kUSBJabraSpeakerOutputId1 = 90003; 35 const uint64 kUSBJabraSpeakerOutputId1 = 90003;
36 const uint64 kUSBJabraSpeakerOutputId2 = 90004; 36 const uint64 kUSBJabraSpeakerOutputId2 = 90004;
37 const uint64 kUSBJabraSpeakerInputId1 = 90005; 37 const uint64 kUSBJabraSpeakerInputId1 = 90005;
38 const uint64 kUSBJabraSpeakerInputId2 = 90006; 38 const uint64 kUSBJabraSpeakerInputId2 = 90006;
39 const uint64 kUSBCameraInputId = 90007; 39 const uint64 kUSBCameraInputId = 90007;
40 40
41 const AudioNode kInternalSpeaker( 41 const AudioNode kInternalSpeaker(
42 false, 42 false,
43 kInternalSpeakerId, 43 kInternalSpeakerId,
44 kInternalSpeakerId,
44 "Fake Speaker", 45 "Fake Speaker",
45 "INTERNAL_SPEAKER", 46 "INTERNAL_SPEAKER",
46 "Speaker", 47 "Speaker",
47 false, 48 false,
48 0 49 0
49 ); 50 );
50 51
51 const AudioNode kHeadphone( 52 const AudioNode kHeadphone(
52 false, 53 false,
53 kHeadphoneId, 54 kHeadphoneId,
55 kHeadphoneId,
54 "Fake Headphone", 56 "Fake Headphone",
55 "HEADPHONE", 57 "HEADPHONE",
56 "Headphone", 58 "Headphone",
57 false, 59 false,
58 0 60 0
59 ); 61 );
60 62
61 const AudioNode kInternalMic( 63 const AudioNode kInternalMic(
62 true, 64 true,
63 kInternalMicId, 65 kInternalMicId,
66 kInternalMicId,
64 "Fake Mic", 67 "Fake Mic",
65 "INTERNAL_MIC", 68 "INTERNAL_MIC",
66 "Internal Mic", 69 "Internal Mic",
67 false, 70 false,
68 0 71 0
69 ); 72 );
70 73
71 const AudioNode kMicJack( 74 const AudioNode kMicJack(
72 true, 75 true,
73 kMicJackId, 76 kMicJackId,
77 kMicJackId,
74 "Fake Mic Jack", 78 "Fake Mic Jack",
75 "MIC", 79 "MIC",
76 "Mic Jack", 80 "Mic Jack",
77 false, 81 false,
78 0 82 0
79 ); 83 );
80 84
81 const AudioNode kUSBMic( 85 const AudioNode kUSBMic(
82 true, 86 true,
83 kUSBMicId, 87 kUSBMicId,
88 kUSBMicId,
84 "Fake USB Mic", 89 "Fake USB Mic",
85 "USB", 90 "USB",
86 "USB Microphone", 91 "USB Microphone",
87 false, 92 false,
88 0 93 0
89 ); 94 );
90 95
91 const AudioNode kKeyboardMic( 96 const AudioNode kKeyboardMic(
92 true, 97 true,
93 kKeyboardMicId, 98 kKeyboardMicId,
99 kKeyboardMicId,
94 "Fake Keyboard Mic", 100 "Fake Keyboard Mic",
95 "KEYBOARD_MIC", 101 "KEYBOARD_MIC",
96 "Keyboard Mic", 102 "Keyboard Mic",
97 false, 103 false,
98 0 104 0
99 ); 105 );
100 106
101 const AudioNode kOtherTypeOutput( 107 const AudioNode kOtherTypeOutput(
102 false, 108 false,
103 kOtherTypeOutputId, 109 kOtherTypeOutputId,
110 kOtherTypeOutputId,
104 "Output Device", 111 "Output Device",
105 "SOME_OTHER_TYPE", 112 "SOME_OTHER_TYPE",
106 "Other Type Output Device", 113 "Other Type Output Device",
107 false, 114 false,
108 0 115 0
109 ); 116 );
110 117
111 const AudioNode kOtherTypeInput( 118 const AudioNode kOtherTypeInput(
112 true, 119 true,
113 kOtherTypeInputId, 120 kOtherTypeInputId,
121 kOtherTypeInputId,
114 "Input Device", 122 "Input Device",
115 "SOME_OTHER_TYPE", 123 "SOME_OTHER_TYPE",
116 "Other Type Input Device", 124 "Other Type Input Device",
117 false, 125 false,
118 0 126 0
119 ); 127 );
120 128
121 const AudioNode kBluetoothHeadset(false, 129 const AudioNode kBluetoothHeadset(false,
122 kBluetoothHeadsetId, 130 kBluetoothHeadsetId,
131 kBluetoothHeadsetId,
123 "Bluetooth Headset", 132 "Bluetooth Headset",
124 "BLUETOOTH", 133 "BLUETOOTH",
125 "Bluetooth Headset 1", 134 "Bluetooth Headset 1",
126 false, 135 false,
127 0); 136 0);
128 137
129 const AudioNode kHDMIOutput(false, 138 const AudioNode kHDMIOutput(false,
130 kHDMIOutputId, 139 kHDMIOutputId,
140 kHDMIOutputId,
131 "HDMI output", 141 "HDMI output",
132 "HDMI", 142 "HDMI",
133 "HDMI output", 143 "HDMI output",
134 false, 144 false,
135 0); 145 0);
136 146
137 const AudioNode kUSBHeadphone1(false, 147 const AudioNode kUSBHeadphone1(false,
138 kUSBHeadphoneId1, 148 kUSBHeadphoneId1,
149 kUSBHeadphoneId1,
139 "USB Headphone", 150 "USB Headphone",
140 "USB", 151 "USB",
141 "USB Headphone 1", 152 "USB Headphone 1",
142 false, 153 false,
143 0); 154 0);
144 155
145 const AudioNode kUSBHeadphone2(false, 156 const AudioNode kUSBHeadphone2(false,
146 kUSBHeadphoneId2, 157 kUSBHeadphoneId2,
158 kUSBHeadphoneId2,
147 "USB Headphone", 159 "USB Headphone",
148 "USB", 160 "USB",
149 "USB Headphone 1", 161 "USB Headphone 1",
150 false, 162 false,
151 0); 163 0);
152 164
153 const AudioNode kUSBJabraSpeakerOutput1(false, 165 const AudioNode kUSBJabraSpeakerOutput1(false,
154 kUSBJabraSpeakerOutputId1, 166 kUSBJabraSpeakerOutputId1,
167 kUSBJabraSpeakerOutputId1,
155 "Jabra Speaker 1", 168 "Jabra Speaker 1",
156 "USB", 169 "USB",
157 "Jabra Speaker 1", 170 "Jabra Speaker 1",
158 false, 171 false,
159 0); 172 0);
160 173
161 const AudioNode kUSBJabraSpeakerOutput2(false, 174 const AudioNode kUSBJabraSpeakerOutput2(false,
162 kUSBJabraSpeakerOutputId2, 175 kUSBJabraSpeakerOutputId2,
176 kUSBJabraSpeakerOutputId2,
163 "Jabra Speaker 2", 177 "Jabra Speaker 2",
164 "USB", 178 "USB",
165 "Jabra Speaker 2", 179 "Jabra Speaker 2",
166 false, 180 false,
167 0); 181 0);
168 182
169 const AudioNode kUSBJabraSpeakerInput1(true, 183 const AudioNode kUSBJabraSpeakerInput1(true,
170 kUSBJabraSpeakerInputId1, 184 kUSBJabraSpeakerInputId1,
185 kUSBJabraSpeakerInputId1,
171 "Jabra Speaker 1", 186 "Jabra Speaker 1",
172 "USB", 187 "USB",
173 "Jabra Speaker", 188 "Jabra Speaker",
174 false, 189 false,
175 0); 190 0);
176 191
177 const AudioNode kUSBJabraSpeakerInput2(true, 192 const AudioNode kUSBJabraSpeakerInput2(true,
178 kUSBJabraSpeakerInputId2, 193 kUSBJabraSpeakerInputId2,
194 kUSBJabraSpeakerInputId2,
179 "Jabra Speaker 2", 195 "Jabra Speaker 2",
180 "USB", 196 "USB",
181 "Jabra Speaker 2", 197 "Jabra Speaker 2",
182 false, 198 false,
183 0); 199 0);
184 200
185 const AudioNode kUSBCameraInput(true, 201 const AudioNode kUSBCameraInput(true,
186 kUSBCameraInputId, 202 kUSBCameraInputId,
203 kUSBCameraInputId,
187 "USB Camera", 204 "USB Camera",
188 "USB", 205 "USB",
189 "USB Camera", 206 "USB Camera",
190 false, 207 false,
191 0); 208 0);
192 209
193 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver { 210 class TestObserver : public chromeos::CrasAudioHandler::AudioObserver {
194 public: 211 public:
195 TestObserver() 212 TestObserver()
196 : active_output_node_changed_count_(0), 213 : active_output_node_changed_count_(0),
(...skipping 1835 matching lines...) Expand 10 before | Expand all | Expand 10 after
2032 cras_audio_handler_->GetPrimaryActiveOutputNode()); 2049 cras_audio_handler_->GetPrimaryActiveOutputNode());
2033 EXPECT_TRUE(cras_audio_handler_->has_alternative_output()); 2050 EXPECT_TRUE(cras_audio_handler_->has_alternative_output());
2034 2051
2035 // Ensure the internal microphone has been selected as the active input, 2052 // Ensure the internal microphone has been selected as the active input,
2036 // and the input device with some random unknown type is handled gracefully. 2053 // and the input device with some random unknown type is handled gracefully.
2037 AudioDevice active_input; 2054 AudioDevice active_input;
2038 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode()); 2055 EXPECT_EQ(kInternalMic.id, cras_audio_handler_->GetPrimaryActiveInputNode());
2039 EXPECT_TRUE(cras_audio_handler_->has_alternative_input()); 2056 EXPECT_TRUE(cras_audio_handler_->has_alternative_input());
2040 } 2057 }
2041 2058
2059 TEST_F(CrasAudioHandlerTest, UnselectActiveDeviceAndPlugAnother) {
jennyz 2015/12/17 22:50:21 The test case name can be changed to something mor
hychao 2015/12/29 10:57:25 Done. This do sounds better, thanks.
2060 AudioNodeList audio_nodes;
2061 AudioNode internal_speaker(kInternalSpeaker);
2062 audio_nodes.push_back(internal_speaker);
2063 AudioNode usb_headset(kUSBHeadphone1);
2064 usb_headset.plugged_time = 80000000;
2065 audio_nodes.push_back(usb_headset);
2066 SetUpCrasAudioHandler(audio_nodes);
2067
2068 // Verify the audio devices size.
2069 AudioDeviceList audio_devices;
2070 cras_audio_handler_->GetAudioDevices(&audio_devices);
2071 EXPECT_EQ(audio_nodes.size(), audio_devices.size());
2072
2073 EXPECT_EQ(kUSBHeadphone1.id,
2074 cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz 2015/12/17 22:50:21 Add some comment will be more helpful to explain w
hychao 2015/12/29 10:57:25 Done.
2075
2076 AudioDevice speaker(kInternalSpeaker);
2077 cras_audio_handler_->SwitchToDevice(speaker, true);
jennyz 2015/12/17 22:50:21 Please add a comment: Now change the active device
hychao 2015/12/29 10:57:25 Done. Also added a check below to make sure USB is
2078
2079 // Unplug USB headset
2080 audio_nodes.clear();
2081 internal_speaker.active = true;
2082 audio_nodes.push_back(internal_speaker);
2083 ChangeAudioNodes(audio_nodes);
2084 EXPECT_EQ(kInternalSpeaker.id,
2085 cras_audio_handler_->GetPrimaryActiveOutputNode());
2086
2087 // Plug the USB headset of the same device stable id, but with different id
jennyz 2015/12/17 22:50:21 nits: add "." at the end. How about: Plug the same
hychao 2015/12/29 10:57:25 Done.
2088 usb_headset.active = false;
2089 usb_headset.id = 98765;
2090 audio_nodes.push_back(usb_headset);
2091 ChangeAudioNodes(audio_nodes);
2092 EXPECT_EQ(kInternalSpeaker.id,
2093 cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz 2015/12/17 22:50:21 Please add a comment: "Since usb headset was inact
hychao 2015/12/29 10:57:25 Done.
2094
2095 // Plug the second USB headset
2096 AudioNode usb_headset2(kUSBHeadphone2);
2097 usb_headset2.plugged_time = 80000001;
2098 audio_nodes.push_back(usb_headset2);
2099 ChangeAudioNodes(audio_nodes);
2100 EXPECT_EQ(kUSBHeadphone2.id,
2101 cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz 2015/12/17 22:50:21 Please add a comment: Since the second usb headset
hychao 2015/12/29 10:57:25 Done.
2102
2103 // Unplug the second USB headset
2104 audio_nodes.clear();
2105 internal_speaker.active = false;
2106 audio_nodes.push_back(internal_speaker);
2107 audio_nodes.push_back(usb_headset);
2108 ChangeAudioNodes(audio_nodes);
2109 EXPECT_EQ(usb_headset.id,
2110 cras_audio_handler_->GetPrimaryActiveOutputNode());
jennyz 2015/12/17 22:50:21 When hotplug a new audio device, there are three c
hychao 2015/12/29 10:57:25 Done.
2111 }
2112
2042 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) { 2113 TEST_F(CrasAudioHandlerTest, ChangeActiveNodesHotrodInit) {
2043 AudioNodeList audio_nodes; 2114 AudioNodeList audio_nodes;
2044 audio_nodes.push_back(kHDMIOutput); 2115 audio_nodes.push_back(kHDMIOutput);
2045 audio_nodes.push_back(kUSBJabraSpeakerOutput1); 2116 audio_nodes.push_back(kUSBJabraSpeakerOutput1);
2046 audio_nodes.push_back(kUSBJabraSpeakerOutput2); 2117 audio_nodes.push_back(kUSBJabraSpeakerOutput2);
2047 audio_nodes.push_back(kUSBJabraSpeakerInput1); 2118 audio_nodes.push_back(kUSBJabraSpeakerInput1);
2048 audio_nodes.push_back(kUSBJabraSpeakerInput2); 2119 audio_nodes.push_back(kUSBJabraSpeakerInput2);
2049 audio_nodes.push_back(kUSBCameraInput); 2120 audio_nodes.push_back(kUSBCameraInput);
2050 SetUpCrasAudioHandler(audio_nodes); 2121 SetUpCrasAudioHandler(audio_nodes);
2051 2122
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after
2651 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output)); 2722 cras_audio_handler_->GetPrimaryActiveOutputDevice(&active_output));
2652 EXPECT_EQ(kInternalSpeaker.id, active_output.id); 2723 EXPECT_EQ(kInternalSpeaker.id, active_output.id);
2653 EXPECT_EQ(kInternalSpeaker.id, 2724 EXPECT_EQ(kInternalSpeaker.id,
2654 cras_audio_handler_->GetPrimaryActiveOutputNode()); 2725 cras_audio_handler_->GetPrimaryActiveOutputNode());
2655 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted()); 2726 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
2656 EXPECT_EQ(1, test_observer_->output_mute_changed_count()); 2727 EXPECT_EQ(1, test_observer_->output_mute_changed_count());
2657 EXPECT_TRUE(test_observer_->output_mute_by_system()); 2728 EXPECT_TRUE(test_observer_->output_mute_by_system());
2658 } 2729 }
2659 2730
2660 } // namespace chromeos 2731 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698