/
BackgroundColorTests.cs
124 lines (104 loc) · 3.18 KB
/
BackgroundColorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Collections;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Xamarin.Forms.Platform.UWP;
using WColor = Windows.UI.Color;
namespace Xamarin.Forms.Platform.UAP.UnitTests
{
[TestFixture]
public class BackgroundColorTests : PlatformTestFixture
{
static IEnumerable TestCases
{
get
{
// SearchBar is currently busted; when 8773 gets merged we can stop filtering it
foreach (var element in BasicViews
.Where(v => !(v is SearchBar))
.Where(v => !(v is Frame)))
{
element.BackgroundColor = Color.AliceBlue;
yield return CreateTestCase(element);
}
}
}
WColor GetBackgroundColor(Control control)
{
if (control is FormsButton button)
{
return (button.BackgroundColor as SolidColorBrush).Color;
}
if (control is StepperControl stepper)
{
return stepper.ButtonBackgroundColor.ToWindowsColor();
}
return (control.Background as SolidColorBrush).Color;
}
WColor GetBackgroundColor(Panel panel)
{
return (panel.Background as SolidColorBrush).Color;
}
WColor GetBackgroundColor(Border border)
{
return (border.Background as SolidColorBrush).Color;
}
async Task<WColor> GetNativeColor(View view)
{
return await Device.InvokeOnMainThreadAsync(() =>
{
var control = GetNativeControl(view);
if (control != null)
{
return GetBackgroundColor(control);
}
var border = GetBorder(view);
if (border != null)
{
return GetBackgroundColor(border);
}
var panel = GetPanel(view);
return GetBackgroundColor(panel);
});
}
[Test, TestCaseSource(nameof(TestCases))]
[Description("View background color should match renderer background color")]
public async Task BackgroundColorConsistent(View view)
{
var nativeColor = await GetNativeColor(view);
var formsColor = view.BackgroundColor.ToWindowsColor();
Assert.That(nativeColor, Is.EqualTo(formsColor));
}
[Test, Category("BackgroundColor"), Category("Frame")]
[Description("Frame background color should match renderer background color")]
public async Task FrameBackgroundColorConsistent()
{
var frame = new Frame() { BackgroundColor = Color.Orange };
var expectedColor = frame.BackgroundColor.ToWindowsColor();
var actualColor = await Device.InvokeOnMainThreadAsync(() =>
{
var renderer = GetRenderer(frame);
var nativeElement = renderer.GetNativeElement() as Border;
var backgroundBrush = nativeElement.Background as SolidColorBrush;
return backgroundBrush.Color;
});
Assert.That(actualColor, Is.EqualTo(expectedColor));
}
[Test, Category("Color"), Category("BoxView")]
public async Task BoxViewColorConsistent()
{
var box = new BoxView() { Color = Color.PapayaWhip };
var expectedColor = box.Color.ToWindowsColor();
var actualColor = await Device.InvokeOnMainThreadAsync(() =>
{
var renderer = GetRenderer(box);
var nativeElement = renderer.GetNativeElement() as Border;
var backgroundBrush = nativeElement.Background as SolidColorBrush;
return backgroundBrush.Color;
});
Assert.That(actualColor, Is.EqualTo(expectedColor));
}
}
}