summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/ash/volume_controller_browsertest_chromeos.cc
blob: 648f343b10a01d40077b35ac4348adef17a99f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/ash/volume_controller_chromeos.h"

#include "chrome/browser/chromeos/audio/audio_handler.h"
#include "chrome/browser/chromeos/audio/audio_mixer.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "ui/base/accelerators/accelerator.h"

namespace {

// Default volume as a percentage in the range [0.0, 100.0].
const double kDefaultVolumePercent = 75.0;

class MockAudioMixer : public chromeos::AudioMixer {
 public:
  MockAudioMixer()
      : volume_(kDefaultVolumePercent),
        is_muted_(false) {
  }

  virtual void Init() OVERRIDE {
  }

  virtual double GetVolumePercent() OVERRIDE {
    return volume_;
  }

  virtual void SetVolumePercent(double percent) OVERRIDE {
    volume_ = percent;
  }

  virtual bool IsMuted() OVERRIDE {
    return is_muted_;
  }

  virtual void SetMuted(bool mute) OVERRIDE {
    is_muted_ = mute;
  }

  virtual bool IsMuteLocked() OVERRIDE {
    return is_mute_locked_;
  }

  virtual void SetMuteLocked(bool locked) OVERRIDE {
    is_mute_locked_ = locked;
  }

  virtual bool IsCaptureMuted() OVERRIDE {
    return is_capture_muted_;
  }

  virtual void SetCaptureMuted(bool mute) OVERRIDE {
    is_capture_muted_ = mute;
  }

  virtual bool IsCaptureMuteLocked() OVERRIDE {
    return is_capture_mute_locked_;
  }

  virtual void SetCaptureMuteLocked(bool locked) OVERRIDE {
    is_capture_mute_locked_ = locked;
  }

 private:
  double volume_;
  bool is_muted_;
  bool is_mute_locked_;
  bool is_capture_muted_;
  bool is_capture_mute_locked_;

  DISALLOW_COPY_AND_ASSIGN(MockAudioMixer);
};

// This class has to be browsertest because AudioHandler uses prefs_service.
class VolumeControllerTest : public InProcessBrowserTest {
 public:
  VolumeControllerTest() {}

  virtual void SetUpOnMainThread() OVERRIDE {
    // First we should shutdown the default audio handler.
    chromeos::AudioHandler::Shutdown();
    audio_mixer_ = new MockAudioMixer;
    chromeos::AudioHandler::InitializeForTesting(audio_mixer_);
  }

  virtual void CleanUpOnMainThread() OVERRIDE {
    chromeos::AudioHandler::Shutdown();
    audio_mixer_ = NULL;
  }

 protected:
  void SetVolumePercent(double percent) {
    volume_controller_.SetVolumePercent(percent);
  }

  void VolumeMute() {
    volume_controller_.HandleVolumeMute(ui::Accelerator());
  }

  void VolumeUnmute() {
    volume_controller_.SetAudioMuted(false);
  }

  void VolumeUp() {
    volume_controller_.HandleVolumeUp(ui::Accelerator());
  }

  void VolumeDown() {
    volume_controller_.HandleVolumeDown(ui::Accelerator());
  }

  MockAudioMixer* audio_mixer() { return audio_mixer_; }

 private:
  VolumeController volume_controller_;
  MockAudioMixer* audio_mixer_;

  DISALLOW_COPY_AND_ASSIGN(VolumeControllerTest);
};

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) {
  // Set initial value as 50%
  audio_mixer()->SetVolumePercent(50.0);

  double initial_volume = audio_mixer()->GetVolumePercent();

  VolumeUp();
  EXPECT_LT(initial_volume, audio_mixer()->GetVolumePercent());
  VolumeDown();
  EXPECT_DOUBLE_EQ(initial_volume, audio_mixer()->GetVolumePercent());
  VolumeDown();
  EXPECT_GT(initial_volume, audio_mixer()->GetVolumePercent());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) {
  // Setting to very small
  audio_mixer()->SetVolumePercent(0.1);

  VolumeDown();
  EXPECT_DOUBLE_EQ(0.0, audio_mixer()->GetVolumePercent());
  VolumeDown();
  EXPECT_DOUBLE_EQ(0.0, audio_mixer()->GetVolumePercent());
  VolumeUp();
  EXPECT_LT(0.0, audio_mixer()->GetVolumePercent());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeAutoMute) {
  // Setting to very small

  // kMuteThresholdPercent = 0.1 in audio_handler.cc.
  SetVolumePercent(0.1);
  EXPECT_EQ(0.0, audio_mixer()->GetVolumePercent());
  EXPECT_TRUE(audio_mixer()->IsMuted());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUnmuteFromZero) {
  // Setting to 0%
  audio_mixer()->SetVolumePercent(0.0);

  VolumeUnmute();
  EXPECT_EQ(4.0 /* kDefaultUnmuteVolumePercent in audio_handler.cc */,
            audio_mixer()->GetVolumePercent());
  EXPECT_FALSE(audio_mixer()->IsMuted());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) {
  // Setting to almost max
  audio_mixer()->SetVolumePercent(99.0);

  VolumeUp();
  EXPECT_DOUBLE_EQ(100.0, audio_mixer()->GetVolumePercent());
  VolumeUp();
  EXPECT_DOUBLE_EQ(100.0, audio_mixer()->GetVolumePercent());
  VolumeDown();
  EXPECT_GT(100.0, audio_mixer()->GetVolumePercent());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) {
  ASSERT_FALSE(audio_mixer()->IsMuted());
  double initial_volume = audio_mixer()->GetVolumePercent();

  VolumeMute();
  EXPECT_TRUE(audio_mixer()->IsMuted());

  // Further mute buttons doesn't have effects.
  VolumeMute();
  EXPECT_TRUE(audio_mixer()->IsMuted());

  // Right after the volume up after set_mute recovers to original volume.
  VolumeUp();
  EXPECT_FALSE(audio_mixer()->IsMuted());
  EXPECT_DOUBLE_EQ(initial_volume, audio_mixer()->GetVolumePercent());

  VolumeMute();
  // After the volume down, the volume goes down to zero explicitly.
  VolumeDown();
  EXPECT_TRUE(audio_mixer()->IsMuted());
  EXPECT_DOUBLE_EQ(0.0, audio_mixer()->GetVolumePercent());

  // Thus, further VolumeUp doesn't recover the volume, it's just slightly
  // bigger than 0.
  VolumeUp();
  EXPECT_LT(0.0, audio_mixer()->GetVolumePercent());
  EXPECT_GT(initial_volume, audio_mixer()->GetVolumePercent());
}

}  // namespace