This repository was archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathBehaviorTest.cs
113 lines (92 loc) · 2.9 KB
/
BehaviorTest.cs
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
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);
}
}
}