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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/telemetry_extension/management/telemetry_management_service_ash.h"
#include <algorithm>
#include <cstdint>
#include <utility>
#include "base/functional/bind.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chromeos/ash/components/audio/audio_device.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
constexpr uint64_t kFakeAudioOutputNodeId =
0x100000001; // From `FakeCrasAudioClient`.
constexpr uint64_t kFakeAudioInputNodeId =
0x100000002; // From `FakeCrasAudioClient`.
} // namespace
class TelemetryManagementServiceAshTest : public testing::Test {
public:
void SetUp() override {}
void TearDown() override {}
crosapi::mojom::TelemetryManagementServiceProxy* management_service() const {
return remote_management_service_.get();
}
CrasAudioHandler& cras_audio_handler() { return cras_audio_handler_.Get(); }
protected:
uint64_t GetFakeAudioNodeId() {
uint64_t nonexistent_node_id = 0;
AudioDeviceList devices;
cras_audio_handler().GetAudioDevices(&devices);
for (const auto& device : devices) {
nonexistent_node_id = std::max(nonexistent_node_id, device.id) + 1;
}
return nonexistent_node_id;
}
private:
base::test::TaskEnvironment task_environment_;
// ScopedCrasAudioHandlerForTesting is a helper class that initializes
// the `CrasAudioHandler` in its constructor with cleanup in its destructor.
ScopedCrasAudioHandlerForTesting cras_audio_handler_;
mojo::Remote<crosapi::mojom::TelemetryManagementService>
remote_management_service_;
std::unique_ptr<crosapi::mojom::TelemetryManagementService>
management_service_{TelemetryManagementServiceAsh::Factory::Create(
remote_management_service_.BindNewPipeAndPassReceiver())};
};
// Tests that AudioSetGain forwards requests to CrasAudioHandler to set the
// audio gain correctly.
TEST_F(TelemetryManagementServiceAshTest, AudioSetGainSuccess) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioInputNodeId, 10);
constexpr int32_t expected_gain = 60;
base::test::TestFuture<bool> future;
management_service()->SetAudioGain(kFakeAudioInputNodeId, expected_gain,
future.GetCallback());
EXPECT_TRUE(future.Get());
EXPECT_EQ(
cras_audio_handler().GetInputGainPercentForDevice(kFakeAudioInputNodeId),
expected_gain);
}
// Tests that AudioSetGain sets gain to max (100) when |gain| exceeds max.
TEST_F(TelemetryManagementServiceAshTest, AudioSetGainInvalidGainAboveMax) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioInputNodeId, 10);
base::test::TestFuture<bool> future;
management_service()->SetAudioGain(kFakeAudioInputNodeId, 999,
future.GetCallback());
EXPECT_TRUE(future.Get());
EXPECT_EQ(
cras_audio_handler().GetInputGainPercentForDevice(kFakeAudioInputNodeId),
100);
}
// Tests that AudioSetGain sets gain to min (0) when |gain| is below min.
TEST_F(TelemetryManagementServiceAshTest, AudioSetGainInvalidGainBelowMin) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioInputNodeId, 10);
base::test::TestFuture<bool> future;
management_service()->SetAudioGain(kFakeAudioInputNodeId, -100,
future.GetCallback());
EXPECT_TRUE(future.Get());
EXPECT_EQ(
cras_audio_handler().GetInputGainPercentForDevice(kFakeAudioInputNodeId),
0);
}
// Tests that AudioSetGain returns false when |node_id| is invalid.
TEST_F(TelemetryManagementServiceAshTest, AudioSetGainInvalidNodeId) {
base::test::TestFuture<bool> future;
management_service()->SetAudioGain(GetFakeAudioNodeId(), 60,
future.GetCallback());
EXPECT_FALSE(future.Get());
}
// Tests that AudioSetGain return false if the audio node is an output node.
TEST_F(TelemetryManagementServiceAshTest, AudioSetGainWithOutputNode) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioOutputNodeId,
10);
base::test::TestFuture<bool> future;
management_service()->SetAudioGain(kFakeAudioOutputNodeId, 60,
future.GetCallback());
EXPECT_FALSE(future.Get());
EXPECT_EQ(
cras_audio_handler().GetInputGainPercentForDevice(kFakeAudioOutputNodeId),
10);
}
// Tests that AudioSetVolume forwards requests to CrasAudioHandler to set the
// audio volume correctly.
TEST_F(TelemetryManagementServiceAshTest, AudioSetVolumeSuccess) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioOutputNodeId,
10);
cras_audio_handler().SetMuteForDevice(kFakeAudioOutputNodeId, false);
constexpr int32_t expected_volume = 60;
base::test::TestFuture<bool> future;
management_service()->SetAudioVolume(kFakeAudioOutputNodeId, expected_volume,
true, future.GetCallback());
EXPECT_TRUE(future.Get());
EXPECT_EQ(cras_audio_handler().GetOutputVolumePercentForDevice(
kFakeAudioOutputNodeId),
expected_volume);
EXPECT_TRUE(
cras_audio_handler().IsOutputMutedForDevice(kFakeAudioOutputNodeId));
}
// Tests that AudioSetVolume sets volume to max (100) when |volume| exceeds max.
TEST_F(TelemetryManagementServiceAshTest, AudioSetVolumeInvalidVolumeAboveMax) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioOutputNodeId,
10);
cras_audio_handler().SetMuteForDevice(kFakeAudioOutputNodeId, false);
base::test::TestFuture<bool> future;
management_service()->SetAudioVolume(kFakeAudioOutputNodeId, 999, true,
future.GetCallback());
EXPECT_TRUE(future.Get());
EXPECT_EQ(cras_audio_handler().GetOutputVolumePercentForDevice(
kFakeAudioOutputNodeId),
100);
EXPECT_TRUE(
cras_audio_handler().IsOutputMutedForDevice(kFakeAudioOutputNodeId));
}
// Tests that AudioSetVolume sets volume to min (0) when |volume| is below min.
TEST_F(TelemetryManagementServiceAshTest, AudioSetVolumeInvalidVolumeBelowMin) {
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioOutputNodeId,
10);
cras_audio_handler().SetMuteForDevice(kFakeAudioOutputNodeId, false);
base::test::TestFuture<bool> future;
management_service()->SetAudioVolume(kFakeAudioOutputNodeId, -100, true,
future.GetCallback());
EXPECT_TRUE(future.Get());
EXPECT_EQ(cras_audio_handler().GetOutputVolumePercentForDevice(
kFakeAudioOutputNodeId),
0);
EXPECT_TRUE(
cras_audio_handler().IsOutputMutedForDevice(kFakeAudioOutputNodeId));
}
// Tests that AudioSetVolume returns false when |node_id| is invalid.
TEST_F(TelemetryManagementServiceAshTest, AudioSetVolumeInvalidNodeId) {
base::test::TestFuture<bool> future;
management_service()->SetAudioVolume(GetFakeAudioNodeId(), 60, false,
future.GetCallback());
EXPECT_FALSE(future.Get());
}
// Tests that AudioSetVolume return false if the audio node is an input node.
TEST_F(TelemetryManagementServiceAshTest, AudioSetVolumeNoUnmuteInput) {
// Input mute state is only recorded in CRAS when the input node is active.
cras_audio_handler().SetActiveInputNodes({kFakeAudioInputNodeId});
// Set to an arbitrary value first.
cras_audio_handler().SetVolumeGainPercentForDevice(kFakeAudioInputNodeId, 10);
cras_audio_handler().SetMuteForDevice(kFakeAudioInputNodeId, true);
EXPECT_TRUE(
cras_audio_handler().IsInputMutedForDevice(kFakeAudioInputNodeId));
base::test::TestFuture<bool> future;
management_service()->SetAudioVolume(kFakeAudioInputNodeId, 60, false,
future.GetCallback());
EXPECT_FALSE(future.Get());
EXPECT_EQ(cras_audio_handler().GetOutputVolumePercentForDevice(
kFakeAudioInputNodeId),
10);
EXPECT_TRUE(
cras_audio_handler().IsInputMutedForDevice(kFakeAudioInputNodeId));
}
} // namespace ash
|