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.
7 #include "ash/accessibility_delegate.h"
8 #include "ash/ash_switches.h"
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
13 #include "chrome/browser/ui/ash/volume_controller_chromeos.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chromeos/audio/chromeos_sounds.h"
16 #include "chromeos/audio/cras_audio_handler.h"
17 #include "chromeos/chromeos_switches.h"
18 #include "media/audio/sounds/sounds_manager.h"
19 #include "ui/base/accelerators/accelerator.h"
23 class SoundsManagerTestImpl : public media::SoundsManager {
25 SoundsManagerTestImpl()
26 : is_sound_initialized_(chromeos::SOUND_COUNT),
27 num_play_requests_(chromeos::SOUND_COUNT) {
30 virtual ~SoundsManagerTestImpl() {}
32 virtual bool Initialize(SoundKey key,
33 const base::StringPiece& /* data */) override {
34 is_sound_initialized_[key] = true;
38 virtual bool Play(SoundKey key) override {
39 ++num_play_requests_[key];
43 virtual base::TimeDelta GetDuration(SoundKey /* key */) override {
44 return base::TimeDelta();
47 bool is_sound_initialized(SoundKey key) const {
48 return is_sound_initialized_[key];
51 int num_play_requests(SoundKey key) const {
52 return num_play_requests_[key];
56 std::vector<bool> is_sound_initialized_;
57 std::vector<int> num_play_requests_;
59 DISALLOW_COPY_AND_ASSIGN(SoundsManagerTestImpl);
62 class VolumeControllerTest : public InProcessBrowserTest {
64 VolumeControllerTest() {}
65 virtual ~VolumeControllerTest() {}
67 virtual void SetUpOnMainThread() override {
68 volume_controller_.reset(new VolumeController());
69 audio_handler_ = chromeos::CrasAudioHandler::Get();
74 volume_controller_->HandleVolumeMute(ui::Accelerator());
78 volume_controller_->HandleVolumeUp(ui::Accelerator());
82 volume_controller_->HandleVolumeDown(ui::Accelerator());
85 chromeos::CrasAudioHandler* audio_handler_; // Not owned.
88 scoped_ptr<VolumeController> volume_controller_;
90 DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest);
93 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) {
94 // Set initial value as 50%
95 const int kInitVolume = 50;
96 audio_handler_->SetOutputVolumePercent(kInitVolume);
98 EXPECT_EQ(audio_handler_->GetOutputVolumePercent(), kInitVolume);
101 EXPECT_LT(kInitVolume, audio_handler_->GetOutputVolumePercent());
103 EXPECT_EQ(kInitVolume, audio_handler_->GetOutputVolumePercent());
105 EXPECT_GT(kInitVolume, audio_handler_->GetOutputVolumePercent());
108 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) {
109 // Setting to very small volume.
110 audio_handler_->SetOutputVolumePercent(1);
113 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
115 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
117 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
120 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) {
121 // Setting to almost max
122 audio_handler_->SetOutputVolumePercent(99);
125 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
127 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
129 EXPECT_GT(100, audio_handler_->GetOutputVolumePercent());
132 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) {
133 ASSERT_FALSE(audio_handler_->IsOutputMuted());
134 const int initial_volume = audio_handler_->GetOutputVolumePercent();
137 EXPECT_TRUE(audio_handler_->IsOutputMuted());
139 // Further mute buttons doesn't have effects.
141 EXPECT_TRUE(audio_handler_->IsOutputMuted());
143 // Right after the volume up after set_mute recovers to original volume.
145 EXPECT_FALSE(audio_handler_->IsOutputMuted());
146 EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent());
149 // After the volume down, the volume goes down to zero explicitly.
151 EXPECT_TRUE(audio_handler_->IsOutputMuted());
152 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
154 // Thus, further VolumeUp doesn't recover the volume, it's just slightly
157 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
158 EXPECT_GT(initial_volume, audio_handler_->GetOutputVolumePercent());
161 class VolumeControllerSoundsTest : public VolumeControllerTest {
163 VolumeControllerSoundsTest() : sounds_manager_(NULL) {}
164 virtual ~VolumeControllerSoundsTest() {}
166 virtual void SetUpInProcessBrowserTestFixture() override {
167 sounds_manager_ = new SoundsManagerTestImpl();
168 media::SoundsManager::InitializeForTesting(sounds_manager_);
171 void EnableSpokenFeedback(bool enabled) {
172 chromeos::AccessibilityManager* manager =
173 chromeos::AccessibilityManager::Get();
174 manager->EnableSpokenFeedback(enabled, ui::A11Y_NOTIFICATION_NONE);
177 bool is_sound_initialized() const {
178 return sounds_manager_->is_sound_initialized(chromeos::SOUND_VOLUME_ADJUST);
181 int num_play_requests() const {
182 return sounds_manager_->num_play_requests(chromeos::SOUND_VOLUME_ADJUST);
186 SoundsManagerTestImpl* sounds_manager_;
188 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsTest);
191 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, Simple) {
192 audio_handler_->SetOutputVolumePercent(50);
194 EnableSpokenFeedback(false /* enabled */);
197 EXPECT_EQ(0, num_play_requests());
199 EnableSpokenFeedback(true /* enabled */);
202 EXPECT_EQ(2, num_play_requests());
205 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, EdgeCases) {
206 EXPECT_TRUE(is_sound_initialized());
207 EnableSpokenFeedback(true /* enabled */);
209 // Check that sound is played on volume up and volume down.
210 audio_handler_->SetOutputVolumePercent(50);
212 EXPECT_EQ(1, num_play_requests());
214 EXPECT_EQ(2, num_play_requests());
216 audio_handler_->SetOutputVolumePercent(99);
218 EXPECT_EQ(3, num_play_requests());
220 audio_handler_->SetOutputVolumePercent(100);
222 EXPECT_EQ(3, num_play_requests());
224 // Check that sound isn't played when audio is muted.
225 audio_handler_->SetOutputVolumePercent(50);
228 ASSERT_TRUE(audio_handler_->IsOutputMuted());
229 EXPECT_EQ(3, num_play_requests());
231 // Check that audio is unmuted and sound is played.
233 ASSERT_FALSE(audio_handler_->IsOutputMuted());
234 EXPECT_EQ(4, num_play_requests());
237 class VolumeControllerSoundsDisabledTest : public VolumeControllerSoundsTest {
239 VolumeControllerSoundsDisabledTest() {}
240 virtual ~VolumeControllerSoundsDisabledTest() {}
242 virtual void SetUpCommandLine(CommandLine* command_line) override {
243 VolumeControllerSoundsTest::SetUpCommandLine(command_line);
244 command_line->AppendSwitch(chromeos::switches::kDisableVolumeAdjustSound);
248 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsDisabledTest);
251 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsDisabledTest,
252 VolumeAdjustSounds) {
253 EXPECT_FALSE(is_sound_initialized());
255 // Check that sound isn't played on volume up and volume down.
256 audio_handler_->SetOutputVolumePercent(50);
259 EXPECT_EQ(0, num_play_requests());