Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix WebView issue in EvaluateJavascriptAsync method using Shell (#14694)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz committed Nov 9, 2021
1 parent 7f870c3 commit 9bab8c9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 13326, "[Bug] [Android] WebView.EvaluateJavascriptAsync thrown NullReferenceException when is offscreen in Shell", PlatformAffected.Android)]
#if UITEST
[NUnit.Framework.Category(UITestCategories.Shell)]
#endif
public class Issue13326 : TestShell
{
const string Test1 = "Tab 1";
const string Test2 = "Tab 2";

protected override void Init()
{
AddBottomTab(CreatePage1(Test1), Test1);
AddBottomTab(CreatePage2(Test2), Test2);

static ContentPage CreatePage1(string title)
{
var layout = new StackLayout();

var instructions = new Label
{
Padding = 12,
BackgroundColor = Color.Black,
TextColor = Color.White,
Text = "Navigate to the second Tab"
};

layout.Children.Add(instructions);

return new ContentPage
{
Title = title,
Content = layout
};
}

static ContentPage CreatePage2(string title)
{
return new Issue13326SecondPage(title);
}
}
}

public class Issue13326SecondPage : ContentPage
{
WebView _webView;

public Issue13326SecondPage(string title)
{
Title = title;

var layout = new StackLayout();

var instructions = new Label
{
Padding = 12,
BackgroundColor = Color.Black,
TextColor = Color.White,
Text = "Navigate back to the first tab and wait some seconds, without crashing the test has passed."
};

_webView = new WebView
{
HeightRequest = 300,
WidthRequest = 300
};

layout.Children.Add(instructions);
layout.Children.Add(_webView);

Content = layout;

Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(5));
await _webView.EvaluateJavaScriptAsync("var a = 10;");
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1806,6 +1806,7 @@
<Compile Include="$(MSBuildThisFileDirectory)FrameBackgroundIssue.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14664.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14192.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue13326.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue14697.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8383.xaml.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue8383-2.xaml.cs" />
Expand Down
5 changes: 4 additions & 1 deletion Xamarin.Forms.Core/WebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ public void Eval(string script)

public async Task<string> EvaluateJavaScriptAsync(string script)
{
if (script == null)
return null;

EvaluateJavaScriptDelegate handler = EvaluateJavaScriptRequested;

if (script == null)
if (handler == null)
return null;

//make all the platforms mimic Android's implementation, which is by far the most complete.
Expand Down

0 comments on commit 9bab8c9

Please sign in to comment.