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 ~SoundsManagerTestImpl() override
{}
32 bool Initialize(SoundKey key
, const base::StringPiece
& /* data */) override
{
33 is_sound_initialized_
[key
] = true;
37 bool Play(SoundKey key
) override
{
38 ++num_play_requests_
[key
];
42 bool Stop(SoundKey key
) override
{
46 base::TimeDelta
GetDuration(SoundKey
/* key */) override
{
47 return base::TimeDelta();
50 bool is_sound_initialized(SoundKey key
) const {
51 return is_sound_initialized_
[key
];
54 int num_play_requests(SoundKey key
) const {
55 return num_play_requests_
[key
];
59 std::vector
<bool> is_sound_initialized_
;
60 std::vector
<int> num_play_requests_
;
62 DISALLOW_COPY_AND_ASSIGN(SoundsManagerTestImpl
);
65 class VolumeControllerTest
: public InProcessBrowserTest
{
67 VolumeControllerTest() {}
68 ~VolumeControllerTest() override
{}
70 void SetUpOnMainThread() override
{
71 volume_controller_
.reset(new VolumeController());
72 audio_handler_
= chromeos::CrasAudioHandler::Get();
77 volume_controller_
->HandleVolumeMute(ui::Accelerator());
81 volume_controller_
->HandleVolumeUp(ui::Accelerator());
85 volume_controller_
->HandleVolumeDown(ui::Accelerator());
88 chromeos::CrasAudioHandler
* audio_handler_
; // Not owned.
91 scoped_ptr
<VolumeController
> volume_controller_
;
93 DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest
);
96 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, VolumeUpAndDown
) {
97 // Set initial value as 50%
98 const int kInitVolume
= 50;
99 audio_handler_
->SetOutputVolumePercent(kInitVolume
);
101 EXPECT_EQ(audio_handler_
->GetOutputVolumePercent(), kInitVolume
);
104 EXPECT_LT(kInitVolume
, audio_handler_
->GetOutputVolumePercent());
106 EXPECT_EQ(kInitVolume
, audio_handler_
->GetOutputVolumePercent());
108 EXPECT_GT(kInitVolume
, audio_handler_
->GetOutputVolumePercent());
111 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, VolumeDownToZero
) {
112 // Setting to very small volume.
113 audio_handler_
->SetOutputVolumePercent(1);
116 EXPECT_EQ(0, audio_handler_
->GetOutputVolumePercent());
118 EXPECT_EQ(0, audio_handler_
->GetOutputVolumePercent());
120 EXPECT_LT(0, audio_handler_
->GetOutputVolumePercent());
123 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, VolumeUpTo100
) {
124 // Setting to almost max
125 audio_handler_
->SetOutputVolumePercent(99);
128 EXPECT_EQ(100, audio_handler_
->GetOutputVolumePercent());
130 EXPECT_EQ(100, audio_handler_
->GetOutputVolumePercent());
132 EXPECT_GT(100, audio_handler_
->GetOutputVolumePercent());
135 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, Mutes
) {
136 ASSERT_FALSE(audio_handler_
->IsOutputMuted());
137 const int initial_volume
= audio_handler_
->GetOutputVolumePercent();
140 EXPECT_TRUE(audio_handler_
->IsOutputMuted());
142 // Further mute buttons doesn't have effects.
144 EXPECT_TRUE(audio_handler_
->IsOutputMuted());
146 // Right after the volume up after set_mute recovers to original volume.
148 EXPECT_FALSE(audio_handler_
->IsOutputMuted());
149 EXPECT_EQ(initial_volume
, audio_handler_
->GetOutputVolumePercent());
152 // After the volume down, the volume goes down to zero explicitly.
154 EXPECT_TRUE(audio_handler_
->IsOutputMuted());
155 EXPECT_EQ(0, audio_handler_
->GetOutputVolumePercent());
157 // Thus, further VolumeUp doesn't recover the volume, it's just slightly
160 EXPECT_LT(0, audio_handler_
->GetOutputVolumePercent());
161 EXPECT_GT(initial_volume
, audio_handler_
->GetOutputVolumePercent());
164 class VolumeControllerSoundsTest
: public VolumeControllerTest
{
166 VolumeControllerSoundsTest() : sounds_manager_(NULL
) {}
167 ~VolumeControllerSoundsTest() override
{}
169 void SetUpInProcessBrowserTestFixture() override
{
170 sounds_manager_
= new SoundsManagerTestImpl();
171 media::SoundsManager::InitializeForTesting(sounds_manager_
);
174 void EnableSpokenFeedback(bool enabled
) {
175 chromeos::AccessibilityManager
* manager
=
176 chromeos::AccessibilityManager::Get();
177 manager
->EnableSpokenFeedback(enabled
, ui::A11Y_NOTIFICATION_NONE
);
180 bool is_sound_initialized() const {
181 return sounds_manager_
->is_sound_initialized(chromeos::SOUND_VOLUME_ADJUST
);
184 int num_play_requests() const {
185 return sounds_manager_
->num_play_requests(chromeos::SOUND_VOLUME_ADJUST
);
189 SoundsManagerTestImpl
* sounds_manager_
;
191 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsTest
);
194 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest
, Simple
) {
195 audio_handler_
->SetOutputVolumePercent(50);
197 EnableSpokenFeedback(false /* enabled */);
200 EXPECT_EQ(0, num_play_requests());
202 EnableSpokenFeedback(true /* enabled */);
205 EXPECT_EQ(2, num_play_requests());
208 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest
, EdgeCases
) {
209 EXPECT_TRUE(is_sound_initialized());
210 EnableSpokenFeedback(true /* enabled */);
212 // Check that sound is played on volume up and volume down.
213 audio_handler_
->SetOutputVolumePercent(50);
215 EXPECT_EQ(1, num_play_requests());
217 EXPECT_EQ(2, num_play_requests());
219 audio_handler_
->SetOutputVolumePercent(99);
221 EXPECT_EQ(3, num_play_requests());
223 audio_handler_
->SetOutputVolumePercent(100);
225 EXPECT_EQ(3, num_play_requests());
227 // Check that sound isn't played when audio is muted.
228 audio_handler_
->SetOutputVolumePercent(50);
231 ASSERT_TRUE(audio_handler_
->IsOutputMuted());
232 EXPECT_EQ(3, num_play_requests());
234 // Check that audio is unmuted and sound is played.
236 ASSERT_FALSE(audio_handler_
->IsOutputMuted());
237 EXPECT_EQ(4, num_play_requests());
240 class VolumeControllerSoundsDisabledTest
: public VolumeControllerSoundsTest
{
242 VolumeControllerSoundsDisabledTest() {}
243 ~VolumeControllerSoundsDisabledTest() override
{}
245 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
246 VolumeControllerSoundsTest::SetUpCommandLine(command_line
);
247 command_line
->AppendSwitch(chromeos::switches::kDisableVolumeAdjustSound
);
251 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsDisabledTest
);
254 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsDisabledTest
,
255 VolumeAdjustSounds
) {
256 EXPECT_FALSE(is_sound_initialized());
258 // Check that sound isn't played on volume up and volume down.
259 audio_handler_
->SetOutputVolumePercent(50);
262 EXPECT_EQ(0, num_play_requests());