Skip to content

Commit

Permalink
Add failing test for #9997.
Browse files Browse the repository at this point in the history
And a passing test for `nth-child` selector.
  • Loading branch information
grokys committed Jan 20, 2023
1 parent 9de82b1 commit 649e4fc
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Avalonia.Data;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Styling;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Xunit;
Expand Down Expand Up @@ -278,6 +280,58 @@ public void Scrolling_Back_To_Focused_Element_Uses_Correct_Element()
Assert.Same(focused, target.GetRealizedElements().First());
}

[Fact]
public void NthChild_Selector_Works()
{
using var app = App();

var style = new Style(x => x.OfType<ContentPresenter>().NthChild(5, 0))
{
Setters = { new Setter(ListBoxItem.BackgroundProperty, Brushes.Red) },
};

var (target, _, _) = CreateTarget(styles: new[] { style });
var realized = target.GetRealizedContainers()!.Cast<ContentPresenter>().ToList();

Assert.Equal(10, realized.Count);

for (var i = 0; i < 10; ++i)
{
var container = realized[i];
var index = target.IndexFromContainer(container);
var expectedBackground = (i == 4 || i == 9) ? Brushes.Red : null;

Assert.Equal(i, index);
Assert.Equal(expectedBackground, container.Background);
}
}

[Fact]
public void NthLastChild_Selector_Works()
{
using var app = App();

var style = new Style(x => x.OfType<ContentPresenter>().NthLastChild(5, 0))
{
Setters = { new Setter(ListBoxItem.BackgroundProperty, Brushes.Red) },
};

var (target, _, _) = CreateTarget(styles: new[] { style });
var realized = target.GetRealizedContainers()!.Cast<ContentPresenter>().ToList();

Assert.Equal(10, realized.Count);

for (var i = 0; i < 10; ++i)
{
var container = realized[i];
var index = target.IndexFromContainer(container);
var expectedBackground = (i == 0 || i == 5) ? Brushes.Red : null;

Assert.Equal(i, index);
Assert.Equal(expectedBackground, container.Background);
}
}

private static IReadOnlyList<int> GetRealizedIndexes(VirtualizingStackPanel target, ItemsControl itemsControl)
{
return target.GetRealizedElements()
Expand Down Expand Up @@ -322,7 +376,8 @@ private static void AssertRealizedControlItems<TContainer>(

private static (VirtualizingStackPanel, ScrollViewer, ItemsControl) CreateTarget(
IEnumerable<object>? items = null,
bool useItemTemplate = true)
bool useItemTemplate = true,
IEnumerable<Style>? styles = null)
{
var target = new VirtualizingStackPanel();

Expand Down Expand Up @@ -351,6 +406,10 @@ private static (VirtualizingStackPanel, ScrollViewer, ItemsControl) CreateTarget

var root = new TestRoot(true, itemsControl);
root.ClientSize = new(100, 100);

if (styles is not null)
root.Styles.AddRange(styles);

root.LayoutManager.ExecuteInitialLayoutPass();

return (target, scroll, itemsControl);
Expand Down

0 comments on commit 649e4fc

Please sign in to comment.