2016-05-26 16:06:52 +03:00
|
|
|
//
|
|
|
|
// UIGestureRecognizer Unit Tests
|
|
|
|
//
|
|
|
|
// Authors:
|
|
|
|
// Sebastien Pouliot <sebastien@xamarin.com>
|
|
|
|
//
|
|
|
|
// Copyright 2013 Xamarin Inc.
|
|
|
|
//
|
|
|
|
|
2017-10-31 19:09:21 +03:00
|
|
|
#if !__WATCHOS__ && !MONOMAC
|
2016-05-26 16:06:52 +03:00
|
|
|
|
|
|
|
using System;
|
2018-10-18 13:31:39 +03:00
|
|
|
using System.Collections.Generic;
|
2016-05-26 16:06:52 +03:00
|
|
|
#if XAMCORE_2_0
|
|
|
|
using Foundation;
|
|
|
|
using UIKit;
|
|
|
|
#else
|
|
|
|
using MonoTouch.Foundation;
|
|
|
|
using MonoTouch.UIKit;
|
|
|
|
#endif
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
namespace MonoTouchFixtures.UIKit {
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
// we want the test to be availble if we use the linker
|
|
|
|
[Preserve (AllMembers = true)]
|
|
|
|
public class GestureRecognizerTest {
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void Null ()
|
|
|
|
{
|
|
|
|
using (var gr = new UIGestureRecognizer (Null)) {
|
|
|
|
// ensure documented null-friendly methods actually are before releasing them in the wild
|
|
|
|
gr.LocationInView (null);
|
|
|
|
// can't call LocationOfTouch, 0 is not valid if there's no touch event
|
|
|
|
// gr.LocationOfTouch (0, null);
|
|
|
|
gr.RemoveTarget (null, null);
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 13:31:39 +03:00
|
|
|
|
|
|
|
[Test]
|
2019-01-28 18:07:06 +03:00
|
|
|
[Ignore ("Issue: https://github.com/xamarin/maccore/issues/1345 && WIP PR: https://github.com/xamarin/xamarin-macios/pull/5462")]
|
2018-10-18 13:31:39 +03:00
|
|
|
public void NoStrongCycles ()
|
|
|
|
{
|
|
|
|
bool finalizedAnyCtor = false;
|
|
|
|
bool finalizedAnyAddTarget1 = false;
|
|
|
|
bool finalizedAnyAddTarget2 = false;
|
|
|
|
|
|
|
|
// Add the gesture recognizers to a list so that they're not collected until after the test
|
|
|
|
// This is to avoid false positives (the callback should be collectible already after disposing the gesture recognizer).
|
|
|
|
var list = new List<UIGestureRecognizer> ();
|
|
|
|
|
|
|
|
var pool = new NSAutoreleasePool ();
|
|
|
|
for (var k = 0; k < 10; k++) {
|
|
|
|
{
|
|
|
|
var notifier = new FinalizerNotifier (() => finalizedAnyCtor = true);
|
|
|
|
using (var gr = new UIGestureRecognizer (() => {
|
|
|
|
GC.KeepAlive (notifier); // Make sure the 'notifier' instance is only collected if the delegate to UIGestureRecognizer is collectable.
|
|
|
|
})) {
|
|
|
|
list.Add (gr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var notifier = new FinalizerNotifier (() => finalizedAnyAddTarget1 = true);
|
|
|
|
using (var gr = new UIGestureRecognizer ()) {
|
|
|
|
gr.AddTarget (() => { GC.KeepAlive (notifier); });
|
|
|
|
list.Add (gr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
var notifier = new FinalizerNotifier (() => finalizedAnyAddTarget2 = true);
|
|
|
|
using (var gr = new UIGestureRecognizer ()) {
|
|
|
|
gr.AddTarget ((obj) => { GC.KeepAlive (notifier); });
|
|
|
|
list.Add (gr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pool.Dispose ();
|
|
|
|
|
|
|
|
TestRuntime.RunAsync (DateTime.Now.AddSeconds (1), () => { GC.Collect (); }, () => finalizedAnyCtor && finalizedAnyAddTarget1 && finalizedAnyAddTarget2);
|
|
|
|
Assert.IsTrue (finalizedAnyCtor, "Any finalized");
|
|
|
|
Assert.IsTrue (finalizedAnyAddTarget1, "AddTarget1 finalized");
|
|
|
|
Assert.IsTrue (finalizedAnyAddTarget2, "AddTarget2 finalized");
|
|
|
|
|
|
|
|
GC.KeepAlive (list);
|
|
|
|
}
|
|
|
|
|
|
|
|
class FinalizerNotifier
|
|
|
|
{
|
|
|
|
public Action Action;
|
|
|
|
public FinalizerNotifier (Action action)
|
|
|
|
{
|
|
|
|
Action = action;
|
|
|
|
}
|
|
|
|
~FinalizerNotifier ()
|
|
|
|
{
|
|
|
|
if (Action != null)
|
|
|
|
Action ();
|
|
|
|
}
|
|
|
|
}
|
2016-05-26 16:06:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !__WATCHOS__
|