volume_controller_browsertest_chromeos.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 "chrome/browser/ui/ash/volume_controller_chromeos.h" 6 7#include "chrome/browser/chromeos/audio/audio_handler.h" 8#include "chrome/browser/chromeos/audio/audio_mixer.h" 9#include "chrome/test/base/in_process_browser_test.h" 10#include "chrome/test/base/ui_test_utils.h" 11#include "ui/base/accelerators/accelerator.h" 12 13namespace { 14 15// Default volume as a percentage in the range [0.0, 100.0]. 16const double kDefaultVolumePercent = 75.0; 17 18class MockAudioMixer : public chromeos::AudioMixer { 19 public: 20 MockAudioMixer() 21 : volume_(kDefaultVolumePercent), 22 is_muted_(false) { 23 } 24 25 virtual void Init() OVERRIDE { 26 } 27 28 virtual double GetVolumePercent() OVERRIDE { 29 return volume_; 30 } 31 32 virtual void SetVolumePercent(double percent) OVERRIDE { 33 volume_ = percent; 34 } 35 36 virtual bool IsMuted() OVERRIDE { 37 return is_muted_; 38 } 39 40 virtual void SetMuted(bool mute) OVERRIDE { 41 is_muted_ = mute; 42 } 43 44 virtual bool IsMuteLocked() OVERRIDE { 45 return is_mute_locked_; 46 } 47 48 virtual void SetMuteLocked(bool locked) OVERRIDE { 49 is_mute_locked_ = locked; 50 } 51 52 virtual bool IsCaptureMuted() OVERRIDE { 53 return is_capture_muted_; 54 } 55 56 virtual void SetCaptureMuted(bool mute) OVERRIDE { 57 is_capture_muted_ = mute; 58 } 59 60 virtual bool IsCaptureMuteLocked() OVERRIDE { 61 return is_capture_mute_locked_; 62 } 63 64 virtual void SetCaptureMuteLocked(bool locked) OVERRIDE { 65 is_capture_mute_locked_ = locked; 66 } 67 68 private: 69 double volume_; 70 bool is_muted_; 71 bool is_mute_locked_; 72 bool is_capture_muted_; 73 bool is_capture_mute_locked_; 74 75 DISALLOW_COPY_AND_ASSIGN(MockAudioMixer); 76}; 77 78// This class has to be browsertest because AudioHandler uses prefs_service. 79class VolumeControllerTest : public InProcessBrowserTest { 80 public: 81 VolumeControllerTest() {} 82 83 virtual void SetUpOnMainThread() OVERRIDE { 84 // First we should shutdown the default audio handler. 85 chromeos::AudioHandler::Shutdown(); 86 audio_mixer_ = new MockAudioMixer; 87 chromeos::AudioHandler::InitializeForTesting(audio_mixer_); 88 } 89 90 virtual void CleanUpOnMainThread() OVERRIDE { 91 chromeos::AudioHandler::Shutdown(); 92 audio_mixer_ = NULL; 93 } 94 95 protected: 96 void SetVolumePercent(double percent) { 97 volume_controller_.SetVolumePercent(percent); 98 } 99 100 void VolumeMute() { 101 volume_controller_.HandleVolumeMute(ui::Accelerator()); 102 } 103 104 void VolumeUnmute() { 105 volume_controller_.SetAudioMuted(false); 106 } 107 108 void VolumeUp() { 109 volume_controller_.HandleVolumeUp(ui::Accelerator()); 110 } 111 112 void VolumeDown() { 113 volume_controller_.HandleVolumeDown(ui::Accelerator()); 114 } 115 116 MockAudioMixer* audio_mixer() { return audio_mixer_; } 117 118 private: 119 VolumeController volume_controller_; 120 MockAudioMixer* audio_mixer_; 121 122 DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest); 123}; 124 125IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) { 126 // Set initial value as 50% 127 audio_mixer()->SetVolumePercent(50.0); 128 129 double initial_volume = audio_mixer()->GetVolumePercent(); 130 131 VolumeUp(); 132 EXPECT_LT(initial_volume, audio_mixer()->GetVolumePercent()); 133 VolumeDown(); 134 EXPECT_DOUBLE_EQ(initial_volume, audio_mixer()->GetVolumePercent()); 135 VolumeDown(); 136 EXPECT_GT(initial_volume, audio_mixer()->GetVolumePercent()); 137} 138 139IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) { 140 // Setting to very small 141 audio_mixer()->SetVolumePercent(0.1); 142 143 VolumeDown(); 144 EXPECT_DOUBLE_EQ(0.0, audio_mixer()->GetVolumePercent()); 145 VolumeDown(); 146 EXPECT_DOUBLE_EQ(0.0, audio_mixer()->GetVolumePercent()); 147 VolumeUp(); 148 EXPECT_LT(0.0, audio_mixer()->GetVolumePercent()); 149} 150 151IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeAutoMute) { 152 // Setting to very small 153 154 // kMuteThresholdPercent = 0.1 in audio_handler.cc. 155 SetVolumePercent(0.1); 156 EXPECT_EQ(0.0, audio_mixer()->GetVolumePercent()); 157 EXPECT_TRUE(audio_mixer()->IsMuted()); 158} 159 160IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUnmuteFromZero) { 161 // Setting to 0% 162 audio_mixer()->SetVolumePercent(0.0); 163 164 VolumeUnmute(); 165 EXPECT_EQ(4.0 /* kDefaultUnmuteVolumePercent in audio_handler.cc */, 166 audio_mixer()->GetVolumePercent()); 167 EXPECT_FALSE(audio_mixer()->IsMuted()); 168} 169 170IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) { 171 // Setting to almost max 172 audio_mixer()->SetVolumePercent(99.0); 173 174 VolumeUp(); 175 EXPECT_DOUBLE_EQ(100.0, audio_mixer()->GetVolumePercent()); 176 VolumeUp(); 177 EXPECT_DOUBLE_EQ(100.0, audio_mixer()->GetVolumePercent()); 178 VolumeDown(); 179 EXPECT_GT(100.0, audio_mixer()->GetVolumePercent()); 180} 181 182IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) { 183 ASSERT_FALSE(audio_mixer()->IsMuted()); 184 double initial_volume = audio_mixer()->GetVolumePercent(); 185 186 VolumeMute(); 187 EXPECT_TRUE(audio_mixer()->IsMuted()); 188 189 // Further mute buttons doesn't have effects. 190 VolumeMute(); 191 EXPECT_TRUE(audio_mixer()->IsMuted()); 192 193 // Right after the volume up after set_mute recovers to original volume. 194 VolumeUp(); 195 EXPECT_FALSE(audio_mixer()->IsMuted()); 196 EXPECT_DOUBLE_EQ(initial_volume, audio_mixer()->GetVolumePercent()); 197 198 VolumeMute(); 199 // After the volume down, the volume goes down to zero explicitly. 200 VolumeDown(); 201 EXPECT_TRUE(audio_mixer()->IsMuted()); 202 EXPECT_DOUBLE_EQ(0.0, audio_mixer()->GetVolumePercent()); 203 204 // Thus, further VolumeUp doesn't recover the volume, it's just slightly 205 // bigger than 0. 206 VolumeUp(); 207 EXPECT_LT(0.0, audio_mixer()->GetVolumePercent()); 208 EXPECT_GT(initial_volume, audio_mixer()->GetVolumePercent()); 209} 210 211} // namespace 212