Skip to content

Commit fa6b1cc

Browse files
MovGP0Johann DirryKeboo
authored
adding unit tests for custom swatch colors (#4025)
* adding unit tests for custom swatch colors * Update tests/MaterialColorUtilities.Tests/CustomSemanticSwatchTests.cs Co-authored-by: Kevin B <Keboo@users.noreply.github.com> * Fix redundant loop in CustomSemanticSwatches test The test method was using a nested loop that shadowed the `isDark` parameter, causing redundant executions. This change removes the inner loop and uses the parameter directly. --------- Co-authored-by: Johann Dirry <johann.dirry@microsea.at> Co-authored-by: Kevin B <Keboo@users.noreply.github.com> Co-authored-by: Kevin Bost <kitokeboo@gmail.com>
1 parent 1afeee1 commit fa6b1cc

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System.Windows.Media;
2+
3+
namespace MaterialColorUtilities.Tests;
4+
5+
public sealed class CustomSemanticSwatchTests
6+
{
7+
[Test]
8+
[Arguments(true)]
9+
[Arguments(false)]
10+
public async Task CustomSemanticSwatches_ExtractExpectedThemeColors(bool isDark)
11+
{
12+
MaterialDynamicColors materialDynamicColors = new();
13+
14+
foreach (var semanticColor in GetSemanticColorExpectations())
15+
{
16+
var expected = isDark ? semanticColor.Dark : semanticColor.Light;
17+
var scheme = CreateSchemeWithCustomErrorSwatch(semanticColor.Swatch, isDark);
18+
19+
await Assert.That(materialDynamicColors.ErrorPaletteKeyColor.GetColor(scheme))
20+
.IsEqualTo(expected.PaletteKey)
21+
.Because($"{semanticColor.Name} palette key did not match for {(isDark ? "dark" : "light")} mode.");
22+
23+
await Assert.That(materialDynamicColors.Error.GetColor(scheme))
24+
.IsEqualTo(expected.Role)
25+
.Because($"{semanticColor.Name} role color did not match for {(isDark ? "dark" : "light")} mode.");
26+
27+
await Assert.That(materialDynamicColors.OnError.GetColor(scheme))
28+
.IsEqualTo(expected.OnRole)
29+
.Because($"{semanticColor.Name} on-role color did not match for {(isDark ? "dark" : "light")} mode.");
30+
31+
await Assert.That(materialDynamicColors.ErrorContainer.GetColor(scheme))
32+
.IsEqualTo(expected.Container)
33+
.Because($"{semanticColor.Name} container color did not match for {(isDark ? "dark" : "light")} mode.");
34+
35+
await Assert.That(materialDynamicColors.OnErrorContainer.GetColor(scheme))
36+
.IsEqualTo(expected.OnContainer)
37+
.Because($"{semanticColor.Name} on-container color did not match for {(isDark ? "dark" : "light")} mode.");
38+
}
39+
}
40+
41+
private static DynamicScheme CreateSchemeWithCustomErrorSwatch(Color semanticSwatch, bool isDark)
42+
{
43+
var sourceColor = Color.FromArgb(255, 106, 156, 89);
44+
var baseScheme = DynamicSchemeFactory.Create(
45+
sourceColor,
46+
Variant.TonalSpot,
47+
isDark,
48+
0.5,
49+
Platform.Phone,
50+
SpecVersion.Spec2025,
51+
primary: null,
52+
secondary: null,
53+
tertiary: null,
54+
neutral: null,
55+
neutralVariant: null,
56+
error: null);
57+
58+
return new DynamicScheme(
59+
baseScheme.SourceColorHct,
60+
baseScheme.Variant,
61+
baseScheme.IsDark,
62+
baseScheme.ContrastLevel,
63+
baseScheme.PlatformType,
64+
baseScheme.SpecVersion,
65+
baseScheme.PrimaryPalette,
66+
baseScheme.SecondaryPalette,
67+
baseScheme.TertiaryPalette,
68+
baseScheme.NeutralPalette,
69+
baseScheme.NeutralVariantPalette,
70+
TonalPalette.FromInt(ColorUtils.ArgbFromColor(semanticSwatch)));
71+
}
72+
73+
private static SemanticColorExpectation[] GetSemanticColorExpectations()
74+
{
75+
return
76+
[
77+
new SemanticColorExpectation(
78+
"WarningHigh",
79+
Color.FromArgb(255, 212, 69, 41),
80+
new ExtractedSemanticColors(
81+
PaletteKey: Color.FromArgb(255, 212, 69, 41),
82+
Role: Color.FromArgb(255, 111, 15, 0),
83+
OnRole: Colors.White,
84+
Container: Color.FromArgb(255, 198, 59, 32),
85+
OnContainer: Colors.White),
86+
new ExtractedSemanticColors(
87+
PaletteKey: Color.FromArgb(255, 212, 69, 41),
88+
Role: Color.FromArgb(255, 255, 210, 201),
89+
OnRole: Color.FromArgb(255, 80, 8, 0),
90+
Container: Color.FromArgb(255, 247, 94, 63),
91+
OnContainer: Colors.Black)),
92+
new SemanticColorExpectation(
93+
"WarningLow",
94+
Color.FromArgb(255, 252, 163, 41),
95+
new ExtractedSemanticColors(
96+
PaletteKey: Color.FromArgb(255, 252, 163, 41),
97+
Role: Color.FromArgb(255, 80, 47, 0),
98+
OnRole: Colors.White,
99+
Container: Color.FromArgb(255, 156, 95, 0),
100+
OnContainer: Colors.White),
101+
new ExtractedSemanticColors(
102+
PaletteKey: Color.FromArgb(255, 252, 163, 41),
103+
Role: Color.FromArgb(255, 255, 213, 169),
104+
OnRole: Color.FromArgb(255, 57, 32, 0),
105+
Container: Color.FromArgb(255, 205, 127, 0),
106+
OnContainer: Colors.Black)),
107+
new SemanticColorExpectation(
108+
"Information",
109+
Color.FromArgb(255, 0, 46, 122),
110+
new ExtractedSemanticColors(
111+
PaletteKey: Color.FromArgb(255, 0, 46, 122),
112+
Role: Color.FromArgb(255, 5, 49, 125),
113+
OnRole: Colors.White,
114+
Container: Color.FromArgb(255, 76, 106, 184),
115+
OnContainer: Colors.White),
116+
new ExtractedSemanticColors(
117+
PaletteKey: Color.FromArgb(255, 0, 46, 122),
118+
Role: Color.FromArgb(255, 210, 219, 255),
119+
OnRole: Color.FromArgb(255, 0, 33, 93),
120+
Container: Color.FromArgb(255, 112, 142, 222),
121+
OnContainer: Colors.Black)),
122+
new SemanticColorExpectation(
123+
"Safe",
124+
Color.FromArgb(255, 41, 138, 64),
125+
new ExtractedSemanticColors(
126+
PaletteKey: Color.FromArgb(255, 41, 138, 64),
127+
Role: Color.FromArgb(255, 0, 64, 21),
128+
OnRole: Colors.White,
129+
Container: Color.FromArgb(255, 24, 126, 53),
130+
OnContainer: Colors.White),
131+
new ExtractedSemanticColors(
132+
PaletteKey: Color.FromArgb(255, 41, 138, 64),
133+
Role: Color.FromArgb(255, 145, 242, 155),
134+
OnRole: Color.FromArgb(255, 0, 45, 12),
135+
Container: Color.FromArgb(255, 69, 163, 86),
136+
OnContainer: Colors.Black)),
137+
new SemanticColorExpectation(
138+
"Error",
139+
Color.FromArgb(255, 163, 23, 26),
140+
new ExtractedSemanticColors(
141+
PaletteKey: Color.FromArgb(255, 163, 23, 26),
142+
Role: Color.FromArgb(255, 115, 0, 9),
143+
OnRole: Colors.White,
144+
Container: Color.FromArgb(255, 201, 53, 49),
145+
OnContainer: Colors.White),
146+
new ExtractedSemanticColors(
147+
PaletteKey: Color.FromArgb(255, 163, 23, 26),
148+
Role: Color.FromArgb(255, 255, 210, 205),
149+
OnRole: Color.FromArgb(255, 84, 0, 4),
150+
Container: Color.FromArgb(255, 251, 89, 80),
151+
OnContainer: Colors.Black))
152+
];
153+
}
154+
155+
private sealed record SemanticColorExpectation(
156+
string Name,
157+
Color Swatch,
158+
ExtractedSemanticColors Light,
159+
ExtractedSemanticColors Dark);
160+
161+
private sealed record ExtractedSemanticColors(
162+
Color PaletteKey,
163+
Color Role,
164+
Color OnRole,
165+
Color Container,
166+
Color OnContainer);
167+
}

0 commit comments

Comments
 (0)