Repository navigation
[tests] Cover WebView JavaScript annotations #12608
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
Merged
simonrozsival
merged 7 commits into
main
from
simonrozsival-nativeaot-webview-annotation
Sep 3, 2026
+203
−0
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
632e75d
[tests] Cover WebView JavaScript annotations
simonrozsival 49b5abe
Address WebView annotation review feedback
simonrozsival f52c8d0
Fix DEX annotation test parsing
simonrozsival 246fb46
Harden DEX annotation parsing
simonrozsival 1b2f44c
Ensure WebView test device cleanup
simonrozsival e020c65
Simplify WebView annotation coverage
simonrozsival 85ecccd
Use parameterized WebView annotation test
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
tests/MSBuildDeviceIntegration/Tests/WebViewJavascriptInterfaceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Text; | ||
| using NUnit.Framework; | ||
| using Xamarin.Android.Tasks; | ||
| using Xamarin.ProjectTools; | ||
|
|
||
| namespace Xamarin.Android.Build.Tests | ||
| { | ||
| [TestFixture] | ||
| [Category ("UsesDevice")] | ||
| public class WebViewJavascriptInterfaceTests : DeviceTest | ||
| { | ||
| const string SuccessResult = "bridge-12542:payload-12542:1"; | ||
| const string SuccessMarker = "# JAVASCRIPT_INTERFACE_RESULT "; | ||
| const string FailureMarker = "# JAVASCRIPT_INTERFACE_FAILURE "; | ||
|
|
||
| [Test] | ||
| public void LocalJavascriptInvokesManagedBridge_LlvmIrCoreClr () | ||
| { | ||
| LocalJavascriptInvokesManagedBridge ("llvm-ir", AndroidRuntime.CoreCLR); | ||
| } | ||
|
|
||
| [Test] | ||
| public void LocalJavascriptInvokesManagedBridge_TrimmableCoreClr () | ||
| { | ||
| LocalJavascriptInvokesManagedBridge ("trimmable", AndroidRuntime.CoreCLR); | ||
| } | ||
|
|
||
| [Test] | ||
| public void LocalJavascriptInvokesManagedBridge_TrimmableNativeAot () | ||
| { | ||
| LocalJavascriptInvokesManagedBridge ("trimmable", AndroidRuntime.NativeAOT); | ||
| } | ||
|
|
||
| void LocalJavascriptInvokesManagedBridge (string typemapImplementation, AndroidRuntime runtime) | ||
| { | ||
| const bool isRelease = true; | ||
| if (IgnoreUnsupportedConfiguration (runtime, release: isRelease)) { | ||
| return; | ||
| } | ||
|
|
||
| var packageSuffix = $"javascriptinterface{typemapImplementation.Replace ("-", "")}"; | ||
| var proj = new XamarinAndroidApplicationProject (packageName: PackageUtils.MakePackageName (runtime, packageSuffix)) { | ||
| IsRelease = isRelease, | ||
| EnableDefaultItems = true, | ||
| OtherBuildItems = { | ||
| new AndroidItem.AndroidJavaSource ("WebViewJavascriptBridgeBase.java") { | ||
| Encoding = new UTF8Encoding (encoderShouldEmitUTF8Identifier: false), | ||
| TextContent = () => """ | ||
| package example; | ||
|
|
||
| public abstract class WebViewJavascriptBridgeBase { | ||
| public WebViewJavascriptBridgeBase () { | ||
| } | ||
|
|
||
| public abstract void reportResultForIssue12542 (String value); | ||
| public abstract void completeTestForIssue12542 (); | ||
| } | ||
| """, | ||
| Metadata = { | ||
| { "Bind", "True" }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| proj.SetRuntime (runtime); | ||
| proj.SetRuntimeIdentifiers (new [] { DeviceAbi }); | ||
| proj.SetProperty ("AndroidTypeMapImplementation", typemapImplementation); | ||
| proj.SetDefaultTargetDevice (); | ||
| proj.Sources.Add (new BuildItem.Source ("WebViewJavascriptBridge.cs") { | ||
| TextContent = () => """ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Android.Webkit; | ||
|
|
||
| namespace UnnamedProject | ||
| { | ||
| public sealed class WebViewJavascriptBridge : Example.WebViewJavascriptBridgeBase | ||
| { | ||
| readonly TaskCompletionSource<string> result = new TaskCompletionSource<string> (TaskCreationOptions.RunContinuationsAsynchronously); | ||
| int invocationCount; | ||
| string resultValue = "missing"; | ||
|
|
||
| public WebViewJavascriptBridge () | ||
| { | ||
| Identity = "bridge-12542"; | ||
| } | ||
|
|
||
| public string Identity { get; } | ||
|
|
||
| public Task<string> Result => result.Task; | ||
|
|
||
| [JavascriptInterface] | ||
| public override void ReportResultForIssue12542 (string value) | ||
| { | ||
| Interlocked.Exchange (ref resultValue, value); | ||
| Interlocked.Increment (ref invocationCount); | ||
| } | ||
|
|
||
| [JavascriptInterface] | ||
| public override void CompleteTestForIssue12542 () | ||
| { | ||
| // JavaScript calls this after the synchronous report call, so the count includes duplicate dispatches. | ||
| string value = Volatile.Read (ref resultValue); | ||
| int count = Volatile.Read (ref invocationCount); | ||
| result.TrySetResult ($"{Identity}:{value}:{count}"); | ||
| } | ||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| """, | ||
| }); | ||
| proj.MainActivity = """" | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Android.App; | ||
| using Android.OS; | ||
| using Android.Webkit; | ||
|
|
||
| namespace UnnamedProject | ||
| { | ||
| [Activity (Label = "UnnamedProject", MainLauncher = true, Icon = "@drawable/icon")] | ||
| public class MainActivity : Activity | ||
| { | ||
| protected override void OnCreate (Bundle bundle) | ||
| { | ||
| base.OnCreate (bundle); | ||
| RunJavascriptInterfaceTest (); | ||
| } | ||
|
|
||
| async void RunJavascriptInterfaceTest () | ||
| { | ||
| const string interfaceName = "managedBridge"; | ||
| const string expectedResult = "bridge-12542:payload-12542:1"; | ||
| const string successMarker = "# JAVASCRIPT_INTERFACE_RESULT "; | ||
| const string failureMarker = "# JAVASCRIPT_INTERFACE_FAILURE "; | ||
| const string html = """ | ||
| <!doctype html> | ||
| <html> | ||
| <body> | ||
| <script> | ||
| managedBridge.reportResultForIssue12542("payload-12542"); | ||
| managedBridge.completeTestForIssue12542(); | ||
| </script> | ||
| </body> | ||
| </html> | ||
| """; | ||
| var bridge = new WebViewJavascriptBridge (); | ||
| var webView = new WebView (this); | ||
| string completion = failureMarker + "unknown"; | ||
| try { | ||
| if (Looper.MyLooper () != Looper.MainLooper) { | ||
| throw new InvalidOperationException ("WebView was not created on the UI thread."); | ||
| } | ||
| webView.Settings.JavaScriptEnabled = true; | ||
| webView.AddJavascriptInterface (bridge, interfaceName); | ||
| SetContentView (webView); | ||
| webView.LoadDataWithBaseURL ("https://localhost/", html, "text/html", "UTF-8", null); | ||
|
|
||
| using var timeout = new CancellationTokenSource (TimeSpan.FromSeconds (15)); | ||
| string result = await bridge.Result.WaitAsync (timeout.Token); | ||
| completion = result == expectedResult | ||
| ? successMarker + result | ||
| : failureMarker + "unexpected-result:" + result; | ||
| } catch (System.OperationCanceledException) { | ||
| completion = failureMarker + "timeout"; | ||
| } catch (Exception ex) { | ||
| completion = failureMarker + ex.GetType ().FullName + ":" + ex.Message; | ||
| } finally { | ||
| RunOnUiThread (() => { | ||
| webView.RemoveJavascriptInterface (interfaceName); | ||
| webView.StopLoading (); | ||
| webView.Destroy (); | ||
| webView.Dispose (); | ||
| bridge.Dispose (); | ||
| Console.WriteLine (completion); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| """"; | ||
|
|
||
| using var builder = CreateApkBuilder (); | ||
| bool installed = false; | ||
| try { | ||
| installed = builder.Install (proj); | ||
| Assert.IsTrue (installed, "Project should have installed."); | ||
|
|
||
| ClearAdbLogcat (); | ||
| RunProjectAndAssert (proj, builder, doNotCleanupOnUpdate: true); | ||
| Assert.IsTrue (WaitForActivityToStart (proj.PackageName, "MainActivity", | ||
| Path.Combine (Root, builder.ProjectDirectory, "activity-logcat.log"), ActivityStartTimeoutInSeconds), "Activity should have started."); | ||
|
|
||
| var resultLog = Path.Combine (Root, builder.ProjectDirectory, "javascript-interface-logcat.log"); | ||
| Assert.IsTrue (MonitorAdbLogcat ( | ||
| line => line.Contains (SuccessMarker, StringComparison.Ordinal) || line.Contains (FailureMarker, StringComparison.Ordinal), | ||
| resultLog, | ||
| timeout: 30), "WebView did not report a JavaScript interface result."); | ||
| var logcat = File.ReadAllText (resultLog); | ||
| StringAssert.Contains (SuccessMarker + SuccessResult, logcat, "The JavaScript report callback should invoke the exact managed bridge instance once."); | ||
| StringAssert.DoesNotContain (FailureMarker, logcat, "WebView reported a JavaScript interface failure."); | ||
| } finally { | ||
| if (installed) { | ||
| try { | ||
| builder.ThrowOnBuildFailure = false; | ||
| if (!builder.Uninstall (proj)) { | ||
| TestContext.Error.WriteLine ($"Failed to uninstall '{proj.PackageName}' during test cleanup."); | ||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| } | ||
| } catch (Exception ex) { | ||
| TestContext.Error.WriteLine ($"Failed to uninstall '{proj.PackageName}' during test cleanup: {ex}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.