-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Bugzilla32871.cs
187 lines (157 loc) · 4.45 KB
/
Bugzilla32871.cs
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
using System;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.ComponentModel;
#if UITEST
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif
namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[NUnit.Framework.Category(UITestCategories.Bugzilla)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Bugzilla, 32871, "Numeric Keyboard does not work when text has a binding to a value", PlatformAffected.Default)]
public class Bugzilla32871 : TestContentPage
{
public static class Ids
{
public const string ValueEntry = nameof(ValueEntry);
public const string ValueLabel = nameof(ValueLabel);
public const string TypeEntry = nameof(TypeEntry);
public const string AddHorizontal = nameof(AddHorizontal);
public const string TypeLabel = nameof(TypeLabel);
public const string BindButton = nameof(BindButton);
}
[Preserve(AllMembers = true)]
public class ViewModel<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
public ViewModel() { }
public T _value;
public T Value
{
get { return _value; }
set
{
_value = value;
s_labelValue.Text = $"value: {value}";
OnPropertyChanged(nameof(Value));
}
}
}
public static Label s_labelValue;
protected override void Init()
{
var stack = new StackLayout();
// text to be coerced
var entryValue = new Entry() { AutomationId = Ids.ValueEntry };
stack.Children.Add(entryValue);
// feedback of the value coerced from text
s_labelValue = new Label() { Text = "value: [none]", AutomationId = Ids.ValueLabel };
stack.Children.Add(s_labelValue);
// the type being coerced from text
var entryType = new Entry() { AutomationId = Ids.TypeEntry };
stack.Children.Add(entryType);
// feedback of the type being coerced from text
var labelType = new Label() { Text = "type: [none]", AutomationId = Ids.TypeLabel };
stack.Children.Add(labelType);
// create view model of the type being coerced; clear text
var bindButton = new Button() { Text = "Bind", AutomationId = Ids.BindButton };
stack.Children.Add(bindButton);
bindButton.Clicked += (o, e) =>
{
try
{
var type = Type.GetType(entryType.Text);
BindingContext = Activator.CreateInstance(
typeof(ViewModel<>).MakeGenericType(type)
);
entryValue.Text = string.Empty;
labelType.Text = $"type: {type.FullName}";
}
catch (Exception ex)
{
labelType.Text = $"type: {ex.Message}";
}
entryValue.SetBinding(Entry.TextProperty, "Value");
};
this.Content = stack;
}
#if UITEST
// [Test]
public void Issue32871Test()
{
RunningApp.WaitForElement(q => q.Marked(Ids.BindButton));
var types = new Type[] {
typeof(double),
typeof(int),
typeof(float),
typeof(decimal),
};
var values = new string[] {
"0.1",
"0",
"-0.1",
"-1.1",
"-.1",
".1",
"00",
"01",
};
foreach (var type in types)
Theory(type, values);
}
public bool TryParse(Type type, string value, ref string result)
{
if (value.EndsWith("."))
return false;
if (value == "-0")
return false;
try
{
result = $"{Convert.ChangeType(value, type)}";
return true;
}
catch
{
return false;
}
}
public void Theory(Type type, string[] values)
{
RunningApp.ClearText(Ids.TypeEntry);
RunningApp.EnterText(Ids.TypeEntry, type.FullName);
RunningApp.Tap(Ids.BindButton);
RunningApp.WaitForElement(q => q.Marked($"type: {type.FullName}"));
foreach (var value in values)
Theory(type, value);
}
public void Theory(Type type, string value)
{
try
{ Convert.ChangeType(value, type); }
catch { return; }
Console.WriteLine($"TEST CASE: type={type.FullName}, value={value}");
RunningApp.ClearText(Ids.ValueEntry);
var input = string.Empty;
var output = string.Empty;
foreach (var c in value)
{
input += c;
RunningApp.EnterText(Ids.ValueEntry, $"{c}");
if (TryParse(type, input, ref output))
input = output;
if (string.IsNullOrEmpty(output))
continue;
RunningApp.WaitForElement(q => q.Marked($"value: {output}"));
}
if (!Equals(Convert.ChangeType(value, type), Convert.ChangeType(output, type)))
throw new Exception($"Value '{value}' entered as '{output}'.");
}
#endif
}
}