summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/FormattedStringTests.cs
blob: 7c247bda4cb6b8742deedf4e8b3addda9f383210 (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
using System;
using System.Collections.ObjectModel;
using NUnit.Framework;

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

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

		[Test]
		public void NullSpansNotAllowed()
		{
			var fs = new FormattedString();
			Assert.That (() => fs.Spans.Add (null), Throws.InstanceOf<ArgumentNullException>());

			fs = new FormattedString();
			fs.Spans.Add (new Span());

			Assert.That (() => {
				fs.Spans[0] = null;
			}, Throws.InstanceOf<ArgumentNullException>());
		}

		[Test]
		public void SpanChangeTriggersSpansPropertyChange()
		{
			var span = new Span();
			var fs = new FormattedString();
			fs.Spans.Add (span);

			bool spansChanged = false;
			fs.PropertyChanged += (s, e) => {
				if (e.PropertyName == "Spans")
					spansChanged = true;
			};

			span.Text = "New text";

			Assert.That (spansChanged, Is.True);
		}

		[Test]
		public void SpanChangesUnsubscribes()
		{
			var span = new Span();
			var fs = new FormattedString();
			fs.Spans.Add (span);
			fs.Spans.Remove (span);

			bool spansChanged = false;
			fs.PropertyChanged += (s, e) => {
				if (e.PropertyName == "Spans")
					spansChanged = true;
			};

			span.Text = "New text";

			Assert.That (spansChanged, Is.False);
		}

		[Test]
		public void AddingSpanTriggersSpansPropertyChange()
		{
			var span = new Span();
			var fs = new FormattedString();
			
			bool spansChanged = false;
			fs.PropertyChanged += (s, e) => {
				if (e.PropertyName == "Spans")
					spansChanged = true;
			};

			fs.Spans.Add (span);

			Assert.That (spansChanged, Is.True);
		}

		[Test]
		public void ImplicitStringConversion()
		{
			string original = "fubar";
			FormattedString fs = original;
			Assert.That (fs, Is.Not.Null);
			Assert.That (fs.Spans.Count, Is.EqualTo (1));
			Assert.That (fs.Spans[0], Is.Not.Null);
			Assert.That (fs.Spans[0].Text, Is.EqualTo (original));
		}

		[Test]
		public void ImplicitStringConversionNull()
		{
			string original = null;
			FormattedString fs = original;
			Assert.That (fs, Is.Not.Null);
			Assert.That (fs.Spans.Count, Is.EqualTo (1));
			Assert.That (fs.Spans[0], Is.Not.Null);
			Assert.That (fs.Spans[0].Text, Is.EqualTo (original));
		}
	}
}