summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla31333.cs
blob: 830982eafdc17f8e672817527133141342f825b3 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

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

#endif

namespace Xamarin.Forms.Controls.Issues
{
	[Preserve (AllMembers=true)]
	[Issue (IssueTracker.Bugzilla, 31333,
		"Focus() on Entry in ViewCell brings up keyboard, but doesn't have cursor in EditText", PlatformAffected.Android)]
	public class Bugzilla31333 : TestContentPage
	{
		[Preserve (AllMembers=true)]
		public class Model31333 : INotifyPropertyChanged
		{
			public string Data
			{
				get { return _data; }
				set
				{
					_data = value;
					OnPropertyChanged ();
				}
			}

			bool _isFocused = false;
			string _data;

			public bool IsFocused
			{
				get { return _isFocused; }
				set
				{
					_isFocused = value;
					OnPropertyChanged ();
				}
			}

			public event PropertyChangedEventHandler PropertyChanged;

			protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null)
			{
				PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
			}
		}

		[Preserve (AllMembers=true)]
		public interface IHaveControlFocusedProperty
		{
			void SetBinding ();
		}

		[Preserve (AllMembers=true)]
		public class ExtendedEntry : Entry, IHaveControlFocusedProperty
		{
			public static readonly BindableProperty IsControlFocusedProperty =
				BindableProperty.Create ("IsControlFocused", typeof(bool), typeof(ExtendedEntry), false);

			public bool IsControlFocused
			{
				get { return (bool)GetValue (IsControlFocusedProperty); }
				set { SetValue (IsControlFocusedProperty, value); }
			}

			protected override void OnPropertyChanged (string propertyName = null)
			{
				base.OnPropertyChanged (propertyName);
				if (propertyName == IsControlFocusedProperty.PropertyName) {
					if (IsControlFocused) {
						Focus ();
					} else {
						Unfocus ();
					}
				}
			}

			public void SetBinding ()
			{
				this.SetBinding (IsControlFocusedProperty, "IsFocused");
			}
		}

		[Preserve (AllMembers=true)]
		public class ExtendedEditor : Editor, IHaveControlFocusedProperty
		{
			public static readonly BindableProperty IsControlFocusedProperty =
				BindableProperty.Create ("IsControlFocused", typeof(bool), typeof(ExtendedEditor), false);

			public bool IsControlFocused
			{
				get { return (bool)GetValue (IsControlFocusedProperty); }
				set { SetValue (IsControlFocusedProperty, value); }
			}

			protected override void OnPropertyChanged (string propertyName = null)
			{
				base.OnPropertyChanged (propertyName);
				if (propertyName == IsControlFocusedProperty.PropertyName) {
					if (IsControlFocused) {
						Focus ();
					} else {
						Unfocus ();
					}
				}
			}

			public void SetBinding ()
			{
				this.SetBinding (IsControlFocusedProperty, "IsFocused");
			}
		}

		[Preserve (AllMembers=true)]
		public class ExtendedCell<T> : ViewCell where T : View, IHaveControlFocusedProperty
		{
			public ExtendedCell ()
			{
				var control = (T)Activator.CreateInstance (typeof(T));
				control.SetBinding ();
				control.HorizontalOptions = LayoutOptions.FillAndExpand;

				View = new StackLayout {
					Orientation = StackOrientation.Horizontal,
					HorizontalOptions = LayoutOptions.StartAndExpand,
					Children = {
						control
					}
				};
			}
		}

		StackLayout CreateListViewTestSection (Type controlType)
		{
			var name = controlType.GenericTypeArguments[0].Name;
			name = name.Replace ("Extended", "");

			var button = new Button () { Text = $"Focus {name} in ListView" };

			var data = new ObservableCollection<Model31333> { new Model31333 () };

			var listView = new ListView {
				VerticalOptions = LayoutOptions.Start,
				ItemsSource = data,
				ItemTemplate = new DataTemplate (controlType)
			};

			button.Clicked += (sender, args) => {
				var item = data[0];
				if (item != null) {
					item.IsFocused = !item.IsFocused;
				}
			};

			return new StackLayout () { Children = { button, listView } };
		}

