forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllViewsTests.cs
More file actions
184 lines (158 loc) · 4.71 KB
/
AllViewsTests.cs
File metadata and controls
184 lines (158 loc) · 4.71 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;
using System.IO;
namespace Terminal.Gui.Views {
public class AllViewsTests {
[Fact]
public void AllViews_Tests_All_Constructors ()
{
Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
foreach (var type in GetAllViewClassesCollection ()) {
Assert.True (Constructors_FullTest (type));
}
Application.Shutdown ();
}
public bool Constructors_FullTest (Type type)
{
foreach (var ctor in type.GetConstructors ()) {
var view = GetTypeInitializer (type, ctor);
if (view != null) {
Assert.True (type.FullName == view.GetType ().FullName);
}
}
return true;
}
private static View GetTypeInitializer (Type type, ConstructorInfo ctor)
{
View viewType = null;
if (type.IsGenericType && type.IsTypeDefinition) {
List<Type> gTypes = new List<Type> ();
foreach (var args in type.GetGenericArguments ()) {
gTypes.Add (typeof (object));
}
type = type.MakeGenericType (gTypes.ToArray ());
Assert.IsType (type, (View)Activator.CreateInstance (type));
} else {
ParameterInfo [] paramsInfo = ctor.GetParameters ();
Type paramType;
List<object> pTypes = new List<object> ();
if (type.IsGenericType) {
foreach (var args in type.GetGenericArguments ()) {
paramType = args.GetType ();
if (args.Name == "T") {
pTypes.Add (typeof (object));
} else {
AddArguments (paramType, pTypes);
}
}
}
foreach (var p in paramsInfo) {
paramType = p.ParameterType;
if (p.HasDefaultValue) {
pTypes.Add (p.DefaultValue);
} else {
AddArguments (paramType, pTypes);
}
}
if (type.IsGenericType && !type.IsTypeDefinition) {
viewType = (View)Activator.CreateInstance (type);
Assert.IsType (type, viewType);
} else {
viewType = (View)ctor.Invoke (pTypes.ToArray ());
Assert.IsType (type, viewType);
}
}
return viewType;
}
private static void AddArguments (Type paramType, List<object> pTypes)
{
if (paramType == typeof (Rect)) {
pTypes.Add (Rect.Empty);
} else if (paramType == typeof (NStack.ustring)) {
pTypes.Add (NStack.ustring.Empty);
} else if (paramType == typeof (int)) {
pTypes.Add (0);
} else if (paramType == typeof (bool)) {
pTypes.Add (true);
} else if (paramType.Name == "IList") {
pTypes.Add (new List<object> ());
} else if (paramType.Name == "View") {
var top = new Toplevel ();
var view = new View ();
top.Add (view);
pTypes.Add (view);
} else if (paramType.Name == "View[]") {
pTypes.Add (new View [] { });
} else if (paramType.Name == "Stream") {
pTypes.Add (new MemoryStream ());
} else if (paramType.Name == "String") {
pTypes.Add (string.Empty);
} else if (paramType.Name == "TreeView`1[T]") {
pTypes.Add (string.Empty);
} else {
pTypes.Add (null);
}
}
List<Type> GetAllViewClassesCollection ()
{
List<Type> types = new List<Type> ();
foreach (Type type in typeof (View).Assembly.GetTypes ()
.Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsPublic && myType.IsSubclassOf (typeof (View)))) {
types.Add (type);
}
return types;
}
[Fact]
public void AllViews_Enter_Leave_Events ()
{
foreach (var type in GetAllViewClassesCollection ()) {
Application.Init (new FakeDriver (), new FakeMainLoop (() => FakeConsole.ReadKey (true)));
var top = Application.Top;
var vType = GetTypeInitializer (type, type.GetConstructor (Array.Empty<Type> ()));
if (vType == null) {
Application.Shutdown ();
continue;
}
vType.X = 0;
vType.Y = 0;
vType.Width = 10;
vType.Height = 1;
var view = new View () {
X = 0,
Y = 1,
Width = 10,
Height = 1,
CanFocus = true
};
var vTypeEnter = 0;
var vTypeLeave = 0;
var viewEnter = 0;
var viewLeave = 0;
vType.Enter += _ => vTypeEnter++;
vType.Leave += _ => vTypeLeave++;
view.Enter += _ => viewEnter++;
view.Leave += _ => viewLeave++;
top.Add (vType, view);
Application.Begin (top);
if (!vType.CanFocus || (vType is Toplevel && ((Toplevel)vType).Modal)) {
Application.Shutdown ();
continue;
}
if (vType is TextView) {
top.ProcessKey (new KeyEvent (Key.Tab | Key.CtrlMask, new KeyModifiers ()));
} else {
top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
}
top.ProcessKey (new KeyEvent (Key.Tab, new KeyModifiers ()));
Assert.Equal (2, vTypeEnter);
Assert.Equal (1, vTypeLeave);
Assert.Equal (1, viewEnter);
Assert.Equal (1, viewLeave);
Application.Shutdown ();
}
}
}
}