gh-13439: Add tests coverage for boosts (gh-13977)

This commit is contained in:
mr. m
2026-06-01 11:24:45 +02:00
committed by GitHub
parent 64e2e49a00
commit 45075e2fbc
24 changed files with 1312 additions and 7 deletions

View File

@@ -0,0 +1,104 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/nsZenBoostsBackend.h"
using zen::detail::AccentCacheSize;
using zen::detail::EnsureCachedAccent;
using zen::detail::IsAccentCached;
using zen::detail::ResetAccentCache;
namespace {
class ZenBoostsAccentCache : public ::testing::Test {
protected:
void SetUp() override { ResetAccentCache(); }
void TearDown() override { ResetAccentCache(); }
};
constexpr nscolor kAccentA = NS_RGBA(80, 120, 200, 200);
constexpr nscolor kAccentB = NS_RGBA(200, 80, 80, 200);
constexpr nscolor kAccentC = NS_RGBA(80, 200, 120, 200);
constexpr nscolor kAccentD = NS_RGBA(200, 200, 80, 200);
constexpr nscolor kAccentE = NS_RGBA(120, 80, 200, 200);
} // namespace
TEST_F(ZenBoostsAccentCache, SizeIsAtLeastFour) {
EXPECT_GE(AccentCacheSize(), 4u);
}
TEST_F(ZenBoostsAccentCache, EmptyAfterReset) {
EnsureCachedAccent(kAccentA, 0.0f);
ResetAccentCache();
EXPECT_FALSE(IsAccentCached(kAccentA, 0.0f));
}
TEST_F(ZenBoostsAccentCache, SameKeyIsCachedAfterEnsure) {
EXPECT_FALSE(IsAccentCached(kAccentA, 0.0f));
EnsureCachedAccent(kAccentA, 0.0f);
EXPECT_TRUE(IsAccentCached(kAccentA, 0.0f));
}
// Keying on accent alone would silently serve a stale complementary accent
// when the rotation changes.
TEST_F(ZenBoostsAccentCache, DifferentRotationOccupiesDistinctEntry) {
EnsureCachedAccent(kAccentA, 0.0f);
EnsureCachedAccent(kAccentA, 90.0f);
EXPECT_TRUE(IsAccentCached(kAccentA, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentA, 90.0f));
}
TEST_F(ZenBoostsAccentCache, DifferentAccentOccupiesDistinctEntry) {
EnsureCachedAccent(kAccentA, 30.0f);
EnsureCachedAccent(kAccentB, 30.0f);
EXPECT_TRUE(IsAccentCached(kAccentA, 30.0f));
EXPECT_TRUE(IsAccentCached(kAccentB, 30.0f));
}
TEST_F(ZenBoostsAccentCache, RoundRobinEvictsOldestEntry) {
ASSERT_EQ(AccentCacheSize(), 4u);
EnsureCachedAccent(kAccentA, 0.0f);
EnsureCachedAccent(kAccentB, 0.0f);
EnsureCachedAccent(kAccentC, 0.0f);
EnsureCachedAccent(kAccentD, 0.0f);
EXPECT_TRUE(IsAccentCached(kAccentA, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentB, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentC, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentD, 0.0f));
EnsureCachedAccent(kAccentE, 0.0f);
EXPECT_FALSE(IsAccentCached(kAccentA, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentB, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentC, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentD, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentE, 0.0f));
}
// A cache hit must not consume a fresh slot, otherwise repeated paints with
// the same accent would evict their own neighbours.
TEST_F(ZenBoostsAccentCache, RepeatEnsureDoesNotChurnTheCache) {
ASSERT_EQ(AccentCacheSize(), 4u);
EnsureCachedAccent(kAccentA, 0.0f);
EnsureCachedAccent(kAccentB, 0.0f);
EnsureCachedAccent(kAccentC, 0.0f);
for (int i = 0; i < 16; ++i) {
EnsureCachedAccent(kAccentA, 0.0f);
}
EXPECT_TRUE(IsAccentCached(kAccentA, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentB, 0.0f));
EXPECT_TRUE(IsAccentCached(kAccentC, 0.0f));
EnsureCachedAccent(kAccentD, 0.0f);
EnsureCachedAccent(kAccentE, 0.0f);
EXPECT_FALSE(IsAccentCached(kAccentA, 0.0f));
}

View File

