File: volume_controller_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (279 lines) | stat: -rw-r--r-- 8,969 bytes parent folder | download | duplicates (6)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <map>
#include <memory>
#include <string_view>

#include "ash/constants/ash_switches.h"
#include "ash/shell.h"
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "chromeos/ash/components/audio/public/cpp/sounds/sounds_manager.h"
#include "chromeos/ash/components/audio/sounds.h"
#include "content/public/test/browser_test.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/test/event_generator.h"

namespace {

using ::ash::AccessibilityManager;

class SoundsManagerTestImpl : public audio::SoundsManager {
 public:
  SoundsManagerTestImpl() = default;

  SoundsManagerTestImpl(const SoundsManagerTestImpl&) = delete;
  SoundsManagerTestImpl& operator=(const SoundsManagerTestImpl&) = delete;

  ~SoundsManagerTestImpl() override = default;

  bool Initialize(SoundKey key,
                  std::string_view /* data */,
                  media::AudioCodec codec) override {
    is_sound_initialized_[key] = true;
    return true;
  }

  bool Play(SoundKey key) override {
    ++num_play_requests_[key];
    return true;
  }

  bool Stop(SoundKey key) override { return true; }

  base::TimeDelta GetDuration(SoundKey /* key */) override {
    return base::TimeDelta();
  }

  bool is_sound_initialized(SoundKey key) { return is_sound_initialized_[key]; }

  int num_play_requests(SoundKey key) { return num_play_requests_[key]; }

 private:
  std::map<SoundKey, bool> is_sound_initialized_;
  std::map<SoundKey, int> num_play_requests_;
};

class VolumeControllerTest : public InProcessBrowserTest {
 public:
  VolumeControllerTest() = default;

  VolumeControllerTest(const VolumeControllerTest&) = delete;
  VolumeControllerTest& operator=(const VolumeControllerTest&) = delete;

  ~VolumeControllerTest() override = default;

  void SetUpOnMainThread() override {
    audio_handler_ = ash::CrasAudioHandler::Get();
  }

  void VolumeUp() {
    ui::test::EventGenerator(ash::Shell::GetPrimaryRootWindow())
        .PressKey(ui::VKEY_VOLUME_UP, ui::EF_NONE);
  }

  void VolumeDown() {
    ui::test::EventGenerator(ash::Shell::GetPrimaryRootWindow())
        .PressKey(ui::VKEY_VOLUME_DOWN, ui::EF_NONE);
  }

  void VolumeMute() {
    ui::test::EventGenerator(ash::Shell::GetPrimaryRootWindow())
        .PressKey(ui::VKEY_VOLUME_MUTE, ui::EF_NONE);
  }

 protected:
  raw_ptr<ash::CrasAudioHandler, DanglingUntriaged>
      audio_handler_;  // Not owned.
};

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpAndDown) {
  // Set initial value as 50%
  const int kInitVolume = 50;
  audio_handler_->SetOutputVolumePercent(kInitVolume);
  int current_volume = audio_handler_->GetOutputVolumePercent();
  EXPECT_EQ(current_volume, kInitVolume);

  current_volume = audio_handler_->GetOutputVolumePercent();
  VolumeUp();
  // number_of_volume_step = 25 mean we split volume into 25 level,
  // and The volume goes up one level each for VolumeUp/VolumeDown event.
  // For initial value is 48 - 51 volume will increase to 52,
  // because 48 - 51 share same level,
  // VolumeUp will increase to the min volume of next level, which is 52
  // Original behavior will set volume to 54
  EXPECT_LT(current_volume, audio_handler_->GetOutputVolumePercent());

  current_volume = audio_handler_->GetOutputVolumePercent();
  VolumeDown();
  // VolumeUp will decrease to the min volume of previous level, which is 48
  // Original behavior will set volume to 50
  EXPECT_GT(current_volume, audio_handler_->GetOutputVolumePercent());

  current_volume = audio_handler_->GetOutputVolumePercent();
  VolumeDown();
  EXPECT_GT(current_volume, audio_handler_->GetOutputVolumePercent());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeDownToZero) {
  // Setting to very small volume.
  audio_handler_->SetOutputVolumePercent(1);

  VolumeDown();
  EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
  VolumeDown();
  EXPECT_EQ(0, audio_handler_->GetOutputVolumePercent());
  VolumeUp();
  EXPECT_LT(0, audio_handler_->GetOutputVolumePercent());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, VolumeUpTo100) {
  // Setting to almost max
  audio_handler_->SetOutputVolumePercent(99);

  VolumeUp();
  EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
  VolumeUp();
  EXPECT_EQ(100, audio_handler_->GetOutputVolumePercent());
  VolumeDown();
  EXPECT_GT(100, audio_handler_->GetOutputVolumePercent());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerTest, Mutes) {
  ASSERT_FALSE(audio_handler_->IsOutputMuted());
  const int initial_volume = audio_handler_->GetOutputVolumePercent();

  VolumeMute();
  EXPECT_TRUE(audio_handler_->IsOutputMuted());

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

  // Right after the volume up after set_mute recovers to original volume.
  // Press volume up key will increase the volume from the original volume.
  VolumeUp();
  EXPECT_FALSE(audio_handler_->IsOutputMuted());
    EXPECT_LT(initial_volume, audio_handler_->GetOutputVolumePercent());

  VolumeMute();
  // After the volume down, press volume down key will decrease the volume from
  // the original volume while the volume is still muted.
  VolumeDown();
    EXPECT_TRUE(audio_handler_->IsOutputMuted());
    EXPECT_EQ(initial_volume, audio_handler_->GetOutputVolumePercent());

    // Thus, further VolumeUp will increase the volume.
    VolumeUp();
    EXPECT_LT(initial_volume, audio_handler_->GetOutputVolumePercent());
}

