Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / ash / volume_controller_browsertest_chromeos.cc
blobd94f7d00a264510bb0c20bfc3d583c6aba64522f
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.
5 #include <vector>
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"
21 namespace {
23 class SoundsManagerTestImpl : public media::SoundsManager {
24 public:
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;
34 return true;
37 bool Play(SoundKey key) override {
38 ++num_play_requests_[key];
39 return true;
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];
54 private:
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 {
62 public:
63 VolumeControllerTest() {}
64 ~VolumeControllerTest() override {}
66 void SetUpOnMainThread() override {
67 volume_controller_.reset(new VolumeController());
68 audio_handler_ = chromeos::CrasAudioHandler::Get();
71 protected:
72 void VolumeMute() {
73 volume_controller_->HandleVolumeMute(ui::Accelerator());
76 void VolumeUp() {
77 volume_controller_->HandleVolumeUp(ui::Accelerator());
80 void VolumeDown() {
81 volume_controller_->HandleVolumeDown(ui::Accelerator());
84 chromeos::CrasAudioHandler* audio_handler_; // Not owned.
86 private:
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);
99 VolumeUp();
100 EXPECT_LT(kInitVolume, audio_handler_->GetOutputVolumePercent());
101 VolumeDown();
102 EXPECT_EQ(kInitVolume, audio_handler_->GetOutputVolumePercent());
103 VolumeDown();
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);
111 VolumeDown();
112 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
113 VolumeDown();
114 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
115 VolumeUp();
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);
123 VolumeUp();
124 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
125 VolumeUp();
126 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
127 VolumeDown();
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();
135 VolumeMute();
136 EXPECT_TRUE(audio_handler_->IsOutputMuted());
138 // Further mute buttons doesn't have effects.
139 VolumeMute();
140 EXPECT_TRUE(audio_handler_->IsOutputMuted());
142 // Right after the volume up after set_mute recovers to original volume.
143 VolumeUp();
144 EXPECT_FALSE(audio_handler_->IsOutputMuted());
145 EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent());
147 VolumeMute();
148 // After the volume down, the volume goes down to zero explicitly.
149 VolumeDown();
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
154 // bigger than 0.
155 VolumeUp();
156 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
157 EXPECT_GT(initial_volume, audio_handler_->GetOutputVolumePercent());
160 class VolumeControllerSoundsTest : public VolumeControllerTest {
161 public:
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);
184 private:
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 */);
194 VolumeUp();
195 VolumeDown();
196 EXPECT_EQ(0, num_play_requests());
198 EnableSpokenFeedback(true /* enabled */);
199 VolumeUp();
200 VolumeDown();
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);
210 VolumeUp();
211 EXPECT_EQ(1, num_play_requests());
212 VolumeDown();
213 EXPECT_EQ(2, num_play_requests());
215 audio_handler_->SetOutputVolumePercent(99);
216 VolumeUp();
217 EXPECT_EQ(3, num_play_requests());
219 audio_handler_->SetOutputVolumePercent(100);
220 VolumeUp();
221 EXPECT_EQ(3, num_play_requests());
223 // Check that sound isn't played when audio is muted.
224 audio_handler_->SetOutputVolumePercent(50);
225 VolumeMute();
226 VolumeDown();
227 ASSERT_TRUE(audio_handler_->IsOutputMuted());
228 EXPECT_EQ(3, num_play_requests());
230 // Check that audio is unmuted and sound is played.
231 VolumeUp();
232 ASSERT_FALSE(audio_handler_->IsOutputMuted());
233 EXPECT_EQ(4, num_play_requests());
236 class VolumeControllerSoundsDisabledTest : public VolumeControllerSoundsTest {
237 public:
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);
246 private:
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);
256 VolumeUp();
257 VolumeDown();
258 EXPECT_EQ(0, num_play_requests());
261 } // namespace