Welcome to mirror list, hosted at ThFree Co, Russian Federation.

EventViewModelTests.cs « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 994e4178668d7d1284f3ec39ff41776621689dfe (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Tests
{
	[TestFixture]
	internal class EventViewModelTests
	{
		[Test]
		public void MethodName()
		{
			const string name = "name";
			var info = new Mock<IEventInfo> ();
			info.SetupGet (i => i.Name).Returns (name);

			var editor = new MockObjectEditor {
				Events = new[] { info.Object }
			};
			const string handler = "handler";
			editor.AttachHandlerAsync (info.Object, handler);

			var ev = new EventViewModel (MockEditorProvider.MockPlatform, info.Object, new[] { editor });

			Assert.That (ev.MethodName, Is.EqualTo (handler));
		}

		[Test]
		public void SetMethodName()
		{
			const string name = "name";
			var info = new Mock<IEventInfo> ();
			info.SetupGet (i => i.Name).Returns (name);

			var editor = new Mock<IObjectEditor> ();
			var eeditor = editor.As<IObjectEventEditor> ();
			eeditor.SetupGet (e => e.Events).Returns (new[] { info.Object });

			var vm = new EventViewModel (MockEditorProvider.MockPlatform, info.Object, new[] { editor.Object });
			Assume.That (vm.MethodName, Is.Null);

			bool changed = false;
			vm.PropertyChanged += (o, e) => {
				if (e.PropertyName == nameof (EventViewModel.MethodName))
					changed = true;
			};

			const string newMethodName = "foo";
			vm.MethodName = newMethodName;

			Assert.That (changed, Is.True);
			eeditor.Verify (i => i.AttachHandlerAsync (info.Object, newMethodName));
		}

		[Test]
		public void ChangeMethodName ()
		{
			const string name = "name";
			var info = new Mock<IEventInfo> ();
			info.SetupGet (i => i.Name).Returns (name);

			const string oldHandler = "oldHandler";
			var editor = new Mock<IObjectEditor> ();
			var eeditor = editor.As<IObjectEventEditor> ();
			eeditor.SetupGet (e => e.Events).Returns (new[] { info.Object });
			eeditor.Setup (e => e.GetHandlersAsync (info.Object)).ReturnsAsync (new[] { oldHandler });
			var vm = new EventViewModel (MockEditorProvider.MockPlatform, info.Object, new[] { editor.Object });
			Assume.That (vm.MethodName, Is.EqualTo (oldHandler));

			bool changed = false;
			vm.PropertyChanged += (o, e) => {
				if (e.PropertyName == nameof (EventViewModel.MethodName))
					changed = true;
			};

			const string newMethodName = "foo";
			vm.MethodName = newMethodName;

			Assert.That (changed, Is.True);
			eeditor.Verify (i => i.DetachHandlerAsync (info.Object, oldHandler));
			eeditor.Verify (i => i.AttachHandlerAsync (info.Object, newMethodName));
		}

		[Test]
		public void DisagreeingValues()
		{
			const string name = "name";
			var info = new Mock<IEventInfo> ();
			info.SetupGet (i => i.Name).Returns (name);

			var editor = new MockObjectEditor {
				Events = new[] { info.Object }
			};
			const string handler = "handler";
			editor.AttachHandlerAsync (info.Object, handler);

			var editor2 = new MockObjectEditor {
				Events = new[] { info.Object }
			};
			editor.AttachHandlerAsync (info.Object, "handler2");

			var ev = new EventViewModel (MockEditorProvider.MockPlatform, info.Object, new[] { editor, editor2 });

			Assert.That (ev.MethodName, Is.Null);
			Assert.That (ev.MultipleValues, Is.True);
		}
	}
}