Browse Source

Fix NotificationCard pseudo class state when NotificationType changes (#21490)

* Fix NotificationCard pseudo class state when NotificationType changes

* Add test for NotificationCard pseudo-class updates
pull/21506/head
yoyo 4 months ago
committed by GitHub
parent
commit
8aaaeb95d2
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 22
      src/Avalonia.Controls/Notifications/NotificationCard.cs
  2. 31
      tests/Avalonia.Controls.UnitTests/NotificationsTests.cs

22
src/Avalonia.Controls/Notifications/NotificationCard.cs

@ -170,24 +170,10 @@ namespace Avalonia.Controls.Notifications
private void UpdateNotificationType()
{
switch (NotificationType)
{
case NotificationType.Error:
PseudoClasses.Add(":error");
break;
case NotificationType.Information:
PseudoClasses.Add(":information");
break;
case NotificationType.Success:
PseudoClasses.Add(":success");
break;
case NotificationType.Warning:
PseudoClasses.Add(":warning");
break;
}
PseudoClasses.Set(":error", NotificationType == NotificationType.Error);
PseudoClasses.Set(":information", NotificationType == NotificationType.Information);
PseudoClasses.Set(":success", NotificationType == NotificationType.Success);
PseudoClasses.Set(":warning", NotificationType == NotificationType.Warning);
}
}
}

31
tests/Avalonia.Controls.UnitTests/NotificationsTests.cs

@ -113,4 +113,35 @@ namespace Avalonia.Controls.UnitTests
Assert.True(!((WindowNotificationManager)manager).Notifications.Any(x => !x.IsClosing));
}
}
public class NotificationCardTests : ScopedTestBase
{
[Fact]
public void Should_Update_Pseudoclasses_When_NotificationType_Changes()
{
var target = new NotificationCard();
target.NotificationType = NotificationType.Error;
AssertPseudoClasses(target, ":error");
target.NotificationType = NotificationType.Information;
AssertPseudoClasses(target, ":information");
target.NotificationType = NotificationType.Success;
AssertPseudoClasses(target, ":success");
target.NotificationType = NotificationType.Warning;
AssertPseudoClasses(target, ":warning");
static void AssertPseudoClasses(NotificationCard target, string expected)
{
Assert.Contains(expected, target.Classes);
foreach (var pseudoclass in new[] { ":error", ":information", ":success", ":warning" }.Where(x => x != expected))
{
Assert.DoesNotContain(pseudoclass, target.Classes);
}
}
}
}
}

Loading…
Cancel
Save