summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/BehaviorTest.cs
blob: 9021738906789f9e32b435e05e3c8a21c734a0e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using NUnit.Framework;

namespace Xamarin.Forms.Core.UnitTests
{
	class MockBehavior<T> : Behavior<T> where T:BindableObject
	{
		public static int AttachCount { get; set; }
		public bool attached;
		public bool detached;

		protected override void OnAttachedTo (BindableObject bindable)
		{
			base.OnAttachedTo (bindable);
			attached = true;
			AttachCount++;
			AssociatedObject = bindable;
		}

		protected override void OnDetachingFrom (BindableObject bindable)
		{
			AttachCount--;
			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);
		}

		[Test]
		//https://bugzilla.xamarin.com/show_bug.cgi?id=44074
		public void TestBehaviorsAreDetachedBeforeGarbageCollection()
		{
			WeakReference weakBindable = null;

			var attachCount = MockBehavior<VisualElement>.AttachCount;

			int i = 0;
			Action create = null;
			create = () =>
			{
				if (i++ < 1024)
				{
					create();
					return;
				}

				var bindable = new MockBindable
				{
					Behaviors = {
						new MockBehavior<VisualElement> ()
					}
				};
				weakBindable = new WeakReference(bindable);
			};

			create();

			GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();

			Assert.False(weakBindable.IsAlive);
			Assert.AreEqual(attachCount, MockBehavior<VisualElement>.AttachCount);
		}
	}
}