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

[Android] Fix for WebView Fails to Load URLs with Certain Encoded Characters #27003

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
58 changes: 58 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue26843.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Maui.Controls.Sample.Issues;

[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 26843, "WebView Fails to Load URLs with Certain Encoded Characters on Android", PlatformAffected.Android)]
public partial class Issue26843 : ContentPage
{
private Label navigationResultLabel;
public Issue26843()
{
navigationResultLabel = new Label
{
AutomationId = "NavigationResultLabel",
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
TextColor = Colors.Black,
FontSize = 14
};

var webView = new WebView
{
AutomationId = "WebView",
ZIndex = 1
};

webView.Source = "https://github.com/SuthiYuvaraj/maui/blob/390e4cd1c5eed59ecaf1fcd37975e7f6f5422d6d/src/Controls/tests/TestCases.HostApp/Resources/Raw/01-A%C4%9F-Sistem%20Bilgi%20G%C3%BCvenli%C4%9Fi%20Md/dotnet%20maui.pdf";
webView.Navigated += OnNavigated;

var layout = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) },
new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }
},
Children = {
navigationResultLabel,
webView
}
};

Grid.SetRow(navigationResultLabel, 0);
Grid.SetRow(webView, 1);

Content = layout;
}

private void OnNavigated(object sender, WebNavigatedEventArgs e)
{
if (e.Result == WebNavigationResult.Success)
{
navigationResultLabel.Text = $"Successfully navigated to the encoded URL";
}
else
{
navigationResultLabel.Text = $"Failed to navigate to the encoded URL";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

internal class Issue26843 : _IssuesUITest
{
public Issue26843(TestDevice device) : base(device) { }

public override string Issue => "WebView Fails to Load URLs with Certain Encoded Characters on Android";

[Test]
[Category(UITestCategories.WebView)]
public void WebViewShouldLoadEncodedUrl()
{
var label = App.WaitForElement("NavigationResultLabel");
App.WaitForElement("WebView");
Assert.That(label.GetText(), Is.EqualTo("Successfully navigated to the encoded URL"));
}
}

2 changes: 1 addition & 1 deletion src/Core/src/Platform/Android/MauiWebView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void IWebViewDelegate.LoadUrl(string? url)
_handler.CurrentNavigationEvent = WebNavigationEvent.NewPage;
}

if (url != null && !url.StartsWith('/') && !Uri.IsWellFormedUriString(url, UriKind.Absolute))
if (url != null && !url.StartsWith('/') && !Uri.TryCreate(url, UriKind.Absolute, out _))
jfversluis marked this conversation as resolved.
Show resolved Hide resolved
{
// URLs like "index.html" can't possibly load, so try "file:///android_asset/index.html"
url = AssetBaseUrl + url;
Expand Down
Loading