test: mvvm tests.

This commit is contained in:
Dong Bin
2025-07-10 11:39:45 +08:00
parent d360ca9ef0
commit d470488c76
3 changed files with 89 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:u="https://irihi.tech/ursa"
xmlns:vm="clr-namespace:HeadlessTest.Ursa.Controls.AnchorTests"
mc:Ignorable="d" d:DesignWidth="800"
d:DesignHeight="450"
x:DataType="vm:AnchorDemoViewModel"
x:CompileBindings="True"
x:Class="HeadlessTest.Ursa.Controls.AnchorTests.TestView2">
<u:Anchor
Width="200"
Margin="24"
ItemsSource="{Binding AnchorItems}">
<u:Anchor.Styles>
<Style x:DataType="vm:AnchorItemViewModel" Selector="u|AnchorItem">
<Setter Property="AnchorId" Value="{Binding AnchorId}" />
</Style>
</u:Anchor.Styles>
<u:Anchor.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Header}"/>
</TreeDataTemplate>
</u:Anchor.ItemTemplate>
</u:Anchor>
</UserControl>

View File

@@ -0,0 +1,46 @@
using System.Collections.ObjectModel;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using CommunityToolkit.Mvvm.ComponentModel;
namespace HeadlessTest.Ursa.Controls.AnchorTests;
public partial class TestView2 : UserControl
{
public TestView2()
{
InitializeComponent();
DataContext = new AnchorDemoViewModel();
}
}
public partial class AnchorDemoViewModel: ObservableObject
{
public List<AnchorItemViewModel> AnchorItems { get; } = new()
{
new AnchorItemViewModel { AnchorId = "anchor1", Header = "Anchor 1" },
new AnchorItemViewModel { AnchorId = "anchor2", Header = "Anchor 2" },
new AnchorItemViewModel
{
AnchorId = "anchor3", Header = "Anchor 3",
Children =
[
new AnchorItemViewModel() { AnchorId = "anchor3-1", Header = "Anchor 3.1" },
new AnchorItemViewModel() { AnchorId = "anchor3-2", Header = "Anchor 3.2" },
new AnchorItemViewModel() { AnchorId = "anchor3-3", Header = "Anchor 3.3" }
]
},
new AnchorItemViewModel { AnchorId = "anchor4", Header = "Anchor 4" },
new AnchorItemViewModel { AnchorId = "anchor5", Header = "Anchor 5" },
new AnchorItemViewModel { AnchorId = "anchor6", Header = "Anchor 6" },
new AnchorItemViewModel { AnchorId = "anchor7", Header = "Anchor 7" },
};
}
public partial class AnchorItemViewModel: ObservableObject
{
[ObservableProperty] private string? _anchorId;
[ObservableProperty] private string? _header;
public ObservableCollection<AnchorItemViewModel>? Children { get; set; }
}

View File

@@ -4,6 +4,7 @@ using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Threading;
using Avalonia.VisualTree;
using Ursa.Controls;
namespace HeadlessTest.Ursa.Controls.AnchorTests;
@@ -39,7 +40,7 @@ public class Tests
Dispatcher.UIThread.RunJobs();
await Task.Delay(800);
Assert.True(item4.IsSelected);
Assert.Equal(300.0 * 3, scrollViewer.Offset.Y, 1.0);
Assert.Equal(300.0 * 3, scrollViewer.Offset.Y, 50.0);
}
[AvaloniaFact]
@@ -79,4 +80,18 @@ public class Tests
Assert.True(item2.IsSelected);
Assert.False(item4.IsSelected);
}
[AvaloniaFact]
public void MVVM_Support()
{
var window = new Window();
var view = new TestView2();
window.Content = view;
window.Show();
Dispatcher.UIThread.RunJobs();
var items = window.GetVisualDescendants().OfType<AnchorItem>().ToList();
Assert.NotEmpty(items);
Assert.Equal(10, items.Count);
}
}