Cockatrice/tests/svg_recolor_test.cpp
Lukas Brübach 9978be9ab7
[Icons] Add SVG currentColor sentinel resolver
Qt's SVG renderer has no CSS cascade, so an authored `currentColor` never
resolves. Resolve the sentinel to a concrete palette color as plain bytes,
before the SVG reaches QSvgRenderer.
2026-10-21 05:11:11 +02:00

53 lines
1.5 KiB
C++

#include "libcockatrice/utility/svg_recolor.h"
#include <QByteArray>
#include <gtest/gtest.h>
namespace
{
TEST(SvgRecolor, ReplacesEverySentinel)
{
const QByteArray svg = R"(<svg><path fill="currentColor"/><path stroke="currentColor"/></svg>)";
const QByteArray result = resolveSvgColorSentinel(svg, "#ff8800");
EXPECT_EQ(result.count("#ff8800"), 2);
EXPECT_FALSE(result.contains("currentColor"));
}
TEST(SvgRecolor, ReplacesSentinelRegardlessOfCase)
{
const QByteArray svg = R"(<svg><path fill="CurrentColor"/><path stroke="CURRENTCOLOR"/></svg>)";
EXPECT_EQ(resolveSvgColorSentinel(svg, "#ff8800").count("#ff8800"), 2);
}
TEST(SvgRecolor, LeavesTheRestOfTheDocumentIntact)
{
const QByteArray svg =
R"(<svg viewBox="0 0 24 24"><!-- an ordinary comment --><path d="M0 0h24v24" fill="currentColor"/></svg>)";
const QByteArray result = resolveSvgColorSentinel(svg, "#123456");
EXPECT_TRUE(result.contains(R"(viewBox="0 0 24 24")"));
EXPECT_TRUE(result.contains(R"(d="M0 0h24v24")"));
EXPECT_TRUE(result.contains("an ordinary comment"));
}
TEST(SvgRecolor, DoesNotModifyTheInput)
{
const QByteArray svg = R"(<svg><path fill="currentColor"/></svg>)";
const QByteArray original = svg;
resolveSvgColorSentinel(svg, "#000000");
EXPECT_EQ(svg, original);
}
TEST(SvgRecolor, NoSentinelIsANoOp)
{
const QByteArray svg = R"(<svg><path fill="#abcdef"/></svg>)";
EXPECT_EQ(resolveSvgColorSentinel(svg, "#ffffff"), svg);
}
} // namespace