Reenable working Enum tests
[mono-project.git] / sdks / wasm / bindings-test.cs
blob505d034643ddbfbf13180e4d396250d74dd2e210
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Threading.Tasks;
5 using System.Net.Http;
6 using System.Reflection;
8 using NUnit.Framework;
9 using WebAssembly;
10 using WebAssembly.Core;
12 public class TestClass {
13 public static int i32_res;
14 public static void InvokeI32 (int a, int b) {
15 i32_res = a + b;
18 public static float f32_res;
19 public static void InvokeFloat (float f) {
20 f32_res = f;
23 public static double f64_res;
24 public static void InvokeDouble (double d) {
25 f64_res = d;
28 public static long i64_res;
29 public static void InvokeLong (long l) {
30 i64_res = l;
33 public static string string_res;
34 public static void InvokeString (string s) {
35 string_res = s;
38 public static string mkstr;
39 public static string InvokeMkString() {
40 mkstr = "lalalala";
41 return mkstr;
44 public static int int_val;
45 public static void InvokeInt (int i) {
46 int_val = i;
49 public static object obj1;
50 public static object InvokeObj1(object obj)
52 obj1 = obj;
53 return obj;
56 public static object obj2;
57 public static object InvokeObj2(object obj)
59 obj2 = obj;
60 return obj;
63 public static object mkobj;
64 public static object InvokeMkobj()
66 mkobj = new object ();
67 return mkobj;
70 public static int first_val, second_val;
71 public static void PlayWithObj(JSObject obj) {
72 first_val = (int)obj.Invoke ("inc");;
73 second_val = (int)obj.Invoke("add", 20);
76 public static object[] js_objs;
77 public static void PlayWithObjTypes (JSObject obj) {
78 js_objs = new object[4];
79 js_objs [0] = obj.Invoke ("return_int");
80 js_objs [1] = obj.Invoke ("return_double");
81 js_objs [2] = obj.Invoke ("return_string");
82 js_objs [3] = obj.Invoke ("return_bool");
85 public static int do_add;
86 public static void UseFunction (JSObject obj) {
87 do_add = (int)obj.Invoke("call", null, 10, 20);
90 public static int dele_res;
91 public static Func<int, int, int> MkDelegate () {
92 return (a, b) => {
93 dele_res = a + b;
94 return dele_res;
98 public static TaskCompletionSource<int> tcs;
99 public static Task<int> task;
100 public static object MkTask () {
101 tcs = new TaskCompletionSource<int> ();
102 task = tcs.Task;
103 return task;
106 public static TaskCompletionSource<object> tcs3;
107 public static Task task3;
108 public static object MkTaskNull () {
109 tcs3 = new TaskCompletionSource<object> ();
110 task3 = tcs3.Task;
111 return task3;
114 public static Task<object> taskString;
115 public static object MkTaskString () {
116 tcs3 = new TaskCompletionSource<object> ();
117 taskString = tcs3.Task;
118 return taskString;
121 public static Task<object> the_promise;
122 public static void InvokePromise (object obj) {
123 the_promise = (Task<object>)obj;
124 the_promise.ContinueWith((t,o) => {
125 Console.WriteLine ("Promise result is {0}", t.Result);
126 }, null, TaskContinuationOptions.ExecuteSynchronously); //must be sync cuz the mainloop pump is gone
129 public static List<JSObject> js_objs_to_dispose = new List<JSObject>();
130 public static void DisposeObject(JSObject obj)
132 js_objs_to_dispose.Add(obj);
133 obj.Dispose();
136 public static object[] js_props;
137 public static void RetrieveObjectProperties (JSObject obj) {
138 js_props = new object[4];
139 js_props [0] = obj.GetObjectProperty ("myInt");
140 js_props [1] = obj.GetObjectProperty ("myDouble");
141 js_props [2] = obj.GetObjectProperty ("myString");
142 js_props [3] = obj.GetObjectProperty ("myBoolean");
145 public static void PopulateObjectProperties (JSObject obj, bool createIfNotExist) {
146 js_props = new object[4];
147 obj.SetObjectProperty ("myInt", 100, createIfNotExist);
148 obj.SetObjectProperty ("myDouble", 4.5, createIfNotExist);
149 obj.SetObjectProperty ("myString", "qwerty", createIfNotExist);
150 obj.SetObjectProperty ("myBoolean", true, createIfNotExist);
153 public static byte[] byteBuffer;
154 public static void MarshalArrayBuffer (ArrayBuffer buffer) {
155 using (var bytes = new Uint8Array(buffer))
156 byteBuffer = bytes.ToArray();
159 public static void MarshalByteBuffer (Uint8Array buffer) {
160 byteBuffer = buffer.ToArray();
163 public static int[] intBuffer;
164 public static void MarshalArrayBufferToInt32Array (ArrayBuffer buffer) {
165 using (var ints = new Int32Array(buffer))
166 intBuffer = ints.ToArray();
169 public static void MarshalInt32Array (Int32Array buffer) {
170 intBuffer = buffer.ToArray();
173 public static void MarshalByteBufferToInts (ArrayBuffer buffer) {
175 using(var bytes = new Uint8Array(buffer)) {
176 var byteBuffer = bytes.ToArray();
177 intBuffer = new int[bytes.Length / sizeof(int)];
178 for (int i = 0; i < bytes.Length; i += sizeof(int))
179 intBuffer[i / sizeof(int)] = BitConverter.ToInt32(byteBuffer, i);
183 public static float[] floatBuffer;
184 public static void MarshalFloat32Array (Float32Array buffer) {
185 floatBuffer = buffer.ToArray();
187 public static void MarshalArrayBufferToFloat32Array (ArrayBuffer buffer) {
188 using (var floats = new Float32Array(buffer))
189 floatBuffer = floats.ToArray();
193 public static void MarshalByteBufferToFloats (byte[] buffer) {
194 floatBuffer = new float[buffer.Length / sizeof(float)];
195 for (int i = 0; i < buffer.Length; i += sizeof(float))
196 floatBuffer[i / sizeof(float)] = BitConverter.ToSingle(buffer, i);
200 public static double[] doubleBuffer;
201 public static void MarshalFloat64Array (Float64Array buffer) {
202 doubleBuffer = buffer.ToArray();
205 public static void MarshalArrayBufferToFloat64Array (ArrayBuffer buffer) {
206 using (var doubles = new Float64Array(buffer))
207 doubleBuffer = doubles.ToArray();
211 public static void MarshalByteBufferToDoubles (ArrayBuffer buffer) {
212 using (var doubles = new Float64Array(buffer))
213 doubleBuffer = doubles.ToArray();
216 public static void SetTypedArraySByte (JSObject obj) {
217 sbyte[] buffer = Enumerable.Repeat((sbyte)0x20, 11).ToArray();
218 obj.SetObjectProperty ("typedArray", Int8Array.From(buffer));
221 public static sbyte[] taSByte;
222 public static void GetTypedArraySByte (JSObject obj) {
223 taSByte = ((Int8Array)obj.GetObjectProperty ("typedArray")).ToArray();
226 public static void SetTypedArrayByte (JSObject obj) {
227 var dragons = "hic sunt dracones";
228 byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dragons);
229 obj.SetObjectProperty ("dracones", Uint8Array.From(buffer));
232 public static byte[] taByte;
233 public static void GetTypedArrayByte (JSObject obj) {
234 taByte = ((Uint8Array)obj.GetObjectProperty ("dracones")).ToArray();
237 public static void SetTypedArrayShort (JSObject obj) {
238 short[] buffer = Enumerable.Repeat((short)0x20, 13).ToArray();
239 obj.SetObjectProperty ("typedArray", Int16Array.From(buffer));
242 public static short[] taShort;
243 public static void GetTypedArrayShort (JSObject obj) {
244 taShort = ((Int16Array)obj.GetObjectProperty ("typedArray")).ToArray();
247 public static void SetTypedArrayUShort (JSObject obj) {
248 ushort[] buffer = Enumerable.Repeat((ushort)0x20, 14).ToArray();
249 obj.SetObjectProperty ("typedArray", Uint16Array.From(buffer));
252 public static ushort[] taUShort;
253 public static void GetTypedArrayUShort (JSObject obj) {
254 taUShort = ((Uint16Array)obj.GetObjectProperty ("typedArray")).ToArray();
258 public static void SetTypedArrayInt (JSObject obj) {
259 int[] buffer = Enumerable.Repeat((int)0x20, 15).ToArray();
260 obj.SetObjectProperty ("typedArray", Int32Array.From(buffer));
263 public static int[] taInt;
264 public static void GetTypedArrayInt (JSObject obj) {
265 taInt = ((Int32Array)obj.GetObjectProperty ("typedArray")).ToArray();
268 public static void SetTypedArrayUInt (JSObject obj) {
269 uint[] buffer = Enumerable.Repeat((uint)0x20, 16).ToArray();
270 obj.SetObjectProperty ("typedArray", Uint32Array.From(buffer));
273 public static uint[] taUInt;
274 public static void GetTypedArrayUInt (JSObject obj) {
275 taUInt = ((Uint32Array)obj.GetObjectProperty ("typedArray")).ToArray();
278 public static void SetTypedArrayFloat (JSObject obj) {
279 float[] buffer = Enumerable.Repeat(3.14f, 17).ToArray();
280 obj.SetObjectProperty ("typedArray", Float32Array.From(buffer));
283 public static float[] taFloat;
284 public static void GetTypedArrayFloat (JSObject obj) {
285 taFloat = ((Float32Array)obj.GetObjectProperty ("typedArray")).ToArray();
289 public static void SetTypedArrayDouble (JSObject obj) {
290 double[] buffer = Enumerable.Repeat(3.14d, 18).ToArray();
291 obj.SetObjectProperty ("typedArray", Float64Array.From(buffer));
294 public static double[] taDouble;
295 public static void GetTypedArrayDouble (JSObject obj) {
296 taDouble = ((Float64Array)obj.GetObjectProperty ("typedArray")).ToArray();
299 public static HttpClient client;
300 public static string fakeClientHandlerString;
301 public static HttpClientHandler fakeClientHandler;
302 public static void SetMessageHandler () {
304 var httpMessageHandler = typeof(HttpClient).GetField("GetHttpMessageHandler",
305 BindingFlags.Static |
306 BindingFlags.NonPublic);
308 httpMessageHandler.SetValue(null, (Func<HttpClientHandler>) (() => {
309 return new FakeHttpClientHandler ();
310 }));
312 client = new HttpClient();
315 public static RequestCache[] requestEnums;
317 public static void SetRequestEnums (RequestCache dflt, RequestCache nostore, RequestCache reload, RequestCache nocache, RequestCache force, RequestCache onlyif)
319 requestEnums = new RequestCache[6];
320 requestEnums[0] = dflt;
321 requestEnums[1] = nostore;
322 requestEnums[2] = reload;
323 requestEnums[3] = nocache;
324 requestEnums[4] = force;
325 requestEnums[5] = onlyif;
328 public static void SetRequestEnumsProperties (JSObject obj)
330 obj.SetObjectProperty("dflt", RequestCache.Default);
331 obj.SetObjectProperty("nostore", RequestCache.NoStore);
332 obj.SetObjectProperty("reload", RequestCache.Reload);
333 obj.SetObjectProperty("nocache", RequestCache.NoCache);
334 obj.SetObjectProperty("force", RequestCache.ForceCache);
335 obj.SetObjectProperty("onlyif", RequestCache.OnlyIfCached);
338 static Function sum;
339 public static void CreateFunctionSum ()
341 sum = new Function("a", "b", "return a + b");
344 public static int sumValue = 0;
345 public static void CallFunctionSum ()
347 sumValue = (int)sum.Call(null, 3, 5);
350 static Function mathMin;
351 public static void CreateFunctionApply ()
353 var math = (JSObject)Runtime.GetGlobalObject("Math");
354 mathMin = (Function)math.GetObjectProperty("min");
358 public static int minValue = 0;
359 public static void CallFunctionApply ()
361 minValue = (int)mathMin.Apply(null, new object[] { 5, 6, 2, 3, 7 });
367 public class FakeHttpClientHandler : HttpClientHandler
369 public FakeHttpClientHandler () : base()
371 TestClass.fakeClientHandlerString = "Fake HttpClientHandler";
372 TestClass.fakeClientHandler = this;
376 public enum RequestCache
378 [Export(EnumValue = ConvertEnum.Default)]
379 Default = -1,
380 [Export("no-store")]
381 NoStore,
382 [Export(EnumValue = ConvertEnum.ToUpper)]
383 Reload,
384 [Export(EnumValue = ConvertEnum.ToLower)]
385 NoCache,
386 [Export("force-cache")]
387 ForceCache,
388 OnlyIfCached = -3636,
392 [TestFixture]
393 public class BindingTests {
394 [Test]
395 public static void MarshalPrimitivesToCS ()
397 TestClass.i32_res = 0;
398 Runtime.InvokeJS ("call_test_method(\"InvokeI32\", \"ii\", [10, 20])");
399 Assert.AreEqual (TestClass.i32_res, 30);
401 TestClass.f32_res = 0;
402 Runtime.InvokeJS ("call_test_method(\"InvokeFloat\", \"f\", [1.5])");
403 Assert.AreEqual (TestClass.f32_res, 1.5f);
405 TestClass.f64_res = 0;
406 Runtime.InvokeJS ("call_test_method(\"InvokeDouble\", \"d\", [4.5])");
407 Assert.AreEqual (TestClass.f64_res, 4.5);
409 TestClass.i64_res = 0;
410 Runtime.InvokeJS ("call_test_method(\"InvokeLong\", \"l\", [99])");
411 Assert.AreEqual (TestClass.i64_res, 99);
414 [Test]
415 public static void MarshalStringToCS ()
417 TestClass.string_res = null;
418 Runtime.InvokeJS ("call_test_method(\"InvokeString\", \"s\", [\"hello\"])");
419 Assert.AreEqual (TestClass.string_res, "hello");
422 [Test]
423 public static void MarshalStringToJS ()
425 TestClass.mkstr = TestClass.string_res = null;
426 Runtime.InvokeJS (@"
427 var str = call_test_method (""InvokeMkString"", ""o"", [ ]);
428 call_test_method (""InvokeString"", ""s"", [ str ]);
430 Assert.IsNotNull(TestClass.mkstr);
432 Assert.AreEqual (TestClass.mkstr, TestClass.string_res);
435 [Test]
436 public static void JSObjectKeepIdentityAcrossCalls ()
438 TestClass.obj1 = TestClass.obj2 = null;
439 Runtime.InvokeJS (@"
440 var obj = { foo: 10 };
441 var res = call_test_method (""InvokeObj1"", ""o"", [ obj ]);
442 call_test_method (""InvokeObj2"", ""o"", [ res ]);
445 Assert.IsNotNull(TestClass.obj1);
446 Assert.AreSame(TestClass.obj1, TestClass.obj2);
449 [Test]
450 public static void CSObjectKeepIdentityAcrossCalls ()
452 TestClass.mkobj = TestClass.obj1 = TestClass.obj2 = null;
453 Runtime.InvokeJS (@"
454 var obj = call_test_method (""InvokeMkobj"", """", [ ]);
455 var res = call_test_method (""InvokeObj1"", ""o"", [ obj ]);
456 call_test_method (""InvokeObj2"", ""o"", [ res ]);
459 Assert.IsNotNull(TestClass.obj1);
460 Assert.AreSame(TestClass.mkobj, TestClass.obj1);
461 Assert.AreSame(TestClass.obj1, TestClass.obj2);
464 [Test]
465 public static void JSInvokeInt() {
466 Runtime.InvokeJS (@"
467 var obj = {
468 foo: 10,
469 inc: function() {
470 var c = this.foo;
471 ++this.foo;
472 return c;
474 add: function(val){
475 return this.foo + val;
478 call_test_method (""PlayWithObj"", ""o"", [ obj ]);
481 Assert.AreEqual (TestClass.first_val, 10);
482 Assert.AreEqual (TestClass.second_val, 31);
485 [Test]
486 public static void JSInvokeTypes() {
487 Runtime.InvokeJS (@"
488 var obj = {
489 return_int: function() { return 100; },
490 return_double: function() { return 4.5; },
491 return_string: function() { return 'qwerty'; },
492 return_bool: function() { return true; },
494 call_test_method (""PlayWithObjTypes"", ""o"", [ obj ]);
497 Assert.AreEqual (TestClass.js_objs [0], 100);
498 Assert.AreEqual (TestClass.js_objs [1], 4.5);
499 Assert.AreEqual (TestClass.js_objs [2], "qwerty");
500 Assert.AreEqual (TestClass.js_objs [3], true);
503 [Test]
504 public static void JSObjectApply() {
505 Runtime.InvokeJS (@"
506 var do_add = function(a, b) { return a + b};
507 call_test_method (""UseFunction"", ""o"", [ do_add ]);
509 Assert.AreEqual (TestClass.do_add, 30);
512 [Test]
513 public static void MarshalDelegate() {
514 TestClass.obj1 = null;
515 Runtime.InvokeJS (@"
516 var dele = call_test_method (""MkDelegate"", """", [ ]);
517 var res = dele (10, 20);
518 call_test_method (""InvokeI32"", ""ii"", [ res, res ]);
521 Assert.AreEqual (30, TestClass.dele_res);
522 Assert.AreEqual (60, TestClass.i32_res);
525 [Test]
526 public static void PassTaskToJS () {
527 TestClass.int_val = 0;
528 Runtime.InvokeJS (@"
529 var tsk = call_test_method (""MkTask"", """", [ ]);
530 tsk.then (function (value) {
531 Module.print ('PassTaskToJS cont with value ' + value);
534 Assert.AreEqual (0, TestClass.int_val);
535 TestClass.tcs.SetResult (99);
536 //FIXME our test harness doesn't suppport async tests.
537 // So manually verify it for now by checking stdout for `PassTaskToJS cont with value 99`
538 //Assert.AreEqual (99, TestClass.int_val);
542 [Test]
543 public static void PassTaskToJS2 () {
544 TestClass.int_val = 0;
545 Runtime.InvokeJS (@"
546 var tsk = call_test_method (""MkTask"", """", [ ]);
547 tsk.then (function (value) {},
548 function (reason) {
549 Module.print ('PassTaskToJS2 cont failed due to ' + reason);
552 Assert.AreEqual (0, TestClass.int_val);
553 TestClass.tcs.SetException (new Exception ("it failed"));
554 //FIXME our test harness doesn't suppport async tests.
555 // So manually verify it for now by checking stdout for `PassTaskToJS2 cont failed due to System.AggregateException...
556 // Assert.AreEqual (99, TestClass.int_val);
559 [Test]
560 public static void PassTaskToJS3 () {
561 TestClass.int_val = 0;
562 Runtime.InvokeJS (@"
563 var tsk = call_test_method (""MkTaskNull"", """", [ ]);
564 tsk.then( () => {
565 Module.print('PassTaskToJS3 cont without value '); // Success!
566 }, reason => {
567 Module.print('PassTaskToJS3 cont failed due to ' + reason); // Error!
568 } );
570 Assert.AreEqual (0, TestClass.int_val);
571 TestClass.tcs3.SetResult (null);
574 [Test]
575 public static void PassTaskToJS4 () {
576 TestClass.int_val = 0;
577 Runtime.InvokeJS (@"
578 var tsk = call_test_method (""MkTaskNull"", """", [ ]);
579 tsk.then( value => {
580 Module.print(value); // Success!
581 }, reason => {
582 Module.print('PassTaskToJS4 cont failed due to ' + reason); // Error!
583 } );
585 Assert.AreEqual (0, TestClass.int_val);
586 TestClass.tcs3.SetException (new Exception ("it failed"));
589 [Test]
590 public static void PassTaskToJS5 () {
591 TestClass.int_val = 0;
592 Runtime.InvokeJS (@"
593 var tsk = call_test_method (""MkTaskString"", """", [ ]);
594 tsk.then( success => {
595 Module.print('PassTaskToJS5 cont with value ' + success); // Success!
596 }, reason => {
597 Module.print('PassTaskToJS5 cont failed due to ' + reason); // Error!
598 } );
600 Assert.AreEqual (0, TestClass.int_val);
601 TestClass.tcs3.SetResult ("Success");
604 [Test]
605 public static void PassTaskToJS6 () {
606 TestClass.int_val = 0;
607 Runtime.InvokeJS (@"
608 var tsk = call_test_method (""MkTaskString"", """", [ ]);
609 tsk.then( success => {
610 Module.print('PassTaskToJS6 cont with value ' + success); // Success!
611 }, reason => {
612 Module.print('PassTaskToJS6 cont failed due to ' + reason); // Error!
613 } );
615 Assert.AreEqual (0, TestClass.int_val);
616 TestClass.tcs3.SetException (new Exception ("it failed"));
619 [Test]
620 public static void PassPromiseToCS () {
621 TestClass.int_val = 0;
622 Runtime.InvokeJS (@"
623 var resolve_func = null;
624 var promise = new Promise(function (resolve, reject) {
625 resolve_func = resolve;
627 call_test_method (""InvokePromise"", ""o"", [ promise ]);
628 resolve_func (111);
630 //FIXME our test harness doesn't suppport async tests.
631 // So manually verify it for now by checking stdout for `Promise result is 111`
632 // Assert.AreEqual (99, TestClass.int_val);
635 [Test]
636 public static void BindStaticMethod () {
637 TestClass.int_val = 0;
638 Runtime.InvokeJS (@"
639 var invoke_int = Module.mono_bind_static_method (""[binding_tests]TestClass:InvokeInt"");
640 invoke_int (200);
643 Assert.AreEqual (200, TestClass.int_val);
646 [Test]
647 public static void InvokeStaticMethod () {
648 TestClass.int_val = 0;
649 Runtime.InvokeJS (@"
650 Module.mono_call_static_method (""[binding_tests]TestClass:InvokeInt"", [ 300 ]);
653 Assert.AreEqual (300, TestClass.int_val);
656 [Test]
657 public static void ResolveMethod () {
658 TestClass.int_val = 0;
659 Runtime.InvokeJS (@"
660 var invoke_int = Module.mono_method_resolve (""[binding_tests]TestClass:InvokeInt"");
661 call_test_method (""InvokeInt"", ""i"", [ invoke_int ]);
664 Assert.AreNotEqual (0, TestClass.int_val);
667 [Test]
668 public static void DisposeObject () {
669 Runtime.InvokeJS (@"
670 var obj1 = {
672 var obj2 = {
674 var obj3 = {
676 call_test_method (""DisposeObject"", ""o"", [ obj3 ]);
677 call_test_method (""DisposeObject"", ""o"", [ obj2 ]);
678 call_test_method (""DisposeObject"", ""o"", [ obj1 ]);
681 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [0].JSHandle);
682 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [1].JSHandle);
683 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [2].JSHandle);
686 [Test]
687 public static void GetObjectProperties () {
688 Runtime.InvokeJS (@"
689 var obj = {myInt: 100, myDouble: 4.5, myString: ""qwerty"", myBoolean: true};
690 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
693 Assert.AreEqual (100, TestClass.js_props [0]);
694 Assert.AreEqual (4.5, TestClass.js_props [1]);
695 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
696 Assert.AreEqual (true, TestClass.js_props [3]);
699 [Test]
700 public static void SetObjectProperties () {
701 Runtime.InvokeJS (@"
702 var obj = {myInt: 200, myDouble: 0, myString: ""foo"", myBoolean: false};
703 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, false ]);
704 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
707 Assert.AreEqual (100, TestClass.js_props [0]);
708 Assert.AreEqual (4.5, TestClass.js_props [1]);
709 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
710 Assert.AreEqual (true, TestClass.js_props [3]);
713 [Test]
714 public static void SetObjectPropertiesIfNotExistsFalse () {
715 // This test will not create the properties if they do not already exist
716 Runtime.InvokeJS (@"
717 var obj = {myInt: 200};
718 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, false ]);
719 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
722 Assert.AreEqual (100, TestClass.js_props [0]);
723 Assert.AreEqual (null, TestClass.js_props [1]);
724 Assert.AreEqual (null, TestClass.js_props [2]);
725 Assert.AreEqual (null, TestClass.js_props [3]);
728 [Test]
729 public static void SetObjectPropertiesIfNotExistsTrue () {
730 // This test will set the value of the property if it exists and will create and
731 // set the value if it does not exists
732 Runtime.InvokeJS (@"
733 var obj = {myInt: 200};
734 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, true ]);
735 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
738 Assert.AreEqual (100, TestClass.js_props [0]);
739 Assert.AreEqual (4.5, TestClass.js_props [1]);
740 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
741 Assert.AreEqual (true, TestClass.js_props [3]);
745 [Test]
746 public static void MarshalArrayBuffer () {
747 Runtime.InvokeJS (@"
748 var buffer = new ArrayBuffer(16);
749 call_test_method (""MarshalArrayBuffer"", ""o"", [ buffer ]);
752 Assert.AreEqual (16, TestClass.byteBuffer.Length);
755 [Test]
756 public static void MarshalArrayBuffer2Int () {
757 Runtime.InvokeJS (@"
758 var buffer = new ArrayBuffer(16);
759 var int32View = new Int32Array(buffer);
760 for (var i = 0; i < int32View.length; i++) {
761 int32View[i] = i * 2;
763 call_test_method (""MarshalArrayBufferToInt32Array"", ""o"", [ buffer ]);
766 Assert.AreEqual (4, TestClass.intBuffer.Length);
767 Assert.AreEqual (0, TestClass.intBuffer[0]);
768 Assert.AreEqual (2, TestClass.intBuffer[1]);
769 Assert.AreEqual (4, TestClass.intBuffer[2]);
770 Assert.AreEqual (6, TestClass.intBuffer[3]);
773 [Test]
774 public static void MarshalArrayBuffer2Int2 () {
776 Runtime.InvokeJS (@"
777 var buffer = new ArrayBuffer(16);
778 var int32View = new Int32Array(buffer);
779 for (var i = 0; i < int32View.length; i++) {
780 int32View[i] = i * 2;
782 call_test_method (""MarshalByteBufferToInts"", ""o"", [ buffer ]);
785 Assert.AreEqual (4, TestClass.intBuffer.Length);
786 Assert.AreEqual (0, TestClass.intBuffer[0]);
787 Assert.AreEqual (2, TestClass.intBuffer[1]);
788 Assert.AreEqual (4, TestClass.intBuffer[2]);
789 Assert.AreEqual (6, TestClass.intBuffer[3]);
793 [Test]
794 public static void MarshalTypedArray () {
795 Runtime.InvokeJS (@"
796 var buffer = new ArrayBuffer(16);
797 var uint8View = new Uint8Array(buffer);
798 call_test_method (""MarshalByteBuffer"", ""o"", [ uint8View ]);
801 Assert.AreEqual (16, TestClass.byteBuffer.Length);
804 [Test]
805 public static void MarshalTypedArray2Int () {
806 Runtime.InvokeJS (@"
807 var buffer = new ArrayBuffer(16);
808 var int32View = new Int32Array(buffer);
809 for (var i = 0; i < int32View.length; i++) {
810 int32View[i] = i * 2;
812 call_test_method (""MarshalInt32Array"", ""o"", [ int32View ]);
815 Assert.AreEqual (4, TestClass.intBuffer.Length);
816 Assert.AreEqual (0, TestClass.intBuffer[0]);
817 Assert.AreEqual (2, TestClass.intBuffer[1]);
818 Assert.AreEqual (4, TestClass.intBuffer[2]);
819 Assert.AreEqual (6, TestClass.intBuffer[3]);
822 [Test]
823 public static void MarshalTypedArray2Float () {
824 Runtime.InvokeJS (@"
825 var typedArray = new Float32Array([1, 2.1334, 3, 4.2, 5]);
826 call_test_method (""MarshalFloat32Array"", ""o"", [ typedArray ]);
829 Assert.AreEqual (1, TestClass.floatBuffer[0]);
830 Assert.AreEqual (2.1334f, TestClass.floatBuffer[1]);
831 Assert.AreEqual (3, TestClass.floatBuffer[2]);
832 Assert.AreEqual (4.2f, TestClass.floatBuffer[3]);
833 Assert.AreEqual (5, TestClass.floatBuffer[4]);
836 // [Test]
837 // public static void MarshalArrayBuffer2Float () {
838 // Runtime.InvokeJS (@"
839 // var buffer = new ArrayBuffer(16);
840 // var float32View = new Float32Array(buffer);
841 // for (var i = 0; i < float32View.length; i++) {
842 // float32View[i] = i * 2.5;
843 // }
844 // call_test_method (""MarshalByteBufferToFloats"", ""o"", [ buffer ]);
845 // ");
847 // Assert.AreEqual (4, TestClass.floatBuffer.Length);
848 // Assert.AreEqual (0, TestClass.floatBuffer[0]);
849 // Assert.AreEqual (2.5f, TestClass.floatBuffer[1]);
850 // Assert.AreEqual (5, TestClass.floatBuffer[2]);
851 // Assert.AreEqual (7.5f, TestClass.floatBuffer[3]);
852 // }
854 [Test]
855 public static void MarshalArrayBuffer2Float2 () {
856 Runtime.InvokeJS (@"
857 var buffer = new ArrayBuffer(16);
858 var float32View = new Float32Array(buffer);
859 for (var i = 0; i < float32View.length; i++) {
860 float32View[i] = i * 2.5;
862 call_test_method (""MarshalArrayBufferToFloat32Array"", ""o"", [ buffer ]);
865 Assert.AreEqual (4, TestClass.floatBuffer.Length);
866 Assert.AreEqual (0, TestClass.floatBuffer[0]);
867 Assert.AreEqual (2.5f, TestClass.floatBuffer[1]);
868 Assert.AreEqual (5, TestClass.floatBuffer[2]);
869 Assert.AreEqual (7.5f, TestClass.floatBuffer[3]);
872 [Test]
873 public static void MarshalTypedArray2Double () {
874 Runtime.InvokeJS (@"
875 var typedArray = new Float64Array([1, 2.1334, 3, 4.2, 5]);
876 call_test_method (""MarshalFloat64Array"", ""o"", [ typedArray ]);
879 Assert.AreEqual (1, TestClass.doubleBuffer[0]);
880 Assert.AreEqual (2.1334d, TestClass.doubleBuffer[1]);
881 Assert.AreEqual (3, TestClass.doubleBuffer[2]);
882 Assert.AreEqual (4.2d, TestClass.doubleBuffer[3]);
883 Assert.AreEqual (5, TestClass.doubleBuffer[4]);
886 [Test]
887 public static void MarshalArrayBuffer2Double () {
888 Runtime.InvokeJS (@"
889 var buffer = new ArrayBuffer(32);
890 var float64View = new Float64Array(buffer);
891 for (var i = 0; i < float64View.length; i++) {
892 float64View[i] = i * 2.5;
894 call_test_method (""MarshalByteBufferToDoubles"", ""o"", [ buffer ]);
897 Assert.AreEqual (4, TestClass.doubleBuffer.Length);
898 Assert.AreEqual (0, TestClass.doubleBuffer[0]);
899 Assert.AreEqual (2.5d, TestClass.doubleBuffer[1]);
900 Assert.AreEqual (5, TestClass.doubleBuffer[2]);
901 Assert.AreEqual (7.5d, TestClass.doubleBuffer[3]);
904 [Test]
905 public static void MarshalArrayBuffer2Double2 () {
906 Runtime.InvokeJS (@"
907 var buffer = new ArrayBuffer(32);
908 var float64View = new Float64Array(buffer);
909 for (var i = 0; i < float64View.length; i++) {
910 float64View[i] = i * 2.5;
912 call_test_method (""MarshalArrayBufferToFloat64Array"", ""o"", [ buffer ]);
915 Assert.AreEqual (4, TestClass.doubleBuffer.Length);
916 Assert.AreEqual (0, TestClass.doubleBuffer[0]);
917 Assert.AreEqual (2.5f, TestClass.doubleBuffer[1]);
918 Assert.AreEqual (5, TestClass.doubleBuffer[2]);
919 Assert.AreEqual (7.5f, TestClass.doubleBuffer[3]);
922 [Test]
923 public static void MarshalTypedArraySByte () {
924 TestClass.int_val = 0;
925 Runtime.InvokeJS (@"
926 var obj = { };
927 call_test_method (""SetTypedArraySByte"", ""o"", [ obj ]);
928 call_test_method (""GetTypedArraySByte"", ""o"", [ obj ]);
930 Assert.AreEqual (11, TestClass.taSByte.Length);
931 Assert.AreEqual (32, TestClass.taSByte[0]);
932 Assert.AreEqual (32, TestClass.taSByte[TestClass.taSByte.Length - 1]);
935 [Test]
936 public static void MarshalTypedArrayByte () {
937 TestClass.int_val = 0;
938 Runtime.InvokeJS (@"
939 var obj = { };
940 call_test_method (""SetTypedArrayByte"", ""o"", [ obj ]);
941 call_test_method (""GetTypedArrayByte"", ""o"", [ obj ]);
943 Assert.AreEqual (17, TestClass.taByte.Length);
944 Assert.AreEqual (104, TestClass.taByte[0]);
945 Assert.AreEqual (115, TestClass.taByte[TestClass.taByte.Length - 1]);
946 Assert.AreEqual ("hic sunt dracones", System.Text.Encoding.Default.GetString(TestClass.taByte));
949 [Test]
950 public static void MarshalTypedArrayShort () {
951 TestClass.int_val = 0;
952 Runtime.InvokeJS (@"
953 var obj = { };
954 call_test_method (""SetTypedArrayShort"", ""o"", [ obj ]);
955 call_test_method (""GetTypedArrayShort"", ""o"", [ obj ]);
957 Assert.AreEqual (13, TestClass.taShort.Length);
958 Assert.AreEqual (32, TestClass.taShort[0]);
959 Assert.AreEqual (32, TestClass.taShort[TestClass.taShort.Length - 1]);
962 [Test]
963 public static void MarshalTypedArrayUShort () {
964 TestClass.int_val = 0;
965 Runtime.InvokeJS (@"
966 var obj = { };
967 call_test_method (""SetTypedArrayUShort"", ""o"", [ obj ]);
968 call_test_method (""GetTypedArrayUShort"", ""o"", [ obj ]);
970 Assert.AreEqual (14, TestClass.taUShort.Length);
971 Assert.AreEqual (32, TestClass.taUShort[0]);
972 Assert.AreEqual (32, TestClass.taUShort[TestClass.taUShort.Length - 1]);
976 [Test]
977 public static void MarshalTypedArrayInt () {
978 TestClass.int_val = 0;
979 Runtime.InvokeJS (@"
980 var obj = { };
981 call_test_method (""SetTypedArrayInt"", ""o"", [ obj ]);
982 call_test_method (""GetTypedArrayInt"", ""o"", [ obj ]);
984 Assert.AreEqual (15, TestClass.taInt.Length);
985 Assert.AreEqual (32, TestClass.taInt[0]);
986 Assert.AreEqual (32, TestClass.taInt[TestClass.taInt.Length - 1]);
989 [Test]
990 public static void MarshalTypedArrayUInt () {
991 TestClass.int_val = 0;
992 Runtime.InvokeJS (@"
993 var obj = { };
994 call_test_method (""SetTypedArrayUInt"", ""o"", [ obj ]);
995 call_test_method (""GetTypedArrayUInt"", ""o"", [ obj ]);
997 Assert.AreEqual (16, TestClass.taUInt.Length);
998 Assert.AreEqual (32, TestClass.taUInt[0]);
999 Assert.AreEqual (32, TestClass.taUInt[TestClass.taUInt.Length - 1]);
1002 [Test]
1003 public static void MarshalTypedArrayFloat () {
1004 TestClass.int_val = 0;
1005 Runtime.InvokeJS (@"
1006 var obj = { };
1007 call_test_method (""SetTypedArrayFloat"", ""o"", [ obj ]);
1008 call_test_method (""GetTypedArrayFloat"", ""o"", [ obj ]);
1010 Assert.AreEqual (17, TestClass.taFloat.Length);
1011 Assert.AreEqual (3.14f, TestClass.taFloat[0]);
1012 Assert.AreEqual (3.14f, TestClass.taFloat[TestClass.taFloat.Length - 1]);
1016 [Test]
1017 public static void MarshalTypedArrayDouble () {
1018 TestClass.int_val = 0;
1019 Runtime.InvokeJS (@"
1020 var obj = { };
1021 call_test_method (""SetTypedArrayDouble"", ""o"", [ obj ]);
1022 call_test_method (""GetTypedArrayDouble"", ""o"", [ obj ]);
1024 Assert.AreEqual (18, TestClass.taDouble.Length);
1025 Assert.AreEqual (3.14d, TestClass.taDouble[0]);
1026 Assert.AreEqual (3.14d, TestClass.taDouble[TestClass.taDouble.Length - 1]);
1029 [Test]
1030 public static void TestFunctionSum () {
1031 TestClass.sumValue = 0;
1032 Runtime.InvokeJS (@"
1033 call_test_method (""CreateFunctionSum"", null, [ ]);
1034 call_test_method (""CallFunctionSum"", null, [ ]);
1036 Assert.AreEqual (8, TestClass.sumValue);
1039 [Test]
1040 public static void TestFunctionApply () {
1041 TestClass.minValue = 0;
1042 Runtime.InvokeJS (@"
1043 call_test_method (""CreateFunctionApply"", null, [ ]);
1044 call_test_method (""CallFunctionApply"", null, [ ]);
1046 Assert.AreEqual (2, TestClass.minValue);
1050 // [Test]
1051 // public static void HttpMessageHandler () {
1052 // TestClass.fakeClientHandlerString = string.Empty;
1053 // TestClass.fakeClientHandler = null;
1054 // TestClass.client = null;
1055 // Runtime.InvokeJS (@"
1056 // call_test_method (""SetMessageHandler"", ""o"", [ ]);
1057 // ");
1058 // Assert.AreEqual ("Fake HttpClientHandler", TestClass.fakeClientHandlerString);
1059 // Assert.AreNotEqual (null, TestClass.fakeClientHandler);
1060 // Assert.AreEqual (typeof(FakeHttpClientHandler), TestClass.fakeClientHandler.GetType());
1061 // Assert.AreNotEqual (null, TestClass.client);
1062 // }
1064 [Test]
1065 public static void MarshalRequestEnums () {
1066 Runtime.InvokeJS (@"
1067 var dflt = ""Default"";
1068 var nostore = ""no-store"";
1069 var reload = ""RELOAD"";
1070 var nocache = ""nocache"";
1071 var force = 3;
1072 var onlyif = -3636;
1073 Module.mono_call_static_method (""[binding_tests]TestClass:SetRequestEnums"", [ dflt, nostore, reload, nocache, force, onlyif ]);
1075 Assert.AreEqual (RequestCache.Default, TestClass.requestEnums[0]);
1076 Assert.AreEqual (RequestCache.NoStore, TestClass.requestEnums[1]);
1077 Assert.AreEqual (RequestCache.Reload, TestClass.requestEnums[2]);
1078 Assert.AreEqual (RequestCache.NoCache, TestClass.requestEnums[3]);
1079 Assert.AreEqual (RequestCache.ForceCache, TestClass.requestEnums[4]);
1080 Assert.AreEqual (RequestCache.OnlyIfCached, TestClass.requestEnums[5]);
1083 [Test]
1084 public static void MarshalRequestEnumProps () {
1085 Runtime.InvokeJS (@"
1086 var obj = {};
1087 Module.mono_call_static_method (""[binding_tests]TestClass:SetRequestEnumsProperties"", [ obj ]);
1088 Module.mono_call_static_method (""[binding_tests]TestClass:SetRequestEnums"", [ obj.dflt, obj.nostore, obj.reload, obj.nocache, obj.force, obj.onlyif ]);
1090 Assert.AreEqual (RequestCache.Default, TestClass.requestEnums[0]);
1091 Assert.AreEqual (RequestCache.NoStore, TestClass.requestEnums[1]);
1092 Assert.AreEqual (RequestCache.Reload, TestClass.requestEnums[2]);
1093 Assert.AreEqual (RequestCache.NoCache, TestClass.requestEnums[3]);
1094 Assert.AreEqual (RequestCache.ForceCache, TestClass.requestEnums[4]);
1095 Assert.AreEqual (RequestCache.OnlyIfCached, TestClass.requestEnums[5]);