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 1 commit
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
217 changes: 217 additions & 0 deletions
217
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,217 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| 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 JavaMethodName = "reportResultForIssue12542"; | ||
| const string JavascriptInterfaceAnnotation = "Landroid/webkit/JavascriptInterface;"; | ||
| const string SuccessResult = "bridge-12542:payload-12542:1"; | ||
| const string SuccessMarker = "# JAVASCRIPT_INTERFACE_RESULT "; | ||
| const string FailureMarker = "# JAVASCRIPT_INTERFACE_FAILURE "; | ||
|
|
||
| [Test] | ||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| 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); | ||
| } | ||
| """, | ||
| 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; | ||
|
|
||
| public WebViewJavascriptBridge () | ||
| { | ||
| Identity = "bridge-12542"; | ||
| } | ||
|
|
||
| public string Identity { get; } | ||
|
|
||
| public Task<string> Result => result.Task; | ||
|
|
||
| [JavascriptInterface] | ||
| public override void ReportResultForIssue12542 (string value) | ||
| { | ||
| int count = Interlocked.Increment (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"); | ||
| </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 (); | ||
| Assert.IsTrue (builder.Install (proj), "Project should have installed."); | ||
|
|
||
| var projectIntermediate = Path.Combine (Root, builder.ProjectDirectory, proj.IntermediateOutputPath); | ||
| var generatedJavaFiles = Directory.GetFiles (projectIntermediate, "WebViewJavascriptBridge.java", SearchOption.AllDirectories); | ||
| Assert.IsNotEmpty (generatedJavaFiles, $"Expected a generated WebViewJavascriptBridge.java under '{projectIntermediate}'."); | ||
| foreach (var generatedJavaFile in generatedJavaFiles) { | ||
| var contents = File.ReadAllText (generatedJavaFile).Replace ("\r\n", "\n"); | ||
| StringAssert.Contains ("@android.webkit.JavascriptInterface", contents, | ||
| $"Generated JCW Java '{generatedJavaFile}' should forward [JavascriptInterface]."); | ||
| StringAssert.Contains ($"public void {JavaMethodName} (java.lang.String p0)", contents, | ||
| $"Generated JCW Java '{generatedJavaFile}' should expose the bridge method."); | ||
| } | ||
|
|
||
| var generatedJava = File.ReadAllText (generatedJavaFiles [0]).Replace ("\r\n", "\n"); | ||
| var packageDeclaration = generatedJava.Split ('\n').Single (line => line.StartsWith ("package ", StringComparison.Ordinal)); | ||
| var javaPackage = packageDeclaration.Substring ("package ".Length).TrimEnd (';'); | ||
| var className = $"L{javaPackage.Replace ('.', '/')}/WebViewJavascriptBridge;"; | ||
| var dexFile = builder.Output.GetIntermediaryPath (Path.Combine ("android", "bin", "classes.dex")); | ||
| FileAssert.Exists (dexFile); | ||
| Assert.IsTrue (DexUtils.ContainsClassWithMethod (className, JavaMethodName, "(Ljava/lang/String;)V", dexFile, AndroidSdkPath), | ||
| $"`{dexFile}` should contain `{className}.{JavaMethodName}`."); | ||
| Assert.IsTrue (DexUtils.ContainsRuntimeMethodAnnotation (JavaMethodName, JavascriptInterfaceAnnotation, dexFile, AndroidSdkPath), | ||
| $"`{dexFile}` should retain `{JavascriptInterfaceAnnotation}` on `{JavaMethodName}`."); | ||
|
|
||
| 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, "Local JavaScript should invoke the exact managed bridge instance once."); | ||
| StringAssert.DoesNotContain (FailureMarker, logcat, "WebView reported a JavaScript interface failure."); | ||
| Assert.IsTrue (builder.Uninstall (proj), "Project should have uninstalled."); | ||
|
simonrozsival marked this conversation as resolved.
Resolved
|
||
| } | ||
| } | ||
| } | ||
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.