OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 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. | |
4 | |
5 #include "extensions/shell/browser/shell_audio_controller_chromeos.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "chromeos/audio/audio_device.h" | |
9 #include "chromeos/audio/cras_audio_handler.h" | |
10 #include "chromeos/dbus/audio_node.h" | |
11 #include "chromeos/dbus/cras_audio_client_stub_impl.h" | |
12 #include "chromeos/dbus/dbus_thread_manager.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace extensions { | |
16 | |
17 class ShellAudioControllerTest : public testing::Test { | |
18 public: | |
19 ShellAudioControllerTest() : next_node_id_(1) { | |
20 audio_client_ = new chromeos::CrasAudioClientStubImpl(); | |
21 audio_client_->SetAudioDevices(chromeos::AudioNodeList()); | |
22 chromeos::DBusThreadManager::GetSetterForTesting()->SetCrasAudioClient( | |
23 make_scoped_ptr(audio_client_).PassAs<chromeos::CrasAudioClient>()); | |
24 | |
25 chromeos::CrasAudioHandler::Initialize( | |
26 new ShellAudioController::PrefHandler()); | |
27 audio_handler_ = chromeos::CrasAudioHandler::Get(); | |
28 | |
29 controller_.reset(new ShellAudioController()); | |
30 } | |
31 | |
32 virtual ~ShellAudioControllerTest() { | |
33 controller_.reset(); | |
34 chromeos::CrasAudioHandler::Shutdown(); | |
35 chromeos::DBusThreadManager::Shutdown(); | |
James Cook
2014/08/28 18:48:37
Just to double-check since I'm not familiar with t
Daniel Erat
2014/08/28 23:27:57
(discussed in person:) looks like it. i'll double-
| |
36 } | |
37 | |
38 protected: | |
39 // Fills a chromeos::AudioNode for use by tests. | |
40 chromeos::AudioNode CreateNode(chromeos::AudioDeviceType type) { | |
41 chromeos::AudioNode node; | |
42 node.is_input = | |
43 type == chromeos::AUDIO_TYPE_MIC || | |
44 type == chromeos::AUDIO_TYPE_INTERNAL_MIC || | |
45 type == chromeos::AUDIO_TYPE_KEYBOARD_MIC; | |
46 node.id = next_node_id_++; | |
47 node.type = chromeos::AudioDevice::GetTypeString(type); | |
48 return node; | |
49 } | |
50 | |
51 // Changes the active state of the node with |id| in |nodes|. | |
52 void SetNodeActive(chromeos::AudioNodeList* nodes, uint64 id, bool active) { | |
53 for (chromeos::AudioNodeList::iterator it = nodes->begin(); | |
54 it != nodes->end(); ++it) { | |
55 if (it->id == id) { | |
56 it->active = active; | |
57 return; | |
58 } | |
59 } | |
60 ASSERT_TRUE(false) << "Didn't find ID " << id; | |
61 } | |
62 | |
63 chromeos::CrasAudioClientStubImpl* audio_client_; // Not owned. | |
64 chromeos::CrasAudioHandler* audio_handler_; // Not owned. | |
65 scoped_ptr<ShellAudioController> controller_; | |
66 | |
67 // Next audio node ID to be returned by CreateNode(). | |
68 uint64 next_node_id_; | |
69 | |
70 private: | |
71 DISALLOW_COPY_AND_ASSIGN(ShellAudioControllerTest); | |
72 }; | |
73 | |
74 // Tests that higher-priority devices are activated as soon as they're | |
75 // connected. | |
76 TEST_F(ShellAudioControllerTest, SelectBestDevices) { | |
77 chromeos::AudioNode internal_speaker = | |
James Cook
2014/08/28 18:48:37
optional: using chromeos::AudioNode and/or AudioNo
Daniel Erat
2014/08/28 23:27:57
done (note to self: james is a reviewer who's okay
| |
78 CreateNode(chromeos::AUDIO_TYPE_INTERNAL_SPEAKER); | |
79 chromeos::AudioNode internal_mic = | |
80 CreateNode(chromeos::AUDIO_TYPE_INTERNAL_MIC); | |
81 chromeos::AudioNode headphone = CreateNode(chromeos::AUDIO_TYPE_HEADPHONE); | |
82 chromeos::AudioNode external_mic = CreateNode(chromeos::AUDIO_TYPE_MIC); | |
83 | |
84 // chromeos::AudioDevice gives the headphone jack a higher priority than the | |
85 // internal speaker and an external mic a higher priority than the internal | |
86 // mic, so we should start out favoring headphones and the external mic. | |
James Cook
2014/08/28 18:48:37
nice per-block documentation
| |
87 chromeos::AudioNodeList all_nodes; | |
88 all_nodes.push_back(internal_speaker); | |
89 all_nodes.push_back(internal_mic); | |
90 all_nodes.push_back(headphone); | |
91 all_nodes.push_back(external_mic); | |
92 audio_client_->ChangeAudioNodes(all_nodes); | |
93 EXPECT_EQ(headphone.id, audio_handler_->GetActiveOutputNode()); | |
94 EXPECT_EQ(external_mic.id, audio_handler_->GetActiveInputNode()); | |
95 | |
96 // Unplug the headphones and mic and check that we switch to the internal | |
97 // devices. | |
98 chromeos::AudioNodeList internal_nodes; | |
99 internal_nodes.push_back(internal_speaker); | |
100 internal_nodes.push_back(internal_mic); | |
101 audio_client_->ChangeAudioNodes(internal_nodes); | |
102 EXPECT_EQ(internal_speaker.id, audio_handler_->GetActiveOutputNode()); | |
103 EXPECT_EQ(internal_mic.id, audio_handler_->GetActiveInputNode()); | |
104 | |
105 // Switch back to the external devices. Mark the previously-activated internal | |
106 // devices as being active so CrasAudioHandler doesn't complain. | |
107 SetNodeActive(&all_nodes, internal_speaker.id, true); | |
108 SetNodeActive(&all_nodes, internal_mic.id, true); | |
109 audio_client_->ChangeAudioNodes(all_nodes); | |
110 EXPECT_EQ(headphone.id, audio_handler_->GetActiveOutputNode()); | |
111 EXPECT_EQ(external_mic.id, audio_handler_->GetActiveInputNode()); | |
112 } | |
113 | |
114 // Tests that active audio devices are unmuted and set to 100% volume. | |
115 TEST_F(ShellAudioControllerTest, MaxVolume) { | |
116 chromeos::AudioNodeList nodes; | |
117 nodes.push_back(CreateNode(chromeos::AUDIO_TYPE_INTERNAL_SPEAKER)); | |
118 nodes.push_back(CreateNode(chromeos::AUDIO_TYPE_INTERNAL_MIC)); | |
119 audio_client_->ChangeAudioNodes(nodes); | |
120 | |
121 EXPECT_FALSE(audio_handler_->IsOutputMuted()); | |
122 EXPECT_FALSE(audio_handler_->IsInputMuted()); | |
123 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent()); | |
124 EXPECT_EQ(100, audio_handler_->GetInputGainPercent()); | |
125 } | |
126 | |
127 } // namespace extensions | |
OLD | NEW |