Skip to content

Commit

Permalink
[tests] misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Git History Editor authored and rolfbjarne committed Jun 5, 2023
1 parent 12f3926 commit 445d25f
Show file tree
Hide file tree
Showing 11 changed files with 266 additions and 86 deletions.
12 changes: 12 additions & 0 deletions tests/common/TestRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ public static bool CheckXcodeVersion (int major, int minor, int build = 0)
return CheckMacSystemVersion (12, 1);
#else
throw new NotImplementedException ();
#endif
case 3:
#if __WATCHOS__
return CheckWatchOSSystemVersion (8, 5);
#elif __TVOS__
return ChecktvOSSystemVersion (15, 4);
#elif __IOS__
return CheckiOSSystemVersion (15, 4);
#elif MONOMAC
return CheckMacSystemVersion (12, 3);
#else
throw new NotImplementedException ();
#endif
default:
throw new NotImplementedException ();
Expand Down
16 changes: 14 additions & 2 deletions tests/common/mac/TestRuntime.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using AppKit;
using Foundation;

#nullable enable

partial class TestRuntime {
public static bool RunAsync (TimeSpan timeout, Action action, Func<bool> check_completed, NSImage imageToShow = null)
{
Expand All @@ -11,12 +13,22 @@ public static bool RunAsync (TimeSpan timeout, Action action, Func<bool> check_c

public static bool RunAsync (DateTime timeout, Action action, Func<bool> check_completed, NSImage imageToShow = null)
{
NSTimer.CreateScheduledTimer (0.01, (v) => action ());
Exception? ex = null;
NSTimer.CreateScheduledTimer (0.01, (v) => {
try {
action ();
} catch (Exception e) {
ex = e;
}
});
do {
if (timeout < DateTime.Now)
return false;
NSRunLoop.Main.RunUntil (NSDate.Now.AddSeconds (0.1));
} while (!check_completed ());
} while (ex is null && !check_completed ());

if (ex is not null)
throw new Exception ($"Async callback failed: {ex.Message}", ex);

return true;
}
Expand Down
83 changes: 63 additions & 20 deletions tests/monotouch-test/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;

using CoreFoundation;
using Foundation;
using UIKit;
using MonoTouch.NUnit.UI;
Expand Down Expand Up @@ -44,44 +47,85 @@ public static void PresentModalViewController (UIViewController vc, double durat
}
}

public static bool RunAsync (DateTime timeout, Task startTask, Task completionTask, UIImage imageToShow = null)
{
return TestRuntime.RunAsync (timeout, startTask, completionTask, imageToShow);
}

public static bool RunAsync (DateTime timeout, Func<Task> startTask, Func<bool> completionTask, UIImage imageToShow = null)
{
return TestRuntime.RunAsync (timeout, startTask, completionTask, imageToShow);
}

public static bool RunAsync (DateTime timeout, Func<Task> startTask, Task completionTask, UIImage imageToShow = null)
{
return TestRuntime.RunAsync (timeout, startTask, completionTask, imageToShow);
}

public static bool RunAsync (DateTime timeout, Action action, Func<bool> check_completed, UIImage imageToShow = null)
{
var vc = new AsyncController (action, imageToShow);
var bckp = window.RootViewController;
var navigation = bckp as UINavigationController;
return TestRuntime.RunAsync (timeout, action, check_completed, imageToShow);
}

public static IDisposable ShowAsyncUI (UIImage? imageToShow = null)
{
var state = new AsyncState (window);
state.Show (imageToShow);
return state;
}
}

class AsyncState : IDisposable {
UIWindow window;
UIViewController? initialRootViewController;
UINavigationController? navigation;

public AsyncState (UIWindow window)
{
this.window = window;
}

public void Show (UIImage imageToShow)
{
var vc = new AsyncController (imageToShow);
initialRootViewController = window.RootViewController;
navigation = window.RootViewController as UINavigationController;

// Pushing something to a navigation controller doesn't seem to work on phones
if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Phone)
navigation = null;

if (navigation is not null) {
navigation.PushViewController (vc, false);
} else {
window.RootViewController = vc;
}
}

