Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / volume_controller_browsertest_chromeos.cc
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.
4
5 #include <vector>
6
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"
20
21 namespace {
22
23 class SoundsManagerTestImpl : public media::SoundsManager {
24  public:
25   SoundsManagerTestImpl()
26     : is_sound_initialized_(chromeos::SOUND_COUNT),
27       num_play_requests_(chromeos::SOUND_COUNT) {
28   }
29
30   virtual ~SoundsManagerTestImpl() {}
31
32   virtual bool Initialize(SoundKey key,
33                           const base::StringPiece& /* data */) override {
34     is_sound_initialized_[key] = true;
35     return true;
36   }
37
38   virtual bool Play(SoundKey key) override {
39     ++num_play_requests_[key];
40     return true;
41   }
42
43   virtual base::TimeDelta GetDuration(SoundKey /* key */) override {
44     return base::TimeDelta();
45   }
46
47   bool is_sound_initialized(SoundKey key) const {
48     return is_sound_initialized_[key];
49   }
50
51   int num_play_requests(SoundKey key) const {
52     return num_play_requests_[key];
53   }
54
55  private:
56   std::vector<bool> is_sound_initialized_;
57   std::vector<int> num_play_requests_;
58
59   DISALLOW_COPY_AND_ASSIGN(SoundsManagerTestImpl);
60 };
61
62 class VolumeControllerTest : public InProcessBrowserTest {
63  public:
64   VolumeControllerTest() {}
65   virtual ~VolumeControllerTest() {}
66
67   virtual void SetUpOnMainThread() override {
68     volume_controller_.reset(new VolumeController());
69     audio_handler_ = chromeos::CrasAudioHandler::Get();
70   }
71
72  protected:
73   void VolumeMute() {
74     volume_controller_->HandleVolumeMute(ui::Accelerator());
75   }
76
77   void VolumeUp() {
78     volume_controller_->HandleVolumeUp(ui::Accelerator());
79   }
80
81   void VolumeDown() {
82     volume_controller_->HandleVolumeDown(ui::Accelerator());
83   }
84
85   chromeos::CrasAudioHandler* audio_handler_;  // Not owned.
86
87  private:
88   scoped_ptr<VolumeController> volume_controller_;
89
90   DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest);
91 };
92
93 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) {
94   // Set initial value as 50%
95   const int kInitVolume = 50;
96   audio_handler_->SetOutputVolumePercent(kInitVolume);
97
98   EXPECT_EQ(audio_handler_->GetOutputVolumePercent(), kInitVolume);
99
100   VolumeUp();
101   EXPECT_LT(kInitVolume, audio_handler_->GetOutputVolumePercent());
102   VolumeDown();
103   EXPECT_EQ(kInitVolume, audio_handler_->GetOutputVolumePercent());
104   VolumeDown();
105   EXPECT_GT(kInitVolume, audio_handler_->GetOutputVolumePercent());
106 }
107
108 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) {
109   // Setting to very small volume.
110   audio_handler_->SetOutputVolumePercent(1);
111
112   VolumeDown();
113   EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
114   VolumeDown();
115   EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
116   VolumeUp();
117   EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
118 }
119
120 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) {
121   // Setting to almost max
122   audio_handler_->SetOutputVolumePercent(99);
123
124   VolumeUp();
125   EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
126   VolumeUp();
127   EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
128   VolumeDown();
129   EXPECT_GT(100, audio_handler_->GetOutputVolumePercent());
130 }
131
132 IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) {
133   ASSERT_FALSE(audio_handler_->IsOutputMuted());
134   const int initial_volume = audio_handler_->GetOutputVolumePercent();
135
136   VolumeMute();
137   EXPECT_TRUE(audio_handler_->IsOutputMuted());
138
139   // Further mute buttons doesn't have effects.
140   VolumeMute();
141   EXPECT_TRUE(audio_handler_->IsOutputMuted());
142
143   // Right after the volume up after set_mute recovers to original volume.
144   VolumeUp();
145   EXPECT_FALSE(audio_handler_->IsOutputMuted());
146   EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent());
147
148   VolumeMute();
149   // After the volume down, the volume goes down to zero explicitly.
150   VolumeDown();
151   EXPECT_TRUE(audio_handler_->IsOutputMuted());
152   EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
153
154   // Thus, further VolumeUp doesn't recover the volume, it's just slightly
155   // bigger than 0.
156   VolumeUp();
157   EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
158   EXPECT_GT(initial_volume, audio_handler_->GetOutputVolumePercent());
159 }
160
161 class VolumeControllerSoundsTest : public VolumeControllerTest {
162  public:
163   VolumeControllerSoundsTest() : sounds_manager_(NULL) {}
164   virtual ~VolumeControllerSoundsTest() {}
165
166   virtual void SetUpInProcessBrowserTestFixture() override {
167     sounds_manager_ = new SoundsManagerTestImpl();
168     media::SoundsManager::InitializeForTesting(sounds_manager_);
169   }
170
171   void EnableSpokenFeedback(bool enabled) {
172     chromeos::AccessibilityManager* manager =
173         chromeos::AccessibilityManager::Get();
174     manager->EnableSpokenFeedback(enabled, ui::A11Y_NOTIFICATION_NONE);
175   }
176
177   bool is_sound_initialized() const {
178     return sounds_manager_->is_sound_initialized(chromeos::SOUND_VOLUME_ADJUST);
179   }
180
181   int num_play_requests() const {
182     return sounds_manager_->num_play_requests(chromeos::SOUND_VOLUME_ADJUST);
183   }
184
185  private:
186   SoundsManagerTestImpl* sounds_manager_;
187
188   DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsTest);
189 };
190
191 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, Simple) {
192   audio_handler_->SetOutputVolumePercent(50);
193
194   EnableSpokenFeedback(false /* enabled */);
195   VolumeUp();
196   VolumeDown();
197   EXPECT_EQ(0, num_play_requests());
198
199   EnableSpokenFeedback(true /* enabled */);
200   VolumeUp();
201   VolumeDown();
202   EXPECT_EQ(2, num_play_requests());
203 }
204
205 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, EdgeCases) {
206   EXPECT_TRUE(is_sound_initialized());
207   EnableSpokenFeedback(true /* enabled */);
208
209   // Check that sound is played on volume up and volume down.
210   audio_handler_->SetOutputVolumePercent(50);
211   VolumeUp();
212   EXPECT_EQ(1, num_play_requests());
213   VolumeDown();
214   EXPECT_EQ(2, num_play_requests());
215
216   audio_handler_->SetOutputVolumePercent(99);
217   VolumeUp();
218   EXPECT_EQ(3, num_play_requests());
219
220   audio_handler_->SetOutputVolumePercent(100);
221   VolumeUp();
222   EXPECT_EQ(3, num_play_requests());
223
224   // Check that sound isn't played when audio is muted.
225   audio_handler_->SetOutputVolumePercent(50);
226   VolumeMute();
227   VolumeDown();
228   ASSERT_TRUE(audio_handler_->IsOutputMuted());
229   EXPECT_EQ(3, num_play_requests());
230
231   // Check that audio is unmuted and sound is played.
232   VolumeUp();
233   ASSERT_FALSE(audio_handler_->IsOutputMuted());
234   EXPECT_EQ(4, num_play_requests());
235 }
236
237 class VolumeControllerSoundsDisabledTest : public VolumeControllerSoundsTest {
238  public:
239   VolumeControllerSoundsDisabledTest() {}
240   virtual ~VolumeControllerSoundsDisabledTest() {}
241
242   virtual void SetUpCommandLine(CommandLine* command_line) override {
243     VolumeControllerSoundsTest::SetUpCommandLine(command_line);
244     command_line->AppendSwitch(chromeos::switches::kDisableVolumeAdjustSound);
245   }
246
247  private:
248   DISALLOW_COPY_AND_ASSIGN(VolumeControllerSoundsDisabledTest);
249 };
250
251 IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsDisabledTest,
252                        VolumeAdjustSounds) {
253   EXPECT_FALSE(is_sound_initialized());
254
255   // Check that sound isn't played on volume up and volume down.
256   audio_handler_->SetOutputVolumePercent(50);
257   VolumeUp();
258   VolumeDown();
259   EXPECT_EQ(0, num_play_requests());
260 }
261
262 }  // namespace