[gitattributes] Do CRLF normalization on sln/proj files
[mono-project.git] / sdks / wasm / bindings-test.cs
blob19d98c58c5798ac9bd3955cd40b6272935e41592
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;
11 public class TestClass {
12 public static int i32_res;
13 public static void InvokeI32 (int a, int b) {
14 i32_res = a + b;
17 public static float f32_res;
18 public static void InvokeFloat (float f) {
19 f32_res = f;
22 public static double f64_res;
23 public static void InvokeDouble (double d) {
24 f64_res = d;
27 public static long i64_res;
28 public static void InvokeLong (long l) {
29 i64_res = l;
32 public static string string_res;
33 public static void InvokeString (string s) {
34 string_res = s;
37 public static string mkstr;
38 public static string InvokeMkString() {
39 mkstr = "lalalala";
40 return mkstr;
43 public static int int_val;
44 public static void InvokeInt (int i) {
45 int_val = i;
48 public static object obj1;
49 public static object InvokeObj1(object obj)
51 obj1 = obj;
52 return obj;
55 public static object obj2;
56 public static object InvokeObj2(object obj)
58 obj2 = obj;
59 return obj;
62 public static object mkobj;
63 public static object InvokeMkobj()
65 mkobj = new object ();
66 return mkobj;
69 public static int first_val, second_val;
70 public static void PlayWithObj(JSObject obj) {
71 first_val = (int)obj.Invoke ("inc");;
72 second_val = (int)obj.Invoke("add", 20);
75 public static object[] js_objs;
76 public static void PlayWithObjTypes (JSObject obj) {
77 js_objs = new object[4];
78 js_objs [0] = obj.Invoke ("return_int");
79 js_objs [1] = obj.Invoke ("return_double");
80 js_objs [2] = obj.Invoke ("return_string");
81 js_objs [3] = obj.Invoke ("return_bool");
84 public static int do_add;
85 public static void UseFunction (JSObject obj) {
86 do_add = (int)obj.Invoke("call", null, 10, 20);
89 public static int dele_res;
90 public static Func<int, int, int> MkDelegate () {
91 return (a, b) => {
92 dele_res = a + b;
93 return dele_res;
97 public static TaskCompletionSource<int> tcs;
98 public static Task<int> task;
99 public static object MkTask () {
100 tcs = new TaskCompletionSource<int> ();
101 task = tcs.Task;
102 return task;
105 public static TaskCompletionSource<object> tcs3;
106 public static Task task3;
107 public static object MkTaskNull () {
108 tcs3 = new TaskCompletionSource<object> ();
109 task3 = tcs3.Task;
110 return task3;
113 public static Task<object> taskString;
114 public static object MkTaskString () {
115 tcs3 = new TaskCompletionSource<object> ();
116 taskString = tcs3.Task;
117 return taskString;
120 public static Task<object> the_promise;
121 public static void InvokePromise (object obj) {
122 the_promise = (Task<object>)obj;
123 the_promise.ContinueWith((t,o) => {
124 Console.WriteLine ("Promise result is {0}", t.Result);
125 }, null, TaskContinuationOptions.ExecuteSynchronously); //must be sync cuz the mainloop pump is gone
128 public static List<JSObject> js_objs_to_dispose = new List<JSObject>();
129 public static void DisposeObject(JSObject obj)
131 js_objs_to_dispose.Add(obj);
132 obj.Dispose();
135 public static object[] js_props;
136 public static void RetrieveObjectProperties (JSObject obj) {
137 js_props = new object[4];
138 js_props [0] = obj.GetObjectProperty ("myInt");
139 js_props [1] = obj.GetObjectProperty ("myDouble");
140 js_props [2] = obj.GetObjectProperty ("myString");
141 js_props [3] = obj.GetObjectProperty ("myBoolean");
144 public static void PopulateObjectProperties (JSObject obj, bool createIfNotExist) {
145 js_props = new object[4];
146 obj.SetObjectProperty ("myInt", 100, createIfNotExist);
147 obj.SetObjectProperty ("myDouble", 4.5, createIfNotExist);
148 obj.SetObjectProperty ("myString", "qwerty", createIfNotExist);
149 obj.SetObjectProperty ("myBoolean", true, createIfNotExist);
152 public static byte[] byteBuffer;
153 public static void MarshalByteBuffer (byte[] buffer) {
154 byteBuffer = buffer;
157 public static int[] intBuffer;
158 public static void MarshalInt32Array (int[] buffer) {
159 intBuffer = buffer;
162 public static void MarshalByteBufferToInts (byte[] buffer) {
163 intBuffer = new int[buffer.Length / sizeof(int)];
164 for (int i = 0; i < buffer.Length; i += sizeof(int))
165 intBuffer[i / sizeof(int)] = BitConverter.ToInt32(buffer, i);
168 public static float[] floatBuffer;
169 public static void MarshalFloat32Array (float[] buffer) {
170 floatBuffer = buffer;
173 public static void MarshalByteBufferToFloats (byte[] buffer) {
174 floatBuffer = new float[buffer.Length / sizeof(float)];
175 for (int i = 0; i < buffer.Length; i += sizeof(float))
176 floatBuffer[i / sizeof(float)] = BitConverter.ToSingle(buffer, i);
180 public static double[] doubleBuffer;
181 public static void MarshalFloat64Array (double[] buffer) {
182 doubleBuffer = buffer;
185 public static void MarshalByteBufferToDoubles (byte[] buffer) {
186 doubleBuffer = new double[buffer.Length / sizeof(double)];
187 for (int i = 0; i < buffer.Length; i += sizeof(double))
188 doubleBuffer[i / sizeof(double)] = BitConverter.ToDouble(buffer, i);
191 public static void SetTypedArraySByte (JSObject obj) {
192 sbyte[] buffer = Enumerable.Repeat((sbyte)0x20, 11).ToArray();
193 obj.SetObjectProperty ("typedArray", buffer);
196 public static sbyte[] taSByte;
197 public static void GetTypedArraySByte (JSObject obj) {
198 taSByte = (sbyte[])obj.GetObjectProperty ("typedArray");
201 public static void SetTypedArrayByte (JSObject obj) {
202 var dragons = "hic sunt dracones";
203 byte[] buffer = System.Text.Encoding.ASCII.GetBytes(dragons);
204 obj.SetObjectProperty ("dracones", buffer);
207 public static byte[] taByte;
208 public static void GetTypedArrayByte (JSObject obj) {
209 taByte = (byte[])obj.GetObjectProperty ("dracones");
212 public static void SetTypedArrayShort (JSObject obj) {
213 short[] buffer = Enumerable.Repeat((short)0x20, 13).ToArray();
214 obj.SetObjectProperty ("typedArray", buffer);
217 public static short[] taShort;
218 public static void GetTypedArrayShort (JSObject obj) {
219 taShort = (short[])obj.GetObjectProperty ("typedArray");
222 public static void SetTypedArrayUShort (JSObject obj) {
223 ushort[] buffer = Enumerable.Repeat((ushort)0x20, 14).ToArray();
224 obj.SetObjectProperty ("typedArray", buffer);
227 public static ushort[] taUShort;
228 public static void GetTypedArrayUShort (JSObject obj) {
229 taUShort = (ushort[])obj.GetObjectProperty ("typedArray");
233 public static void SetTypedArrayInt (JSObject obj) {
234 int[] buffer = Enumerable.Repeat((int)0x20, 15).ToArray();
235 obj.SetObjectProperty ("typedArray", buffer);
238 public static int[] taInt;
239 public static void GetTypedArrayInt (JSObject obj) {
240 taInt = (int[])obj.GetObjectProperty ("typedArray");
243 public static void SetTypedArrayUInt (JSObject obj) {
244 uint[] buffer = Enumerable.Repeat((uint)0x20, 16).ToArray();
245 obj.SetObjectProperty ("typedArray", buffer);
248 public static uint[] taUInt;
249 public static void GetTypedArrayUInt (JSObject obj) {
250 taUInt = (uint[])obj.GetObjectProperty ("typedArray");
253 public static void SetTypedArrayFloat (JSObject obj) {
254 float[] buffer = Enumerable.Repeat(3.14f, 17).ToArray();
255 obj.SetObjectProperty ("typedArray", buffer);
258 public static float[] taFloat;
259 public static void GetTypedArrayFloat (JSObject obj) {
260 taFloat = (float[])obj.GetObjectProperty ("typedArray");
264 public static void SetTypedArrayDouble (JSObject obj) {
265 double[] buffer = Enumerable.Repeat(3.14d, 18).ToArray();
266 obj.SetObjectProperty ("typedArray", buffer);
269 public static double[] taDouble;
270 public static void GetTypedArrayDouble (JSObject obj) {
271 taDouble = (double[])obj.GetObjectProperty ("typedArray");
274 public static HttpClient client;
275 public static string fakeClientHandlerString;
276 public static HttpClientHandler fakeClientHandler;
277 public static void SetMessageHandler () {
279 var httpMessageHandler = typeof(HttpClient).GetField("GetHttpMessageHandler",
280 BindingFlags.Static |
281 BindingFlags.NonPublic);
283 httpMessageHandler.SetValue(null, (Func<HttpClientHandler>) (() => {
284 return new FakeHttpClientHandler ();
285 }));
287 client = new HttpClient();
290 public static RequestCache[] requestEnums;
292 public static void SetRequestEnums (RequestCache dflt, RequestCache nostore, RequestCache reload, RequestCache nocache, RequestCache force, RequestCache onlyif)
294 requestEnums = new RequestCache[6];
295 requestEnums[0] = dflt;
296 requestEnums[1] = nostore;
297 requestEnums[2] = reload;
298 requestEnums[3] = nocache;
299 requestEnums[4] = force;
300 requestEnums[5] = onlyif;
303 public static void SetRequestEnumsProperties (JSObject obj)
305 obj.SetObjectProperty("dflt", RequestCache.Default);
306 obj.SetObjectProperty("nostore", RequestCache.NoStore);
307 obj.SetObjectProperty("reload", RequestCache.Reload);
308 obj.SetObjectProperty("nocache", RequestCache.NoCache);
309 obj.SetObjectProperty("force", RequestCache.ForceCache);
310 obj.SetObjectProperty("onlyif", RequestCache.OnlyIfCached);
316 public class FakeHttpClientHandler : HttpClientHandler
318 public FakeHttpClientHandler () : base()
320 TestClass.fakeClientHandlerString = "Fake HttpClientHandler";
321 TestClass.fakeClientHandler = this;
325 public enum RequestCache
327 [Export(EnumValue = ConvertEnum.Default)]
328 Default = -1,
329 [Export("no-store")]
330 NoStore,
331 [Export(EnumValue = ConvertEnum.ToUpper)]
332 Reload,
333 [Export(EnumValue = ConvertEnum.ToLower)]
334 NoCache,
335 [Export("force-cache")]
336 ForceCache,
337 OnlyIfCached = -3636,
341 [TestFixture]
342 public class BindingTests {
343 [Test]
344 public static void MarshalPrimitivesToCS ()
346 TestClass.i32_res = 0;
347 Runtime.InvokeJS ("call_test_method(\"InvokeI32\", \"ii\", [10, 20])");
348 Assert.AreEqual (TestClass.i32_res, 30);
350 TestClass.f32_res = 0;
351 Runtime.InvokeJS ("call_test_method(\"InvokeFloat\", \"f\", [1.5])");
352 Assert.AreEqual (TestClass.f32_res, 1.5f);
354 TestClass.f64_res = 0;
355 Runtime.InvokeJS ("call_test_method(\"InvokeDouble\", \"d\", [4.5])");
356 Assert.AreEqual (TestClass.f64_res, 4.5);
358 TestClass.i64_res = 0;
359 Runtime.InvokeJS ("call_test_method(\"InvokeLong\", \"l\", [99])");
360 Assert.AreEqual (TestClass.i64_res, 99);
363 [Test]
364 public static void MarshalStringToCS ()
366 TestClass.string_res = null;
367 Runtime.InvokeJS ("call_test_method(\"InvokeString\", \"s\", [\"hello\"])");
368 Assert.AreEqual (TestClass.string_res, "hello");
371 [Test]
372 public static void MarshalStringToJS ()
374 TestClass.mkstr = TestClass.string_res = null;
375 Runtime.InvokeJS (@"
376 var str = call_test_method (""InvokeMkString"", ""o"", [ ]);
377 call_test_method (""InvokeString"", ""s"", [ str ]);
379 Assert.IsNotNull(TestClass.mkstr);
381 Assert.AreEqual (TestClass.mkstr, TestClass.string_res);
384 [Test]
385 public static void JSObjectKeepIdentityAcrossCalls ()
387 TestClass.obj1 = TestClass.obj2 = null;
388 Runtime.InvokeJS (@"
389 var obj = { foo: 10 };
390 var res = call_test_method (""InvokeObj1"", ""o"", [ obj ]);
391 call_test_method (""InvokeObj2"", ""o"", [ res ]);
394 Assert.IsNotNull(TestClass.obj1);
395 Assert.AreSame(TestClass.obj1, TestClass.obj2);
398 [Test]
399 public static void CSObjectKeepIdentityAcrossCalls ()
401 TestClass.mkobj = TestClass.obj1 = TestClass.obj2 = null;
402 Runtime.InvokeJS (@"
403 var obj = call_test_method (""InvokeMkobj"", """", [ ]);
404 var res = call_test_method (""InvokeObj1"", ""o"", [ obj ]);
405 call_test_method (""InvokeObj2"", ""o"", [ res ]);
408 Assert.IsNotNull(TestClass.obj1);
409 Assert.AreSame(TestClass.mkobj, TestClass.obj1);
410 Assert.AreSame(TestClass.obj1, TestClass.obj2);
413 [Test]
414 public static void JSInvokeInt() {
415 Runtime.InvokeJS (@"
416 var obj = {
417 foo: 10,
418 inc: function() {
419 var c = this.foo;
420 ++this.foo;
421 return c;
423 add: function(val){
424 return this.foo + val;
427 call_test_method (""PlayWithObj"", ""o"", [ obj ]);
430 Assert.AreEqual (TestClass.first_val, 10);
431 Assert.AreEqual (TestClass.second_val, 31);
434 [Test]
435 public static void JSInvokeTypes() {
436 Runtime.InvokeJS (@"
437 var obj = {
438 return_int: function() { return 100; },
439 return_double: function() { return 4.5; },
440 return_string: function() { return 'qwerty'; },
441 return_bool: function() { return true; },
443 call_test_method (""PlayWithObjTypes"", ""o"", [ obj ]);
446 Assert.AreEqual (TestClass.js_objs [0], 100);
447 Assert.AreEqual (TestClass.js_objs [1], 4.5);
448 Assert.AreEqual (TestClass.js_objs [2], "qwerty");
449 Assert.AreEqual (TestClass.js_objs [3], true);
452 [Test]
453 public static void JSObjectApply() {
454 Runtime.InvokeJS (@"
455 var do_add = function(a, b) { return a + b};
456 call_test_method (""UseFunction"", ""o"", [ do_add ]);
458 Assert.AreEqual (TestClass.do_add, 30);
461 [Test]
462 public static void MarshalDelegate() {
463 TestClass.obj1 = null;
464 Runtime.InvokeJS (@"
465 var dele = call_test_method (""MkDelegate"", """", [ ]);
466 var res = dele (10, 20);
467 call_test_method (""InvokeI32"", ""ii"", [ res, res ]);
470 Assert.AreEqual (TestClass.dele_res, 30);
471 Assert.AreEqual (TestClass.i32_res, 60);
474 [Test]
475 public static void PassTaskToJS () {
476 TestClass.int_val = 0;
477 Runtime.InvokeJS (@"
478 var tsk = call_test_method (""MkTask"", """", [ ]);
479 tsk.then (function (value) {
480 Module.print ('PassTaskToJS cont with value ' + value);
483 Assert.AreEqual (0, TestClass.int_val);
484 TestClass.tcs.SetResult (99);
485 //FIXME our test harness doesn't suppport async tests.
486 // So manually verify it for now by checking stdout for `PassTaskToJS cont with value 99`
487 //Assert.AreEqual (99, TestClass.int_val);
491 [Test]
492 public static void PassTaskToJS2 () {
493 TestClass.int_val = 0;
494 Runtime.InvokeJS (@"
495 var tsk = call_test_method (""MkTask"", """", [ ]);
496 tsk.then (function (value) {},
497 function (reason) {
498 Module.print ('PassTaskToJS2 cont failed due to ' + reason);
501 Assert.AreEqual (0, TestClass.int_val);
502 TestClass.tcs.SetException (new Exception ("it failed"));
503 //FIXME our test harness doesn't suppport async tests.
504 // So manually verify it for now by checking stdout for `PassTaskToJS2 cont failed due to System.AggregateException...
505 // Assert.AreEqual (99, TestClass.int_val);
508 [Test]
509 public static void PassTaskToJS3 () {
510 TestClass.int_val = 0;
511 Runtime.InvokeJS (@"
512 var tsk = call_test_method (""MkTaskNull"", """", [ ]);
513 tsk.then( () => {
514 Module.print('PassTaskToJS3 cont without value '); // Success!
515 }, reason => {
516 Module.print('PassTaskToJS3 cont failed due to ' + reason); // Error!
517 } );
519 Assert.AreEqual (0, TestClass.int_val);
520 TestClass.tcs3.SetResult (null);
523 [Test]
524 public static void PassTaskToJS4 () {
525 TestClass.int_val = 0;
526 Runtime.InvokeJS (@"
527 var tsk = call_test_method (""MkTaskNull"", """", [ ]);
528 tsk.then( value => {
529 Module.print(value); // Success!
530 }, reason => {
531 Module.print('PassTaskToJS4 cont failed due to ' + reason); // Error!
532 } );
534 Assert.AreEqual (0, TestClass.int_val);
535 TestClass.tcs3.SetException (new Exception ("it failed"));
538 [Test]
539 public static void PassTaskToJS5 () {
540 TestClass.int_val = 0;
541 Runtime.InvokeJS (@"
542 var tsk = call_test_method (""MkTaskString"", """", [ ]);
543 tsk.then( success => {
544 Module.print('PassTaskToJS5 cont with value ' + success); // Success!
545 }, reason => {
546 Module.print('PassTaskToJS5 cont failed due to ' + reason); // Error!
547 } );
549 Assert.AreEqual (0, TestClass.int_val);
550 TestClass.tcs3.SetResult ("Success");
553 [Test]
554 public static void PassTaskToJS6 () {
555 TestClass.int_val = 0;
556 Runtime.InvokeJS (@"
557 var tsk = call_test_method (""MkTaskString"", """", [ ]);
558 tsk.then( success => {
559 Module.print('PassTaskToJS6 cont with value ' + success); // Success!
560 }, reason => {
561 Module.print('PassTaskToJS6 cont failed due to ' + reason); // Error!
562 } );
564 Assert.AreEqual (0, TestClass.int_val);
565 TestClass.tcs3.SetException (new Exception ("it failed"));
568 [Test]
569 public static void PassPromiseToCS () {
570 TestClass.int_val = 0;
571 Runtime.InvokeJS (@"
572 var resolve_func = null;
573 var promise = new Promise(function (resolve, reject) {
574 resolve_func = resolve;
576 call_test_method (""InvokePromise"", ""o"", [ promise ]);
577 resolve_func (111);
579 //FIXME our test harness doesn't suppport async tests.
580 // So manually verify it for now by checking stdout for `Promise result is 111`
581 // Assert.AreEqual (99, TestClass.int_val);
584 [Test]
585 public static void BindStaticMethod () {
586 TestClass.int_val = 0;
587 Runtime.InvokeJS (@"
588 var invoke_int = Module.mono_bind_static_method (""[binding_tests]TestClass:InvokeInt"");
589 invoke_int (200);
592 Assert.AreEqual (200, TestClass.int_val);
595 [Test]
596 public static void InvokeStaticMethod () {
597 TestClass.int_val = 0;
598 Runtime.InvokeJS (@"
599 Module.mono_call_static_method (""[binding_tests]TestClass:InvokeInt"", [ 300 ]);
602 Assert.AreEqual (300, TestClass.int_val);
605 [Test]
606 public static void ResolveMethod () {
607 TestClass.int_val = 0;
608 Runtime.InvokeJS (@"
609 var invoke_int = Module.mono_method_resolve (""[binding_tests]TestClass:InvokeInt"");
610 call_test_method (""InvokeInt"", ""i"", [ invoke_int ]);
613 Assert.AreNotEqual (0, TestClass.int_val);
616 [Test]
617 public static void DisposeObject () {
618 Runtime.InvokeJS (@"
619 var obj1 = {
621 var obj2 = {
623 var obj3 = {
625 call_test_method (""DisposeObject"", ""o"", [ obj3 ]);
626 call_test_method (""DisposeObject"", ""o"", [ obj2 ]);
627 call_test_method (""DisposeObject"", ""o"", [ obj1 ]);
630 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [0].JSHandle);
631 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [1].JSHandle);
632 Assert.AreEqual (-1, TestClass.js_objs_to_dispose [2].JSHandle);
635 [Test]
636 public static void GetObjectProperties () {
637 Runtime.InvokeJS (@"
638 var obj = {myInt: 100, myDouble: 4.5, myString: ""qwerty"", myBoolean: true};
639 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
642 Assert.AreEqual (100, TestClass.js_props [0]);
643 Assert.AreEqual (4.5, TestClass.js_props [1]);
644 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
645 Assert.AreEqual (true, TestClass.js_props [3]);
648 [Test]
649 public static void SetObjectProperties () {
650 Runtime.InvokeJS (@"
651 var obj = {myInt: 200, myDouble: 0, myString: ""foo"", myBoolean: false};
652 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, false ]);
653 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
656 Assert.AreEqual (100, TestClass.js_props [0]);
657 Assert.AreEqual (4.5, TestClass.js_props [1]);
658 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
659 Assert.AreEqual (true, TestClass.js_props [3]);
662 [Test]
663 public static void SetObjectPropertiesIfNotExistsFalse () {
664 // This test will not create the properties if they do not already exist
665 Runtime.InvokeJS (@"
666 var obj = {myInt: 200};
667 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, false ]);
668 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
671 Assert.AreEqual (100, TestClass.js_props [0]);
672 Assert.AreEqual (null, TestClass.js_props [1]);
673 Assert.AreEqual (null, TestClass.js_props [2]);
674 Assert.AreEqual (null, TestClass.js_props [3]);
677 [Test]
678 public static void SetObjectPropertiesIfNotExistsTrue () {
679 // This test will set the value of the property if it exists and will create and
680 // set the value if it does not exists
681 Runtime.InvokeJS (@"
682 var obj = {myInt: 200};
683 call_test_method (""PopulateObjectProperties"", ""oi"", [ obj, true ]);
684 call_test_method (""RetrieveObjectProperties"", ""o"", [ obj ]);
687 Assert.AreEqual (100, TestClass.js_props [0]);
688 Assert.AreEqual (4.5, TestClass.js_props [1]);
689 Assert.AreEqual ("qwerty", TestClass.js_props [2]);
690 Assert.AreEqual (true, TestClass.js_props [3]);
694 [Test]
695 public static void MarshalArrayBuffer () {
696 Runtime.InvokeJS (@"
697 var buffer = new ArrayBuffer(16);
698 call_test_method (""MarshalByteBuffer"", ""o"", [ buffer ]);
701 Assert.AreEqual (16, TestClass.byteBuffer.Length);
704 [Test]
705 public static void MarshalArrayBuffer2Int () {
706 // This really does not work to be honest
707 // The length of the marshalled array is 16 ints but
708 // the first 4 ints will be correct and the rest will
709 // probably be trash from memory
710 Runtime.InvokeJS (@"
711 var buffer = new ArrayBuffer(16);
712 var int32View = new Int32Array(buffer);
713 for (var i = 0; i < int32View.length; i++) {
714 int32View[i] = i * 2;
716 call_test_method (""MarshalInt32Array"", ""o"", [ buffer ]);
719 Assert.AreEqual (16, TestClass.intBuffer.Length);
720 Assert.AreEqual (0, TestClass.intBuffer[0]);
721 Assert.AreEqual (2, TestClass.intBuffer[1]);
722 Assert.AreEqual (4, TestClass.intBuffer[2]);
723 Assert.AreEqual (6, TestClass.intBuffer[3]);
726 [Test]
727 public static void MarshalArrayBuffer2Int2 () {
729 Runtime.InvokeJS (@"
730 var buffer = new ArrayBuffer(16);
731 var int32View = new Int32Array(buffer);
732 for (var i = 0; i < int32View.length; i++) {
733 int32View[i] = i * 2;
735 call_test_method (""MarshalByteBufferToInts"", ""o"", [ buffer ]);
738 Assert.AreEqual (4, TestClass.intBuffer.Length);
739 Assert.AreEqual (0, TestClass.intBuffer[0]);
740 Assert.AreEqual (2, TestClass.intBuffer[1]);
741 Assert.AreEqual (4, TestClass.intBuffer[2]);
742 Assert.AreEqual (6, TestClass.intBuffer[3]);
746 [Test]
747 public static void MarshalTypedArray () {
748 Runtime.InvokeJS (@"
749 var buffer = new ArrayBuffer(16);
750 var uint8View = new Uint8Array(buffer);
751 call_test_method (""MarshalByteBuffer"", ""o"", [ uint8View ]);
754 Assert.AreEqual (16, TestClass.byteBuffer.Length);
757 [Test]
758 public static void MarshalTypedArray2Int () {
759 Runtime.InvokeJS (@"
760 var buffer = new ArrayBuffer(16);
761 var int32View = new Int32Array(buffer);
762 for (var i = 0; i < int32View.length; i++) {
763 int32View[i] = i * 2;
765 call_test_method (""MarshalInt32Array"", ""o"", [ int32View ]);
768 Assert.AreEqual (4, TestClass.intBuffer.Length);
769 Assert.AreEqual (0, TestClass.intBuffer[0]);
770 Assert.AreEqual (2, TestClass.intBuffer[1]);
771 Assert.AreEqual (4, TestClass.intBuffer[2]);
772 Assert.AreEqual (6, TestClass.intBuffer[3]);
775 [Test]
776 public static void MarshalTypedArray2Float () {
777 Runtime.InvokeJS (@"
778 var typedArray = new Float32Array([1, 2.1334, 3, 4.2, 5]);
779 call_test_method (""MarshalFloat32Array"", ""o"", [ typedArray ]);
782 Assert.AreEqual (1, TestClass.floatBuffer[0]);
783 Assert.AreEqual (2.1334f, TestClass.floatBuffer[1]);
784 Assert.AreEqual (3, TestClass.floatBuffer[2]);
785 Assert.AreEqual (4.2f, TestClass.floatBuffer[3]);
786 Assert.AreEqual (5, TestClass.floatBuffer[4]);
789 [Test]
790 public static void MarshalArrayBuffer2Float () {
791 Runtime.InvokeJS (@"
792 var buffer = new ArrayBuffer(16);
793 var float32View = new Float32Array(buffer);
794 for (var i = 0; i < float32View.length; i++) {
795 float32View[i] = i * 2.5;
797 call_test_method (""MarshalByteBufferToFloats"", ""o"", [ buffer ]);
800 Assert.AreEqual (4, TestClass.floatBuffer.Length);
801 Assert.AreEqual (0, TestClass.floatBuffer[0]);
802 Assert.AreEqual (2.5f, TestClass.floatBuffer[1]);
803 Assert.AreEqual (5, TestClass.floatBuffer[2]);
804 Assert.AreEqual (7.5f, TestClass.floatBuffer[3]);
807 [Test]
808 public static void MarshalArrayBuffer2Float2 () {
809 // This really does not work to be honest
810 // The length of the marshalled array is 16 floats but
811 // the first 4 floats will be correct and the rest will
812 // probably be trash from memory
813 Runtime.InvokeJS (@"
814 var buffer = new ArrayBuffer(16);
815 var float32View = new Float32Array(buffer);
816 for (var i = 0; i < float32View.length; i++) {
817 float32View[i] = i * 2.5;
819 call_test_method (""MarshalFloat32Array"", ""o"", [ buffer ]);
822 Assert.AreEqual (16, TestClass.floatBuffer.Length);
823 Assert.AreEqual (0, TestClass.floatBuffer[0]);
824 Assert.AreEqual (2.5f, TestClass.floatBuffer[1]);
825 Assert.AreEqual (5, TestClass.floatBuffer[2]);
826 Assert.AreEqual (7.5f, TestClass.floatBuffer[3]);
829 [Test]
830 public static void MarshalTypedArray2Double () {
831 Runtime.InvokeJS (@"
832 var typedArray = new Float64Array([1, 2.1334, 3, 4.2, 5]);
833 call_test_method (""MarshalFloat64Array"", ""o"", [ typedArray ]);
836 Assert.AreEqual (1, TestClass.doubleBuffer[0]);
837 Assert.AreEqual (2.1334d, TestClass.doubleBuffer[1]);
838 Assert.AreEqual (3, TestClass.doubleBuffer[2]);
839 Assert.AreEqual (4.2d, TestClass.doubleBuffer[3]);
840 Assert.AreEqual (5, TestClass.doubleBuffer[4]);
843 [Test]
844 public static void MarshalArrayBuffer2Double () {
845 Runtime.InvokeJS (@"
846 var buffer = new ArrayBuffer(32);
847 var float64View = new Float64Array(buffer);
848 for (var i = 0; i < float64View.length; i++) {
849 float64View[i] = i * 2.5;
851 call_test_method (""MarshalByteBufferToDoubles"", ""o"", [ buffer ]);
854 Assert.AreEqual (4, TestClass.doubleBuffer.Length);
855 Assert.AreEqual (0, TestClass.doubleBuffer[0]);
856 Assert.AreEqual (2.5d, TestClass.doubleBuffer[1]);
857 Assert.AreEqual (5, TestClass.doubleBuffer[2]);
858 Assert.AreEqual (7.5d, TestClass.doubleBuffer[3]);
861 [Test]
862 public static void MarshalArrayBuffer2Double2 () {
863 // This really does not work to be honest
864 // The length of the marshalled array is 32 doubles but
865 // the first 4 doubles will be correct and the rest will
866 // probably be trash from memory
867 Runtime.InvokeJS (@"
868 var buffer = new ArrayBuffer(32);
869 var float64View = new Float64Array(buffer);
870 for (var i = 0; i < float64View.length; i++) {
871 float64View[i] = i * 2.5;
873 call_test_method (""MarshalFloat64Array"", ""o"", [ buffer ]);
876 Assert.AreEqual (32, TestClass.doubleBuffer.Length);
877 Assert.AreEqual (0, TestClass.doubleBuffer[0]);
878 Assert.AreEqual (2.5f, TestClass.doubleBuffer[1]);
879 Assert.AreEqual (5, TestClass.doubleBuffer[2]);
880 Assert.AreEqual (7.5f, TestClass.doubleBuffer[3]);
883 [Test]
884 public static void MarshalTypedArraySByte () {
885 TestClass.int_val = 0;
886 Runtime.InvokeJS (@"
887 var obj = { };
888 call_test_method (""SetTypedArraySByte"", ""o"", [ obj ]);
889 call_test_method (""GetTypedArraySByte"", ""o"", [ obj ]);
891 Assert.AreEqual (11, TestClass.taSByte.Length);
892 Assert.AreEqual (32, TestClass.taSByte[0]);
893 Assert.AreEqual (32, TestClass.taSByte[TestClass.taSByte.Length - 1]);
896 [Test]
897 public static void MarshalTypedArrayByte () {
898 TestClass.int_val = 0;
899 Runtime.InvokeJS (@"
900 var obj = { };
901 call_test_method (""SetTypedArrayByte"", ""o"", [ obj ]);
902 call_test_method (""GetTypedArrayByte"", ""o"", [ obj ]);
904 Assert.AreEqual (17, TestClass.taByte.Length);
905 Assert.AreEqual (104, TestClass.taByte[0]);
906 Assert.AreEqual (115, TestClass.taByte[TestClass.taByte.Length - 1]);
907 Assert.AreEqual ("hic sunt dracones", System.Text.Encoding.Default.GetString(TestClass.taByte));
910 [Test]
911 public static void MarshalTypedArrayShort () {
912 TestClass.int_val = 0;
913 Runtime.InvokeJS (@"
914 var obj = { };
915 call_test_method (""SetTypedArrayShort"", ""o"", [ obj ]);
916 call_test_method (""GetTypedArrayShort"", ""o"", [ obj ]);
918 Assert.AreEqual (13, TestClass.taShort.Length);
919 Assert.AreEqual (32, TestClass.taShort[0]);
920 Assert.AreEqual (32, TestClass.taShort[TestClass.taShort.Length - 1]);
923 [Test]
924 public static void MarshalTypedArrayUShort () {
925 TestClass.int_val = 0;
926 Runtime.InvokeJS (@"
927 var obj = { };
928 call_test_method (""SetTypedArrayUShort"", ""o"", [ obj ]);
929 call_test_method (""GetTypedArrayUShort"", ""o"", [ obj ]);
931 Assert.AreEqual (14, TestClass.taUShort.Length);
932 Assert.AreEqual (32, TestClass.taUShort[0]);
933 Assert.AreEqual (32, TestClass.taUShort[TestClass.taUShort.Length - 1]);
937 [Test]
938 public static void MarshalTypedArrayInt () {
939 TestClass.int_val = 0;
940 Runtime.InvokeJS (@"
941 var obj = { };
942 call_test_method (""SetTypedArrayInt"", ""o"", [ obj ]);
943 call_test_method (""GetTypedArrayInt"", ""o"", [ obj ]);
945 Assert.AreEqual (15, TestClass.taInt.Length);
946 Assert.AreEqual (32, TestClass.taInt[0]);
947 Assert.AreEqual (32, TestClass.taInt[TestClass.taInt.Length - 1]);
950 [Test]
951 public static void MarshalTypedArrayUInt () {
952 TestClass.int_val = 0;
953 Runtime.InvokeJS (@"
954 var obj = { };
955 call_test_method (""SetTypedArrayUInt"", ""o"", [ obj ]);
956 call_test_method (""GetTypedArrayUInt"", ""o"", [ obj ]);
958 Assert.AreEqual (16, TestClass.taUInt.Length);
959 Assert.AreEqual (32, TestClass.taUInt[0]);
960 Assert.AreEqual (32, TestClass.taUInt[TestClass.taUInt.Length - 1]);
963 [Test]
964 public static void MarshalTypedArrayFloat () {
965 TestClass.int_val = 0;
966 Runtime.InvokeJS (@"
967 var obj = { };
968 call_test_method (""SetTypedArrayFloat"", ""o"", [ obj ]);
969 call_test_method (""GetTypedArrayFloat"", ""o"", [ obj ]);
971 Assert.AreEqual (17, TestClass.taFloat.Length);
972 Assert.AreEqual (3.14f, TestClass.taFloat[0]);
973 Assert.AreEqual (3.14f, TestClass.taFloat[TestClass.taFloat.Length - 1]);
977 [Test]
978 public static void MarshalTypedArrayDouble () {
979 TestClass.int_val = 0;
980 Runtime.InvokeJS (@"
981 var obj = { };
982 call_test_method (""SetTypedArrayDouble"", ""o"", [ obj ]);
983 call_test_method (""GetTypedArrayDouble"", ""o"", [ obj ]);
985 Assert.AreEqual (18, TestClass.taDouble.Length);
986 Assert.AreEqual (3.14d, TestClass.taDouble[0]);
987 Assert.AreEqual (3.14d, TestClass.taDouble[TestClass.taDouble.Length - 1]);
991 [Test]
992 public static void HttpMessageHandler () {
993 TestClass.fakeClientHandlerString = string.Empty;
994 TestClass.fakeClientHandler = null;
995 TestClass.client = null;
996 Runtime.InvokeJS (@"
997 call_test_method (""SetMessageHandler"", ""o"", [ ]);
999 Assert.AreEqual ("Fake HttpClientHandler", TestClass.fakeClientHandlerString);
1000 Assert.AreNotEqual (null, TestClass.fakeClientHandler);
1001 Assert.AreEqual (typeof(FakeHttpClientHandler), TestClass.fakeClientHandler.GetType());
1002 Assert.AreNotEqual (null, TestClass.client);
1005 [Test]
1006 public static void MarshalRequestEnums () {
1007 Runtime.InvokeJS (@"
1008 var dflt = ""Default"";
1009 var nostore = ""no-store"";
1010 var reload = ""RELOAD"";
1011 var nocache = ""nocache"";
1012 var force = 3;
1013 var onlyif = -3636;
1014 Module.mono_call_static_method (""[binding_tests]TestClass:SetRequestEnums"", [ dflt, nostore, reload, nocache, force, onlyif ]);
1016 Assert.AreEqual (RequestCache.Default, TestClass.requestEnums[0]);
1017 Assert.AreEqual (RequestCache.NoStore, TestClass.requestEnums[1]);
1018 Assert.AreEqual (RequestCache.Reload, TestClass.requestEnums[2]);
1019 Assert.AreEqual (RequestCache.NoCache, TestClass.requestEnums[3]);
1020 Assert.AreEqual (RequestCache.ForceCache, TestClass.requestEnums[4]);
1021 Assert.AreEqual (RequestCache.OnlyIfCached, TestClass.requestEnums[5]);
1024 [Test]
1025 public static void MarshalRequestEnumProps () {
1026 Runtime.InvokeJS (@"
1027 var obj = {};
1028 Module.mono_call_static_method (""[binding_tests]TestClass:SetRequestEnumsProperties"", [ obj ]);
1029 Module.mono_call_static_method (""[binding_tests]TestClass:SetRequestEnums"", [ obj.dflt, obj.nostore, obj.reload, obj.nocache, obj.force, obj.onlyif ]);
1031 Assert.AreEqual (RequestCache.Default, TestClass.requestEnums[0]);
1032 Assert.AreEqual (RequestCache.NoStore, TestClass.requestEnums[1]);
1033 Assert.AreEqual (RequestCache.Reload, TestClass.requestEnums[2]);
1034 Assert.AreEqual (RequestCache.NoCache, TestClass.requestEnums[3]);
1035 Assert.AreEqual (RequestCache.ForceCache, TestClass.requestEnums[4]);
1036 Assert.AreEqual (RequestCache.OnlyIfCached, TestClass.requestEnums[5]);