Add test to more explicitly cover both 'params array' and 'generics'-style interop APIs

This commit is contained in:
Steve Sanderson 2017-12-15 19:41:20 +00:00
parent 09eccb52c5
commit 04064f983c
4 changed files with 67 additions and 2 deletions

View File

@ -45,6 +45,16 @@
</form>
</fieldset>
<fieldset>
<legend>Call JS from .NET (no boxing)</legend>
<form id="callJsNoBoxing">
<input id="callJsNoBoxingNumberA" value="28" /> /
<input id="callJsNoBoxingNumberB" value="4" /> =
<input id="callJsNoBoxingResult" readonly />
<button type="submit" disabled>Go</button>
</form>
</fieldset>
<p id="loadingIndicator">Loading...</p>
<script src="loader.js"></script>
@ -91,6 +101,14 @@
el('callJsResult').value = result;
};
el('callJsNoBoxing').onsubmit = function (evt) {
evt.preventDefault();
var a = parseInt(el('callJsNoBoxingNumberA').value);
var b = parseInt(el('callJsNoBoxingNumberB').value);
var result = invokeMonoMethod('MonoSanityClient', 'MonoSanityClient', 'Examples', 'CallJsNoBoxing', [a, b]);
el('callJsNoBoxingResult').value = result;
};
function el(id) {
return document.getElementById(id);
}
@ -115,6 +133,15 @@
return result === null || result === undefined
? result // Pass through null/undefined so we can verify this is handled upstream
: javaScriptStringToDotNetString(result.toString());
},
divideNumbersUnmarshalled: function (a, b) {
// In this example, neither the arguments nor return value are boxed
// -- we expect to receive and return numbers directly
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
}
};
</script>

View File

@ -31,7 +31,8 @@ namespace MonoSanityClient
public static string EvaluateJavaScript(string expression)
{
var result = Runtime.InvokeJS<string, object, object, string>(out var exceptionMessage, "evaluateJsExpression", expression, null, null);
// For tests that call this method, we'll exercise the 'InvokeJSArray' code path
var result = Runtime.InvokeJSArray<string>(out var exceptionMessage, "evaluateJsExpression", expression, null, null);
if (exceptionMessage != null)
{
return $".NET got exception: {exceptionMessage}";
@ -39,5 +40,18 @@ namespace MonoSanityClient
return $".NET received: {(result ?? "(NULL)")}";
}
public static string CallJsNoBoxing(int numberA, int numberB)
{
// For tests that call this method, we'll exercise the 'InvokeJS' code path
// since that doesn't box the params
var result = Runtime.InvokeJS<int, int, object, int>(out var exceptionMessage, "divideNumbersUnmarshalled", numberA, numberB, null);
if (exceptionMessage != null)
{
return $".NET got exception: {exceptionMessage}";
}
return $".NET received: {result}";
}
}
}

View File

@ -14,6 +14,6 @@ namespace WebAssembly
public static extern TRes InvokeJS<T0, T1, T2, TRes>(out string exception, string funcName, T0 arg0, T1 arg1, T2 arg2);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern TRes InvokeJSArray<TRes>(out string exception, string funcName, object[] args);
public static extern TRes InvokeJSArray<TRes>(out string exception, string funcName, params object[] args);
}
}

View File

@ -101,6 +101,30 @@ namespace Microsoft.Blazor.E2ETest.Tests
Assert.Equal(".NET received: (NULL)", result);
}
[Fact]
public void CanCallJsFunctionsWithoutBoxing()
{
Navigate("/", noReload: true);
SetValue(Browser, "callJsNoBoxingNumberA", "108");
SetValue(Browser, "callJsNoBoxingNumberB", "4");
Browser.FindElement(By.CssSelector("#callJsNoBoxing button")).Click();
Assert.Equal(".NET received: 27", GetValue(Browser, "callJsNoBoxingResult"));
}
[Fact]
public void CanCallJsFunctionsWithoutBoxingAndReceiveException()
{
Navigate("/", noReload: true);
SetValue(Browser, "callJsNoBoxingNumberA", "1");
SetValue(Browser, "callJsNoBoxingNumberB", "0");
Browser.FindElement(By.CssSelector("#callJsNoBoxing button")).Click();
Assert.StartsWith(".NET got exception: Division by zero", GetValue(Browser, "callJsNoBoxingResult"));
}
private static string GetValue(IWebDriver webDriver, string elementId)
{
var element = webDriver.FindElement(By.Id(elementId));