summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla31330.cs
blob: 75e30b134a5d345d7c2ba425e1d144590aa070ae (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
using System;

using Xamarin.Forms.CustomAttributes;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest.iOS;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve (AllMembers = true)]
	[Issue (IssueTracker.Bugzilla, 31330, "Disabled context actions appear enabled")]
	public class Bugzilla31330 : TestContentPage
	{
		protected override void Init ()
		{
			var vm = new ListViewModel ();
			BindingContext = vm;
			vm.Init ();
			var listview = new ListView ();
			listview.SetBinding (ListView.ItemsSourceProperty, new Binding ("Items"));
			listview.ItemTemplate = new DataTemplate (typeof(CustomTextCell));
			listview.ItemSelected += (object sender, SelectedItemChangedEventArgs e) => {
				(e.SelectedItem as ListItemViewModel).CanExecute = true;
				((e.SelectedItem as ListItemViewModel).DeleteItemCommand as Command).ChangeCanExecute ();
			};
			// Initialize ui here instead of ctor
			Content = listview;
		}

		[Preserve (AllMembers = true)]
		public class CustomTextCell : TextCell
		{
			public CustomTextCell ()
			{
				SetBinding (TextProperty, new Binding ("Title"));
				var deleteMenuItem = new MenuItem ();
				deleteMenuItem.Text = "Delete";
				deleteMenuItem.IsDestructive = true;
				deleteMenuItem.SetBinding (MenuItem.CommandProperty, new Binding ("DeleteItemCommand"));
				ContextActions.Add (deleteMenuItem);
			}
		}

		[Preserve (AllMembers = true)]
		public class ListViewModel : ViewModel
		{
			public void Init ()
			{
				Items.Add (new ListItemViewModel (this) { Title = string.Format ("Something {0}", Items.Count.ToString ()) });
				Items.Add (new ListItemViewModel (this) { Title = string.Format ("Something {0}", Items.Count.ToString ()) });
				Items.Add (new ListItemViewModel (this) { Title = string.Format ("Something {0}", Items.Count.ToString ()) });
			}

			public ObservableCollection<ListItemViewModel> Items { get; } = new ObservableCollection<ListItemViewModel>();

			ICommand _disabledCommand;

			public ICommand DisabledCommand {
				get {
					if (_disabledCommand == null) {
						_disabledCommand = new Command (() => {
						}, () => false);
					}

					return _disabledCommand;
				}
			}

			ICommand _addItemCommand;

			public ICommand AddItemCommand {
				get {
					if (_addItemCommand == null) {
						_addItemCommand = new Command (() => Items.Add (new ListItemViewModel (this) { Title = string.Format ("Something {0}", Items.Count.ToString ()) }));
					}

					return _addItemCommand;
				}
			}
		}

		[Preserve (AllMembers = true)]
		public class ListItemViewModel : ViewModel
		{
			public bool CanExecute = false;
			readonly ListViewModel _listViewModel;

			public ListItemViewModel (ListViewModel listViewModel)
			{
				if (listViewModel == null) {
					throw new ArgumentNullException ("listViewModel");
				}
				_listViewModel = listViewModel;
			}

			public string Title { get; set; }

			ICommand _deleteItemCommand;

			public ICommand DeleteItemCommand {
				get {
					if (_deleteItemCommand == null) {
						_deleteItemCommand = new Command (() => _listViewModel.Items.Remove (this), () => CanExecute);
					}

					return _deleteItemCommand;
				}
			}

			ICommand _otherCommand;

			public ICommand OtherCommand {
				get {
					if (_otherCommand == null) {
						_otherCommand = new Command (() => {
						}, () => false);
					}

					return _otherCommand;
				}
			}
		}

		#if UITEST
		[Test]
		public void Bugzilla31330Test ()
		{
			RunningApp.WaitForElement (c => c.Marked ("Something 2"));
			var screenBounds = RunningApp.Query (q => q.Raw ("* index:0"))[0].Rect;

			var cell = RunningApp.Query (c => c.Marked ("Something 1")) [0];
			var cell2 = RunningApp.Query (c => c.Marked ("Something 2")) [0];
#if __IOS__
			RunningApp.DragCoordinates (screenBounds.Width - 10, cell.Rect.CenterY, 0, cell.Rect.CenterY);
			RunningApp.WaitForElement (c => c.Marked ("Delete"));
			RunningApp.Tap (c => c.Marked ("Delete"));
			RunningApp.WaitForElement (c => c.Marked ("Something 1"));
			RunningApp.Tap (c => c.Marked ("Something 2"));
			RunningApp.DragCoordinates (screenBounds.Width - 10, cell2.Rect.CenterY, 0, cell2.Rect.CenterY);
			RunningApp.Tap (c => c.Marked ("Delete"));
			RunningApp.WaitForNoElement (c => c.Marked ("Something 2"));
#else
			RunningApp.TouchAndHoldCoordinates (cell.Rect.CenterX, cell.Rect.CenterY);
			RunningApp.WaitForElement (c => c.Marked ("Delete"));
			RunningApp.Tap (c => c.Marked ("Delete"));
			RunningApp.Back ();
			RunningApp.WaitForElement (c => c.Marked ("Something 1"));
			RunningApp.Tap (c => c.Marked ("Something 2"));
			RunningApp.TouchAndHoldCoordinates (cell2.Rect.CenterX, cell2.Rect.CenterY);
			RunningApp.Tap (c => c.Marked ("Delete"));
			RunningApp.WaitForNoElement (c => c.Marked ("Something 2"));
#endif
		}
#endif
	}
}