Skip to content

Commit

Permalink
Added a UI test (dotnet#21948)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo authored and PureWeen committed Jul 5, 2024
1 parent e2b239e commit ab1f2c6
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue21948.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21948">
<Button Text="Open a new window"
AutomationId="button"
Clicked="OpenNewWindowClicked" />
</ContentPage>
79 changes: 79 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue21948.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Platform;
using Microsoft.Maui;
using System.Linq;
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;
#if IOS || MACCATALYST
using UIKit;
using CoreGraphics;
#endif

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.ManualTest, 21948, "Crash upon resuming the app 'window was already activated'", PlatformAffected.iOS)]
public partial class Issue21948 : ContentPage
{
public Issue21948()
{
InitializeComponent();
}

public void OpenNewWindowClicked(object obj, EventArgs e)
{
#if IOS || MACCATALYST
OpenNewWindow();
#endif
}

#if IOS || MACCATALYST
async void OpenNewWindow()
{
var uIWindow = new UIWindow();
var keyWindow = (this.Window.Handler.PlatformView as UIWindow);
if (keyWindow?.WindowLevel == UIWindowLevel.Normal)
keyWindow.WindowLevel = -1;

var page = new ContentPage();
this.AddLogicalChild(page);
var handler = page.ToHandler(this.Handler.MauiContext);

uIWindow.RootViewController = new UIViewController();
uIWindow.WindowLevel = UIWindowLevel.Normal;
uIWindow.MakeKeyAndVisible();

// Simulate backgrounding the app
nint taskId = UIApplication.BackgroundTaskInvalid;
taskId = UIApplication.SharedApplication.BeginBackgroundTask(() =>
{
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});

// Simulate background time
await Task.Delay(2000);
UIApplication.SharedApplication.EndBackgroundTask(taskId);

var rvc = uIWindow.RootViewController;

if (rvc != null)
{
await rvc.DismissViewControllerAsync(false);
rvc.Dispose();
}

// Simulate bringing the app back to the foreground
await Task.Delay(1000);

uIWindow.RootViewController = null;
uIWindow.Hidden = true;
keyWindow.WindowLevel = UIWindowLevel.Normal;
this.RemoveLogicalChild(page);
}
#endif
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#if IOS || MACCATALYST
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues
{
public class Issue21948 : _IssuesUITest
{
public Issue21948(TestDevice device) : base(device) { }

public override string Issue => "Crash upon resuming the app 'window was already activated'";

[Test]
public void OpenAlertWithModals()
{
App.WaitForElement("button");
App.Click("button");

// The test passes if no exception is thrown
App.WaitForElement("button");
}
}
}
#endif

0 comments on commit ab1f2c6

Please sign in to comment.