Skip to content

Commit c7c6fef

Browse files
Copilotmattleibow
andcommitted
Add demo test methods and final documentation for HybridWebView exception handling
Co-authored-by: mattleibow <1096616+mattleibow@users.noreply.github.com>
1 parent 0cdd88f commit c7c6fef

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/Controls/tests/DeviceTests/Elements/HybridWebView/HybridWebViewTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,31 @@ public Task InvokeDotNetExceptionHandling(string methodName, string expectedExce
417417
Assert.Equal(methodName, invokeJavaScriptTarget.LastMethodCalled);
418418
});
419419

420+
[Fact]
421+
public Task DemonstrateExceptionHandlingFunctionality() =>
422+
RunTest("exceptiondemo.html", async (hybridWebView) =>
423+
{
424+
var invokeJavaScriptTarget = new TestDotNetMethods();
425+
hybridWebView.SetInvokeJavaScriptTarget(invokeJavaScriptTarget);
426+
427+
// Test successful method call
428+
var successResult = await hybridWebView.EvaluateJavaScriptAsync("testSuccessfulCall()");
429+
430+
// Wait a bit for the operation to complete
431+
await Task.Delay(1000);
432+
433+
// Test exception handling - this should NOT throw an exception in C# test code
434+
// because the JavaScript should catch it
435+
var exceptionResult = await hybridWebView.EvaluateJavaScriptAsync("testExceptionCall()");
436+
437+
// Wait a bit for the operation to complete
438+
await Task.Delay(1000);
439+
440+
// Verify both methods were called
441+
// Note: ThrowTestException should be the last method called since it executes after GetSuccessMessage
442+
Assert.Equal("ThrowTestException", invokeJavaScriptTarget.LastMethodCalled);
443+
});
444+
420445
[Fact]
421446
public Task InvokeDotNetExceptionPreservesStackTrace() =>
422447
RunTest("invokedotnetexceptiontests.html", async (hybridWebView) =>
@@ -698,6 +723,19 @@ public void Invoke_ThrowsCustomException()
698723
throw new TestCustomException("Custom exception for testing", 42);
699724
}
700725

726+
// Simple demo methods
727+
public string GetSuccessMessage()
728+
{
729+
UpdateLastMethodCalled();
730+
return "Method called successfully!";
731+
}
732+
733+
public string ThrowTestException(string parameter)
734+
{
735+
UpdateLastMethodCalled();
736+
throw new InvalidOperationException($"This is a test exception with parameter: {parameter}");
737+
}
738+
701739
private void UpdateLastMethodCalled([CallerMemberName] string methodName = null)
702740
{
703741
LastMethodCalled = methodName;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Exception Handling Test</title>
5+
<script src="scripts/HybridWebView.js"></script>
6+
</head>
7+
<body>
8+
<h1>HybridWebView Exception Handling Test</h1>
9+
10+
<button id="testSuccess" onclick="testSuccessfulCall()">Test Successful Call</button>
11+
<button id="testException" onclick="testExceptionCall()">Test Exception Call</button>
12+
13+
<div id="results"></div>
14+
15+
<script>
16+
async function testSuccessfulCall() {
17+
try {
18+
const result = await window.HybridWebView.InvokeDotNet('GetSuccessMessage');
19+
document.getElementById('results').innerHTML = '<p style="color: green;">Success: ' + result + '</p>';
20+
} catch (error) {
21+
document.getElementById('results').innerHTML = '<p style="color: red;">Unexpected error: ' + error.message + '</p>';
22+
}
23+
}
24+
25+
async function testExceptionCall() {
26+
try {
27+
const result = await window.HybridWebView.InvokeDotNet('ThrowTestException', 'test parameter');
28+
document.getElementById('results').innerHTML = '<p style="color: red;">ERROR: Should have thrown exception but got: ' + result + '</p>';
29+
} catch (error) {
30+
document.getElementById('results').innerHTML =
31+
'<div style="color: orange;">' +
32+
'<p><strong>Exception caught successfully!</strong></p>' +
33+
'<p>Type: ' + (error.dotNetType || 'JavaScript Error') + '</p>' +
34+
'<p>Message: ' + error.message + '</p>' +
35+
'<p>Stack trace available: ' + (error.dotNetStackTrace ? 'Yes' : 'No') + '</p>' +
36+
'</div>';
37+
}
38+
}
39+
</script>
40+
41+
<div id="htmlLoaded"></div>
42+
</body>
43+
</html>

0 commit comments

Comments
 (0)