volume_controller_browsertest_chromeos.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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 "ash/ash_switches.h" 6#include "base/memory/scoped_ptr.h" 7#include "chrome/browser/browser_process.h" 8#include "chrome/browser/ui/ash/volume_controller_chromeos.h" 9#include "chrome/test/base/in_process_browser_test.h" 10#include "chromeos/audio/cras_audio_handler.h" 11#include "ui/base/accelerators/accelerator.h" 12 13namespace { 14 15class VolumeControllerTest : public InProcessBrowserTest { 16 public: 17 VolumeControllerTest() {} 18 virtual ~VolumeControllerTest() {} 19 20 virtual void SetUpOnMainThread() OVERRIDE { 21 CHECK(ash::switches::UseNewAudioHandler()); 22 volume_controller_.reset(new VolumeController()); 23 audio_handler_ = chromeos::CrasAudioHandler::Get(); 24 } 25 26 protected: 27 void VolumeMute() { 28 volume_controller_->HandleVolumeMute(ui::Accelerator()); 29 } 30 31 void VolumeUp() { 32 volume_controller_->HandleVolumeUp(ui::Accelerator()); 33 } 34 35 void VolumeDown() { 36 volume_controller_->HandleVolumeDown(ui::Accelerator()); 37 } 38 39 chromeos::CrasAudioHandler* audio_handler_; // Not owned. 40 41 private: 42 scoped_ptr<VolumeController> volume_controller_; 43 44 DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest); 45}; 46 47IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) { 48 // Set initial value as 50% 49 const int kInitVolume = 50; 50 audio_handler_->SetOutputVolumePercent(kInitVolume); 51 52 EXPECT_EQ(audio_handler_->GetOutputVolumePercent(), kInitVolume); 53 54 VolumeUp(); 55 EXPECT_LT(kInitVolume, audio_handler_->GetOutputVolumePercent()); 56 VolumeDown(); 57 EXPECT_EQ(kInitVolume, audio_handler_->GetOutputVolumePercent()); 58 VolumeDown(); 59 EXPECT_GT(kInitVolume, audio_handler_->GetOutputVolumePercent()); 60} 61 62IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) { 63 // Setting to very small volume. 64 audio_handler_->SetOutputVolumePercent(1); 65 66 VolumeDown(); 67 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent()); 68 VolumeDown(); 69 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent()); 70 VolumeUp(); 71 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent()); 72} 73 74IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) { 75 // Setting to almost max 76 audio_handler_->SetOutputVolumePercent(99); 77 78 VolumeUp(); 79 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent()); 80 VolumeUp(); 81 EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent()); 82 VolumeDown(); 83 EXPECT_GT(100, audio_handler_->GetOutputVolumePercent()); 84} 85 86IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) { 87 ASSERT_FALSE(audio_handler_->IsOutputMuted()); 88 const int initial_volume = audio_handler_->GetOutputVolumePercent(); 89 90 VolumeMute(); 91 EXPECT_TRUE(audio_handler_->IsOutputMuted()); 92 93 // Further mute buttons doesn't have effects. 94 VolumeMute(); 95 EXPECT_TRUE(audio_handler_->IsOutputMuted()); 96 97 // Right after the volume up after set_mute recovers to original volume. 98 VolumeUp(); 99 EXPECT_FALSE(audio_handler_->IsOutputMuted()); 100 EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent()); 101 102 VolumeMute(); 103 // After the volume down, the volume goes down to zero explicitly. 104 VolumeDown(); 105 EXPECT_TRUE(audio_handler_->IsOutputMuted()); 106 EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent()); 107 108 // Thus, further VolumeUp doesn't recover the volume, it's just slightly 109 // bigger than 0. 110 VolumeUp(); 111 EXPECT_LT(0, audio_handler_->GetOutputVolumePercent()); 112 EXPECT_GT(initial_volume, audio_handler_->GetOutputVolumePercent()); 113} 114 115} // namespace 116