summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla58833.cs
blob: 9979227ab5b9bcfd38b9247bfad4f0f092332b49 (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
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.Generic;
using System.Diagnostics;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
	[Category(UITestCategories.Gestures)]
	[Category(UITestCategories.ListView)]
	[Category(UITestCategories.Cells)]
#endif

	[Preserve(AllMembers = true)]
	[Issue(IssueTracker.Bugzilla, 58833, "ListView SelectedItem Binding does not fire", PlatformAffected.Android)]
	public class Bugzilla58833 : TestContentPage
	{
		const string ItemSelectedSuccess = "ItemSelected Success";
		const string TapGestureSucess = "TapGesture Fired";
		Label _resultLabel;
		static Label s_tapGestureFired;

		[Preserve(AllMembers = true)]
		class TestCell : ViewCell
		{
			readonly Label _content;

			internal static int s_index;

			public TestCell()
			{
				_content = new Label();

				if (s_index % 2 == 0)
				{
					_content.GestureRecognizers.Add(new TapGestureRecognizer
					{
						Command = new Command(() =>
						{
							s_tapGestureFired.Text = TapGestureSucess;
						})
					});
				}

				View = _content;
				ContextActions.Add(new MenuItem { Text = s_index++ + " Action" });
			}

			protected override void OnBindingContextChanged()
			{
				base.OnBindingContextChanged();
				_content.Text = (string)BindingContext;
			}
		}

		protected override void Init()
		{
			TestCell.s_index = 0;

			_resultLabel = new Label { Text = "Testing..." };
			s_tapGestureFired = new Label { Text = "Testing..." };

			var items = new List<string>();
			for (int i = 0; i < 5; i++)
				items.Add($"Item #{i}");

			var list = new ListView
			{
				ItemTemplate = new DataTemplate(typeof(TestCell)),
				ItemsSource = items
			};
			list.ItemSelected += List_ItemSelected;

			Content = new StackLayout
			{
				Children = {
					_resultLabel,
					s_tapGestureFired,
					list
				}
			};
		}

		void List_ItemSelected(object sender, SelectedItemChangedEventArgs e)
		{
			_resultLabel.Text = ItemSelectedSuccess;
		}

#if UITEST
		protected override bool Isolate => true;

		[Test]
		public void Bugzilla58833Test()
		{
			// Item #1 should not have a tap gesture, so it should be selectable
			RunningApp.WaitForElement(q => q.Marked("Item #1"));
			RunningApp.Tap(q => q.Marked("Item #1"));
			RunningApp.WaitForElement(q => q.Marked(ItemSelectedSuccess));

			// Item #2 should have a tap gesture
			RunningApp.WaitForElement(q => q.Marked("Item #2"));
			RunningApp.Tap(q => q.Marked("Item #2"));
			RunningApp.WaitForElement(q => q.Marked(TapGestureSucess));

			// Both items should allow access to the context menu
			RunningApp.ActivateContextMenu("Item #2");
			RunningApp.WaitForElement("2 Action");
#if __ANDROID__
			RunningApp.Back();
#else 
			RunningApp.Tap(q => q.Marked("Item #3"));
#endif

			RunningApp.ActivateContextMenu("Item #1");
			RunningApp.WaitForElement("1 Action");
#if __ANDROID__
			RunningApp.Back();
#else 
			RunningApp.Tap(q => q.Marked("Item #3"));
#endif

		}
#endif
		}
}