|
3 | 3 |
|
4 | 4 | using System.Runtime.InteropServices.JavaScript;
|
5 | 5 | using System.Collections.Generic;
|
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
6 | 8 | using Xunit;
|
7 | 9 |
|
8 | 10 | namespace System.Runtime.InteropServices.JavaScript.Tests
|
@@ -899,5 +901,122 @@ public static void InvokeJSNotInGlobalScope()
|
899 | 901 | var result = Runtime.InvokeJS(@"var test_local_variable_name = 5; globalThis.test_local_variable_name");
|
900 | 902 | Assert.Null(result);
|
901 | 903 | }
|
| 904 | + |
| 905 | + private static async Task<bool> MarshalTask(string helperMethodName, string helperMethodArgs = "", string resolvedBody = "") |
| 906 | + { |
| 907 | + Runtime.InvokeJS( |
| 908 | + @"globalThis.__test_promise_completed = false; " + |
| 909 | + @"globalThis.__test_promise_resolved = false; " + |
| 910 | + @"globalThis.__test_promise_failed = false; " + |
| 911 | + $@"var t = App.call_test_method ('{helperMethodName}', [ {helperMethodArgs} ], 'i'); " + |
| 912 | + "t.finally(result => { globalThis.__test_promise_completed = true; }); " + |
| 913 | + "t.then(result => { globalThis.__test_promise_resolved = true; " + resolvedBody + " }); " + |
| 914 | + "t.catch(e => { console.log(e); globalThis.__test_promise_failed = true; }); " |
| 915 | + ); |
| 916 | + |
| 917 | + await Task.Delay(1); |
| 918 | + |
| 919 | + var completed = bool.Parse(Runtime.InvokeJS(@"globalThis.__test_promise_completed")); |
| 920 | + Assert.True(completed, "JavasScript promise did not completed."); |
| 921 | + |
| 922 | + var resolved = bool.Parse(Runtime.InvokeJS(@"globalThis.__test_promise_resolved")); |
| 923 | + return resolved; |
| 924 | + } |
| 925 | + |
| 926 | + private static async Task MarshalTaskReturningInt(string helperMethodName) |
| 927 | + { |
| 928 | + HelperMarshal._intValue = 0; |
| 929 | + |
| 930 | + bool success = await MarshalTask(helperMethodName, "7", "App.call_test_method ('InvokeInt', [ result ], 'i');"); |
| 931 | + |
| 932 | + Assert.True(success, $"{helperMethodName} didn't succeeded."); |
| 933 | + Assert.Equal(7, HelperMarshal._intValue); |
| 934 | + } |
| 935 | + |
| 936 | + [Fact] |
| 937 | + public static async Task MarshalSynchronousTask() |
| 938 | + { |
| 939 | + bool success = await MarshalTask("SynchronousTask"); |
| 940 | + Assert.True(success, "SynchronousTask didn't succeeded."); |
| 941 | + } |
| 942 | + |
| 943 | + [Fact] |
| 944 | + public static async Task MarshalAsynchronousTask() |
| 945 | + { |
| 946 | + bool success = await MarshalTask("AsynchronousTask"); |
| 947 | + Assert.True(success, "AsynchronousTask didn't succeeded."); |
| 948 | + } |
| 949 | + |
| 950 | + [Fact] |
| 951 | + public static Task MarshalSynchronousTaskInt() |
| 952 | + { |
| 953 | + return MarshalTaskReturningInt("SynchronousTaskInt"); |
| 954 | + } |
| 955 | + |
| 956 | + [Fact] |
| 957 | + public static Task MarshalAsynchronousTaskInt() |
| 958 | + { |
| 959 | + return MarshalTaskReturningInt("AsynchronousTaskInt"); |
| 960 | + } |
| 961 | + |
| 962 | + [Fact] |
| 963 | + public static async Task MarshalFailedSynchronousTask() |
| 964 | + { |
| 965 | + bool success = await MarshalTask("FailedSynchronousTask"); |
| 966 | + Assert.False(success, "FailedSynchronousTask didn't failed."); |
| 967 | + } |
| 968 | + |
| 969 | + [Fact] |
| 970 | + public static async Task MarshalFailedAsynchronousTask() |
| 971 | + { |
| 972 | + bool success = await MarshalTask("FailedAsynchronousTask"); |
| 973 | + Assert.False(success, "FailedAsynchronousTask didn't failed."); |
| 974 | + } |
| 975 | + |
| 976 | + [Fact] |
| 977 | + [ActiveIssue("https://github.com/dotnet/runtime/issues/61368")] |
| 978 | + public static async Task MarshalSynchronousValueTaskDoesNotWorkYet() |
| 979 | + { |
| 980 | + bool success = await MarshalTask("SynchronousValueTask"); |
| 981 | + Assert.True(success, "SynchronousValueTask didn't succeeded."); |
| 982 | + } |
| 983 | + |
| 984 | + [Fact] |
| 985 | + [ActiveIssue("https://github.com/dotnet/runtime/issues/61368")] |
| 986 | + public static async Task MarshalAsynchronousValueTaskDoesNotWorkYet() |
| 987 | + { |
| 988 | + bool success = await MarshalTask("AsynchronousValueTask"); |
| 989 | + Assert.True(success, "AsynchronousValueTask didn't succeeded."); |
| 990 | + } |
| 991 | + |
| 992 | + [Fact] |
| 993 | + [ActiveIssue("https://github.com/dotnet/runtime/issues/61368")] |
| 994 | + public static Task MarshalSynchronousValueTaskIntDoesNotWorkYet() |
| 995 | + { |
| 996 | + return MarshalTaskReturningInt("SynchronousValueTaskInt"); |
| 997 | + } |
| 998 | + |
| 999 | + [Fact] |
| 1000 | + [ActiveIssue("https://github.com/dotnet/runtime/issues/61368")] |
| 1001 | + public static Task MarshalAsynchronousValueTaskIntDoesNotWorkYet() |
| 1002 | + { |
| 1003 | + return MarshalTaskReturningInt("AsynchronousValueTaskInt"); |
| 1004 | + } |
| 1005 | + |
| 1006 | + [Fact] |
| 1007 | + [ActiveIssue("https://github.com/dotnet/runtime/issues/61368")] |
| 1008 | + public static async Task MarshalFailedSynchronousValueTaskDoesNotWorkYet() |
| 1009 | + { |
| 1010 | + bool success = await MarshalTask("FailedSynchronousValueTask"); |
| 1011 | + Assert.False(success, "FailedSynchronousValueTask didn't failed."); |
| 1012 | + } |
| 1013 | + |
| 1014 | + [Fact] |
| 1015 | + [ActiveIssue("https://github.com/dotnet/runtime/issues/61368")] |
| 1016 | + public static async Task MarshalFailedAsynchronousValueTaskDoesNotWorkYet() |
| 1017 | + { |
| 1018 | + bool success = await MarshalTask("FailedAsynchronousValueTask"); |
| 1019 | + Assert.False(success, "FailedAsynchronousValueTask didn't failed."); |
| 1020 | + } |
902 | 1021 | }
|
903 | 1022 | }
|
0 commit comments