Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / audio / audio_apitest.cc
blob19fc0b67b8eb044029634565549cfa73dc235b84
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
3 // found in the LICENSE file.
5 #include "base/message_loop/message_loop.h"
6 #include "extensions/shell/test/shell_apitest.h"
7 #if defined(OS_CHROMEOS)
8 #include "chromeos/audio/audio_devices_pref_handler_stub.h"
9 #include "chromeos/audio/cras_audio_handler.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_cras_audio_client.h"
12 #endif
13 #include "extensions/test/extension_test_message_listener.h"
15 namespace extensions {
17 #if defined(OS_CHROMEOS)
18 using chromeos::AudioDevice;
19 using chromeos::AudioDeviceList;
20 using chromeos::AudioNode;
21 using chromeos::AudioNodeList;
23 const uint64_t kJabraSpeaker1Id = 30001;
24 const uint64_t kJabraSpeaker2Id = 30002;
25 const uint64_t kHDMIOutputId = 30003;
26 const uint64_t kJabraMic1Id = 40001;
27 const uint64_t kJabraMic2Id = 40002;
28 const uint64_t kWebcamMicId = 40003;
30 const AudioNode kJabraSpeaker1(false,
31 kJabraSpeaker1Id,
32 "Jabra Speaker",
33 "USB",
34 "Jabra Speaker 1",
35 false,
36 0);
38 const AudioNode kJabraSpeaker2(false,
39 kJabraSpeaker2Id,
40 "Jabra Speaker",
41 "USB",
42 "Jabra Speaker 2",
43 false,
44 0);
46 const AudioNode kHDMIOutput(false,
47 kHDMIOutputId,
48 "HDMI output",
49 "HDMI",
50 "HDA Intel MID",
51 false,
52 0);
54 const AudioNode
55 kJabraMic1(true, kJabraMic1Id, "Jabra Mic", "USB", "Jabra Mic 1", false, 0);
57 const AudioNode
58 kJabraMic2(true, kJabraMic2Id, "Jabra Mic", "USB", "Jabra Mic 2", false, 0);
60 const AudioNode kUSBCameraMic(true,
61 kWebcamMicId,
62 "Webcam Mic",
63 "USB",
64 "Logitech Webcam",
65 false,
66 0);
68 class AudioApiTest : public ShellApiTest {
69 public:
70 AudioApiTest() : cras_audio_handler_(NULL), fake_cras_audio_client_(NULL) {}
71 ~AudioApiTest() override {}
73 void SetUpCrasAudioHandlerWithTestingNodes(const AudioNodeList& audio_nodes) {
74 chromeos::DBusThreadManager* dbus_manager =
75 chromeos::DBusThreadManager::Get();
76 DCHECK(dbus_manager);
77 fake_cras_audio_client_ = static_cast<chromeos::FakeCrasAudioClient*>(
78 dbus_manager->GetCrasAudioClient());
79 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
80 audio_nodes);
81 cras_audio_handler_ = chromeos::CrasAudioHandler::Get();
82 DCHECK(cras_audio_handler_);
83 message_loop_.RunUntilIdle();
86 void ChangeAudioNodes(const AudioNodeList& audio_nodes) {
87 DCHECK(fake_cras_audio_client_);
88 fake_cras_audio_client_->SetAudioNodesAndNotifyObserversForTesting(
89 audio_nodes);
90 message_loop_.RunUntilIdle();
93 protected:
94 base::MessageLoopForUI message_loop_;
95 chromeos::CrasAudioHandler* cras_audio_handler_; // Not owned.
96 chromeos::FakeCrasAudioClient* fake_cras_audio_client_; // Not owned.
99 IN_PROC_BROWSER_TEST_F(AudioApiTest, Audio) {
100 // Set up the audio nodes for testing.
101 AudioNodeList audio_nodes;
102 audio_nodes.push_back(kJabraSpeaker1);
103 audio_nodes.push_back(kJabraSpeaker2);
104 audio_nodes.push_back(kHDMIOutput);
105 audio_nodes.push_back(kJabraMic1);
106 audio_nodes.push_back(kJabraMic2);
107 audio_nodes.push_back(kUSBCameraMic);
108 SetUpCrasAudioHandlerWithTestingNodes(audio_nodes);
110 EXPECT_TRUE(RunAppTest("api_test/audio")) << message_;
113 IN_PROC_BROWSER_TEST_F(AudioApiTest, OnLevelChangedOutputDevice) {
114 AudioNodeList audio_nodes;
115 audio_nodes.push_back(kJabraSpeaker1);
116 audio_nodes.push_back(kHDMIOutput);
117 SetUpCrasAudioHandlerWithTestingNodes(audio_nodes);
119 // Verify the jabra speaker is the active output device.
120 AudioDevice device;
121 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
122 EXPECT_EQ(device.id, kJabraSpeaker1.id);
124 // Loads background app.
125 ExtensionTestMessageListener load_listener("loaded", false);
126 ExtensionTestMessageListener result_listener("success", false);
127 result_listener.set_failure_message("failure");
128 ASSERT_TRUE(LoadApp("api_test/audio/volume_change"));
129 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
131 // Change output device volume.
132 const int kVolume = 60;
133 cras_audio_handler_->SetOutputVolumePercent(kVolume);
135 // Verify the output volume is changed to the designated value.
136 EXPECT_EQ(kVolume, cras_audio_handler_->GetOutputVolumePercent());
137 EXPECT_EQ(kVolume,
138 cras_audio_handler_->GetOutputVolumePercentForDevice(device.id));
140 // Verify the background app got the OnOutputNodeVolumeChanged event
141 // with the expected node id and volume value.
142 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
143 EXPECT_EQ("success", result_listener.message());
146 IN_PROC_BROWSER_TEST_F(AudioApiTest, OnOutputMuteChanged) {
147 AudioNodeList audio_nodes;
148 audio_nodes.push_back(kJabraSpeaker1);
149 audio_nodes.push_back(kHDMIOutput);
150 SetUpCrasAudioHandlerWithTestingNodes(audio_nodes);
152 // Verify the jabra speaker is the active output device.
153 AudioDevice device;
154 EXPECT_TRUE(cras_audio_handler_->GetPrimaryActiveOutputDevice(&device));
155 EXPECT_EQ(device.id, kJabraSpeaker1.id);
157 // Mute the output.
158 cras_audio_handler_->SetOutputMute(true);
159 EXPECT_TRUE(cras_audio_handler_->IsOutputMuted());
161 // Loads background app.
162 ExtensionTestMessageListener load_listener("loaded", false);
163 ExtensionTestMessageListener result_listener("success", false);
164 result_listener.set_failure_message("failure");
165 ASSERT_TRUE(LoadApp("api_test/audio/output_mute_change"));
166 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
168 // Un-mute the output.
169 cras_audio_handler_->SetOutputMute(false);
170 EXPECT_FALSE(cras_audio_handler_->IsOutputMuted());
172 // Verify the background app got the OnMuteChanged event
173 // with the expected output un-muted state.
174 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
175 EXPECT_EQ("success", result_listener.message());
178 IN_PROC_BROWSER_TEST_F(AudioApiTest, OnInputMuteChanged) {
179 AudioNodeList audio_nodes;
180 audio_nodes.push_back(kJabraMic1);
181 audio_nodes.push_back(kUSBCameraMic);
182 SetUpCrasAudioHandlerWithTestingNodes(audio_nodes);
184 // Set the jabra mic to be the active input device.
185 AudioDevice jabra_mic(kJabraMic1);
186 cras_audio_handler_->SwitchToDevice(jabra_mic, true);
187 EXPECT_EQ(kJabraMic1.id, cras_audio_handler_->GetPrimaryActiveInputNode());
189 // Un-mute the input.
190 cras_audio_handler_->SetInputMute(false);
191 EXPECT_FALSE(cras_audio_handler_->IsInputMuted());
193 // Loads background app.
194 ExtensionTestMessageListener load_listener("loaded", false);
195 ExtensionTestMessageListener result_listener("success", false);
196 result_listener.set_failure_message("failure");
197 ASSERT_TRUE(LoadApp("api_test/audio/input_mute_change"));
198 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
200 // Mute the input.
201 cras_audio_handler_->SetInputMute(true);
202 EXPECT_TRUE(cras_audio_handler_->IsInputMuted());
204 // Verify the background app got the OnMuteChanged event
205 // with the expected input muted state.
206 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
207 EXPECT_EQ("success", result_listener.message());
210 IN_PROC_BROWSER_TEST_F(AudioApiTest, OnNodesChangedAddNodes) {
211 AudioNodeList audio_nodes;
212 audio_nodes.push_back(kJabraSpeaker1);
213 audio_nodes.push_back(kJabraSpeaker2);
214 SetUpCrasAudioHandlerWithTestingNodes(audio_nodes);
215 const size_t init_device_size = audio_nodes.size();
217 AudioDeviceList audio_devices;
218 cras_audio_handler_->GetAudioDevices(&audio_devices);
219 EXPECT_EQ(init_device_size, audio_devices.size());
221 // Load background app.
222 ExtensionTestMessageListener load_listener("loaded", false);
223 ExtensionTestMessageListener result_listener("success", false);
224 result_listener.set_failure_message("failure");
225 ASSERT_TRUE(LoadApp("api_test/audio/add_nodes"));
226 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
228 // Plug in HDMI output.
229 audio_nodes.push_back(kHDMIOutput);
230 ChangeAudioNodes(audio_nodes);
231 cras_audio_handler_->GetAudioDevices(&audio_devices);
232 EXPECT_EQ(init_device_size + 1, audio_devices.size());
234 // Verify the background app got the OnNodesChanged event
235 // with the new node added.
236 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
237 EXPECT_EQ("success", result_listener.message());
240 IN_PROC_BROWSER_TEST_F(AudioApiTest, OnNodesChangedRemoveNodes) {
241 AudioNodeList audio_nodes;
242 audio_nodes.push_back(kJabraMic1);
243 audio_nodes.push_back(kJabraMic2);
244 audio_nodes.push_back(kUSBCameraMic);
245 SetUpCrasAudioHandlerWithTestingNodes(audio_nodes);
246 const size_t init_device_size = audio_nodes.size();
248 AudioDeviceList audio_devices;
249 cras_audio_handler_->GetAudioDevices(&audio_devices);
250 EXPECT_EQ(init_device_size, audio_devices.size());
252 // Load background app.
253 ExtensionTestMessageListener load_listener("loaded", false);
254 ExtensionTestMessageListener result_listener("success", false);
255 result_listener.set_failure_message("failure");
256 ASSERT_TRUE(LoadApp("api_test/audio/remove_nodes"));
257 ASSERT_TRUE(load_listener.WaitUntilSatisfied());
259 // Remove camera mic.
260 audio_nodes.erase(audio_nodes.begin() + init_device_size - 1);
261 ChangeAudioNodes(audio_nodes);
262 cras_audio_handler_->GetAudioDevices(&audio_devices);
263 EXPECT_EQ(init_device_size - 1, audio_devices.size());
265 // Verify the background app got the onNodesChanged event
266 // with the last node removed.
267 ASSERT_TRUE(result_listener.WaitUntilSatisfied());
268 EXPECT_EQ("success", result_listener.message());
271 #endif // OS_CHROMEOS
273 } // namespace extensions