summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/CellTests.cs
blob: 0006b4cf680604df5399858695743e11ba1f9e45 (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
using NUnit.Framework;
using System;

namespace Xamarin.Forms.Core.UnitTests
{
	[TestFixture]
	public class CellTests : BaseTestFixture
	{
		internal class TestCell : Cell
		{
			public bool OnAppearingSent { get; set; }
			public bool OnDisappearingSent { get; set; }

			protected override void OnAppearing ()
			{
				base.OnAppearing ();
				OnAppearingSent = true;
			}

			protected override void OnDisappearing ()
			{
				base.OnDisappearing ();
				OnDisappearingSent = true;
			}
		}

		[Test]
		public void Selected ()
		{
			var cell = new TestCell ();

			bool tapped = false;
			cell.Tapped += (sender, args) => tapped = true;

			cell.OnTapped();
			Assert.IsTrue (tapped);
		}

		[Test]
		public void AppearingEvent ()
		{
			var cell = new TestCell ();

			bool emitted = false;
			cell.Appearing += (sender, args) => emitted = true;

			cell.SendAppearing ();
			Assert.True (emitted);
			Assert.True (cell.OnAppearingSent);
			Assert.False (cell.OnDisappearingSent);
		}

		[Test]
		public void DisappearingEvent ()
		{
			var cell = new TestCell ();

			bool emitted = false;
			cell.Disappearing += (sender, args) => emitted = true;

			cell.SendDisappearing ();
			Assert.True (emitted);
			Assert.False (cell.OnAppearingSent);
			Assert.True (cell.OnDisappearingSent);
		}

		[Test]
		public void TestBindingContextPropagationOnImageCell ()
		{
			var context = new object ();
			var cell = new ImageCell ();
			cell.BindingContext = context;
			var source = new FileImageSource ();
			cell.ImageSource = source;
			Assert.AreSame (context, source.BindingContext);

			cell = new ImageCell ();
			source = new FileImageSource ();
			cell.ImageSource = source;
			cell.BindingContext = context;
			Assert.AreSame (context, source.BindingContext);
		}

		[Test]
		public void HasContextActions()
		{
			bool changed = false;

			var cell = new TextCell();
			cell.PropertyChanged += (sender, args) => {
				if (args.PropertyName == "HasContextActions")
					changed = true;
			};

			Assert.That (cell.HasContextActions, Is.False);
			Assert.That (changed, Is.False);

			var collection = cell.ContextActions;

			Assert.That (cell.HasContextActions, Is.False);
			Assert.That (changed, Is.False);

			collection.Add (new MenuItem());

			Assert.That (cell.HasContextActions, Is.True);
			Assert.That (changed, Is.True);
		}

		[Test]
		public void MenuItemsGetBindingContext()
		{
			var cell = new TextCell {
				ContextActions = {
					new MenuItem ()
				}
			};

			object bc = new object ();

			cell.BindingContext = bc;
			Assert.That (cell.ContextActions [0].BindingContext, Is.SameAs (bc));

			cell = new TextCell { BindingContext = new object () };
			cell.ContextActions.Add (new MenuItem ());

			Assert.That (cell.ContextActions [0].BindingContext, Is.SameAs (cell.BindingContext));
		}

		[Test]
		public void RenderHeightINPCFromParent()
		{
			var lv = new ListView();
			var cell = new TextCell();
			cell.Parent = lv;

			int changing = 0, changed = 0;
			cell.PropertyChanging += (sender, args) => {
				if (args.PropertyName == "RenderHeight")
					changing++;
			};

			cell.PropertyChanged += (sender, args) => {
				if (args.PropertyName == "RenderHeight")
					changed++;
			};

			lv.RowHeight = 5;

			Assume.That (cell.RenderHeight, Is.EqualTo (5));

			Assert.That (changing, Is.EqualTo (1));
			Assert.That (changed, Is.EqualTo (1));
		}

		[Test]
		public async void ForceUpdateSizeCallsAreRateLimited()
		{
			var lv = new ListView { HasUnevenRows = true };
			var cell = new ViewCell { Parent = lv };

			int numberOfCalls = 0;
			cell.ForceUpdateSizeRequested += (object sender, System.EventArgs e) => { numberOfCalls++; };

			cell.ForceUpdateSize ();
			cell.ForceUpdateSize ();
			cell.ForceUpdateSize ();
			cell.ForceUpdateSize ();

			await System.Threading.Tasks.Task.Delay (TimeSpan.FromMilliseconds (150));

			Assert.AreEqual (1, numberOfCalls);
		}

		[Test]
		public async void ForceUpdateSizeWillNotBeCalledIfParentIsNotAListViewWithUnevenRows ()
		{
			var lv = new ListView { HasUnevenRows = false };
			var cell = new ViewCell { Parent = lv };

			int numberOfCalls = 0;
			cell.ForceUpdateSizeRequested += (object sender, System.EventArgs e) => { numberOfCalls++; };

			cell.ForceUpdateSize ();

			await System.Threading.Tasks.Task.Delay (TimeSpan.FromMilliseconds (16));

			Assert.AreEqual (0, numberOfCalls);
		}
	}
}