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 base::TimeDelta
GetDuration(SoundKey
/* key */) override
{
43 return base::TimeDelta();
46 bool is_sound_initialized(SoundKey key
) const {
47 return is_sound_initialized_
[key
];
50 int num_play_requests(SoundKey key
) const {
51 return num_play_requests_
[key
];
55 std::vector
<bool> is_sound_initialized_
;
56 std::vector
<int> num_play_requests_
;
58 DISALLOW_COPY_AND_ASSIGN(SoundsManagerTestImpl
);
61 class VolumeControllerTest
: public InProcessBrowserTest
{
63 VolumeControllerTest() {}
64 ~VolumeControllerTest() override
{}
66 void SetUpOnMainThread() override
{
67 volume_controller_
.reset(new VolumeController());
68 audio_handler_
= chromeos::CrasAudioHandler::Get();
73 volume_controller_
->HandleVolumeMute(ui::Accelerator());
77 volume_controller_
->HandleVolumeUp(ui::Accelerator());
81 volume_controller_
->HandleVolumeDown(ui::Accelerator());
84 chromeos::CrasAudioHandler
* audio_handler_
; // Not owned.
87 scoped_ptr
<VolumeController
> volume_controller_
;
89 DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest
);
92 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, VolumeUpAndDown
) {
93 // Set initial value as 50%
94 const int kInitVolume
= 50;
95 audio_handler_
->SetOutputVolumePercent(kInitVolume
);
97 EXPECT_EQ(audio_handler_
->GetOutputVolumePercent(), kInitVolume
);
100 EXPECT_LT(kInitVolume
, audio_handler_
->GetOutputVolumePercent());
102 EXPECT_EQ(kInitVolume
, audio_handler_
->GetOutputVolumePercent());
104 EXPECT_GT(kInitVolume
, audio_handler_
->GetOutputVolumePercent());
107 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, VolumeDownToZero
) {
108 // Setting to very small volume.
109 audio_handler_
->SetOutputVolumePercent(1);
112 EXPECT_EQ(0, audio_handler_
->GetOutputVolumePercent());
114 EXPECT_EQ(0, audio_handler_
->GetOutputVolumePercent());
116 EXPECT_LT(0, audio_handler_
->GetOutputVolumePercent());
119 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, VolumeUpTo100
) {
120 // Setting to almost max
121 audio_handler_
->SetOutputVolumePercent(99);
124 EXPECT_EQ(100, audio_handler_
->GetOutputVolumePercent());
126 EXPECT_EQ(100, audio_handler_
->GetOutputVolumePercent());
128 EXPECT_GT(100, audio_handler_
->GetOutputVolumePercent());
131 IN_PROC_BROWSER_TEST_F(VolumeControllerTest
, Mutes
) {
132 ASSERT_FALSE(audio_handler_
->IsOutputMuted());
133 const int initial_volume
= audio_handler_
->GetOutputVolumePercent();
136 EXPECT_TRUE(audio_handler_
->IsOutputMuted());
138 // Further mute buttons doesn't have effects.
140 EXPECT_TRUE(audio_handler_
->IsOutputMuted());
142 // Right after the volume up after set_mute recovers to original volume.
144 EXPECT_FALSE(audio_handler_
->IsOutputMuted());
145 EXPECT_EQ(initial_volume
, audio_handler_
->GetOutputVolumePercent());
148 // After the volume down, the volume goes down to zero explicitly.
150 EXPECT_TRUE(audio_handler_
->IsOutputMuted());
151 EXPECT_EQ(0, audio_handler_
->GetOutputVolumePercent());
153 // Thus, further VolumeUp doesn't recover the volume, it's just slightly
156 EXPECT_LT(0, audio_handler_
->GetOutputVolumePercent());
157 EXPECT_GT(initial_volume
, audio_handler_
->GetOutputVolumePercent());
160 class VolumeControllerSoundsTest
: public VolumeControllerTest
{
162 VolumeControllerSoundsTest() : sounds_manager_(NULL
) {}
163 ~VolumeControllerSoundsTest() override
{}
165 void SetUpInProcessBrowserTestFixture() override
{
166 sounds_manager_
= new SoundsManagerTestImpl();
167 media::SoundsManager::InitializeForTesting(sounds_manager_
);
170 void EnableSpokenFeedback(bool enabled
) {
171 chromeos::AccessibilityManager
* manager
=
172 chromeos::AccessibilityManager::Get();
173 manager
->EnableSpokenFeedback(enabled
, ui::A11Y_NOTIFICATION_NONE
);
176 bool is_sound_initialized() const {
177 return sounds_manager_
->is_sound_initialized(chromeos::SOUND_VOLUME_ADJUST
);
180 int num_play_requests() const {
181 return sounds_manager_
->num_play_requests(chromeos::SOUND_VOLUME_ADJUST
);
185 SoundsManagerTestImpl
* sounds_manager_
;
187 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsTest
);
190 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest
, Simple
) {
191 audio_handler_
->SetOutputVolumePercent(50);
193 EnableSpokenFeedback(false /* enabled */);
196 EXPECT_EQ(0, num_play_requests());
198 EnableSpokenFeedback(true /* enabled */);
201 EXPECT_EQ(2, num_play_requests());
204 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest
, EdgeCases
) {
205 EXPECT_TRUE(is_sound_initialized());
206 EnableSpokenFeedback(true /* enabled */);
208 // Check that sound is played on volume up and volume down.
209 audio_handler_
->SetOutputVolumePercent(50);
211 EXPECT_EQ(1, num_play_requests());
213 EXPECT_EQ(2, num_play_requests());
215 audio_handler_
->SetOutputVolumePercent(99);
217 EXPECT_EQ(3, num_play_requests());
219 audio_handler_
->SetOutputVolumePercent(100);
221 EXPECT_EQ(3, num_play_requests());
223 // Check that sound isn't played when audio is muted.
224 audio_handler_
->SetOutputVolumePercent(50);
227 ASSERT_TRUE(audio_handler_
->IsOutputMuted());
228 EXPECT_EQ(3, num_play_requests());
230 // Check that audio is unmuted and sound is played.
232 ASSERT_FALSE(audio_handler_
->IsOutputMuted());
233 EXPECT_EQ(4, num_play_requests());
236 class VolumeControllerSoundsDisabledTest
: public VolumeControllerSoundsTest
{
238 VolumeControllerSoundsDisabledTest() {}
239 ~VolumeControllerSoundsDisabledTest() override
{}
241 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
242 VolumeControllerSoundsTest::SetUpCommandLine(command_line
);
243 command_line
->AppendSwitch(chromeos::switches::kDisableVolumeAdjustSound
);
247 DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsDisabledTest
);
250 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsDisabledTest
,
251 VolumeAdjustSounds
) {
252 EXPECT_FALSE(is_sound_initialized());
254 // Check that sound isn't played on volume up and volume down.
255 audio_handler_
->SetOutputVolumePercent(50);
258 EXPECT_EQ(0, num_play_requests());