Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect defaults when adding non-BindableObject as AbsoluteLayout child #19839

Merged
merged 1 commit into from Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Controls/src/Core/Layout/AbsoluteLayout.cs
Expand Up @@ -154,6 +154,11 @@ protected override void OnUpdate(int index, IView view, IView oldView)

class AbsoluteLayoutInfo
{
public AbsoluteLayoutInfo()
{
LayoutBounds = new Rect(0, 0, AutoSize, AutoSize);
}

public AbsoluteLayoutFlags LayoutFlags { get; set; }
public Rect LayoutBounds { get; set; }
}
Expand Down
@@ -0,0 +1,64 @@
#nullable enable
using Xunit;

namespace Microsoft.Maui.Controls.Core.UnitTests.Layouts
{
[Category("Layout")]
public class AbsoluteLayoutTests : BaseTestFixture
{
[Fact]
public void BoundsDefaultsConsistentForNewChildren()
{
var layout = new AbsoluteLayout();

var child1 = new Label { }; // BindableObject
var child2 = NSubstitute.Substitute.For<IView>(); // Not a BindableObject

layout.Add(child1);
layout.Add(child2);

var bounds1 = layout.GetLayoutBounds(child1);
var bounds2 = layout.GetLayoutBounds(child2);

// The default layout bounds given to each of these child IViews _should_ be the same
Assert.Equal(bounds1.X, bounds2.X);
Assert.Equal(bounds1.Y, bounds2.Y);
Assert.Equal(bounds1.Width, bounds2.Width);
Assert.Equal(bounds1.Height, bounds2.Height);
}

[Fact]
public void BoundsDefaultsAttachedProperty()
{
var layout = new AbsoluteLayout();

var child = new Label { }; // BindableObject

layout.Add(child);

var bounds = layout.GetLayoutBounds(child);

Assert.Equal(0, bounds.X);
Assert.Equal(0, bounds.Y);
Assert.Equal(AbsoluteLayout.AutoSize, bounds.Width);
Assert.Equal(AbsoluteLayout.AutoSize, bounds.Height);
}

[Fact]
public void BoundsDefaultsRegularProperty()
{
var layout = new AbsoluteLayout();

var child = NSubstitute.Substitute.For<IView>(); // Not a BindableObject

layout.Add(child);

var bounds = layout.GetLayoutBounds(child);

Assert.Equal(0, bounds.X);
Assert.Equal(0, bounds.Y);
Assert.Equal(AbsoluteLayout.AutoSize, bounds.Width);
Assert.Equal(AbsoluteLayout.AutoSize, bounds.Height);
}
}
}