summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/MarginTests.cs
blob: fd5debfa934766b4471d8c6b2137e5b3f30e6038 (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
using NUnit.Framework;
using Xamarin.Forms;

namespace Xamarin.Forms.Core.UnitTests
{
	public class MarginTests : BaseTestFixture
	{
		[SetUp]
		public override void Setup()
		{
			base.Setup ();
			Device.PlatformServices = new MockPlatformServices ();
		}

		[TearDown]
		public override void TearDown()
		{
			base.TearDown ();
			Device.PlatformServices = null;
		}

		[Test]
		public void GetSizeRequestIncludesMargins ()
		{
			var platform = new UnitPlatform ((b, d, e) => new SizeRequest(new Size(100,50)));

			var parent = new ContentView {
				Platform = platform,
				IsPlatformEnabled = true,
			};
			var child = new Button {
				Text = "Test",
				Platform = platform,
				IsPlatformEnabled = true,
			};
			

			child.Margin = new Thickness (10, 20, 30, 40);
			parent.Content = child;

			var result = parent.Measure (double.PositiveInfinity, double.PositiveInfinity, MeasureFlags.IncludeMargins);
			Assert.AreEqual (new Size (140, 110), result.Request);
		}

		[Test]
		public void MarginsAffectPositionInContentView ()
		{
			var platform = new UnitPlatform ((b, d, e) => new SizeRequest(new Size(100,50)));

			var parent = new ContentView {
				Platform = platform,
				IsPlatformEnabled = true,
			};
			var child = new Button {
				Text = "Test",
				Platform = platform,
				IsPlatformEnabled = true,
			};
			

			child.Margin = new Thickness (10, 20, 30, 40);
			parent.Content = child;

			parent.Layout (new Rectangle (0, 0, 140, 110));
			Assert.AreEqual (new Rectangle (10, 20, 100, 50), child.Bounds);
		}

		[Test]
		public void ChangingMarginCausesRelayout ()
		{
			var platform = new UnitPlatform ((b, d, e) => new SizeRequest(new Size(100,50)));

			var parent = new ContentView {
				Platform = platform,
				IsPlatformEnabled = true,
			};
			var child = new Button {
				Text = "Test",
				VerticalOptions = LayoutOptions.Start,
				HorizontalOptions = LayoutOptions.Start,
				Platform = platform,
				IsPlatformEnabled = true,
			};
			

			child.Margin = new Thickness (10, 20, 30, 40);
			parent.Content = child;

			parent.Layout (new Rectangle (0, 0, 1000, 1000));
			Assert.AreEqual (new Rectangle (10, 20, 100, 50), child.Bounds);
		}

		[Test]
		public void IntegrationTest ()
		{
			var platform = new UnitPlatform ((b, d, e) => new SizeRequest(new Size(100,50)));

			var parent = new StackLayout {
				Platform = platform,
				Spacing = 0,
				IsPlatformEnabled = true,
			};

			var child1 = new Button {
				Text = "Test",
				VerticalOptions = LayoutOptions.Start,
				HorizontalOptions = LayoutOptions.Start,
				Platform = platform,
				IsPlatformEnabled = true,
			};

			var child2 = new Button {
				Text = "Test",
				Platform = platform,
				IsPlatformEnabled = true,
			};

			child2.Margin = new Thickness (5, 10, 15, 20);

			parent.Children.Add (child1);
			parent.Children.Add (child2);

			parent.Layout (new Rectangle (0, 0, 1000, 1000));

			Assert.AreEqual (new Rectangle (0, 0, 100, 50), child1.Bounds);
			Assert.AreEqual (new Rectangle (5, 60, 980, 50), child2.Bounds);

			child1.Margin = new Thickness (10, 20, 30, 40);

			Assert.AreEqual (new Rectangle (10, 20, 100, 50), child1.Bounds);
			Assert.AreEqual (new Rectangle (5, 120, 980, 50), child2.Bounds);
		}
	}
}