Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ui / ash / volume_controller_browsertest_chromeos.cc
blobe2daa718285128c5e4966ad745b23cebde5c4ec7
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 bool Stop(SoundKey key) override {
43 return true;
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];
58 private:
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 {
66 public:
67 VolumeControllerTest() {}
68 ~VolumeControllerTest() override {}
70 void SetUpOnMainThread() override {
71 volume_controller_.reset(new VolumeController());
72 audio_handler_ = chromeos::CrasAudioHandler::Get();
75 protected:
76 void VolumeMute() {
77 volume_controller_->HandleVolumeMute(ui::Accelerator());
80 void VolumeUp() {
81 volume_controller_->HandleVolumeUp(ui::Accelerator());
84 void VolumeDown() {
85 volume_controller_->HandleVolumeDown(ui::Accelerator());
88 chromeos::CrasAudioHandler* audio_handler_; // Not owned.
90 private:
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);
103 VolumeUp();
104 EXPECT_LT(kInitVolume, audio_handler_->GetOutputVolumePercent());
105 VolumeDown();
106 EXPECT_EQ(kInitVolume, audio_handler_->GetOutputVolumePercent());
107 VolumeDown();
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);
115 VolumeDown();
116 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
117 VolumeDown();
118 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
119 VolumeUp();
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);
127 VolumeUp();
128 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
129 VolumeUp();
130 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
131 VolumeDown();
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();
139 VolumeMute();
140 EXPECT_TRUE(audio_handler_->IsOutputMuted());
142 // Further mute buttons doesn't have effects.
143 VolumeMute();
144 EXPECT_TRUE(audio_handler_->IsOutputMuted());
146 // Right after the volume up after set_mute recovers to original volume.
147 VolumeUp();
148 EXPECT_FALSE(audio_handler_->IsOutputMuted());
149 EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent());
151 VolumeMute();
152 // After the volume down, the volume goes down to zero explicitly.
153 VolumeDown();
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
158 // bigger than 0.
159 VolumeUp();
160 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
161 EXPECT_GT(initial_volume, audio_handler_->GetOutputVolumePercent());
164 class VolumeControllerSoundsTest : public VolumeControllerTest {
165 public:
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);
188 private:
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 */);
198 VolumeUp();
199 VolumeDown();
200 EXPECT_EQ(0, num_play_requests());
202 EnableSpokenFeedback(true /* enabled */);
203 VolumeUp();
204 VolumeDown();
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);
214 VolumeUp();
215 EXPECT_EQ(1, num_play_requests());
216 VolumeDown();
217 EXPECT_EQ(2, num_play_requests());
219 audio_handler_->SetOutputVolumePercent(99);
220 VolumeUp();
221 EXPECT_EQ(3, num_play_requests());
223 audio_handler_->SetOutputVolumePercent(100);
224 VolumeUp();
225 EXPECT_EQ(3, num_play_requests());
227 // Check that sound isn't played when audio is muted.
228 audio_handler_->SetOutputVolumePercent(50);
229 VolumeMute();
230 VolumeDown();
231 ASSERT_TRUE(audio_handler_->IsOutputMuted());
232 EXPECT_EQ(3, num_play_requests());
234 // Check that audio is unmuted and sound is played.
235 VolumeUp();
236 ASSERT_FALSE(audio_handler_->IsOutputMuted());
237 EXPECT_EQ(4, num_play_requests());
240 class VolumeControllerSoundsDisabledTest : public VolumeControllerSoundsTest {
241 public:
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);
250 private:
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);
260 VolumeUp();
261 VolumeDown();
262 EXPECT_EQ(0, num_play_requests());
265 } // namespace