2016-03-22 23:02:25 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Linq;
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Xamarin.Forms.Core.UnitTests
|
|
|
|
{
|
|
|
|
internal class NaiveLayout : Layout<View>
|
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
protected override void LayoutChildren(double x, double y, double width, double height)
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
foreach (var child in ((IElementController)this).LogicalChildren.Cast<View>())
|
|
|
|
{
|
|
|
|
var result = new Rectangle(x, y, 0, 0);
|
|
|
|
var request = child.GetSizeRequest(double.PositiveInfinity, double.PositiveInfinity);
|
2016-03-22 23:02:25 +03:00
|
|
|
result.Width = request.Request.Width;
|
|
|
|
result.Height = request.Request.Height;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
child.Layout(result);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
public class LayoutUnitTests : BaseTestFixture
|
|
|
|
{
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestRaiseChild()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
var child1 = new View();
|
|
|
|
var child2 = new View();
|
|
|
|
var child3 = new View();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
|
|
|
view.Children.Add(child2);
|
|
|
|
view.Children.Add(child3);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool reordered = false;
|
|
|
|
view.ChildrenReordered += (sender, args) => reordered = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.RaiseChild(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(child1, ((IElementController)view).LogicalChildren[2]);
|
|
|
|
Assert.True(reordered);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.RaiseChild(child2);
|
|
|
|
Assert.AreEqual(child2, ((IElementController)view).LogicalChildren[2]);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestRaiseUnownedChild()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
var child1 = new View();
|
|
|
|
var child2 = new View();
|
|
|
|
var child3 = new View();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
|
|
|
view.Children.Add(child3);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool reordered = false;
|
|
|
|
view.ChildrenReordered += (sender, args) => reordered = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.RaiseChild(child2);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.False(reordered);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestLowerChild()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
var child1 = new View();
|
|
|
|
var child2 = new View();
|
|
|
|
var child3 = new View();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
|
|
|
view.Children.Add(child2);
|
|
|
|
view.Children.Add(child3);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool reordered = false;
|
|
|
|
view.ChildrenReordered += (sender, args) => reordered = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.LowerChild(child3);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(child3, ((IElementController)view).LogicalChildren[0]);
|
|
|
|
Assert.True(reordered);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.LowerChild(child2);
|
|
|
|
Assert.AreEqual(child2, ((IElementController)view).LogicalChildren[0]);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestLowerUnownedChild()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
var child1 = new View();
|
|
|
|
var child2 = new View();
|
|
|
|
var child3 = new View();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
|
|
|
view.Children.Add(child3);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool reordered = false;
|
|
|
|
view.ChildrenReordered += (sender, args) => reordered = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.LowerChild(child2);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.False(reordered);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestAdd()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
|
|
|
var child1 = new View();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool added = false;
|
|
|
|
view.ChildAdded += (sender, args) => added = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.True(added);
|
|
|
|
Assert.AreEqual(child1, ((IElementController)view).LogicalChildren[0]);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestDoubleAdd()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
|
|
|
var child1 = new View();
|
|
|
|
view.Children.Add(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool added = false;
|
|
|
|
view.ChildAdded += (sender, args) => added = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.False(added);
|
|
|
|
Assert.AreEqual(child1, ((IElementController)view).LogicalChildren[0]);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestRemove()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
|
|
|
var child1 = new View();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
bool removed = false;
|
|
|
|
view.ChildRemoved += (sender, args) => removed = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Remove(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.True(removed);
|
|
|
|
Assert.False(((IElementController)view).LogicalChildren.Any());
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestGenericEnumerator()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
var children = new[] {
|
|
|
|
new View (),
|
|
|
|
new View (),
|
|
|
|
new View ()
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var child in children)
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
int i = 0;
|
2020-09-29 13:15:44 +03:00
|
|
|
foreach (var child in ((IElementController)view).LogicalChildren)
|
|
|
|
{
|
|
|
|
Assert.AreEqual(children[i], child);
|
2016-03-22 23:02:25 +03:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestEnumerator()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var view = new NaiveLayout();
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
var children = new[] {
|
2016-03-22 23:02:25 +03:00
|
|
|
new View (),
|
|
|
|
new View (),
|
|
|
|
new View ()
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (var child in children)
|
2020-09-29 13:15:44 +03:00
|
|
|
view.Children.Add(child);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
|
|
|
int i = 0;
|
2020-09-29 13:15:44 +03:00
|
|
|
var enumerator = (((IElementController)view).LogicalChildren as IEnumerable).GetEnumerator();
|
|
|
|
while (enumerator.MoveNext())
|
|
|
|
{
|
|
|
|
Assert.AreEqual(children[i], enumerator.Current as View);
|
2016-03-22 23:02:25 +03:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestInitializerSyntax()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
View view1, view2;
|
2020-09-29 13:15:44 +03:00
|
|
|
var group = new NaiveLayout
|
|
|
|
{
|
2016-03-22 23:02:25 +03:00
|
|
|
Children = {
|
|
|
|
(view1 = new View ()),
|
|
|
|
(view2 = new View ())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(2, ((IElementController)group).LogicalChildren.Count);
|
|
|
|
Assert.IsTrue(((IElementController)group).LogicalChildren.Contains(view1));
|
|
|
|
Assert.IsTrue(((IElementController)group).LogicalChildren.Contains(view2));
|
|
|
|
Assert.AreEqual(view1, ((IElementController)group).LogicalChildren[0]);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestChildren()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
View view1, view2;
|
2020-09-29 13:15:44 +03:00
|
|
|
var group = new NaiveLayout
|
|
|
|
{
|
2016-03-22 23:02:25 +03:00
|
|
|
Children = {
|
|
|
|
(view1 = new View ()),
|
|
|
|
(view2 = new View ())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(2, group.Children.Count);
|
|
|
|
Assert.IsTrue(group.Children.Contains(view1));
|
|
|
|
Assert.IsTrue(group.Children.Contains(view2));
|
|
|
|
Assert.AreEqual(view1, group.Children[0]);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void TestDefaultLayout()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
|
|
|
View view;
|
2020-09-29 13:15:44 +03:00
|
|
|
var group = new NaiveLayout
|
|
|
|
{
|
2016-03-22 23:02:25 +03:00
|
|
|
IsPlatformEnabled = true,
|
|
|
|
Children = {
|
|
|
|
(view = new View {
|
|
|
|
WidthRequest = 50,
|
|
|
|
HeightRequest = 20,
|
|
|
|
IsPlatformEnabled = true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
group.Layout(new Rectangle(0, 0, 400, 400));
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.AreEqual(new Rectangle(0, 0, 50, 20), view.Bounds);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void ThrowsInvalidOperationOnSelfAdd()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var group = new NaiveLayout();
|
|
|
|
Assert.Throws<InvalidOperationException>(() => group.Children.Add(group));
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
2020-09-29 13:15:44 +03:00
|
|
|
public void ReorderChildrenDoesNotRaiseChildAddedOrRemoved()
|
2016-03-22 23:02:25 +03:00
|
|
|
{
|
2020-09-29 13:15:44 +03:00
|
|
|
var child1 = new BoxView();
|
|
|
|
var child2 = new BoxView();
|
|
|
|
var layout = new NaiveLayout
|
|
|
|
{
|
|
|
|
Children = { child1, child2 }
|
2016-03-22 23:02:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
var added = false;
|
|
|
|
var removed = false;
|
|
|
|
|
|
|
|
layout.ChildAdded += (sender, args) => added = true;
|
|
|
|
layout.ChildRemoved += (sender, args) => removed = true;
|
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
layout.RaiseChild(child1);
|
2016-03-22 23:02:25 +03:00
|
|
|
|
2020-09-29 13:15:44 +03:00
|
|
|
Assert.False(added);
|
|
|
|
Assert.False(removed);
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
2016-03-28 01:48:31 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void AddToSecondLayoutRemovesFromOriginal()
|
|
|
|
{
|
|
|
|
var child = new BoxView();
|
|
|
|
var layout1 = new NaiveLayout();
|
|
|
|
var layout2 = new NaiveLayout();
|
|
|
|
|
|
|
|
layout1.Children.Add(child);
|
|
|
|
layout2.Children.Add(child);
|
|
|
|
|
|
|
|
Assert.False(layout1.Children.Contains(child));
|
|
|
|
}
|
2016-03-22 23:02:25 +03:00
|
|
|
}
|
|
|
|
}
|