summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/EventsConnection.xaml.cs
blob: 5727693e58ef71b25baf0e45b9cfb43dd6fb4c8d (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
using System;
using System.Collections.Generic;

using Xamarin.Forms;
using NUnit.Framework;

namespace Xamarin.Forms.Xaml.UnitTests
{
	public class BaseForEvents : ContentView
	{
		protected int baseClicked;
		protected void HandleClickedOnBase (object sender, EventArgs e)
		{
			baseClicked++;
		}
	}

	public class ElementWithEvent : ContentView
	{
		public event EventHandler Clicked;

		public void SendClicked ()
		{
			var eh = Clicked;
			if (eh != null)
				eh (this, EventArgs.Empty);
		}
	}

	public class ElementWithGenericEvent : ContentView
	{
		public event EventHandler<ItemTappedEventArgs> Clicked;

		public void SendClicked ()
		{
			var eh = Clicked;
			if (eh != null)
				eh (this, new ItemTappedEventArgs ("foo", "bar"));
		}
	}

	public partial class EventsConnection : BaseForEvents
	{
		public EventsConnection ()
		{
			InitializeComponent ();
		}

		public EventsConnection (bool useCompiledXaml)
		{
			//this stub will be replaced at compile time
		}

		int clicked;
		public void HandleClicked (object sender, EventArgs e)
		{
			clicked++;
		}

		int genericClicked;
		public void HandleGenericClicked (object sender, ItemTappedEventArgs e)
		{
			genericClicked++;
		}

		int asyncPrivateClicked;
#pragma warning disable 1998 // considered for removal
		async void HandleClickedPrivateAsync (object sender, EventArgs e)
#pragma warning restore 1998
		{
			asyncPrivateClicked++;
		}

		[TestFixture]
		public class Tests
		{
			[TestCase (false)]
			[TestCase (true)]
			public void TestClicked (bool useCompiledXaml)
			{
				var layout = new EventsConnection (useCompiledXaml);
				Assert.AreEqual (0, layout.clicked);
				layout.elementWithEvent.SendClicked ();
				Assert.AreEqual (1, layout.clicked);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void TestGenericClicked (bool useCompiledXaml)
			{
				var layout = new EventsConnection (useCompiledXaml);
				Assert.AreEqual (0, layout.genericClicked);
				layout.elementWithGenericEvent.SendClicked ();
				Assert.AreEqual (1, layout.genericClicked);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void TestHandlerOnBase (bool useCompiledXaml)
			{
				var layout = new EventsConnection (useCompiledXaml);
				Assert.AreEqual (0, layout.baseClicked);
				layout.eventHandlerOnBase.SendClicked ();
				Assert.AreEqual (1, layout.baseClicked);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void TestAsyncPrivateHandler (bool useCompiledXaml)
			{
				var layout = new EventsConnection (useCompiledXaml);
				Assert.AreEqual (0, layout.asyncPrivateClicked);
				layout.elementwithAsyncprivateHandler.SendClicked ();
				Assert.AreEqual (1, layout.asyncPrivateClicked);
			}
		}
	}
}