-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathDelegateTests.cs
More file actions
199 lines (170 loc) · 8.21 KB
/
DelegateTests.cs
File metadata and controls
199 lines (170 loc) · 8.21 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices.JavaScript;
using System.Collections.Generic;
using Xunit;
namespace System.Runtime.InteropServices.JavaScript.Tests
{
public static class DelegateTests
{
private static Function _objectPrototype;
[Fact]
public static void InvokeFunction()
{
HelperMarshal._functionResultValue = 0;
HelperMarshal._i32Value = 0;
Runtime.InvokeJS(@"
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
var res = funcDelegate (10, 20);
App.call_test_method (""InvokeI32"", [ res, res ]);
");
Assert.Equal(30, HelperMarshal._functionResultValue);
Assert.Equal(60, HelperMarshal._i32Value);
}
[Fact]
public static void InvokeFunctionInLoopUsingConstanceValues()
{
HelperMarshal._functionResultValue = 0;
HelperMarshal._i32Value = 0;
Runtime.InvokeJS(@"
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
var res = funcDelegate (10, 20);
for (x = 0; x < 1000; x++)
{
res = funcDelegate (10, 20);
}
App.call_test_method (""InvokeI32"", [ res, res ]);
");
Assert.Equal(30, HelperMarshal._functionResultValue);
Assert.Equal(60, HelperMarshal._i32Value);
}
[Fact]
public static void InvokeFunctionInLoopUsingIncrementedValues()
{
HelperMarshal._functionResultValue = 0;
HelperMarshal._i32Value = 0;
Runtime.InvokeJS(@"
var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
var res = funcDelegate (10, 20);
for (x = 0; x < 1000; x++)
{
res = funcDelegate (x, x);
}
App.call_test_method (""InvokeI32"", [ res, res ]);
");
Assert.Equal(1998, HelperMarshal._functionResultValue);
Assert.Equal(3996, HelperMarshal._i32Value);
}
[Fact]
public static void InvokeActionTReturnedByInvokingFuncT()
{
HelperMarshal._functionActionResultValue = 0;
HelperMarshal._functionActionResultValueOfAction = 0;
Runtime.InvokeJS(@"
var funcDelegate = App.call_test_method (""CreateFunctionDelegateWithAction"", [ ]);
var actionDelegate = funcDelegate (10, 20);
actionDelegate(30,40);
");
Assert.Equal(30, HelperMarshal._functionActionResultValue);
Assert.Equal(70, HelperMarshal._functionActionResultValueOfAction);
}
[Fact]
public static void InvokeActionIntInt()
{
HelperMarshal._actionResultValue = 0;
Runtime.InvokeJS(@"
var actionDelegate = App.call_test_method (""CreateActionDelegate"", [ ]);
actionDelegate(30,40);
");
Assert.Equal(70, HelperMarshal._actionResultValue);
}
[Fact]
public static void InvokeActionFloatIntToIntInt()
{
HelperMarshal._actionResultValue = 0;
Runtime.InvokeJS(@"
var actionDelegate = App.call_test_method (""CreateActionDelegate"", [ ]);
actionDelegate(3.14,40);
");
Assert.Equal(43, HelperMarshal._actionResultValue);
}
[Fact]
public static void InvokeDelegateMethod()
{
HelperMarshal._delMethodResultValue = string.Empty;
Runtime.InvokeJS(@"
var del = App.call_test_method (""CreateDelegateMethod"", [ ]);
del(""Hic sunt dracones"");
");
Assert.Equal("Hic sunt dracones", HelperMarshal._delMethodResultValue);
}
[Fact]
public static void InvokeDelegateMethodReturnString()
{
HelperMarshal._delMethodStringResultValue = string.Empty;
Runtime.InvokeJS(@"
var del = App.call_test_method (""CreateDelegateMethodReturnString"", [ ]);
var res = del(""Hic sunt dracones"");
App.call_test_method (""SetTestString1"", [ res ]);
");
Assert.Equal("Received: Hic sunt dracones", HelperMarshal._delMethodStringResultValue);
}
[Theory]
[InlineData("CreateCustomMultiCastDelegate_VoidString", "Moin")]
[InlineData("CreateMultiCastAction_VoidString", "MoinMoin")]
public static void InvokeMultiCastDelegate_VoidString(string creator, string testStr)
{
HelperMarshal._delegateCallResult = string.Empty;
Runtime.InvokeJS($@"
var del = App.call_test_method (""{creator}"", [ ]);
del(""{testStr}"");
");
Assert.Equal($" Hello, {testStr}! GoodMorning, {testStr}!", HelperMarshal._delegateCallResult);
}
[Theory]
[InlineData("CreateDelegateFromAnonymousMethod_VoidString")]
[InlineData("CreateDelegateFromLambda_VoidString")]
[InlineData("CreateDelegateFromMethod_VoidString")]
[InlineData("CreateActionT_VoidString")]
public static void InvokeDelegate_VoidString(string creator)
{
HelperMarshal._delegateCallResult = string.Empty;
var s = Runtime.InvokeJS($@"
var del = App.call_test_method (""{creator}"", [ ]);
del(""Hic sunt dracones"");
");
Assert.Equal("Notification received for: Hic sunt dracones", HelperMarshal._delegateCallResult);
}
public static IEnumerable<object[]> ArrayType_TestData()
{
_objectPrototype ??= new Function("return Object.prototype.toString;");
yield return new object[] { _objectPrototype.Call(), "Uint8Array", Uint8Array.From(new byte[10]) };
yield return new object[] { _objectPrototype.Call(), "Uint8ClampedArray", Uint8ClampedArray.From(new byte[10]) };
yield return new object[] { _objectPrototype.Call(), "Int8Array", Int8Array.From(new sbyte[10]) };
yield return new object[] { _objectPrototype.Call(), "Uint16Array", Uint16Array.From(new ushort[10]) };
yield return new object[] { _objectPrototype.Call(), "Int16Array", Int16Array.From(new short[10]) };
yield return new object[] { _objectPrototype.Call(), "Uint32Array", Uint32Array.From(new uint[10]) };
yield return new object[] { _objectPrototype.Call(), "Int32Array", Int32Array.From(new int[10]) };
yield return new object[] { _objectPrototype.Call(), "Float32Array", Float32Array.From(new float[10]) };
yield return new object[] { _objectPrototype.Call(), "Float64Array", Float64Array.From(new double[10]) };
yield return new object[] { _objectPrototype.Call(), "Array", new Array(10) };
}
[Theory]
[MemberData(nameof(ArrayType_TestData))]
public static void InvokeFunctionAcceptingArrayTypes(Function objectPrototype, string creator, JSObject arrayType )
{
HelperMarshal._funcActionBufferObjectResultValue = arrayType;
Assert.Equal(10, HelperMarshal._funcActionBufferObjectResultValue.Length);
Assert.Equal($"[object {creator}]", objectPrototype.Call(HelperMarshal._funcActionBufferObjectResultValue));
Runtime.InvokeJS($@"
var buffer = new {creator}(50);
var del = App.call_test_method (""CreateFunctionAccepting{creator}"", [ ]);
var setAction = del(buffer);
setAction(buffer);
");
Assert.Equal(50, HelperMarshal._funcActionBufferObjectResultValue.Length);
Assert.Equal(HelperMarshal._funcActionBufferObjectResultValue.Length, HelperMarshal._funcActionBufferResultLengthValue);
Assert.Equal($"[object {creator}]", objectPrototype.Call(HelperMarshal._funcActionBufferObjectResultValue));
}
}
}