summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/LabelTests.cs
blob: 5e2ffb53f71660068e1eb0bb1cab6a268717088c (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using NUnit.Framework;
using NUnit.Framework.Constraints;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class LabelTests : BaseTestFixture
	{
		[SetUp]
		public override void Setup ()
		{
			base.Setup ();
			Device.PlatformServices = new MockPlatformServices ();
		}

		[TearDown]
		public override void TearDown ()
		{
			base.TearDown ();
			Device.PlatformServices = null;
		}

		[Test]
		public void TextAndAttributedTextMutuallyExclusive ()
		{
			var label = new Label ();
			Assert.IsNull (label.Text);
			Assert.IsNull (label.FormattedText);

			label.Text = "Foo";
			Assert.AreEqual ("Foo", label.Text);
			Assert.IsNull (label.FormattedText);

			var fs = new FormattedString ();
			label.FormattedText = fs;
			Assert.IsNull (label.Text);
			Assert.AreSame (fs, label.FormattedText);

			label.Text = "Foo";
			Assert.AreEqual ("Foo", label.Text);
			Assert.IsNull (label.FormattedText);
		}

		[Test]
		public void InvalidateMeasureWhenTextChanges ()
		{
			var label = new Label();

			bool fired;
			label.MeasureInvalidated += (sender, args) =>
			{
				fired = true;
			};

			fired = false;
			label.Text = "Foo";
			Assert.IsTrue (fired);

			var fs = new FormattedString ();

			fired = false;
			label.FormattedText = fs;
			Assert.IsTrue (fired);

			fired = false;
			fs.Spans.Add (new Span {Text = "bar"});
			Assert.IsTrue (fired);
		}

		[Test]
		public void AssignToFontStructUpdatesFontFamily (
			[Values (NamedSize.Default, NamedSize.Large, NamedSize.Medium, NamedSize.Small, NamedSize.Micro)] NamedSize size,
			[Values (FontAttributes.None, FontAttributes.Bold, FontAttributes.Italic, FontAttributes.Bold | FontAttributes.Italic)] FontAttributes attributes)
		{
			var label = new Label {Platform = new UnitPlatform ()};
			double startSize = label.FontSize;
			var startAttributes = label.FontAttributes;

			bool firedSizeChanged = false;
			bool firedAttributesChanged = false;
			label.PropertyChanged += (sender, args) => {
				if (args.PropertyName == Label.FontSizeProperty.PropertyName)
					firedSizeChanged = true;
				if (args.PropertyName == Label.FontAttributesProperty.PropertyName)
					firedAttributesChanged = true;
			};

			label.Font = Font.OfSize ("Testing123", size).WithAttributes (attributes);

			Assert.AreEqual (Device.GetNamedSize (size, typeof (Label), true), label.FontSize);
			Assert.AreEqual (attributes, label.FontAttributes);
			Assert.AreEqual (startSize != label.FontSize, firedSizeChanged);
			Assert.AreEqual (startAttributes != label.FontAttributes, firedAttributesChanged);
		}

		[Test]
		public void AssignToFontFamilyUpdatesFont ()
		{
			var label = new Label {Platform = new UnitPlatform ()};

			label.FontFamily = "CrazyFont";
			Assert.AreEqual (label.Font, Font.OfSize ("CrazyFont", label.FontSize));
		}

		[Test]
		public void AssignToFontSizeUpdatesFont ()
		{
			var label = new Label {Platform = new UnitPlatform ()};

			label.FontSize = 1000;
			Assert.AreEqual (label.Font, Font.SystemFontOfSize (1000));
		}

		[Test]
		public void AssignedToFontSizeUpdatesFontDouble ()
		{
			var label = new Label {Platform = new UnitPlatform ()};

			label.FontSize = 10.7;
			Assert.AreEqual (label.Font, Font.SystemFontOfSize (10.7));
		}

		[Test]
		public void AssignedToFontSizeDouble ()
		{
			var label = new Label {Platform = new UnitPlatform ()};

			label.FontSize = 10.7;
			Assert.AreEqual (label.FontSize, 10.7);
		}


		[Test]
		public void AssignToFontAttributesUpdatesFont ()
		{
			var label = new Label {Platform = new UnitPlatform ()};

			label.FontAttributes = FontAttributes.Italic | FontAttributes.Bold;
			Assert.AreEqual (label.Font, Font.SystemFontOfSize (label.FontSize, FontAttributes.Bold | FontAttributes.Italic));
		}

		[Test]
		public void LabelResizesWhenFontChanges ()
		{
			var label = new Label {Platform = new UnitPlatform ((ve, w, h) => {
				var l = (Label) ve;
				return new SizeRequest(new Size(l.Font.FontSize, l.Font.FontSize));
			}), IsPlatformEnabled = true};

			Assert.AreEqual (label.Font.FontSize, label.GetSizeRequest (double.PositiveInfinity, double.PositiveInfinity).Request.Width);

			bool fired = false;

			label.MeasureInvalidated += (sender, args) => {
				Assert.AreEqual (25, label.GetSizeRequest (double.PositiveInfinity, double.PositiveInfinity).Request.Width);
				fired = true;
			};


			label.FontSize = 25;

			Assert.True (fired);
		}

		[Test]
		public void FontSizeConverterTests ()
		{
			var converter = new FontSizeConverter ();
			Assert.AreEqual (12, converter.ConvertFromInvariantString ("12"));
			Assert.AreEqual (10.7, converter.ConvertFromInvariantString ("10.7"));
		}

		[Test]
		public void FontSizeCanBeSetFromStyle ()
		{
			var label = new Label ();

			Assert.AreEqual (10.0, label.FontSize);

			label.SetValue (Label.FontSizeProperty, 1.0, true);
			Assert.AreEqual (1.0, label.FontSize);
		}

		[Test]
		public void ManuallySetFontSizeNotOverridenByStyle ()
		{
			var label = new Label ();

			Assert.AreEqual (10.0, label.FontSize);

			label.SetValue (Label.FontSizeProperty, 2.0, false);
			Assert.AreEqual (2.0, label.FontSize);

			label.SetValue (Label.FontSizeProperty, 1.0, true);
			Assert.AreEqual (2.0, label.FontSize);
		}

		[Test]
		public void ChangingHorizontalTextAlignmentFiresXAlignChanged ()
		{
			var label = new Label () { HorizontalTextAlignment = TextAlignment.Center };

			var xAlignFired = false;
			var horizontalTextAlignmentFired = false;

			label.PropertyChanged += (sender, args) => {
				if (args.PropertyName == "XAlign") {
					xAlignFired	= true;
				} else if (args.PropertyName == Label.HorizontalTextAlignmentProperty.PropertyName) {
					horizontalTextAlignmentFired = true;
				}
			};

			label.HorizontalTextAlignment = TextAlignment.End;

			Assert.True(xAlignFired);
			Assert.True(horizontalTextAlignmentFired);
		}

		[Test]
		public void ChangingVerticalTextAlignmentFiresYAlignChanged ()
		{
			var label = new Label () { VerticalTextAlignment = TextAlignment.Center };

			var yAlignFired = false;
			var verticalTextAlignmentFired = false;

			label.PropertyChanged += (sender, args) => {
				if (args.PropertyName == "YAlign") {
					yAlignFired = true;
				} else if (args.PropertyName == Label.VerticalTextAlignmentProperty.PropertyName) {
					verticalTextAlignmentFired = true;
				}
			};

			label.VerticalTextAlignment = TextAlignment.End;

			Assert.True (yAlignFired);
			Assert.True (verticalTextAlignmentFired);
		}

		[Test]
		public void EntryCellXAlignBindingMatchesHorizontalTextAlignmentBinding ()
		{
			var vm = new ViewModel ();
			vm.HorizontalAlignment = TextAlignment.Center;

			var labelXAlign = new Label () { BindingContext = vm };
			labelXAlign.SetBinding (Label.XAlignProperty, new Binding ("HorizontalAlignment"));

			var labelHorizontalTextAlignment = new Label () { BindingContext = vm };
			labelHorizontalTextAlignment.SetBinding (Label.HorizontalTextAlignmentProperty, new Binding ("HorizontalAlignment"));

			Assert.AreEqual (TextAlignment.Center, labelXAlign.XAlign);
			Assert.AreEqual (TextAlignment.Center, labelHorizontalTextAlignment.HorizontalTextAlignment);

			vm.HorizontalAlignment = TextAlignment.End;

			Assert.AreEqual (TextAlignment.End, labelXAlign.XAlign);
			Assert.AreEqual (TextAlignment.End, labelHorizontalTextAlignment.HorizontalTextAlignment);
		}

		[Test]
		public void EntryCellYAlignBindingMatchesVerticalTextAlignmentBinding ()
		{
			var vm = new ViewModel ();
			vm.VerticalAlignment = TextAlignment.Center;

			var labelYAlign = new Label () { BindingContext = vm };
			labelYAlign.SetBinding (Label.YAlignProperty, new Binding ("VerticalAlignment"));

			var labelVerticalTextAlignment = new Label () { BindingContext = vm };
			labelVerticalTextAlignment.SetBinding (Label.VerticalTextAlignmentProperty, new Binding ("VerticalAlignment"));

			Assert.AreEqual (TextAlignment.Center, labelYAlign.YAlign);
			Assert.AreEqual (TextAlignment.Center, labelVerticalTextAlignment.VerticalTextAlignment);

			vm.VerticalAlignment = TextAlignment.End;

			Assert.AreEqual (TextAlignment.End, labelYAlign.YAlign);
			Assert.AreEqual (TextAlignment.End, labelVerticalTextAlignment.VerticalTextAlignment);
		}

		sealed class ViewModel : INotifyPropertyChanged
		{
			TextAlignment horizontalAlignment;
			TextAlignment verticalAlignment;

			public TextAlignment HorizontalAlignment
			{
				get { return horizontalAlignment; }
				set
				{
					horizontalAlignment = value;
					OnPropertyChanged();
				}
			}

			public TextAlignment VerticalAlignment
			{
				get { return verticalAlignment; }
				set
				{
					verticalAlignment = value;
					OnPropertyChanged();
				}
			}

			public event PropertyChangedEventHandler PropertyChanged;

			void OnPropertyChanged ([CallerMemberName] string propertyName = null)
			{
				PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
			}
		}
	}
}