-
Notifications
You must be signed in to change notification settings - Fork 3.5k
adding unit tests for custom swatch colors #4025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MovGP0
wants to merge
1
commit into
MaterialDesignInXAML:master
Choose a base branch
from
MovGP0:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+168
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168
tests/MaterialColorUtilities.Tests/CustomSemanticSwatchTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| using System.Windows.Media; | ||
|
|
||
| namespace MaterialColorUtilities.Tests; | ||
|
|
||
| public sealed class CustomSemanticSwatchTests | ||
| { | ||
| [Test] | ||
| public async Task CustomSemanticSwatches_ExtractExpectedThemeColors() | ||
| { | ||
| var materialDynamicColors = new MaterialDynamicColors(); | ||
|
|
||
| foreach (var semanticColor in GetSemanticColorExpectations()) | ||
| { | ||
| foreach (var isDark in new[] { false, true }) | ||
| { | ||
| var expected = isDark ? semanticColor.Dark : semanticColor.Light; | ||
| var scheme = CreateSchemeWithCustomErrorSwatch(semanticColor.Swatch, isDark); | ||
|
|
||
| await Assert.That(materialDynamicColors.ErrorPaletteKeyColor.GetColor(scheme)) | ||
| .IsEqualTo(expected.PaletteKey) | ||
| .Because($"{semanticColor.Name} palette key did not match for {(isDark ? "dark" : "light")} mode."); | ||
|
|
||
| await Assert.That(materialDynamicColors.Error.GetColor(scheme)) | ||
| .IsEqualTo(expected.Role) | ||
| .Because($"{semanticColor.Name} role color did not match for {(isDark ? "dark" : "light")} mode."); | ||
|
|
||
| await Assert.That(materialDynamicColors.OnError.GetColor(scheme)) | ||
| .IsEqualTo(expected.OnRole) | ||
| .Because($"{semanticColor.Name} on-role color did not match for {(isDark ? "dark" : "light")} mode."); | ||
|
|
||
| await Assert.That(materialDynamicColors.ErrorContainer.GetColor(scheme)) | ||
| .IsEqualTo(expected.Container) | ||
| .Because($"{semanticColor.Name} container color did not match for {(isDark ? "dark" : "light")} mode."); | ||
|
|
||
| await Assert.That(materialDynamicColors.OnErrorContainer.GetColor(scheme)) | ||
| .IsEqualTo(expected.OnContainer) | ||
| .Because($"{semanticColor.Name} on-container color did not match for {(isDark ? "dark" : "light")} mode."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static DynamicScheme CreateSchemeWithCustomErrorSwatch(Color semanticSwatch, bool isDark) | ||
| { | ||
| var sourceColor = Color.FromArgb(255, 106, 156, 89); | ||
| var baseScheme = DynamicSchemeFactory.Create( | ||
| sourceColor, | ||
| Variant.TonalSpot, | ||
| isDark, | ||
| 0.5, | ||
| Platform.Phone, | ||
| SpecVersion.Spec2025, | ||
| primary: null, | ||
| secondary: null, | ||
| tertiary: null, | ||
| neutral: null, | ||
| neutralVariant: null, | ||
| error: null); | ||
|
|
||
| return new DynamicScheme( | ||
| baseScheme.SourceColorHct, | ||
| baseScheme.Variant, | ||
| baseScheme.IsDark, | ||
| baseScheme.ContrastLevel, | ||
| baseScheme.PlatformType, | ||
| baseScheme.SpecVersion, | ||
| baseScheme.PrimaryPalette, | ||
| baseScheme.SecondaryPalette, | ||
| baseScheme.TertiaryPalette, | ||
| baseScheme.NeutralPalette, | ||
| baseScheme.NeutralVariantPalette, | ||
| TonalPalette.FromInt(ColorUtils.ArgbFromColor(semanticSwatch))); | ||
| } | ||
|
|
||
| private static SemanticColorExpectation[] GetSemanticColorExpectations() | ||
| { | ||
| return | ||
| [ | ||
| new SemanticColorExpectation( | ||
| "WarningHigh", | ||
| Color.FromArgb(255, 212, 69, 41), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 212, 69, 41), | ||
| Role: Color.FromArgb(255, 111, 15, 0), | ||
| OnRole: Colors.White, | ||
| Container: Color.FromArgb(255, 198, 59, 32), | ||
| OnContainer: Colors.White), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 212, 69, 41), | ||
| Role: Color.FromArgb(255, 255, 210, 201), | ||
| OnRole: Color.FromArgb(255, 80, 8, 0), | ||
| Container: Color.FromArgb(255, 247, 94, 63), | ||
| OnContainer: Colors.Black)), | ||
| new SemanticColorExpectation( | ||
| "WarningLow", | ||
| Color.FromArgb(255, 252, 163, 41), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 252, 163, 41), | ||
| Role: Color.FromArgb(255, 80, 47, 0), | ||
| OnRole: Colors.White, | ||
| Container: Color.FromArgb(255, 156, 95, 0), | ||
| OnContainer: Colors.White), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 252, 163, 41), | ||
| Role: Color.FromArgb(255, 255, 213, 169), | ||
| OnRole: Color.FromArgb(255, 57, 32, 0), | ||
| Container: Color.FromArgb(255, 205, 127, 0), | ||
| OnContainer: Colors.Black)), | ||
| new SemanticColorExpectation( | ||
| "Information", | ||
| Color.FromArgb(255, 0, 46, 122), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 0, 46, 122), | ||
| Role: Color.FromArgb(255, 5, 49, 125), | ||
| OnRole: Colors.White, | ||
| Container: Color.FromArgb(255, 76, 106, 184), | ||
| OnContainer: Colors.White), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 0, 46, 122), | ||
| Role: Color.FromArgb(255, 210, 219, 255), | ||
| OnRole: Color.FromArgb(255, 0, 33, 93), | ||
| Container: Color.FromArgb(255, 112, 142, 222), | ||
| OnContainer: Colors.Black)), | ||
| new SemanticColorExpectation( | ||
| "Safe", | ||
| Color.FromArgb(255, 41, 138, 64), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 41, 138, 64), | ||
| Role: Color.FromArgb(255, 0, 64, 21), | ||
| OnRole: Colors.White, | ||
| Container: Color.FromArgb(255, 24, 126, 53), | ||
| OnContainer: Colors.White), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 41, 138, 64), | ||
| Role: Color.FromArgb(255, 145, 242, 155), | ||
| OnRole: Color.FromArgb(255, 0, 45, 12), | ||
| Container: Color.FromArgb(255, 69, 163, 86), | ||
| OnContainer: Colors.Black)), | ||
| new SemanticColorExpectation( | ||
| "Error", | ||
| Color.FromArgb(255, 163, 23, 26), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 163, 23, 26), | ||
| Role: Color.FromArgb(255, 115, 0, 9), | ||
| OnRole: Colors.White, | ||
| Container: Color.FromArgb(255, 201, 53, 49), | ||
| OnContainer: Colors.White), | ||
| new ExtractedSemanticColors( | ||
| PaletteKey: Color.FromArgb(255, 163, 23, 26), | ||
| Role: Color.FromArgb(255, 255, 210, 205), | ||
| OnRole: Color.FromArgb(255, 84, 0, 4), | ||
| Container: Color.FromArgb(255, 251, 89, 80), | ||
| OnContainer: Colors.Black)) | ||
| ]; | ||
| } | ||
|
|
||
| private sealed record SemanticColorExpectation( | ||
| string Name, | ||
| Color Swatch, | ||
| ExtractedSemanticColors Light, | ||
| ExtractedSemanticColors Dark); | ||
|
|
||
| private sealed record ExtractedSemanticColors( | ||
| Color PaletteKey, | ||
| Color Role, | ||
| Color OnRole, | ||
| Color Container, | ||
| Color OnContainer); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small thing here, rather than iterating over the bool array, I would pass them as argument.