summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests/BehaviorTest.cs')
-rw-r--r--Xamarin.Forms.Core.UnitTests/BehaviorTest.cs110
1 files changed, 110 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs b/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
new file mode 100644
index 00000000..78e0cfb0
--- /dev/null
+++ b/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
@@ -0,0 +1,110 @@
+using System;
+using NUnit.Framework;
+
+namespace Xamarin.Forms.Core.UnitTests
+{
+ internal class MockBehavior<T> : Behavior<T> where T:BindableObject
+ {
+ public bool attached;
+ public bool detached;
+
+ protected override void OnAttachedTo (BindableObject bindable)
+ {
+ base.OnAttachedTo (bindable);
+ attached = true;
+ AssociatedObject = bindable;
+ }
+
+ protected override void OnDetachingFrom (BindableObject bindable)
+ {
+ detached = true;
+ base.OnDetachingFrom (bindable);
+ AssociatedObject = null;
+ }
+
+ public BindableObject AssociatedObject {get;set;}
+ }
+
+ [TestFixture]
+ public class BehaviorTest : BaseTestFixture
+ {
+ [Test]
+ public void AttachAndDetach ()
+ {
+ var behavior = new MockBehavior<MockBindable> ();
+ var bindable = new MockBindable ();
+
+ Assert.False (behavior.attached);
+ Assert.False (behavior.detached);
+ Assert.Null (behavior.AssociatedObject);
+
+ ((IAttachedObject)behavior).AttachTo (bindable);
+
+ Assert.True (behavior.attached);
+ Assert.False (behavior.detached);
+ Assert.AreSame (bindable, behavior.AssociatedObject);
+
+ ((IAttachedObject)behavior).DetachFrom (bindable);
+
+ Assert.True (behavior.attached);
+ Assert.True (behavior.detached);
+ Assert.Null (behavior.AssociatedObject);
+ }
+
+ [Test]
+ public void AttachToTypeCompatibleWithTargetType ()
+ {
+ var behavior = new MockBehavior<MockBindable> ();
+ var bindable = new View ();
+
+ Assert.Throws<InvalidOperationException> (() => ((IAttachedObject)behavior).AttachTo (bindable));
+ }
+
+ [Test]
+ public void BehaviorsInCollectionAreAttachedWhenCollectionIsAttached ()
+ {
+ var behavior = new MockBehavior<MockBindable> ();
+ var collection = new AttachedCollection<Behavior> ();
+ var bindable = new MockBindable ();
+ collection.Add (behavior);
+ Assert.Null (behavior.AssociatedObject);
+
+ ((IAttachedObject)collection).AttachTo (bindable);
+ Assert.AreSame (bindable, behavior.AssociatedObject);
+
+ ((IAttachedObject)collection).DetachFrom (bindable);
+ Assert.Null (behavior.AssociatedObject);
+ }
+
+ [Test]
+ public void BehaviorsAddedToAttachedCollectionAreAttached ()
+ {
+ var behavior = new MockBehavior<MockBindable> ();
+ var collection = new AttachedCollection<Behavior> ();
+ var bindable = new MockBindable ();
+ ((IAttachedObject)collection).AttachTo (bindable);
+ Assert.Null (behavior.AssociatedObject);
+
+ collection.Add (behavior);
+ Assert.AreSame (bindable, behavior.AssociatedObject);
+
+ collection.Remove (behavior);
+ Assert.Null (behavior.AssociatedObject);
+ }
+
+ [Test]
+ public void TestBehaviorsAttachedDP ()
+ {
+ var behavior = new MockBehavior<MockBindable> ();
+ var bindable = new MockBindable ();
+ var collection = bindable.Behaviors;
+ Assert.Null (behavior.AssociatedObject);
+
+ collection.Add (behavior);
+ Assert.AreSame (bindable, behavior.AssociatedObject);
+
+ collection.Remove (behavior);
+ Assert.Null (behavior.AssociatedObject);
+ }
+ }
+} \ No newline at end of file