Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HybridWebView] Always serialize the value in JS when invoking #27068

Merged
merged 15 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@
},

"__TriggerAsyncCallback": function __TriggerAsyncCallback(taskId, result) {
// Make sure the result is a string
if (result && typeof (result) !== 'string') {
result = JSON.stringify(result);
}

window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptCompleted', taskId + '|' + result);
const json = JSON.stringify(result);
window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptCompleted', taskId + '|' + json);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Task InvokeJavaScriptMethodWithParametersAndNullsAndComplexResult() =>
});

[Fact]
public Task InvokeJavaScriptMethodWithParametersAndResult() =>
public Task InvokeJavaScriptMethodWithParametersAndDecimalResult() =>
RunTest(async (hybridWebView) =>
{
var x = 123.456m;
Expand All @@ -91,6 +91,153 @@ public Task InvokeJavaScriptMethodWithParametersAndResult() =>
Assert.Equal(777.777m, result);
});

[Theory]
[InlineData(-123.456)]
[InlineData(0.0)]
[InlineData(123.456)]
public Task InvokeJavaScriptMethodWithParametersAndDoubleResult(double expected) =>
RunTest(async (hybridWebView) =>
{
var result = await hybridWebView.InvokeJavaScriptAsync<double>(
"EchoParameter",
HybridWebViewTestContext.Default.Double,
[expected],
[HybridWebViewTestContext.Default.Double]);

Assert.Equal(expected, result);
});

[Theory]
[InlineData(null)]
[InlineData(-123.456)]
[InlineData(0.0)]
[InlineData(123.456)]
public Task InvokeJavaScriptMethodWithParametersAndNullableDoubleResult(double? expected) =>
RunTest(async (hybridWebView) =>
{
var result = await hybridWebView.InvokeJavaScriptAsync<double?>(
"EchoParameter",
HybridWebViewTestContext.Default.NullableDouble,
[expected],
[HybridWebViewTestContext.Default.NullableDouble]);

Assert.Equal(expected, result);
});

[Fact]
public Task InvokeJavaScriptMethodWithParametersAndNewDoubleResult() =>
RunTest(async (hybridWebView) =>
{
var x = 123.456m;
var y = 654.321m;

var result = await hybridWebView.InvokeJavaScriptAsync<double>(
"EvaluateMeWithParamsAndReturn",
HybridWebViewTestContext.Default.Double,
[x, y],
[HybridWebViewTestContext.Default.Decimal, HybridWebViewTestContext.Default.Decimal]);

Assert.Equal(777.777, result);
});

[Theory]
[InlineData(-123)]
[InlineData(0)]
[InlineData(123)]
public Task InvokeJavaScriptMethodWithParametersAndIntResult(int expected) =>
RunTest(async (hybridWebView) =>
{
var result = await hybridWebView.InvokeJavaScriptAsync<int>(
"EchoParameter",
HybridWebViewTestContext.Default.Int32,
[expected],
[HybridWebViewTestContext.Default.Int32]);

Assert.Equal(expected, result);
});

[Theory]
[InlineData(null)]
[InlineData(-123)]
[InlineData(0)]
[InlineData(123)]
public Task InvokeJavaScriptMethodWithParametersAndNullableIntResult(int? expected) =>
RunTest(async (hybridWebView) =>
{
var result = await hybridWebView.InvokeJavaScriptAsync<int?>(
"EchoParameter",
HybridWebViewTestContext.Default.NullableInt32,
[expected],
[HybridWebViewTestContext.Default.NullableInt32]);

Assert.Equal(expected, result);
});

[Fact]
public Task InvokeJavaScriptMethodWithParametersAndNewIntResult() =>
RunTest(async (hybridWebView) =>
{
var x = 123;
var y = 654;

var result = await hybridWebView.InvokeJavaScriptAsync<int>(
"EvaluateMeWithParamsAndReturn",
HybridWebViewTestContext.Default.Int32,
[x, y],
[HybridWebViewTestContext.Default.Int32, HybridWebViewTestContext.Default.Int32]);

Assert.Equal(777, result);
});

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("foo")]
[InlineData("null")]
[InlineData("undefined")]
public Task InvokeJavaScriptMethodWithParametersAndStringResult(string expected) =>
RunTest(async (hybridWebView) =>
{
var result = await hybridWebView.InvokeJavaScriptAsync<string>(
"EchoParameter",
HybridWebViewTestContext.Default.String,
[expected],
[HybridWebViewTestContext.Default.String]);

Assert.Equal(expected, result);
});

[Fact]
public Task InvokeJavaScriptMethodWithParametersAndNewStringResult() =>
RunTest(async (hybridWebView) =>
{
var x = "abc";
var y = "def";

var result = await hybridWebView.InvokeJavaScriptAsync<string>(
"EvaluateMeWithParamsAndStringReturn",
HybridWebViewTestContext.Default.String,
[x, y],
[HybridWebViewTestContext.Default.String, HybridWebViewTestContext.Default.String]);

Assert.Equal("abcdef", result);
});

[Theory]
[InlineData(true)]
[InlineData(false)]
public Task InvokeJavaScriptMethodWithParametersAndBoolResult(bool expected) =>
RunTest(async (hybridWebView) =>
{
var result = await hybridWebView.InvokeJavaScriptAsync<bool>(
"EchoParameter",
HybridWebViewTestContext.Default.Boolean,
[expected],
[HybridWebViewTestContext.Default.Boolean]);

Assert.Equal(expected, result);
});

[Fact]
public Task InvokeJavaScriptMethodWithParametersAndComplexResult() =>
RunTest(async (hybridWebView) =>
Expand Down Expand Up @@ -314,6 +461,11 @@ public class ComputationResult
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(ComputationResult))]
[JsonSerializable(typeof(decimal))]
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(int?))]
[JsonSerializable(typeof(double))]
[JsonSerializable(typeof(double?))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(Dictionary<string, string>))]
internal partial class HybridWebViewTestContext : JsonSerializerContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,23 @@

window.HybridWebView.SendRawMessage('You said: ' + msg)
});

// test method invoke with returning the parameter
function EchoParameter(value) {
return value;
}

// test method invoke with simple parameters and simple return value
// test evaluate javascript to invoke
function EvaluateMeWithParamsAndReturn(s1, s2) {
return s1 + s2;
}

// test method invoke with simple parameters and simple return value
// test evaluate javascript to invoke
function EvaluateMeWithParamsAndStringReturn(s1, s2) {
return `${s1}${s2}`;
}

// test method invoke with parameters, and returning complex type
function AddNumbers(a, b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,8 @@
},

"__TriggerAsyncCallback": function __TriggerAsyncCallback(taskId, result) {
// Make sure the result is a string
if (result && typeof (result) !== 'string') {
result = JSON.stringify(result);
}

window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptCompleted', taskId + '|' + result);
const json = JSON.stringify(result);
window.HybridWebView.__SendMessageInternal('__InvokeJavaScriptCompleted', taskId + '|' + json);
}
}

Expand Down
Loading