xamarin-macios/tests/monotouch-test/AppDelegate.cs

152 строки
4.2 KiB
C#
Исходник Обычный вид История

#if !__WATCHOS__ && !MONOMAC
2016-05-26 16:06:52 +03:00
using System;
using System.Collections.Generic;
using System.Reflection;
using Foundation;
using UIKit;
using MonoTouch.NUnit.UI;
using NUnit.Framework.Internal;
using MonoTouchFixtures.BackgroundTasks;
2016-05-26 16:06:52 +03:00
namespace MonoTouchFixtures {
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to
// application events from iOS.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate {
// class-level declarations
static UIWindow window;
TouchRunner runner;
#if __IOS__ && !__MACCATALYST__
2016-05-26 16:06:52 +03:00
public override bool AccessibilityPerformMagicTap ()
{
try {
runner.OpenWriter ("Magic Tap");
runner.Run (runner.LoadedTest as TestSuite);
}
finally {
runner.CloseWriter ();
}
return true;
}
#endif
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
#if __MACCATALYST__
// Debug spew to track down https://github.com/xamarin/maccore/issues/2414
Console.WriteLine ("AppDelegate.FinishedLaunching");
#endif
2016-05-26 16:06:52 +03:00
// create a new window instance based on the screen size
window = new UIWindow (UIScreen.MainScreen.Bounds);
runner = new TouchRunner (window);
// tests can be inside the main assembly
runner.Add (Assembly.GetExecutingAssembly ());
runner.Add (typeof(EmbeddedResources.ResourcesTest).Assembly);
runner.Add (typeof(Xamarin.BindingTests.ProtocolTest).Assembly);
window.RootViewController = new UINavigationController (runner.GetViewController ());
// make the window visible
window.MakeKeyAndVisible ();
// required for the background tasks tests, we can only register the tasks in this method
BGTaskSchedulerTest.RegisterTestTasks ();
2016-05-26 16:06:52 +03:00
return true;
}
static void Main (string[] args)
{
// Make sure we have at least one reference to the bindings project so that mcs doesn't strip the reference to it.
GC.KeepAlive (typeof(Bindings.Test.UltimateMachine));
UIApplication.Main (args, null, typeof (AppDelegate));
}
2016-05-26 16:06:52 +03:00
public static void PresentModalViewController (UIViewController vc, double duration)
{
var bckp = window.RootViewController;
window.RootViewController = vc;
try {
NSRunLoop.Main.RunUntil (NSDate.Now.AddSeconds (duration));
} finally {
window.RootViewController = bckp;
}
}
public static bool RunAsync (DateTime timeout, Action action, Func<bool> check_completed, UIImage imageToShow = null)
2016-05-26 16:06:52 +03:00
{
var vc = new AsyncController (action, imageToShow);
2016-05-26 16:06:52 +03:00
var bckp = window.RootViewController;
var navigation = bckp as UINavigationController;
if (navigation != null) {
navigation.PushViewController (vc, false);
} else {
window.RootViewController = vc;
}
2016-05-26 16:06:52 +03:00
try {
do {
if (timeout < DateTime.Now)
return false;
NSRunLoop.Main.RunUntil (NSDate.Now.AddSeconds (0.1));
} while (!check_completed ());
} finally {
if (navigation != null) {
navigation.PopViewController (false);
} else {
window.RootViewController = bckp;
}
2016-05-26 16:06:52 +03:00
}
return true;
}
}
class AsyncController : UIViewController {
Action action;
UIImage imageToShow;
2016-05-26 16:06:52 +03:00
static int counter;
public AsyncController (Action action, UIImage imageToShow = null)
2016-05-26 16:06:52 +03:00
{
this.action = action;
this.imageToShow = imageToShow;
2016-05-26 16:06:52 +03:00
counter++;
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
switch (counter % 2) {
case 0:
View.BackgroundColor = UIColor.Yellow;
break;
default:
View.BackgroundColor = UIColor.LightGray;
break;
}
if (imageToShow != null) {
var imgView = new UIImageView (View.Bounds);
imgView.Image = imageToShow;
imgView.ContentMode = UIViewContentMode.Center;
View.AddSubview (imgView);
}
2016-05-26 16:06:52 +03:00
NSTimer.CreateScheduledTimer (0.01, (v) => action ());
}
}
}
#endif // !__WATCHOS__