-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTest_IMaaResource.cs
More file actions
249 lines (214 loc) · 8.83 KB
/
Test_IMaaResource.cs
File metadata and controls
249 lines (214 loc) · 8.83 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
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
using MaaFramework.Binding.Abstractions;
using MaaFramework.Binding.Buffers;
using MaaFramework.Binding.Notification;
using System.Text.Json;
namespace MaaFramework.Binding.UnitTests;
/// <summary>
/// Test <see cref="IMaaResource"/> and <see cref="MaaResource"/>.
/// </summary>
[TestClass]
// ReSharper disable once InconsistentNaming
public class Test_IMaaResource
{
public static Dictionary<MaaTypes, object> NewData => new()
{
#if MAA_NATIVE
{ MaaTypes.Native, new MaaResource() },
#endif
};
public static Dictionary<MaaTypes, object> ImageData = new()
{
#if MAA_NATIVE
{ MaaTypes.Native, MaaImage.Load<Buffers.MaaImageBuffer>(Common.ImagePath).Buffer },
#endif
};
public static Dictionary<MaaTypes, object> Data { get; private set; } = default!;
public static Dictionary<MaaTypes, object> UnloadedData { get; private set; } = default!;
[ClassInitialize]
public static void InitializeClass(TestContext context)
{
UnloadedData = NewData;
Data = NewData;
foreach (var data in Data.Values.Cast<IMaaResource>())
{
Assert.IsFalse(data.IsInvalid);
// 3 ways to subscribe to the Callback event
data.Callback += Common.OnCallback;
data.Callback += Common.NotificationHandlerRegistry.OnCallback;
data.Callback += Common.OnResourceLoading.ToCallback();
_ = data.SetInference_UseCpu();
}
}
[ClassCleanup(ClassCleanupBehavior.EndOfClass)]
public static void CleanUpClass()
{
Common.DisposeData(Data.Values.Cast<IMaaDisposable>());
Common.DisposeData(UnloadedData.Values.Cast<IMaaDisposable>());
Common.DisposeData(ImageData.Values.Cast<IMaaDisposable>());
}
#pragma warning disable S2699 // Tests should include assertions
[TestMethod]
public void CreateInstances()
{
#if MAA_NATIVE
using var native1 = new MaaResource();
using var native2 = new MaaResource(Common.BundlePath, Common.BundlePath);
using var native3 = new MaaResource(CheckStatusOption.None, Common.BundlePath);
using var native4 = new MaaResource(new List<string> { Common.BundlePath, Common.BundlePath });
using var native5 = new MaaResource(CheckStatusOption.None, new List<string>());
#endif
}
#pragma warning restore S2699 // Tests should include assertions
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_AppendBundle_IsLoaded(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
Assert.IsTrue(
maaResource.IsLoaded);
var job =
maaResource.AppendBundle(Common.BundlePath);
Interface_IMaaPost_Success(job);
Assert.IsTrue(
maaResource.IsLoaded);
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_AppendOcrModel(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
// OCR model path may not exist in test environment, but the API should work
var job = maaResource.AppendOcrModel(Common.BundlePath);
Assert.AreNotEqual(
MaaJobStatus.Invalid, job.Status);
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_AppendPipeline(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
var job = maaResource.AppendPipeline(Common.BundlePath);
Interface_IMaaPost_Success(job);
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_AppendImage(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
var job = maaResource.AppendImage(Common.BundlePath);
Interface_IMaaPost_Success(job);
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_OverridePipeline_OverrideNext_GetNodeData(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
var DiffParam = Custom.DiffParam;
var DiffEntry = Custom.DiffEntry;
Assert.IsTrue(
maaResource.OverridePipeline(DiffParam));
Assert.IsTrue(
maaResource.OverrideNext(DiffEntry, [DiffEntry]));
Assert.IsTrue(
maaResource.GetNodeData(DiffEntry, out var data));
Assert.IsNotNull(data);
using var document = JsonDocument.Parse(data);
var root = document.RootElement;
Assert.IsTrue(root.TryGetProperty("next", out var nextElement),
"Expected JSON to contain a 'next' property.");
Assert.AreEqual(JsonValueKind.Array, nextElement.ValueKind,
"Expected 'next' to be a JSON array.");
var containsDiffEntry = nextElement
.EnumerateArray()
.Any(element =>
element.ValueKind == JsonValueKind.Object &&
element.TryGetProperty("name", out var nameProperty) &&
nameProperty.GetString() == DiffEntry);
Assert.IsTrue(containsDiffEntry,
$"Expected 'next' array to contain an element with name '{DiffEntry}'.");
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_OverrideImage(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
Assert.IsTrue(
maaResource.OverrideImage("NewImageName", (IMaaImageBuffer)ImageData[type]));
}
private static void Interface_IMaaPost_Success(MaaJob job)
{
Assert.AreNotEqual(
MaaJobStatus.Invalid, job.Status);
Assert.AreEqual(
MaaJobStatus.Succeeded, job.Wait());
Assert.AreEqual(
$"MaaJob {{ Status = Succeeded, Id = {job.Id} }}", job.ToString());
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(UnloadedData))]
public void Interface_Hash_NodeList(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
Assert.IsTrue(
string.IsNullOrWhiteSpace(maaResource.Hash));
Assert.AreEqual(
0, maaResource.NodeList.Count);
Interface_IMaaPost_Success(
maaResource.AppendBundle(Common.BundlePath));
Assert.IsFalse(
string.IsNullOrWhiteSpace(maaResource.Hash));
Assert.AreNotEqual(
0, maaResource.NodeList.Count);
Assert.IsTrue(
maaResource.NodeList.Any(static s => s == "EmptyNode"));
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.InferenceDevice, (int)InferenceDevice.CPU)]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.InferenceDevice, InferenceDevice.CPU)]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.InferenceExecutionProvider, (int)InferenceExecutionProvider.CPU)]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.InferenceExecutionProvider, InferenceExecutionProvider.CPU)]
public void Interface_SetOption(MaaTypes type, IMaaResource maaResource, ResourceOption opt, object arg)
{
Assert.IsNotNull(maaResource);
Assert.IsTrue(
maaResource.SetOption(opt, arg));
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_Clear(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
Assert.IsTrue(
maaResource.Clear());
Assert.IsTrue(
maaResource.Clear(includeCustomResource: true));
}
#region Invalid data tests
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.Invalid, "Anything")]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.InferenceDevice, 0.0)]
[MaaData(MaaTypes.All, nameof(Data), ResourceOption.InferenceExecutionProvider, 0.0)]
public void Interface_SetOption_InvalidData(MaaTypes type, IMaaResource maaResource, ResourceOption opt, object arg)
{
Assert.IsNotNull(maaResource);
_ = Assert.ThrowsExactly<NotSupportedException>(()
=> maaResource.SetOption(opt, arg));
}
[TestMethod]
[MaaData(MaaTypes.All, nameof(Data))]
public void Interface_Register_Unregister_InvalidData(MaaTypes type, IMaaResource maaResource)
{
Assert.IsNotNull(maaResource);
_ = Assert.ThrowsExactly<NotImplementedException>(()
=> maaResource.Register(Custom.InvalidResource));
_ = Assert.ThrowsExactly<NotImplementedException>(()
=> maaResource.Register(nameof(Custom.InvalidResource), Custom.InvalidResource));
_ = Assert.ThrowsExactly<NotImplementedException>(()
=> maaResource.Unregister(Custom.InvalidResource));
_ = Assert.ThrowsExactly<NotImplementedException>(()
=> maaResource.Unregister<Custom.TestInvalidResource>(nameof(Custom.InvalidResource)));
_ = Assert.ThrowsExactly<NotImplementedException>(()
=> maaResource.Clear<Custom.TestInvalidResource>());
}
#endregion
}