OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 <memory> | |
6 #include <vector> | |
7 | |
8 #include "ash/common/accessibility_delegate.h" | |
9 #include "ash/common/accessibility_types.h" | |
10 #include "base/command_line.h" | |
11 #include "base/macros.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h" | |
14 #include "chrome/browser/ui/ash/volume_controller_chromeos.h" | |
15 #include "chrome/test/base/in_process_browser_test.h" | |
16 #include "chromeos/audio/chromeos_sounds.h" | |
17 #include "chromeos/audio/cras_audio_handler.h" | |
18 #include "chromeos/chromeos_switches.h" | |
19 #include "media/audio/sounds/sounds_manager.h" | |
20 #include "ui/base/accelerators/accelerator.h" | |
21 | |
22 namespace { | |
23 | |
24 class SoundsManagerTestImpl : public media::SoundsManager { | |
25 public: | |
26 SoundsManagerTestImpl() | |
27 : is_sound_initialized_(chromeos::SOUND_COUNT), | |
28 num_play_requests_(chromeos::SOUND_COUNT) { | |
29 } | |
30 | |
31 ~SoundsManagerTestImpl() override {} | |
32 | |
33 bool Initialize(SoundKey key, const base::StringPiece& /* data */) override { | |
34 is_sound_initialized_[key] = true; | |
35 return true; | |
36 } | |
37 | |
38 bool Play(SoundKey key) override { | |
39 ++num_play_requests_[key]; | |
40 return true; | |
41 } | |
42 | |
43 bool Stop(SoundKey key) override { | |
44 return true; | |
45 } | |
46 | |
47 base::TimeDelta GetDuration(SoundKey /* key */) override { | |
48 return base::TimeDelta(); | |
49 } | |
50 | |
51 bool is_sound_initialized(SoundKey key) const { | |
52 return is_sound_initialized_[key]; | |
53 } | |
54 | |
55 int num_play_requests(SoundKey key) const { | |
56 return num_play_requests_[key]; | |
57 } | |
58 | |
59 private: | |
60 std::vector<bool> is_sound_initialized_; | |
61 std::vector<int> num_play_requests_; | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(SoundsManagerTestImpl); | |
64 }; | |
65 | |
66 class VolumeControllerTest : public InProcessBrowserTest { | |
67 public: | |
68 VolumeControllerTest() {} | |
69 ~VolumeControllerTest() override {} | |
70 | |
71 void SetUpOnMainThread() override { | |
72 volume_controller_.reset(new VolumeController()); | |
73 audio_handler_ = chromeos::CrasAudioHandler::Get(); | |
74 } | |
75 | |
76 protected: | |
77 chromeos::CrasAudioHandler* audio_handler_; // Not owned. | |
78 std::unique_ptr<VolumeController> volume_controller_; | |
79 | |
80 private: | |
81 DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest); | |
82 }; | |
83 | |
84 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) { | |
85 // Set initial value as 50% | |
86 const int kInitVolume = 50; | |
87 audio_handler_->SetOutputVolumePercent(kInitVolume); | |
88 | |
89 EXPECT_EQ(audio_handler_->GetOutputVolumePercent(), kInitVolume); | |
90 | |
91 volume_controller_->VolumeUp(); | |
92 EXPECT_LT(kInitVolume, audio_handler_->GetOutputVolumePercent()); | |
93 volume_controller_->VolumeDown(); | |
94 EXPECT_EQ(kInitVolume, audio_handler_->GetOutputVolumePercent()); | |
95 volume_controller_->VolumeDown(); | |
96 EXPECT_GT(kInitVolume, audio_handler_->GetOutputVolumePercent()); | |
97 } | |
98 | |
99 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) { | |
100 // Setting to very small volume. | |
101 audio_handler_->SetOutputVolumePercent(1); | |
102 | |
103 volume_controller_->VolumeDown(); | |
104 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent()); | |
105 volume_controller_->VolumeDown(); | |
106 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent()); | |
107 volume_controller_->VolumeUp(); | |
108 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent()); | |
109 } | |
110 | |
111 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) { | |
112 // Setting to almost max | |
113 audio_handler_->SetOutputVolumePercent(99); | |
114 | |
115 volume_controller_->VolumeUp(); | |
116 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent()); | |
117 volume_controller_->VolumeUp(); | |
118 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent()); | |
119 volume_controller_->VolumeDown(); | |
120 EXPECT_GT(100, audio_handler_->GetOutputVolumePercent()); | |
121 } | |
122 | |
123 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) { | |
124 ASSERT_FALSE(audio_handler_->IsOutputMuted()); | |
125 const int initial_volume = audio_handler_->GetOutputVolumePercent(); | |
126 | |
127 volume_controller_->VolumeMute(); | |
128 EXPECT_TRUE(audio_handler_->IsOutputMuted()); | |
129 | |
130 // Further mute buttons doesn't have effects. | |
131 volume_controller_->VolumeMute(); | |
132 EXPECT_TRUE(audio_handler_->IsOutputMuted()); | |
133 | |
134 // Right after the volume up after set_mute recovers to original volume. | |
135 volume_controller_->VolumeUp(); | |
136 EXPECT_FALSE(audio_handler_->IsOutputMuted()); | |
137 EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent()); | |
138 | |
139 volume_controller_->VolumeMute(); | |
140 // After the volume down, the volume goes down to zero explicitly. | |
141 volume_controller_->VolumeDown(); | |
142 EXPECT_TRUE(audio_handler_->IsOutputMuted()); | |
143 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent()); | |
144 | |
145 // Thus, further VolumeUp doesn't recover the volume, it's just slightly | |
146 // bigger than 0. | |
147 volume_controller_->VolumeUp(); | |
148 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent()); | |
149 EXPECT_GT(initial_volume, audio_handler_->GetOutputVolumePercent()); | |
150 } | |
151 | |
152 class VolumeControllerSoundsTest : public VolumeControllerTest { | |
153 public: | |
154 VolumeControllerSoundsTest() : sounds_manager_(NULL) {} | |
155 ~VolumeControllerSoundsTest() override {} | |
156 | |
157 void SetUpInProcessBrowserTestFixture() override { | |
158 sounds_manager_ = new SoundsManagerTestImpl(); | |
159 media::SoundsManager::InitializeForTesting(sounds_manager_); | |
160 } | |
161 | |
162 void EnableSpokenFeedback(bool enabled) { | |
163 chromeos::AccessibilityManager* manager = | |
164 chromeos::AccessibilityManager::Get(); | |
165 manager->EnableSpokenFeedback(enabled, ash::A11Y_NOTIFICATION_NONE); | |
166 } | |
167 | |
168 bool is_sound_initialized() const { | |
169 return sounds_manager_->is_sound_initialized(chromeos::SOUND_VOLUME_ADJUST); | |
170 } | |
171 | |
172 int num_play_requests() const { | |
173 return sounds_manager_->num_play_requests(chromeos::SOUND_VOLUME_ADJUST); | |
174 } | |
175 | |
176 private: | |
177 SoundsManagerTestImpl* sounds_manager_; | |
178 | |
179 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsTest); | |
180 }; | |
181 | |
182 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, Simple) { | |
183 audio_handler_->SetOutputVolumePercent(50); | |
184 | |
185 EnableSpokenFeedback(false /* enabled */); | |
186 volume_controller_->VolumeUp(); | |
187 volume_controller_->VolumeDown(); | |
188 EXPECT_EQ(0, num_play_requests()); | |
189 | |
190 EnableSpokenFeedback(true /* enabled */); | |
191 volume_controller_->VolumeUp(); | |
192 volume_controller_->VolumeDown(); | |
193 EXPECT_EQ(2, num_play_requests()); | |
194 } | |
195 | |
196 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, EdgeCases) { | |
197 EXPECT_TRUE(is_sound_initialized()); | |
198 EnableSpokenFeedback(true /* enabled */); | |
199 | |
200 // Check that sound is played on volume up and volume down. | |
201 audio_handler_->SetOutputVolumePercent(50); | |
202 volume_controller_->VolumeUp(); | |
203 EXPECT_EQ(1, num_play_requests()); | |
204 volume_controller_->VolumeDown(); | |
205 EXPECT_EQ(2, num_play_requests()); | |
206 | |
207 audio_handler_->SetOutputVolumePercent(99); | |
208 volume_controller_->VolumeUp(); | |
209 EXPECT_EQ(3, num_play_requests()); | |
210 | |
211 audio_handler_->SetOutputVolumePercent(100); | |
212 volume_controller_->VolumeUp(); | |
213 EXPECT_EQ(3, num_play_requests()); | |
214 | |
215 // Check that sound isn't played when audio is muted. | |
216 audio_handler_->SetOutputVolumePercent(50); | |
217 volume_controller_->VolumeMute(); | |
218 volume_controller_->VolumeDown(); | |
219 ASSERT_TRUE(audio_handler_->IsOutputMuted()); | |
220 EXPECT_EQ(3, num_play_requests()); | |
221 | |
222 // Check that audio is unmuted and sound is played. | |
223 volume_controller_->VolumeUp(); | |
224 ASSERT_FALSE(audio_handler_->IsOutputMuted()); | |
225 EXPECT_EQ(4, num_play_requests()); | |
226 } | |
227 | |
228 class VolumeControllerSoundsDisabledTest : public VolumeControllerSoundsTest { | |
229 public: | |
230 VolumeControllerSoundsDisabledTest() {} | |
231 ~VolumeControllerSoundsDisabledTest() override {} | |
232 | |
233 void SetUpCommandLine(base::CommandLine* command_line) override { | |
234 VolumeControllerSoundsTest::SetUpCommandLine(command_line); | |
235 command_line->AppendSwitch(chromeos::switches::kDisableVolumeAdjustSound); | |
236 } | |
237 | |
238 private: | |
239 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsDisabledTest); | |
240 }; | |
241 | |
242 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsDisabledTest, | |
243 VolumeAdjustSounds) { | |
244 EXPECT_FALSE(is_sound_initialized()); | |
245 | |
246 // Check that sound isn't played on volume up and volume down. | |
247 audio_handler_->SetOutputVolumePercent(50); | |
248 volume_controller_->VolumeUp(); | |
249 volume_controller_->VolumeDown(); | |
250 EXPECT_EQ(0, num_play_requests()); | |
251 } | |
252 | |
253 } // namespace | |
OLD | NEW |