-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ToolbarExtensionsTests.cs
209 lines (181 loc) · 6.23 KB
/
ToolbarExtensionsTests.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.Design.Widget;
using Android.Support.V7.View.Menu;
using Android.Text;
using Android.Text.Style;
using Android.Views;
using Android.Widget;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Platform.Android.UnitTests;
using AToolBar = Android.Support.V7.Widget.Toolbar;
using AView = Android.Views.View;
[assembly: ExportRenderer(typeof(TestShell), typeof(TestShellRenderer))]
namespace Xamarin.Forms.Platform.Android.UnitTests
{
public class ToolbarExtensionsTests : PlatformTestFixture
{
[Test, Category("ToolbarExtensions")]
[Description("ToolbarItem Text Set Correctly")]
public void TextSetsCorrectlyWithNoTintColor()
{
List<ToolbarItem> sortedItems = new List<ToolbarItem>()
{
new ToolbarItem() { IsEnabled = true, Text = "a" },
new ToolbarItem() { IsEnabled = true, Text = "b" },
new ToolbarItem() { IsEnabled = true, Text = "c" },
};
var settings = new ToolbarSettings(sortedItems);
SetupToolBar(settings, Context);
int i = 0;
foreach(var textView in settings.TextViews)
{
Assert.AreEqual(sortedItems[i].Text, textView.Text);
i++;
}
}
[Test, Category("ToolbarExtensions")]
[Description("ToolbarItem with Empty String doesn't crash")]
public void DoesntCrashWithEmptyStringOnText()
{
List<ToolbarItem> sortedItems = new List<ToolbarItem>()
{
new ToolbarItem(),
};
// If this doesn't crash test has passed
SetupToolBar(new ToolbarSettings(sortedItems), Context);
}
[Test, Category("ToolbarExtensions")]
[Description("Validate ToolBarItem TextColor Changes")]
public void ToolBarItemsColoredCorrectlyBasedOnEnabledDisabled()
{
try
{
List<ToolbarItem> sortedItems = new List<ToolbarItem>()
{
new ToolbarItem() { IsEnabled = true, Text = "a" },
new ToolbarItem() { IsEnabled = true, Text = "b" },
new ToolbarItem() { IsEnabled = true, Text = "c" },
};
var settings = new ToolbarSettings(sortedItems) { TintColor = Color.Red };
SetupToolBar(settings, Context);
AToolBar aToolBar = settings.ToolBar;
List<IMenuItem> menuItemsCreated = settings.MenuItemsCreated;
Assert.IsTrue(menuItemsCreated[2].IsEnabled, "Initial state of menu Item is not enabled");
sortedItems[2].IsEnabled = false;
Assert.IsTrue(menuItemsCreated[0].IsEnabled, "Menu Item 1 is incorrectly disabled");
Assert.IsTrue(menuItemsCreated[1].IsEnabled, "Menu Item 2 is incorrectly disabled");
Assert.IsFalse(menuItemsCreated[2].IsEnabled, "Menu Item 3 is incorrectly disabled");
var textViews = settings.TextViews.ToList();
Assert.AreEqual(3, textViews.Count, $"{textViews.Count} textviews retrieved which it should have been 3");
settings.Layout();
for (int i = 0; i < 3; i++)
{
global::Android.Graphics.Color androidColor;
if (i != 2)
{
androidColor = Color.Red.ToAndroid();
}
else
{
androidColor = Color.Red.MultiplyAlpha(0.302).ToAndroid();
}
textViews[i].AssertContainsColor(androidColor);
Assert.AreEqual(sortedItems[i].Text, textViews[i].Text);
}
}
catch(Exception exc)
{
Assert.Fail(exc.ToString());
}
}
[Test, Category("ToolbarExtensions")]
[Description("Secondary ToolBarItems don't Change Color based on ForegroundColor")]
public void SecondaryToolbarItemsDontChangeColor()
{
List<ToolbarItem> sortedItems = new List<ToolbarItem>()
{
new ToolbarItem() { IsEnabled = true, Text = "a", Order = ToolbarItemOrder.Secondary },
};
var settings = new ToolbarSettings(sortedItems) { TintColor = Color.Red };
SetupToolBar(settings, Context);
AToolBar aToolBar = settings.ToolBar;
IMenuItem menuItem = settings.MenuItemsCreated.First();
MenuItemImpl menuItemImpl = (MenuItemImpl)menuItem;
Assert.IsNotNull(menuItemImpl, "menuItem is not of type MenuItemImpl");
if(menuItemImpl.TitleFormatted is SpannableString tf)
{
var colorSpan =
tf.GetSpans(0, tf.Length(), Java.Lang.Class.FromType(typeof(ForegroundColorSpan)))
.OfType<ForegroundColorSpan>()
.FirstOrDefault();
if (colorSpan != null)
{
Assert.AreNotEqual(
colorSpan.ForegroundColor,
(int)Color.Red.ToAndroid(),
"Secondary Menu Item Incorrectly set to ForegroundColor");
}
}
}
static void SetupToolBar(ToolbarSettings settings, Context context)
{
foreach(var item in settings.ToolbarItems)
{
if (String.IsNullOrWhiteSpace(item.AutomationId) && !String.IsNullOrWhiteSpace(item.Text))
item.AutomationId = item.Text;
}
settings.ToolBar = new AToolBar(context);
ToolbarExtensions.UpdateMenuItems(
settings.ToolBar,
settings.ToolbarItems,
context,
settings.TintColor,
OnToolbarItemPropertyChanged,
settings.MenuItemsCreated,
settings.ToolbarItemsCreated
);
void OnToolbarItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
settings.ToolBar.OnToolbarItemPropertyChanged(e,
(ToolbarItem)sender, settings.ToolbarItems, context, settings.TintColor, OnToolbarItemPropertyChanged,
settings.MenuItemsCreated,
settings.ToolbarItemsCreated);
}
}
[Preserve(AllMembers = true)]
public class ToolbarSettings
{
public ToolbarSettings(List<ToolbarItem> toolbarItems)
{
ToolbarItems = toolbarItems;
MenuItemsCreated = new List<IMenuItem>();
ToolbarItemsCreated = new List<ToolbarItem>();
}
public void Layout(int width = 800, int height = 200)
{
int widthSpec = AView.MeasureSpec.MakeMeasureSpec(width, MeasureSpecMode.Exactly);
int heightSpec = AView.MeasureSpec.MakeMeasureSpec(height, MeasureSpecMode.Exactly);
ToolBar.Measure(widthSpec, heightSpec);
ToolBar.Layout(0, 0, width, height);
}
public List<ToolbarItem> ToolbarItems;
public List<ToolbarItem> ToolbarItemsCreated;
public AToolBar ToolBar;
public Color? TintColor;
public List<IMenuItem> MenuItemsCreated;
public IEnumerable<ActionMenuItemView> TextViews =>
ToolBar.GetChildrenOfType<ActionMenuItemView>()
.OrderBy(x => x.Text);
}
}
}