Browse Source

A few more tests.

Trying to get a repro for an issue seen with DataBox but so far unable to repro in a unit test. Anyway, these tests should be useful.
pull/9677/head
Steven Kirk 4 years ago
parent
commit
a9fd9f4474
  1. 85
      tests/Avalonia.Controls.UnitTests/ListBoxTests.cs
  2. 12
      tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs

85
tests/Avalonia.Controls.UnitTests/ListBoxTests.cs

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive.Subjects;
using Avalonia.Collections;
@ -663,6 +665,71 @@ namespace Avalonia.Controls.UnitTests
Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
}
[Fact]
public void Handles_Resetting_Items()
{
var items = new ResettingCollection(100);
var target = new ListBox
{
Template = ListBoxTemplate(),
Items = items,
ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas { Height = 10 }),
};
Prepare(target);
var realized = target.GetRealizedContainers()
.Cast<ListBoxItem>()
.Select(x => (string)x.DataContext)
.ToList();
Assert.Equal(Enumerable.Range(0, 10).Select(x => $"Item{x}"), realized);
items.Reverse();
Layout(target);
realized = target.GetRealizedContainers()
.Cast<ListBoxItem>()
.Select(x => (string)x.DataContext)
.ToList();
Assert.Equal(Enumerable.Range(0, 10).Select(x => $"Item{99 - x}"), realized);
}
[Fact]
public void Handles_Resetting_Items_With_Existing_Selection_And_AutoScrollToSelectedItem()
{
var items = new ResettingCollection(100);
var target = new ListBox
{
Template = ListBoxTemplate(),
Items = items,
ItemTemplate = new FuncDataTemplate<string>((_, __) => new Canvas { Height = 10 }),
AutoScrollToSelectedItem = true,
SelectedIndex = 1,
};
Prepare(target);
var realized = target.GetRealizedContainers()
.Cast<ListBoxItem>()
.Select(x => (string)x.DataContext)
.ToList();
Assert.Equal(Enumerable.Range(0, 10).Select(x => $"Item{x}"), realized);
items.Reverse();
Layout(target);
realized = target.GetRealizedContainers()
.Cast<ListBoxItem>()
.Select(x => (string)x.DataContext)
.ToList();
// "Item1" should remain selected, and now be at the bottom of the viewport.
Assert.Equal(Enumerable.Range(0, 10).Select(x => $"Item{10 - x}"), realized);
}
private static void RaiseKeyEvent(ListBox listBox, Key key, KeyModifiers inputModifiers = 0)
{
listBox.RaiseEvent(new KeyEventArgs
@ -711,5 +778,23 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(true, first.IsSelected);
}
}
private class ResettingCollection : List<string>, INotifyCollectionChanged
{
public ResettingCollection(int itemCount)
{
AddRange(Enumerable.Range(0, itemCount).Select(x => $"Item{x}"));
}
public new void Reverse()
{
base.Reverse();
CollectionChanged?.Invoke(
this,
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public event NotifyCollectionChangedEventHandler CollectionChanged;
}
}
}

12
tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs

@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
@ -79,6 +80,17 @@ namespace Avalonia.Controls.UnitTests
AssertRealizedItems(target, itemsControl, 20, 10);
}
[Fact]
public void Scrolls_To_Index()
{
using var app = App();
var (target, scroll, itemsControl) = CreateTarget();
target.ScrollIntoView(20);
AssertRealizedItems(target, itemsControl, 11, 10);
}
[Fact]
public void Creates_Elements_On_Item_Insert()
{

Loading…
Cancel
Save