		StackLayout CreateTableViewTestSection<T> () where T : View, IHaveControlFocusedProperty
		{
			var name = typeof(T).Name;
			name = name.Replace ("Extended", "");

			var button = new Button () { Text = $"Focus {name} in Table" };

			var data = new Model31333 ();

			var tableView = new TableView {
				VerticalOptions = LayoutOptions.Start
			};

			var tableRoot = new TableRoot();
			var tableSection = new TableSection();

			var cell = new ExtendedCell<T> ();

			cell.BindingContext = data;

			tableSection.Add(cell);
			tableRoot.Add (tableSection);
			tableView.Root = tableRoot;

			button.Clicked += (sender, args) => {
				var item = data;
				if (item != null) {
					item.IsFocused = !item.IsFocused;
				}
			};

			return new StackLayout () { Children = { button, tableView } };
		}

		protected override void Init ()
		{
			var entrySection = CreateListViewTestSection (typeof(ExtendedCell<ExtendedEntry>));
			var editorSection = CreateListViewTestSection (typeof(ExtendedCell<ExtendedEditor>));

			var entryTableSection = CreateTableViewTestSection<ExtendedEntry> ();
			var editorTableSection = CreateTableViewTestSection<ExtendedEditor> ();

			Content = new StackLayout () { Children = { entrySection, editorSection, entryTableSection, editorTableSection } };
		}

#if UITEST
		[Test]
#if __MACOS__
		[Ignore("EnterText on UITest.Desktop not implemented")]
#endif
		[UiTest (typeof(NavigationPage))]
		public void Issue31333FocusEntryInListViewCell ()
		{
			RunningApp.Tap (q => q.Marked ("Focus Entry in ListView"));
			RunningApp.Screenshot ("Entry control in ListView cell is focused");
			RunningApp.EnterText ("Entry in ListView Success");
			Assert.True(RunningApp.Query(query => query.Text("Entry in ListView Success")).Length > 0);
			RunningApp.Screenshot ("Entry in ListView Success");
			RunningApp.Tap(q => q.Marked("Focus Entry in ListView"));
		}

		[Test]
#if __MACOS__
		[Ignore("EnterText on UITest.Desktop not implemented")]
#endif
		[UiTest (typeof(NavigationPage))]
		public void Issue31333FocusEditorInListViewCell ()
		{
			RunningApp.Tap (q => q.Marked ("Focus Editor in ListView"));
			RunningApp.Screenshot ("Editor control in ListView cell is focused");
			RunningApp.EnterText ("Editor in ListView Success");
			Assert.True(RunningApp.Query(query => query.Text("Editor in ListView Success")).Length > 0);
			RunningApp.Screenshot ("Editor in ListView Success");
			RunningApp.Tap(q => q.Marked("Focus Editor in ListView"));
		}

		
		[Test]
#if __MACOS__
		[Ignore("EnterText on UITest.Desktop not implemented")]
#endif
		[UiTest (typeof(NavigationPage))]
		public void Issue31333FocusEntryInTableViewCell ()
		{
			RunningApp.Tap (q => q.Marked ("Focus Entry in Table"));
			RunningApp.Screenshot ("Entry control in TableView cell is focused");
			RunningApp.EnterText ("Entry in TableView Success");
			Assert.True(RunningApp.Query(query => query.Text("Entry in TableView Success")).Length > 0);
			RunningApp.Screenshot ("Entry in TableView Success");
			RunningApp.Tap(q => q.Marked("Focus Entry in Table"));
		}

		[Test]
#if __MACOS__
		[Ignore("EnterText on UITest.Desktop not implemented")]
#endif
		[UiTest (typeof(NavigationPage))]
		public void Issue31333FocusEditorInTableViewCell ()
		{
			RunningApp.Tap (q => q.Marked ("Focus Editor in Table"));
			RunningApp.Screenshot ("Editor control in TableView cell is focused");
			RunningApp.EnterText ("Editor in TableView Success");
			Assert.True(RunningApp.Query(query => query.Text("Editor in TableView Success")).Length > 0);
			RunningApp.Screenshot ("Editor in TableView Success");
			RunningApp.Tap(q => q.Marked("Focus Editor in Table"));
		}
#endif
	}
}