@@ -0,0 +1,42 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "mozilla/nsZenBoostsBackend.h"
using zen::nsZenBoostsBackend;
namespace {
const nscolor kResolveColors[] = {
NS_RGBA(0, 0, 0, 255), NS_RGBA(255, 255, 255, 255),
NS_RGBA(128, 128, 128, 255), NS_RGBA(255, 0, 0, 255),
NS_RGBA(0, 255, 0, 255), NS_RGBA(0, 0, 255, 255),
NS_RGBA(40, 44, 52, 255), NS_RGBA(248, 248, 248, 255),
NS_RGBA(20, 22, 28, 255), NS_RGBA(80, 80, 80, 200),
NS_RGBA(240, 17, 99, 1), NS_RGBA(0, 0, 0, 0),
};
} // namespace
// Removing the null-frame guard would crash chrome-process callers that
// legitimately pass nullptr (canvas getComputedStyle, font-palette binding,
// the StyleColor(nscolor)/StyleColor(StyleAbsoluteColor) overloads).
TEST(ZenBoostsResolveStyleColor, NullFrameIsIdentity)
{
for (nscolor c : kResolveColors) {
EXPECT_EQ(nsZenBoostsBackend::ResolveStyleColor(c, nullptr), c);
}
}
TEST(ZenBoostsResolveStyleColor, NullFrameIsIdempotent)
{
for (nscolor c : kResolveColors) {
nscolor once = nsZenBoostsBackend::ResolveStyleColor(c, nullptr);
nscolor twice = nsZenBoostsBackend::ResolveStyleColor(once, nullptr);
EXPECT_EQ(once, c);
EXPECT_EQ(twice, c);
}
}

View File

@@ -3,7 +3,9 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
UNIFIED_SOURCES += [
"TestZenBoostsAccentCache.cpp",
"TestZenBoostsColorFilter.cpp",
"TestZenBoostsResolveStyleColor.cpp",
]
FINAL_LIBRARY = "xul-gtest"

View File

@@ -450,11 +450,9 @@ inline static void GetZenBoostsDataForFrame(const nsIFrame* aFrame,
} // namespace
#ifdef ENABLE_TESTS
namespace detail {
// Thin forwarders that give unit tests access to the pure color math without
// pulling in the singleton / BrowsingContext. They are defined here, after the
// anonymous namespace, so they can reach those file-local implementations.
nsZenAccentOklab PrecomputeAccent(nscolor aAccentColor) {
return zenPrecomputeAccent(aAccentColor);
}
@@ -474,7 +472,33 @@ nscolor InvertColorChannel(nscolor aColor) {
return zenInvertColorChannel(aColor);
}
size_t AccentCacheSize() { return kAccentCacheSize; }
void ResetAccentCache() {
for (auto& entry : sAccentCache) {
entry.valid = false;
entry.accentNS = 0;
entry.rotationDeg = 0.0f;
}
sAccentCacheNext = 0;
}
bool IsAccentCached(nscolor aAccentNS, float aRotationDeg) {
for (const auto& entry : sAccentCache) {
if (entry.valid && entry.accentNS == aAccentNS &&
entry.rotationDeg == aRotationDeg) {
return true;
}
}
return false;
}
void EnsureCachedAccent(nscolor aAccentNS, float aRotationDeg) {
(void)GetCachedAccent(aAccentNS, aRotationDeg);
}
} // namespace detail
#endif // ENABLE_TESTS
static mozilla::StaticRefPtr<nsZenBoostsBackend> sZenBoostsBackend;

View File

@@ -27,10 +27,9 @@ struct nsZenAccentOklab {
float contrastFactor;
};
#ifdef ENABLE_TESTS
// Test-only forwarders into the file-local color math and accent cache.
namespace detail {
// Pure color-math primitives, exposed for unit testing. These have no
// dependency on the singleton, the BrowsingContext, or the process type, so
// they can be exercised directly from gtest.
nsZenAccentOklab PrecomputeAccent(nscolor aAccentColor);
nsZenAccentOklab RotateAccent(const nsZenAccentOklab& aBase,
float aRotationDeg);
@@ -38,7 +37,13 @@ nscolor FilterColorChannel(nscolor aOriginalColor,
const nsZenAccentOklab& aAccent,
const nsZenAccentOklab& aComplementary);
nscolor InvertColorChannel(nscolor aColor);
size_t AccentCacheSize();
void ResetAccentCache();
bool IsAccentCached(nscolor aAccentNS, float aRotationDeg);
void EnsureCachedAccent(nscolor aAccentNS, float aRotationDeg);
} // namespace detail
#endif // ENABLE_TESTS
class nsZenBoostsBackend final : public nsISupports {
public: