test: add banner test.

This commit is contained in:
Dong Bin
2025-01-03 20:04:13 +08:00
parent d09431a9f5
commit c61d37e9e8
6 changed files with 121 additions and 7 deletions

View File

@@ -19,6 +19,11 @@
Classes.Bordered="{Binding Bordered}" Classes.Bordered="{Binding Bordered}"
Content="This is the Demo of Ursa Banner. " Content="This is the Demo of Ursa Banner. "
Header="Welcome to Ursa" Header="Welcome to Ursa"
Width="300"
Height="100"
HorizontalAlignment="Left"
VerticalAlignment="Top"
CanClose="True"
Type="{Binding SelectedType}" /> Type="{Binding SelectedType}" />
</Border> </Border>
</Grid> </Grid>

View File

@@ -1,4 +1,5 @@
using Avalonia; using System.Diagnostics.CodeAnalysis;
using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Media; using Avalonia.Media;
@@ -12,12 +13,14 @@ public class Avatar : Button
public static readonly StyledProperty<object?> HoverMaskProperty = AvaloniaProperty.Register<Avatar, object?>( public static readonly StyledProperty<object?> HoverMaskProperty = AvaloniaProperty.Register<Avatar, object?>(
nameof(HoverMask)); nameof(HoverMask));
[ExcludeFromCodeCoverage]
public IImage? Source public IImage? Source
{ {
get => GetValue(SourceProperty); get => GetValue(SourceProperty);
set => SetValue(SourceProperty, value); set => SetValue(SourceProperty, value);
} }
[ExcludeFromCodeCoverage]
public object? HoverMask public object? HoverMask
{ {
get => GetValue(HoverMaskProperty); get => GetValue(HoverMaskProperty);

View File

@@ -1,8 +1,10 @@
using System.Diagnostics.CodeAnalysis;
using Avalonia; using Avalonia;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Metadata; using Avalonia.Controls.Metadata;
using Avalonia.Controls.Notifications; using Avalonia.Controls.Notifications;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity; using Avalonia.Interactivity;
using Irihi.Avalonia.Shared.Helpers; using Irihi.Avalonia.Shared.Helpers;
@@ -20,6 +22,7 @@ public class Banner: HeaderedContentControl
public static readonly StyledProperty<bool> CanCloseProperty = AvaloniaProperty.Register<Banner, bool>( public static readonly StyledProperty<bool> CanCloseProperty = AvaloniaProperty.Register<Banner, bool>(
nameof(CanClose)); nameof(CanClose));
[ExcludeFromCodeCoverage]
public bool CanClose public bool CanClose
{ {
get => GetValue(CanCloseProperty); get => GetValue(CanCloseProperty);
@@ -29,6 +32,7 @@ public class Banner: HeaderedContentControl
public static readonly StyledProperty<bool> ShowIconProperty = AvaloniaProperty.Register<Banner, bool>( public static readonly StyledProperty<bool> ShowIconProperty = AvaloniaProperty.Register<Banner, bool>(
nameof(ShowIcon), true); nameof(ShowIcon), true);
[ExcludeFromCodeCoverage]
public bool ShowIcon public bool ShowIcon
{ {
get => GetValue(ShowIconProperty); get => GetValue(ShowIconProperty);
@@ -38,6 +42,7 @@ public class Banner: HeaderedContentControl
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<Banner, object?>( public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<Banner, object?>(
nameof(Icon)); nameof(Icon));
[ExcludeFromCodeCoverage]
public object? Icon public object? Icon
{ {
get => GetValue(IconProperty); get => GetValue(IconProperty);
@@ -47,6 +52,7 @@ public class Banner: HeaderedContentControl
public static readonly StyledProperty<NotificationType> TypeProperty = AvaloniaProperty.Register<Banner, NotificationType>( public static readonly StyledProperty<NotificationType> TypeProperty = AvaloniaProperty.Register<Banner, NotificationType>(
nameof(Type)); nameof(Type));
[ExcludeFromCodeCoverage]
public NotificationType Type public NotificationType Type
{ {
get => GetValue(TypeProperty); get => GetValue(TypeProperty);
@@ -63,6 +69,6 @@ public class Banner: HeaderedContentControl
private void OnCloseClick(object? sender, RoutedEventArgs args) private void OnCloseClick(object? sender, RoutedEventArgs args)
{ {
IsVisible = false; SetCurrentValue(IsVisibleProperty, false);
} }
} }

View File

@@ -0,0 +1,87 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.VisualTree;
using UrsaControls = Ursa.Controls;
namespace HeadlessTest.Ursa.Controls.BannerTests;
public class Tests
{
[AvaloniaFact]
public void Click_On_Banner_Close_Button_Should_Hide_Banner()
{
// Arrange
var window = new Window();
var banner = new UrsaControls.Banner()
{
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Left,
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top,
Width = 300,
Height = 100,
CanClose = true
};
window.Content = banner;
window.Show();
Assert.True(banner.IsVisible);
var closeButton = banner.GetTemplateChildren().OfType<Button>()
.FirstOrDefault(a => a.Name == UrsaControls.Banner.PART_CloseButton);
Assert.NotNull(closeButton);
Assert.True(closeButton.IsVisible);
var clickPosition = closeButton.Bounds.Center;
clickPosition = new Point(clickPosition.X + 20, clickPosition.Y + 20);
// Act
window.MouseDown(clickPosition, MouseButton.Left);
window.MouseUp(clickPosition, MouseButton.Left);
// Assert
Assert.False(banner.IsVisible);
}
[AvaloniaFact]
public void Click_On_Banner_Does_Nothing_If_Cannot_Close()
{
// Arrange
var window = new Window();
var banner = new UrsaControls.Banner()
{
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Left,
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Top,
Width = 300,
Height = 100,
CanClose = false
};
window.Content = banner;
window.Show();
Assert.True(banner.IsVisible);
var closeButton = banner.GetTemplateChildren().OfType<Button>()
.FirstOrDefault(a => a.Name == UrsaControls.Banner.PART_CloseButton);
Assert.NotNull(closeButton);
Assert.False(closeButton.IsVisible);
var clickPosition = closeButton.Bounds.Center;
clickPosition = new Point(clickPosition.X + 20, clickPosition.Y + 20);
// Act
window.MouseDown(clickPosition, MouseButton.Left);
window.MouseUp(clickPosition, MouseButton.Left);
// Assert
Assert.True(banner.IsVisible);
}
}

View File

@@ -10,11 +10,12 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia.Headless.XUnit" Version="11.1.3" /> <PackageReference Include="Avalonia.Headless.XUnit" Version="11.2.3" />
<PackageReference Include="Avalonia.Skia" Version="11.2.3" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0"/> <PackageReference Include="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="Semi.Avalonia" Version="11.1.0.4" /> <PackageReference Include="Semi.Avalonia" Version="11.2.1.3" />
<PackageReference Include="xunit" Version="2.5.3"/> <PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/> <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup> </ItemGroup>

View File

@@ -9,5 +9,17 @@ namespace HeadlessTest.Ursa;
public class TestAppBuilder public class TestAppBuilder
{ {
public static AppBuilder BuildAvaloniaApp() => public static AppBuilder BuildAvaloniaApp() =>
AppBuilder.Configure<App>().UseHeadless(new AvaloniaHeadlessPlatformOptions()); AppBuilder
.Configure<App>()
.UseHeadless(new AvaloniaHeadlessPlatformOptions());
} }
public class SkiaTestAppBuilder
{
public static AppBuilder BuildAvaloniaApp() =>
AppBuilder
.Configure<App>()
.UseSkia()
.UseHeadless(new AvaloniaHeadlessPlatformOptions(){ UseHeadlessDrawing = false});
}