try {
do {
if (timeout < DateTime.Now)
return false;
NSRunLoop.Main.RunUntil (NSDate.Now.AddSeconds (0.1));
} while (!check_completed ());
} finally {
if (navigation is not null) {
navigation.PopViewController (false);
} else {
window.RootViewController = bckp;
}
public void Hide ()
{
if (initialRootViewController is null)
return;
if (navigation is not null) {
navigation.PopViewController (false);
} else {
window.RootViewController = initialRootViewController;
}
initialRootViewController = null; // set to null so if we're called again we know to do nothing.
}

return true;
public void Dispose ()
{
Hide ();
}
}

class AsyncController : UIViewController {
Action action;
UIImage imageToShow;
static int counter;

public AsyncController (Action action, UIImage imageToShow = null)
public AsyncController (UIImage imageToShow = null)
{
this.action = action;
this.imageToShow = imageToShow;
counter++;
}
Expand All @@ -104,7 +148,6 @@ public override void ViewDidLoad ()
imgView.ContentMode = UIViewContentMode.Center;
View.AddSubview (imgView);
}
NSTimer.CreateScheduledTimer (0.01, (v) => action ());
}
}

Expand Down
25 changes: 8 additions & 17 deletions tests/monotouch-test/Foundation/UrlProtocolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,19 @@ public void RegistrarTest ()
// Networking seems broken on our macOS 10.9 bot, so skip this test.
TestRuntime.AssertSystemVersion (ApplePlatform.MacOSX, 10, 10, throwIfOtherPlatform: false);

Exception ex = null;
var done = new ManualResetEvent (false);
var success = false;

