File: shell_audio_controller_chromeos_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (135 lines) | stat: -rw-r--r-- 5,145 bytes parent folder | download | duplicates (9)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/shell/browser/shell_audio_controller_chromeos.h"

#include <stdint.h>

#include <memory>

#include "base/memory/ptr_util.h"
#include "chromeos/ash/components/audio/audio_device.h"
#include "chromeos/ash/components/audio/audio_devices_pref_handler.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "chromeos/ash/components/dbus/audio/audio_node.h"
#include "chromeos/ash/components/dbus/audio/fake_cras_audio_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

using ::ash::AudioDevice;
using ::ash::AudioDeviceType;
using ::ash::AudioNode;
using ::ash::AudioNodeList;
using ::ash::CrasAudioHandler;

class ShellAudioControllerTest : public testing::Test {
 public:
  ShellAudioControllerTest() : next_node_id_(1) {
    ash::CrasAudioClient::InitializeFake();
    audio_client()->SetAudioNodesForTesting(AudioNodeList());
    CrasAudioHandler::InitializeForTesting();

    controller_ = std::make_unique<ShellAudioController>();
  }

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

  ~ShellAudioControllerTest() override {
    controller_.reset();
    CrasAudioHandler::Shutdown();
    ash::CrasAudioClient::Shutdown();
  }

 protected:
  // Fills a AudioNode for use by tests.
  AudioNode CreateNode(AudioDeviceType type) {
    AudioNode node;
    node.is_input = type == AudioDeviceType::kMic ||
                    type == AudioDeviceType::kInternalMic ||
                    type == AudioDeviceType::kKeyboardMic;
    node.id = next_node_id_++;
    node.type = AudioDevice::GetTypeString(type);
    return node;
  }

  // Changes the active state of the node with |id| in |nodes|.
  void SetNodeActive(AudioNodeList* nodes, uint64_t id, bool active) {
    for (AudioNodeList::iterator it = nodes->begin();
         it != nodes->end(); ++it) {
      if (it->id == id) {
        it->active = active;
        return;
      }
    }
    ASSERT_TRUE(false) << "Didn't find ID " << id;
  }

  ash::FakeCrasAudioClient* audio_client() {
    return ash::FakeCrasAudioClient::Get();
  }

  CrasAudioHandler* audio_handler() { return CrasAudioHandler::Get(); }

  std::unique_ptr<ShellAudioController> controller_;

  // Next audio node ID to be returned by CreateNode().
  uint64_t next_node_id_;
};

// Tests that higher-priority devices are activated as soon as they're
// connected.
TEST_F(ShellAudioControllerTest, SelectBestDevices) {
  AudioNode internal_speaker = CreateNode(AudioDeviceType::kInternalSpeaker);
  AudioNode internal_mic = CreateNode(AudioDeviceType::kInternalMic);
  AudioNode headphone = CreateNode(AudioDeviceType::kHeadphone);
  AudioNode external_mic = CreateNode(AudioDeviceType::kMic);

  // AudioDevice gives the headphone jack a higher priority than the internal
  // speaker and an external mic a higher priority than the internal mic, so we
  // should start out favoring headphones and the external mic.
  AudioNodeList all_nodes;
  all_nodes.push_back(internal_speaker);
  all_nodes.push_back(internal_mic);
  all_nodes.push_back(headphone);
  all_nodes.push_back(external_mic);
  audio_client()->SetAudioNodesAndNotifyObserversForTesting(all_nodes);
  EXPECT_EQ(headphone.id, audio_handler()->GetPrimaryActiveOutputNode());
  EXPECT_EQ(external_mic.id, audio_handler()->GetPrimaryActiveInputNode());

  // Unplug the headphones and mic and check that we switch to the internal
  // devices.
  AudioNodeList internal_nodes;
  internal_nodes.push_back(internal_speaker);
  internal_nodes.push_back(internal_mic);
  audio_client()->SetAudioNodesAndNotifyObserversForTesting(internal_nodes);
  EXPECT_EQ(internal_speaker.id, audio_handler()->GetPrimaryActiveOutputNode());
  EXPECT_EQ(internal_mic.id, audio_handler()->GetPrimaryActiveInputNode());

  // Switch back to the external devices. Mark the previously-activated internal
  // devices as being active so CrasAudioHandler doesn't complain.
  SetNodeActive(&all_nodes, internal_speaker.id, true);
  SetNodeActive(&all_nodes, internal_mic.id, true);
  audio_client()->SetAudioNodesAndNotifyObserversForTesting(all_nodes);
  EXPECT_EQ(headphone.id, audio_handler()->GetPrimaryActiveOutputNode());
  EXPECT_EQ(external_mic.id, audio_handler()->GetPrimaryActiveInputNode());
}

// Tests that active audio devices are unmuted and have correct initial volume.
TEST_F(ShellAudioControllerTest, InitialVolume) {
  AudioNodeList nodes;
  nodes.push_back(CreateNode(AudioDeviceType::kInternalSpeaker));
  nodes.push_back(CreateNode(AudioDeviceType::kInternalMic));
  audio_client()->SetAudioNodesAndNotifyObserversForTesting(nodes);

  EXPECT_FALSE(audio_handler()->IsOutputMuted());
  EXPECT_FALSE(audio_handler()->IsInputMuted());
  EXPECT_EQ(ash::AudioDevicesPrefHandler::kDefaultOutputVolumePercent,
            audio_handler()->GetOutputVolumePercent());

  EXPECT_EQ(75.0, audio_handler()->GetInputGainPercent());
}

}  // namespace extensions