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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webcodecs/codec_pressure_gauge.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "build/build_config.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/modules/webcodecs/reclaimable_codec.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
using testing::_;
namespace blink {
namespace {
constexpr size_t kTestPressureThreshold = 5;
using MockPressureChangeCallback =
base::MockCallback<CodecPressureGauge::PressureThresholdChangedCallback>;
} // namespace
class CodecPressureGaugeTest
: public testing::TestWithParam<ReclaimableCodec::CodecType> {
public:
using RegistrationResult = CodecPressureGauge::RegistrationResult;
CodecPressureGaugeTest() {
PressureGauge().set_pressure_threshold_for_testing(kTestPressureThreshold);
}
~CodecPressureGaugeTest() override = default;
CodecPressureGauge& PressureGauge() {
return CodecPressureGauge::GetInstance(GetParam());
}
};
TEST_P(CodecPressureGaugeTest, DefaultState) {
// Sanity check.
EXPECT_EQ(0u, PressureGauge().global_pressure_for_testing());
EXPECT_FALSE(PressureGauge().is_global_pressure_exceeded_for_testing());
}
TEST_P(CodecPressureGaugeTest, GaugeIsSharedForDecodersEncoders) {
// Sanity check.
bool gauge_is_shared =
&CodecPressureGauge::GetInstance(ReclaimableCodec::CodecType::kDecoder) ==
&CodecPressureGauge::GetInstance(ReclaimableCodec::CodecType::kEncoder);
#if BUILDFLAG(IS_WIN)
EXPECT_FALSE(gauge_is_shared);
#else
EXPECT_TRUE(gauge_is_shared);
#endif
}
TEST_P(CodecPressureGaugeTest, RegisterUnregisterCallbacks) {
MockPressureChangeCallback callback;
RegistrationResult result_a =
PressureGauge().RegisterPressureCallback(callback.Get());
RegistrationResult result_b =
PressureGauge().RegisterPressureCallback(callback.Get());
// Callbacks should have different IDs.
EXPECT_NE(result_a.first, result_b.first);
// We should not have exceeded global pressure.
EXPECT_FALSE(result_a.second);
EXPECT_FALSE(result_b.second);
PressureGauge().UnregisterPressureCallback(result_a.first, 0);
PressureGauge().UnregisterPressureCallback(result_b.first, 0);
}
TEST_P(CodecPressureGaugeTest, IncrementDecrement) {
MockPressureChangeCallback callback;
EXPECT_CALL(callback, Run(_)).Times(0);
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
for (size_t i = 1u; i <= kTestPressureThreshold; ++i) {
PressureGauge().Increment();
EXPECT_EQ(i, PressureGauge().global_pressure_for_testing());
}
for (size_t i = kTestPressureThreshold; i > 0; --i) {
PressureGauge().Decrement();
EXPECT_EQ(i - 1u, PressureGauge().global_pressure_for_testing());
}
// Test cleanup.
PressureGauge().UnregisterPressureCallback(result.first, 0);
}
TEST_P(CodecPressureGaugeTest, UnregisterAllLeftoverPressure) {
MockPressureChangeCallback callback;
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
// Increase pressure up to the threshold.
for (size_t i = 0; i < kTestPressureThreshold; ++i)
PressureGauge().Increment();
EXPECT_EQ(kTestPressureThreshold,
PressureGauge().global_pressure_for_testing());
// Releasing all pressure should reset global pressure.
PressureGauge().UnregisterPressureCallback(result.first,
kTestPressureThreshold);
EXPECT_EQ(0u, PressureGauge().global_pressure_for_testing());
}
TEST_P(CodecPressureGaugeTest, UnregisterPartialLeftoverPressure) {
MockPressureChangeCallback callback;
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
RegistrationResult other_result =
PressureGauge().RegisterPressureCallback(callback.Get());
// Increase pressure up to the threshold.
for (size_t i = 0; i < kTestPressureThreshold; ++i)
PressureGauge().Increment();
constexpr size_t kPartialPressure = 3;
EXPECT_EQ(kTestPressureThreshold,
PressureGauge().global_pressure_for_testing());
// Releasing partial pressure should properly update global pressure.
PressureGauge().UnregisterPressureCallback(result.first, kPartialPressure);
EXPECT_EQ(kTestPressureThreshold - kPartialPressure,
PressureGauge().global_pressure_for_testing());
// Test cleanup
PressureGauge().UnregisterPressureCallback(
other_result.first, PressureGauge().global_pressure_for_testing());
}
TEST_P(CodecPressureGaugeTest, ExceedingThresholdRunsCallbacks) {
MockPressureChangeCallback callback;
MockPressureChangeCallback other_callback;
EXPECT_CALL(callback, Run(true));
EXPECT_CALL(other_callback, Run(true));
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
RegistrationResult other_result =
PressureGauge().RegisterPressureCallback(other_callback.Get());
for (size_t i = 0; i < kTestPressureThreshold; ++i)
PressureGauge().Increment();
// We should be at the limit, but not over it.
EXPECT_FALSE(PressureGauge().is_global_pressure_exceeded_for_testing());
// Pass over the threshold.
PressureGauge().Increment();
EXPECT_TRUE(PressureGauge().is_global_pressure_exceeded_for_testing());
testing::Mock::VerifyAndClearExpectations(&callback);
testing::Mock::VerifyAndClearExpectations(&other_callback);
// Test cleanup
PressureGauge().UnregisterPressureCallback(
result.first, PressureGauge().global_pressure_for_testing());
PressureGauge().UnregisterPressureCallback(other_result.first, 0);
}
TEST_P(CodecPressureGaugeTest, PassingUnderThresholdRunsCallbacks_Decrement) {
MockPressureChangeCallback callback;
MockPressureChangeCallback other_callback;
EXPECT_CALL(other_callback, Run(false));
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
// Make sure we are above the threshold.
for (size_t i = 0; i < kTestPressureThreshold + 1; ++i)
PressureGauge().Increment();
RegistrationResult other_result =
PressureGauge().RegisterPressureCallback(other_callback.Get());
// Make the results match the expected global threshold.
EXPECT_TRUE(PressureGauge().is_global_pressure_exceeded_for_testing());
EXPECT_TRUE(other_result.second);
// Reset expectations.
testing::Mock::VerifyAndClearExpectations(&callback);
EXPECT_CALL(callback, Run(false));
// Pass under the global threshold via a call to Decrement().
PressureGauge().Decrement();
EXPECT_FALSE(PressureGauge().is_global_pressure_exceeded_for_testing());
testing::Mock::VerifyAndClearExpectations(&other_callback);
// Test cleanup
PressureGauge().UnregisterPressureCallback(
result.first, PressureGauge().global_pressure_for_testing());
PressureGauge().UnregisterPressureCallback(other_result.first, 0);
}
TEST_P(CodecPressureGaugeTest, PassingUnderThresholdRunsCallbacks_Unregister) {
MockPressureChangeCallback callback;
MockPressureChangeCallback other_callback;
EXPECT_CALL(other_callback, Run(false));
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
// Make sure we are above the threshold.
for (size_t i = 0; i < kTestPressureThreshold + 1; ++i)
PressureGauge().Increment();
RegistrationResult other_result =
PressureGauge().RegisterPressureCallback(other_callback.Get());
// Make the results match the expected global threshold.
EXPECT_TRUE(PressureGauge().is_global_pressure_exceeded_for_testing());
EXPECT_TRUE(other_result.second);
// Pass under the global threshold unregistering.
constexpr size_t kPartialPressure = 3;
PressureGauge().UnregisterPressureCallback(result.first, kPartialPressure);
EXPECT_FALSE(PressureGauge().is_global_pressure_exceeded_for_testing());
testing::Mock::VerifyAndClearExpectations(&other_callback);
// Test cleanup.
PressureGauge().UnregisterPressureCallback(
other_result.first, PressureGauge().global_pressure_for_testing());
}
TEST_P(CodecPressureGaugeTest, RepeatedlyCrossingThresholds) {
size_t number_of_exceeds_calls = 0u;
size_t number_of_receeds_calls = 0u;
auto pressure_cb = [&](bool pressure_exceeded) {
if (pressure_exceeded)
++number_of_exceeds_calls;
else
++number_of_receeds_calls;
};
RegistrationResult result = PressureGauge().RegisterPressureCallback(
base::BindLambdaForTesting(pressure_cb));
// Make sure we at the threshold.
for (size_t i = 0; i < kTestPressureThreshold; ++i)
PressureGauge().Increment();
EXPECT_EQ(0u, number_of_exceeds_calls);
EXPECT_EQ(0u, number_of_receeds_calls);
constexpr size_t kNumberOfCrossings = 3;
// Go back and forth across the threshold.
for (size_t i = 1; i <= kNumberOfCrossings; ++i) {
PressureGauge().Increment();
EXPECT_EQ(i, number_of_exceeds_calls);
PressureGauge().Decrement();
EXPECT_EQ(i, number_of_receeds_calls);
}
// Test cleanup.
PressureGauge().UnregisterPressureCallback(
result.first, PressureGauge().global_pressure_for_testing());
}
TEST_P(CodecPressureGaugeTest, ZeroThreshold) {
constexpr size_t kZeroPressureThreshold = 0u;
PressureGauge().set_pressure_threshold_for_testing(kZeroPressureThreshold);
MockPressureChangeCallback callback;
EXPECT_CALL(callback, Run(true));
EXPECT_CALL(callback, Run(false));
RegistrationResult result =
PressureGauge().RegisterPressureCallback(callback.Get());
PressureGauge().Increment();
PressureGauge().Decrement();
// Test cleanup.
PressureGauge().UnregisterPressureCallback(result.first, 0);
PressureGauge().set_pressure_threshold_for_testing(kTestPressureThreshold);
}
INSTANTIATE_TEST_SUITE_P(
,
CodecPressureGaugeTest,
testing::Values(ReclaimableCodec::CodecType::kDecoder,
ReclaimableCodec::CodecType::kEncoder));
} // namespace blink
|