Task.Run (async () => {
try {
var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
config.WeakProtocolClasses = NSArray.FromNSObjects (new Class (typeof (CustomUrlProtocol)));
var session = NSUrlSession.FromConfiguration (config);
var custom_url = new NSUrl ("foo://server");
using (var task = await session.CreateDownloadTaskAsync (custom_url)) {
success = true;
}
} catch (Exception e) {
ex = e;
} finally {
done.Set ();
var task = Task.Run (async () => {
var config = NSUrlSessionConfiguration.DefaultSessionConfiguration;
config.WeakProtocolClasses = NSArray.FromNSObjects (new Class (typeof (CustomUrlProtocol)));
var session = NSUrlSession.FromConfiguration (config);
var custom_url = new NSUrl ("foo://server");
using (var task = await session.CreateDownloadTaskAsync (custom_url)) {
success = true;
}
});

Assert.IsTrue (TestRuntime.RunAsync (DateTime.Now.AddSeconds (10), () => { }, () => done.WaitOne (0)), "Timed out");
Assert.IsNull (ex, "Exception");
Assert.IsTrue (TestRuntime.RunAsync (DateTime.Now.AddSeconds (10), task), "Timed out");
Assert.That (CustomUrlProtocol.State, Is.EqualTo (5), "State");
Assert.IsTrue (success, "Success");
}
Expand Down
2 changes: 2 additions & 0 deletions tests/monotouch-test/HealthKit/HKWorkoutBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public void GetSeriesBuilderNullReturnTest ()
{
#if MONOMAC
TestRuntime.AssertXcodeVersion (14, 0);
#else
TestRuntime.AssertXcodeVersion (11, 0);
#endif

var store = new HKHealthStore ();
Expand Down
111 changes: 99 additions & 12 deletions tests/monotouch-test/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,118 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Foundation;
using UIKit;
using ObjCRuntime;
using System.Runtime.InteropServices;

#nullable enable

namespace Foundation {
public static class NSRunLoop_Extensions {
// Returns true if task completed before the timeout,
// otherwise returns false
public static bool RunUntil (this NSRunLoop self, Task task, DateTime timeout)
{
while (true) {
if (task.IsCompleted)
return true;
if (timeout <= DateTime.Now)
return false;
self.RunUntil (NSDate.Now.AddSeconds (0.1));
}
}
}
}

partial class TestRuntime {
public static bool RunAsync (TimeSpan timeout, Task task, UIImage imageToShow = null)
{
return RunAsync (DateTime.Now.Add (timeout), task, imageToShow);
}

public static bool RunAsync (DateTime timeout, Task task, UIImage imageToShow = null)
{
return RunAsync (timeout, task, Task.CompletedTask, imageToShow);
}

public static bool RunAsync (TimeSpan timeout, Task startTask, Task completionTask, UIImage imageToShow = null)
{
return RunAsync (DateTime.Now.Add (timeout), startTask, completionTask, imageToShow);
}

public static bool RunAsync (DateTime timeout, Task startTask, Task completionTask, UIImage imageToShow = null)
{
#if !__WATCHOS__
using var ui = AppDelegate.ShowAsyncUI (imageToShow);
#endif

var runLoop = NSRunLoop.Main;
if (!runLoop.RunUntil (startTask, timeout))
return false;
startTask.GetAwaiter ().GetResult (); // Trigger any captured exceptions.

if (!runLoop.RunUntil (completionTask, timeout))
return false;
completionTask.GetAwaiter ().GetResult (); // Trigger any captured exceptions.

return true;
}

public static bool RunAsync (TimeSpan timeout, Task startTask, Func<bool> check_completed, UIImage imageToShow = null)
{
return RunAsync (DateTime.Now.Add (timeout), startTask, check_completed, imageToShow);
}

public static bool RunAsync (DateTime timeout, Task startTask, Func<bool> check_completed, UIImage imageToShow = null)
{
var completionTaskSource = new TaskCompletionSource<bool> ();

var checkCompletionTimer = NSTimer.CreateRepeatingScheduledTimer (0.1, (NSTimer timer) => {
if (check_completed ()) {
completionTaskSource.SetResult (true);
timer.Invalidate ();
}
});

try {
return RunAsync (timeout, startTask, completionTaskSource.Task, imageToShow);
} finally {
checkCompletionTimer.Invalidate ();
}
}

public static bool RunAsync (TimeSpan timeout, Func<Task> startTask, Func<bool> check_completed, UIImage imageToShow = null)
{
return RunAsync (DateTime.Now.Add (timeout), startTask, check_completed, imageToShow);
}

public static bool RunAsync (DateTime timeout, Func<Task> startTask, Func<bool> check_completed, UIImage imageToShow = null)
{
return RunAsync (timeout, startTask (), check_completed, imageToShow);
}

public static bool RunAsync (TimeSpan timeout, Func<Task> startTask, Task completionTask, UIImage imageToShow = null)
{
return RunAsync (DateTime.Now.Add (timeout), startTask, completionTask, imageToShow);
}

public static bool RunAsync (DateTime timeout, Func<Task> startTask, Task completionTask, UIImage imageToShow = null)
{
return RunAsync (timeout, startTask (), completionTask, imageToShow);
}

public static bool RunAsync (TimeSpan timeout, Action action, Func<bool> check_completed, UIImage imageToShow = null)
{
return RunAsync (DateTime.Now.Add (timeout), action, check_completed, imageToShow);
}

public static bool RunAsync (DateTime timeout, Action action, Func<bool> check_completed, UIImage imageToShow = null)
{
#if __WATCHOS__
NSTimer.CreateScheduledTimer (0.01, (v) => action ());
do {
if (timeout < DateTime.Now)
return false;
NSRunLoop.Main.RunUntil (NSDate.Now.AddSeconds (0.1));
} while (!check_completed ());

return true;
#else
return AppDelegate.RunAsync (timeout, action, check_completed, imageToShow);
#endif
var startTask = new Task (action);
return RunAsync (timeout, startTask, check_completed, imageToShow);
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/monotouch-test/Network/NWPathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ public void EnumerateGatewayNullCallbackTest ()
[Test]
public void EnumerateGatewayTest ()
{
TestRuntime.AssertXcodeVersion (11, 0);

var e1 = new ManualResetEvent (false);
var e2 = new ManualResetEvent (false);
var monitor = new NWPathMonitor ();
Expand Down
2 changes: 1 addition & 1 deletion tests/monotouch-test/Network/NWResolutionReportTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Init ()
connectedEvent = new AutoResetEvent (false);
reportEvent = new AutoResetEvent (false);
resolutionEvent = new AutoResetEvent (false);
host = NetworkResources.MicrosoftUri.Host;
host = NetworkResources.Httpbin.Uri.Host;
// we create a connection which we are going to use to get the availabe
// interfaces, that way we can later test protperties of the NWParameters class.
using (var parameters = NWParameters.CreateUdp ())
Expand Down
Loading

0 comments on commit 445d25f

Please sign in to comment.