Skip to content

Commit 4c7a15d

Browse files
authored
Add test coverage for public API surface of BrushConverter (#10632)
1 parent 2828f75 commit 4c7a15d

File tree

1 file changed

+361
-0
lines changed
  • src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/System/Windows/Media/Generated

1 file changed

+361
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System;
7+
using System.Collections.Generic;
8+
using System.ComponentModel.Design.Serialization;
9+
using System.Text;
10+
11+
namespace System.Windows.Media.Generated;
12+
13+
/// <summary>
14+
/// TODO: Add ITypeDescriptorContext-related tests
15+
/// </summary>
16+
public sealed class BrushConverterTests
17+
{
18+
[Theory]
19+
// Valid type
20+
[InlineData(true, typeof(string))]
21+
// Invalid types
22+
[InlineData(false, typeof(KnownColor))]
23+
[InlineData(false, typeof(ColorContext))]
24+
[InlineData(false, typeof(Color))]
25+
[InlineData(false, typeof(Brush))]
26+
public void CanConvertFrom_ReturnsExpected(bool expected, Type sourceType)
27+
{
28+
BrushConverter converter = new();
29+
30+
Assert.Equal(expected, converter.CanConvertFrom(sourceType));
31+
}
32+
33+
[Theory]
34+
// Valid type
35+
[InlineData(true, typeof(string))]
36+
// Invalid types
37+
[InlineData(false, typeof(InstanceDescriptor))]
38+
[InlineData(false, typeof(KnownColor))]
39+
[InlineData(false, typeof(ColorContext))]
40+
[InlineData(false, typeof(Color))]
41+
[InlineData(false, typeof(Brush))]
42+
public void CanConvertTo_ReturnsExpected(bool expected, Type destinationType)
43+
{
44+
BrushConverter converter = new();
45+
46+
Assert.Equal(expected, converter.CanConvertTo(destinationType));
47+
}
48+
49+
[MemberData(nameof(ConvertFrom_NamedBrush_ReturnsExpectedBrush_Data))]
50+
[Theory]
51+
public void ConvertFrom_NamedBrush_ReturnsExpectedBrush(SolidColorBrush expectedColor, string colorName)
52+
{
53+
BrushConverter converter = new();
54+
object? result = converter.ConvertFrom(colorName);
55+
56+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
57+
Assert.IsType<SolidColorBrush>(result);
58+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
59+
}
60+
61+
public static IEnumerable<object[]> ConvertFrom_NamedBrush_ReturnsExpectedBrush_Data
62+
{
63+
get
64+
{
65+
yield return new object[] { Brushes.Red, "Red" };
66+
yield return new object[] { Brushes.Blue, "Blue" };
67+
yield return new object[] { Brushes.Green, "Green" };
68+
yield return new object[] { Brushes.Orange, "Orange" };
69+
yield return new object[] { Brushes.Yellow, "Yellow" };
70+
71+
yield return new object[] { Brushes.Black, "Black" };
72+
yield return new object[] { Brushes.White, "White" };
73+
yield return new object[] { Brushes.Gray, "Gray" };
74+
yield return new object[] { Brushes.DarkGray, "DarkGray" };
75+
yield return new object[] { Brushes.LightGray, "LightGray" };
76+
77+
yield return new object[] { Brushes.Purple, "Purple" };
78+
yield return new object[] { Brushes.Magenta, "Magenta" };
79+
yield return new object[] { Brushes.Pink, "Pink" };
80+
yield return new object[] { Brushes.Brown, "Brown" };
81+
yield return new object[] { Brushes.Cyan, "Cyan" };
82+
83+
yield return new object[] { Brushes.Olive, "Olive" };
84+
yield return new object[] { Brushes.Navy, "Navy" };
85+
yield return new object[] { Brushes.Teal, "Teal" };
86+
yield return new object[] { Brushes.Maroon, "Maroon" };
87+
yield return new object[] { Brushes.Silver, "Silver" };
88+
89+
yield return new object[] { Brushes.Gold, "Gold" };
90+
yield return new object[] { Brushes.Coral, "Coral" };
91+
yield return new object[] { Brushes.Indigo, "Indigo" };
92+
yield return new object[] { Brushes.Violet, "Violet" };
93+
yield return new object[] { Brushes.Crimson, "Crimson" };
94+
95+
// Wrong casing
96+
yield return new object[] { Brushes.Chartreuse, "chartreuse" };
97+
yield return new object[] { Brushes.Khaki, "khaki" };
98+
yield return new object[] { Brushes.Tomato, "tomato" };
99+
100+
yield return new object[] { Brushes.LightBlue, " LightBlue " };
101+
yield return new object[] { Brushes.LightCoral, " LightCoral" };
102+
yield return new object[] { Brushes.OldLace, "OldLace " };
103+
}
104+
}
105+
106+
[MemberData(nameof(ConvertFrom_ContextColor_ReturnsExpectedBrush_Data))]
107+
[Theory]
108+
public void ConvertFrom_ContextColor_ReturnsExpectedBrush(SolidColorBrush expectedColor, string hexColor)
109+
{
110+
BrushConverter converter = new();
111+
object? result = converter.ConvertFrom(hexColor);
112+
113+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
114+
Assert.IsType<SolidColorBrush>(result);
115+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
116+
}
117+
118+
public static IEnumerable<object[]> ConvertFrom_ContextColor_ReturnsExpectedBrush_Data
119+
{
120+
get
121+
{
122+
// As we don't pack our color profiles, this is probably the only way
123+
string? homeDrive = Environment.GetEnvironmentVariable("HOMEDRIVE");
124+
125+
if (homeDrive is null)
126+
Assert.Fail("%HOMEDRIVE% environment variable not present");
127+
128+
yield return new object[] { new SolidColorBrush(Color.FromAValues(1.0f, [0.0f, 0.5f, 1.0f, 1.0f], new Uri($"file://{homeDrive}/Windows/system32/spool/drivers/color/RSWOP.icm"))),
129+
$"ContextColor file://{homeDrive}/Windows/system32/spool/drivers/color/RSWOP.icm 1.0, 0.0, 0.5, 1.0, 1.0" };
130+
131+
yield return new object[] { new SolidColorBrush(Color.FromAValues(1.0f, [0.5f, 0.5f, 1.0f, 1.0f], new Uri($"file://{homeDrive}/Windows/system32/spool/drivers/color/RSWOP.icm"))),
132+
$"ContextColor file://{homeDrive}/Windows/system32/spool/drivers/color/RSWOP.icm 1.0,0.5,0.5,1.0,1.0" };
133+
134+
yield return new object[] { new SolidColorBrush(Color.FromAValues(1.0f, [0.5f, 0.5f, 1.0f, 0.7f], new Uri($"file://{homeDrive}/Windows/system32/spool/drivers/color/RSWOP.icm"))),
135+
$"ContextColor file://{homeDrive}/Windows/system32/spool/drivers/color/RSWOP.icm 1.0, 0.5, 0.5, 1.0, 0.7" };
136+
}
137+
}
138+
139+
[MemberData(nameof(ConvertFrom_ScRGB_ReturnsExpectedBrush_Data))]
140+
[Theory]
141+
public void ConvertFrom_ScRGB_ReturnsExpectedBrush(SolidColorBrush expectedColor, string hexColor)
142+
{
143+
BrushConverter converter = new();
144+
object? result = converter.ConvertFrom(hexColor);
145+
146+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
147+
Assert.IsType<SolidColorBrush>(result);
148+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
149+
}
150+
151+
public static IEnumerable<object[]> ConvertFrom_ScRGB_ReturnsExpectedBrush_Data
152+
{
153+
get
154+
{
155+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 1f, 0f, 0f)), "sc#1,1,0,0" }; // Fully opaque red
156+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 0f, 1f, 0f)), "sc#1,0,1,0" }; // Fully opaque green
157+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 0f, 0f, 1f)), "sc#1,0,0,1" }; // Fully opaque blue
158+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0.5f, 1f, 1f, 0f)), "sc#0.5,1,1,0" }; // Semi-transparent yellow
159+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0f, 0f, 0f, 0f)), "sc#0,0,0,0" }; // Fully transparent black
160+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 0.5f, 0.5f, 0.5f)), "sc#1,0.5,0.5,0.5" }; // Fully opaque gray
161+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0.75f, 0.5f, 0f, 0.5f)), "sc#0.75,0.5,0,0.5" }; // Semi-transparent purple
162+
163+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 1f, 0f, 0f)), "sc#1, 1, 0, 0" }; // Extra space after commas
164+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 1f, 0f, 0f)), " sc#1,1,0,0" }; // Leading space
165+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 1f, 0f, 0f)), "sc#1,1,0,0 " }; // Trailing space
166+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 1f, 0f, 0f)), " sc#1,1,0,0 " }; // Excessive surrounding whitespace
167+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0.9f, 0.8f, 0.7f, 0.6f)), "sc#0.9,0.8,0.7,0.6" }; // Non-integer values
168+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(1f, 0.12345f, 0.6789f, 0.54321f)), "sc#1,0.12345,0.6789,0.54321" }; // Values with extended precision
169+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0f, 1f, 0f, 0f)), "sc#0 ,1 ,0 ,0" }; // Spaces directly around commas
170+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0.5f, 0.5f, 0.5f, 0.5f)), "sc# .5 , .5 , .5 , .5" }; // Leading decimals with spaces
171+
yield return new object[] { new SolidColorBrush(Color.FromScRgb(0.75f, 0.5f, 0f, 0.5f)), "sc#0.75,0.50,0.00,0.50" }; // Explicit zero padding
172+
173+
}
174+
}
175+
176+
[MemberData(nameof(ConvertFrom_RGB_Short_HexColor_ReturnsExpectedBrush_Data))]
177+
[Theory]
178+
public void ConvertFrom_RGB_Short_HexColor_ReturnsExpectedBrush(SolidColorBrush expectedColor, string hexColor)
179+
{
180+
BrushConverter converter = new();
181+
object? result = converter.ConvertFrom(hexColor);
182+
183+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
184+
Assert.IsType<SolidColorBrush>(result);
185+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
186+
}
187+
188+
public static IEnumerable<object[]> ConvertFrom_RGB_Short_HexColor_ReturnsExpectedBrush_Data
189+
{
190+
get
191+
{
192+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x00, 0x00, 0x00)), "#000" };
193+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0xFF)), "#FfF" };
194+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x11, 0x22, 0x33)), "#123" };
195+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x44, 0x55, 0x66)), "#456" };
196+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x77, 0x88, 0x99)), "#789" };
197+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xAA, 0xBB, 0xCC)), "#ABC" };
198+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xDD, 0xEE, 0xFF)), "#DEF" };
199+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x11, 0x33, 0x55)), "#135" };
200+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x22, 0x44, 0x66)), "#246" };
201+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x33, 0x55, 0x77)), "#357" };
202+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x44, 0x66, 0x88)), "#468" };
203+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x55, 0x77, 0x99)), "#579" };
204+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x66, 0x88, 0xAA)), "#68A" };
205+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x77, 0x99, 0xBB)), "#79B" };
206+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x88, 0xAA, 0xCC)), "#8AC" };
207+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x99, 0xBB, 0xDD)), "#9BD" };
208+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xCC, 0xBB, 0xAA)), "#CBA" };
209+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xFF, 0xEE, 0xDD)), " #FED" };
210+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x88, 0x99, 0xAA)), "#89A " };
211+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x00, 0x77, 0xFF)), " #07F " };
212+
}
213+
}
214+
215+
[MemberData(nameof(ConvertFrom_ARGB_Short_HexColor_ReturnsExpectedBrush_Data))]
216+
[Theory]
217+
public void ConvertFrom_ARGB_Short_HexColor_ReturnsExpectedBrush(SolidColorBrush expectedColor, string hexColor)
218+
{
219+
BrushConverter converter = new();
220+
object? result = converter.ConvertFrom(hexColor);
221+
222+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
223+
Assert.IsType<SolidColorBrush>(result);
224+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
225+
}
226+
227+
public static IEnumerable<object[]> ConvertFrom_ARGB_Short_HexColor_ReturnsExpectedBrush_Data
228+
{
229+
get
230+
{
231+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0xAA, 0xFF, 0x22, 0x55)), "#AF25" };
232+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x11, 0x22, 0x33, 0x44)), "#1234" };
233+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0xFF, 0x00, 0x00, 0xFF)), "#F00F" };
234+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0xCC, 0x33, 0x66, 0xFF)), "#C36f" };
235+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x77, 0x88, 0x99, 0xAA)), "#789A" };
236+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x77, 0x33, 0x66, 0x22)), " #7362" };
237+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x66, 0x88, 0x99, 0x33)), "#6893 " };
238+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0xEE, 0xDD, 0xCC, 0xBB)), " #EDCB " };
239+
}
240+
}
241+
242+
[MemberData(nameof(ConvertFrom_RGB_Long_HexColor_ReturnsExpectedBrush_Data))]
243+
[Theory]
244+
public void ConvertFrom_RGB_Long_HexColor_ReturnsExpectedBrush(SolidColorBrush expectedColor, string hexColor)
245+
{
246+
BrushConverter converter = new();
247+
object? result = converter.ConvertFrom(hexColor);
248+
249+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
250+
Assert.IsType<SolidColorBrush>(result);
251+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
252+
}
253+
254+
public static IEnumerable<object[]> ConvertFrom_RGB_Long_HexColor_ReturnsExpectedBrush_Data
255+
{
256+
get
257+
{
258+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x12, 0x34, 0x56)), "#123456" };
259+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xAA, 0xBB, 0xCC)), "#AABBCC" };
260+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0x00)), "#FF0000" };
261+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0x00)), "#00FF00" };
262+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x00, 0x00, 0xFF)), "#0000FF" };
263+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xFF, 0xFF, 0x00)), "#FFFF00" };
264+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x00, 0xFF, 0xFF)), "#00FFFF" };
265+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xFF, 0x00, 0xFF)), "#FF00FF" };
266+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xC0, 0xC0, 0xC0)), "#C0C0C0" };
267+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x80, 0x80, 0x80)), "#808080" };
268+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x12, 0x12, 0x12)), "#121212" };
269+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x34, 0x56, 0x78)), "#345678" };
270+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x90, 0xAB, 0xCD)), "#90ABCD" };
271+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xDE, 0xAD, 0xBE)), "#DEADBE" };
272+
273+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xEF, 0xBE, 0xAD)), "#eFBEAD" };
274+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x12, 0x34, 0xFF)), "#1234ff" };
275+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x56, 0x78, 0x9A)), "#56789a" };
276+
277+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0xBC, 0xDE, 0xF0)), " #BCDEF0" };
278+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x01, 0x23, 0x45)), " #012345 " };
279+
yield return new object[] { new SolidColorBrush(Color.FromRgb(0x67, 0x89, 0xAB)), "#6789AB " };
280+
}
281+
}
282+
283+
[MemberData(nameof(ConvertFrom_ARGB_Long_HexColor_ReturnsExpectedBrush_Data))]
284+
[Theory]
285+
public void ConvertFrom_ARGB_Long_HexColor_ReturnsExpectedBrush(SolidColorBrush expectedColor, string hexColor)
286+
{
287+
BrushConverter converter = new();
288+
object? result = converter.ConvertFrom(hexColor);
289+
290+
// We serialize here back to string as SolidColorBrush doesn't override Equals nor has IEquatable<T>
291+
Assert.IsType<SolidColorBrush>(result);
292+
Assert.Equal(expectedColor.ToString(), ((SolidColorBrush)result).ToString());
293+
}
294+
295+
public static IEnumerable<object[]> ConvertFrom_ARGB_Long_HexColor_ReturnsExpectedBrush_Data
296+
{
297+
get
298+
{
299+
yield return new object[] { Brushes.Red, $"#{KnownColor.Red:X}" };
300+
yield return new object[] { Brushes.Blue, $"#{KnownColor.Blue:X}" };
301+
yield return new object[] { Brushes.Green, $"#{KnownColor.Green:X}" };
302+
yield return new object[] { Brushes.Orange, $"#{KnownColor.Orange:X}" };
303+
yield return new object[] { Brushes.Yellow, $"#{KnownColor.Yellow:X}" };
304+
305+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x25, 0x37, 0x64, 0x88)), "#25376488" };
306+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0xAF, 0x25, 0x46, 0xCC)), "#AF2546CC" };
307+
308+
// Malformed bu acceptable
309+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x44, 0x37, 0x64, 0x88)), " #44376488" };
310+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x35, 0x25, 0x46, 0xCC)), " #352546CC " };
311+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0x25, 0x37, 0x14, 0x25)), " #25371425 " };
312+
yield return new object[] { new SolidColorBrush(Color.FromArgb(0xAF, 0x25, 0x46, 0x22)), "#AF254622 " };
313+
}
314+
}
315+
316+
// ConvertFrom checks for non-strings, strings are evaluated by Parsers
317+
[InlineData("#FF00000000")]
318+
[InlineData("Not A Color")]
319+
[InlineData("#FF00 00FF")]
320+
[InlineData("NotAColor")]
321+
[InlineData("#FF000FF")]
322+
// Wrong casing on ScRgb (sc#)
323+
[InlineData("SC#1,0,1,0")]
324+
[InlineData("Sc#1,1,0,1")]
325+
// RGB; ARGB; RRGGBB; AARRGGBB but not 5
326+
[InlineData("#F12F1")]
327+
[InlineData(" # ")]
328+
[InlineData(" ")]
329+
[InlineData("# ")]
330+
[InlineData("#")]
331+
[InlineData("")]
332+
[Theory]
333+
public void ConvertFrom_InvalidColor_ThrowsFormatException(string color)
334+
{
335+
BrushConverter converter = new();
336+
337+
Assert.Throws<FormatException>(() => converter.ConvertFrom(color));
338+
}
339+
340+
// ConvertFrom checks for non-strings, strings are evaluated by Parsers
341+
[InlineData(typeof(SolidColorBrush))]
342+
[InlineData(0xFF00_FF00)]
343+
[InlineData(96.0d)]
344+
[InlineData(72.0f)]
345+
[Theory]
346+
public void ConvertFrom_InvalidColor_ThrowsNotSupportedException(object notString)
347+
{
348+
BrushConverter converter = new();
349+
350+
Assert.Throws<NotSupportedException>(() => converter.ConvertFrom(notString));
351+
}
352+
353+
[Fact]
354+
public void ConvertFrom_NULL_ThrowsNotSupportedException()
355+
{
356+
BrushConverter converter = new();
357+
358+
// TODO: Remove suppression once nullable annotations are done
359+
Assert.Throws<NotSupportedException>(() => converter.ConvertFrom(null!));
360+
}
361+
}

0 commit comments

Comments
 (0)