Xamarin.Forms/Xamarin.Forms.Core.UnitTests/FrameUnitTests.cs

323 строки
5.8 KiB
C#
Исходник Постоянная ссылка Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
public class FrameUnitTests : BaseTestFixture
{
[Test]
2020-09-29 13:15:44 +03:00
public void TestConstructor()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
Frame frame = new Frame();
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.Null(frame.Content);
Assert.AreEqual(new Thickness(20, 20, 20, 20), frame.Padding);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void TestPackWithoutChild()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
Frame frame = new Frame();
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
var parent = new NaiveLayout();
2016-03-22 23:02:25 +03:00
bool thrown = false;
2020-09-29 13:15:44 +03:00
try
{
parent.Children.Add(frame);
}
catch
{
2016-03-22 23:02:25 +03:00
thrown = true;
}
2020-09-29 13:15:44 +03:00
Assert.False(thrown);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void TestPackWithChild()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
Frame frame = new Frame
{
Content = new View()
2016-03-22 23:02:25 +03:00
};
2020-09-29 13:15:44 +03:00
var parent = new NaiveLayout();
2016-03-22 23:02:25 +03:00
bool thrown = false;
2020-09-29 13:15:44 +03:00
try
{
parent.Children.Add(frame);
}
catch
{
2016-03-22 23:02:25 +03:00
thrown = true;
}
2020-09-29 13:15:44 +03:00
Assert.False(thrown);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void TestSetChild()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
Frame frame = new Frame();
var child1 = new Label();
2016-03-22 23:02:25 +03:00
bool added = false;
frame.ChildAdded += (sender, e) => added = true;
frame.Content = child1;
2020-09-29 13:15:44 +03:00
Assert.True(added);
Assert.AreEqual(child1, frame.Content);
2016-03-22 23:02:25 +03:00
added = false;
frame.Content = child1;
2020-09-29 13:15:44 +03:00
Assert.False(added);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void TestReplaceChild()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
Frame frame = new Frame();
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
var child1 = new Label();
var child2 = new Label();
2016-03-22 23:02:25 +03:00
frame.Content = child1;
bool removed = false;
bool added = false;
frame.ChildRemoved += (sender, e) => removed = true;
frame.ChildAdded += (sender, e) => added = true;
frame.Content = child2;
2020-09-29 13:15:44 +03:00
Assert.True(removed);
Assert.True(added);
Assert.AreEqual(child2, frame.Content);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void TestFrameLayout()
2016-03-22 23:02:25 +03:00
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 200,
IsPlatformEnabled = true
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Size(140, 240), frame.GetSizeRequest(double.PositiveInfinity, double.PositiveInfinity).Request);
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 300, 300));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(20, 20, 260, 260), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void TestDoesNotThrowOnSetNullChild()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
Assert.DoesNotThrow(() => new Frame { Content = null });
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void WidthRequest()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 200,
IsPlatformEnabled = true
},
IsPlatformEnabled = true,
WidthRequest = 20
};
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Size(60, 240), frame.GetSizeRequest(double.PositiveInfinity, double.PositiveInfinity).Request);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void HeightRequest()
2016-03-22 23:02:25 +03:00
{
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 200,
IsPlatformEnabled = true,
},
IsPlatformEnabled = true,
HeightRequest = 20
};
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Size(140, 60), frame.GetSizeRequest(double.PositiveInfinity, double.PositiveInfinity).Request);
2016-03-22 23:02:25 +03:00
}
[Test]
2020-09-29 13:15:44 +03:00
public void LayoutVerticallyCenter()
2016-03-22 23:02:25 +03:00
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 100,
IsPlatformEnabled = true,
VerticalOptions = LayoutOptions.Center
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 200, 200));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(20, 50, 160, 100), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
public void LayoutVerticallyBegin()
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 100,
IsPlatformEnabled = true,
VerticalOptions = LayoutOptions.Start
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 200, 200));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(20, 20, 160, 100), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
public void LayoutVerticallyEnd()
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 100,
IsPlatformEnabled = true,
VerticalOptions = LayoutOptions.End
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 200, 200));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(20, 80, 160, 100), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
public void LayoutHorizontallyCenter()
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 100,
IsPlatformEnabled = true,
HorizontalOptions = LayoutOptions.Center
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 200, 200));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(50, 20, 100, 160), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
public void LayoutHorizontallyBegin()
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 100,
IsPlatformEnabled = true,
HorizontalOptions = LayoutOptions.Start
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 200, 200));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(20, 20, 100, 160), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
public void LayoutHorizontallyEnd()
{
View child;
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Content = child = new View
{
2016-03-22 23:02:25 +03:00
WidthRequest = 100,
HeightRequest = 100,
IsPlatformEnabled = true,
HorizontalOptions = LayoutOptions.End
},
IsPlatformEnabled = true,
};
2020-09-29 13:15:44 +03:00
frame.Layout(new Rectangle(0, 0, 200, 200));
2016-03-22 23:02:25 +03:00
2020-09-29 13:15:44 +03:00
Assert.AreEqual(new Rectangle(80, 20, 100, 160), child.Bounds);
2016-03-22 23:02:25 +03:00
}
[Test]
public void SettingPaddingThroughStyle()
{
2020-09-29 13:15:44 +03:00
var frame = new Frame
{
Style = new Style(typeof(Frame))
{
Setters = {
new Setter {Property = Layout.PaddingProperty, Value = 0}
}
}
};
Assert.AreEqual(new Thickness(0), frame.Padding);
}
}
}