class VolumeControllerSoundsTest : public VolumeControllerTest {
 public:
  VolumeControllerSoundsTest() : sounds_manager_(nullptr) {}

  VolumeControllerSoundsTest(const VolumeControllerSoundsTest&) = delete;
  VolumeControllerSoundsTest& operator=(const VolumeControllerSoundsTest&) =
      delete;

  ~VolumeControllerSoundsTest() override = default;

  void SetUpInProcessBrowserTestFixture() override {
    sounds_manager_ = new SoundsManagerTestImpl();
    audio::SoundsManager::InitializeForTesting(sounds_manager_);
  }

  bool is_sound_initialized() const {
    return sounds_manager_->is_sound_initialized(
        static_cast<int>(ash::Sound::kVolumeAdjust));
  }

  int num_play_requests() const {
    return sounds_manager_->num_play_requests(
        static_cast<int>(ash::Sound::kVolumeAdjust));
  }

 private:
  raw_ptr<SoundsManagerTestImpl, DanglingUntriaged> sounds_manager_;
};

IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, Simple) {
  audio_handler_->SetOutputVolumePercent(50);

  AccessibilityManager::Get()->EnableSpokenFeedback(false);
  VolumeUp();
  VolumeDown();
  EXPECT_EQ(0, num_play_requests());

  AccessibilityManager::Get()->EnableSpokenFeedback(true);
  VolumeUp();
  VolumeDown();
  EXPECT_EQ(2, num_play_requests());
}

IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsTest, EdgeCases) {
  EXPECT_TRUE(is_sound_initialized());
  AccessibilityManager::Get()->EnableSpokenFeedback(true);

  // Check that sound is played on volume up and volume down.
  audio_handler_->SetOutputVolumePercent(50);
  VolumeUp();
  EXPECT_EQ(1, num_play_requests());
  VolumeDown();
  EXPECT_EQ(2, num_play_requests());

  audio_handler_->SetOutputVolumePercent(99);
  VolumeUp();
  EXPECT_EQ(3, num_play_requests());

  audio_handler_->SetOutputVolumePercent(100);
  VolumeUp();
  EXPECT_EQ(3, num_play_requests());

  // Check that sound isn't played when audio is muted.
  audio_handler_->SetOutputVolumePercent(50);
  VolumeMute();
  VolumeDown();
  ASSERT_TRUE(audio_handler_->IsOutputMuted());
  EXPECT_EQ(3, num_play_requests());

  // Check that audio is unmuted and sound is played.
  VolumeUp();
  ASSERT_FALSE(audio_handler_->IsOutputMuted());
  EXPECT_EQ(4, num_play_requests());
}

class VolumeControllerSoundsDisabledTest : public VolumeControllerSoundsTest {
 public:
  VolumeControllerSoundsDisabledTest() = default;

  VolumeControllerSoundsDisabledTest(
      const VolumeControllerSoundsDisabledTest&) = delete;
  VolumeControllerSoundsDisabledTest& operator=(
      const VolumeControllerSoundsDisabledTest&) = delete;

  ~VolumeControllerSoundsDisabledTest() override = default;

  void SetUpCommandLine(base::CommandLine* command_line) override {
    VolumeControllerSoundsTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(ash::switches::kDisableVolumeAdjustSound);
  }
};

IN_PROC_BROWSER_TEST_F(VolumeControllerSoundsDisabledTest, VolumeAdjustSounds) {
  EXPECT_FALSE(is_sound_initialized());

  // Check that sound isn't played on volume up and volume down.
  audio_handler_->SetOutputVolumePercent(50);
  VolumeUp();
  VolumeDown();
  EXPECT_EQ(0, num_play_requests());
}

}  // namespace