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

namespace Xamarin.Forms.Core.UnitTests
{
	public abstract class CommandSourceTests<T> : BaseTestFixture
		where T : BindableObject
	{
		[Test]
		public void TestCommand ()
		{
			var source = CreateSource();

			bool executed = false;
			source.SetValue (CommandProperty, new Command (o => {
				executed = true;
				Assert.AreEqual (source, o);
			}));

			source.SetValue (CommandParameterProperty, source);

			Activate (source);

			Assert.True (executed);
		}

		[Test]
		public void CommandCanExecuteModifiesEnabled ([Values(true, false)] bool initial)
		{
			bool canExecute = initial;
			Command command;
			var source = CreateSource();
			source.SetValue (CommandProperty, command = new Command (() => { }, () => canExecute));

			Assert.AreEqual (canExecute, source.GetValue (IsEnabledProperty));

			canExecute = !initial;
			command.ChangeCanExecute ();

			Assert.AreEqual (canExecute, source.GetValue (IsEnabledProperty));
		}

		[Test]
		public void ReenabledAfterCommandRemoved()
		{
			var source = CreateSource();
			source.SetValue (CommandProperty, new Command (() => { }, () => false));

			Assert.That (source.GetValue (IsEnabledProperty), Is.False);

			source.SetValue (CommandProperty, null);

			Assert.That (source.GetValue (IsEnabledProperty), Is.True);
		}

		[Test]
		public void CommandUnhooksOnNull ()
		{
			bool canExecute = false;
			Command command;
			var source = CreateSource();

			bool raised = false;
			source.SetValue (CommandProperty, command = new Command (() => { }, () => {
				raised = true;
				return canExecute;
			}));

			raised = false;
			source.SetValue (CommandProperty, null);

			canExecute = true;
			command.ChangeCanExecute ();

			Assert.False (raised);
		}

		[Test]
		public void CommandCanExecuteInvokedOnCommandSet ()
		{
			bool fired = false;
			Func<bool> canExecute = () => {
				fired = true;
				return true;
			};

			Assert.IsFalse (fired);
			var source = CreateSource();
			source.SetValue (CommandProperty, new Command (() => { }, canExecute));

			Assert.True (fired);
		}

		[Test]
		public void CommandCanExecuteInvokedOnCommandParameterSet ()
		{
			bool fired;
			Func<bool> canExecute = () => {
				fired = true;
				return true;
			};

			var source = CreateSource();
			source.SetValue (CommandProperty, new Command (() => { }, canExecute));

			fired = false;
			Assert.IsFalse (fired);
			source.SetValue (CommandParameterProperty, new object ());
			Assert.True (fired);
		}

		[Test]
		public void CommandCanExecuteInvokedOnChange()
		{
			bool fired;
			Func<bool> canExecute = () => {
				fired = true;
				return true;
			};

			var cmd = new Command (() => { }, canExecute);
			var source = CreateSource();
			source.SetValue (CommandProperty, cmd);

			fired = false;
			
			cmd.ChangeCanExecute();

			Assert.That (fired, Is.True, "CanExecute was not called when the event was raised");
		}

		class BoolViewModel
			: MockViewModel
		{
			bool toggle;

			public bool Toggle
			{
				get { return toggle; }
				set
				{
					if (toggle == value)
						return;

					toggle = value;
					OnPropertyChanged();
				}
			}
		}

		[Test]
		public void EnabledUpdatesDoNotRemoveBindings()
		{
			var vm = new BoolViewModel { Toggle = true };
			var source = CreateSource();
			source.BindingContext = vm;
			source.SetBinding (IsEnabledProperty, "Toggle");

			Assert.That (source.GetValue (IsEnabledProperty), Is.True);

			source.SetValue (CommandProperty, new Command (() => { }));

			Assert.That (source.GetIsBound (IsEnabledProperty), Is.True);
		}

		protected abstract T CreateSource();
		protected abstract void Activate (T source);

		protected abstract BindableProperty IsEnabledProperty
		{
			get;
		}

		protected abstract BindableProperty CommandProperty
		{
			get;
		}

		protected abstract BindableProperty CommandParameterProperty
		{
			get;
		}
	}
}