|
| 1 | +using System.Windows.Input; |
| 2 | +using System.Windows.Media; |
| 3 | + |
| 4 | +namespace MaterialDesignThemes.Wpf.Tests; |
| 5 | + |
| 6 | +[TestExecutor<STAThreadExecutor>] |
| 7 | +public class ChipTests |
| 8 | +{ |
| 9 | + [Test] |
| 10 | + public async Task TestIconProperty() |
| 11 | + { |
| 12 | + Chip chip = new(); |
| 13 | + |
| 14 | + // Assert defaults |
| 15 | + await Assert.That(Chip.IconProperty.Name).IsEqualTo("Icon"); |
| 16 | + await Assert.That(chip.Icon).IsNull(); |
| 17 | + |
| 18 | + // Assert setting works |
| 19 | + var icon = new PackIcon { Kind = PackIconKind.Home }; |
| 20 | + chip.Icon = icon; |
| 21 | + await Assert.That(chip.Icon).IsEqualTo(icon); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public async Task TestIconBackgroundProperty() |
| 26 | + { |
| 27 | + Chip chip = new(); |
| 28 | + |
| 29 | + // Assert defaults |
| 30 | + await Assert.That(Chip.IconBackgroundProperty.Name).IsEqualTo("IconBackground"); |
| 31 | + await Assert.That(chip.IconBackground).IsNull(); |
| 32 | + |
| 33 | + // Assert setting works |
| 34 | + chip.IconBackground = Brushes.Red; |
| 35 | + await Assert.That(chip.IconBackground).IsEqualTo(Brushes.Red); |
| 36 | + } |
| 37 | + |
| 38 | + [Test] |
| 39 | + public async Task TestIconForegroundProperty() |
| 40 | + { |
| 41 | + Chip chip = new(); |
| 42 | + |
| 43 | + // Assert defaults |
| 44 | + await Assert.That(Chip.IconForegroundProperty.Name).IsEqualTo("IconForeground"); |
| 45 | + await Assert.That(chip.IconForeground).IsNull(); |
| 46 | + |
| 47 | + // Assert setting works |
| 48 | + chip.IconForeground = Brushes.Blue; |
| 49 | + await Assert.That(chip.IconForeground).IsEqualTo(Brushes.Blue); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public async Task TestIsDeletableProperty() |
| 54 | + { |
| 55 | + Chip chip = new(); |
| 56 | + |
| 57 | + // Assert defaults |
| 58 | + await Assert.That(Chip.IsDeletableProperty.Name).IsEqualTo("IsDeletable"); |
| 59 | + await Assert.That(chip.IsDeletable).IsFalse(); |
| 60 | + |
| 61 | + // Assert setting works |
| 62 | + chip.IsDeletable = true; |
| 63 | + await Assert.That(chip.IsDeletable).IsTrue(); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public async Task TestDeleteCommandProperty() |
| 68 | + { |
| 69 | + Chip chip = new(); |
| 70 | + |
| 71 | + // Assert defaults |
| 72 | + await Assert.That(Chip.DeleteCommandProperty.Name).IsEqualTo("DeleteCommand"); |
| 73 | + await Assert.That(chip.DeleteCommand).IsNull(); |
| 74 | + |
| 75 | + // Assert setting works |
| 76 | + var command = new TestCommand(); |
| 77 | + chip.DeleteCommand = command; |
| 78 | + await Assert.That(chip.DeleteCommand).IsEqualTo(command); |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public async Task TestDeleteCommandParameterProperty() |
| 83 | + { |
| 84 | + Chip chip = new(); |
| 85 | + |
| 86 | + // Assert defaults |
| 87 | + await Assert.That(Chip.DeleteCommandParameterProperty.Name).IsEqualTo("DeleteCommandParameter"); |
| 88 | + await Assert.That(chip.DeleteCommandParameter).IsNull(); |
| 89 | + |
| 90 | + // Assert setting works |
| 91 | + var parameter = new object(); |
| 92 | + chip.DeleteCommandParameter = parameter; |
| 93 | + await Assert.That(chip.DeleteCommandParameter).IsEqualTo(parameter); |
| 94 | + } |
| 95 | + |
| 96 | + [Test] |
| 97 | + public async Task TestDeleteToolTipProperty() |
| 98 | + { |
| 99 | + Chip chip = new(); |
| 100 | + |
| 101 | + // Assert defaults |
| 102 | + await Assert.That(Chip.DeleteToolTipProperty.Name).IsEqualTo("DeleteToolTip"); |
| 103 | + await Assert.That(chip.DeleteToolTip).IsNull(); |
| 104 | + |
| 105 | + // Assert setting works |
| 106 | + chip.DeleteToolTip = "Delete this chip"; |
| 107 | + await Assert.That(chip.DeleteToolTip).IsEqualTo("Delete this chip"); |
| 108 | + } |
| 109 | + |
| 110 | + [Test] |
| 111 | + public async Task DeleteClickEvent_WhenDeleteButtonClicked_EventIsRaised() |
| 112 | + { |
| 113 | + Chip chip = new(); |
| 114 | + chip.ApplyDefaultStyle(); |
| 115 | + List<RoutedEventArgs> invocations = new(); |
| 116 | + |
| 117 | + chip.DeleteClick += OnDeleteClick; |
| 118 | + |
| 119 | + void OnDeleteClick(object? sender, RoutedEventArgs e) |
| 120 | + => invocations.Add(e); |
| 121 | + |
| 122 | + // Find and click the delete button |
| 123 | + var deleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 124 | + await Assert.That(deleteButton).IsNotNull(); |
| 125 | + |
| 126 | + deleteButton!.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, deleteButton)); |
| 127 | + |
| 128 | + await Assert.That(invocations.Count).IsEqualTo(1); |
| 129 | + await Assert.That(invocations[0].Source).IsEqualTo(chip); |
| 130 | + } |
| 131 | + |
| 132 | + [Test] |
| 133 | + public async Task DeleteCommand_WhenDeleteButtonClicked_CommandIsExecuted() |
| 134 | + { |
| 135 | + Chip chip = new(); |
| 136 | + chip.ApplyDefaultStyle(); |
| 137 | + var command = new TestCommand(); |
| 138 | + var parameter = "test-parameter"; |
| 139 | + |
| 140 | + chip.DeleteCommand = command; |
| 141 | + chip.DeleteCommandParameter = parameter; |
| 142 | + |
| 143 | + // Find and click the delete button |
| 144 | + var deleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 145 | + await Assert.That(deleteButton).IsNotNull(); |
| 146 | + |
| 147 | + deleteButton!.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, deleteButton)); |
| 148 | + |
| 149 | + await Assert.That(command.ExecuteCount).IsEqualTo(1); |
| 150 | + await Assert.That(command.LastParameter).IsEqualTo(parameter); |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public async Task DeleteCommand_WhenCanExecuteReturnsFalse_CommandIsNotExecuted() |
| 155 | + { |
| 156 | + Chip chip = new(); |
| 157 | + chip.ApplyDefaultStyle(); |
| 158 | + var command = new TestCommand { CanExecuteResult = false }; |
| 159 | + |
| 160 | + chip.DeleteCommand = command; |
| 161 | + |
| 162 | + // Find and click the delete button |
| 163 | + var deleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 164 | + await Assert.That(deleteButton).IsNotNull(); |
| 165 | + |
| 166 | + deleteButton!.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, deleteButton)); |
| 167 | + |
| 168 | + await Assert.That(command.ExecuteCount).IsEqualTo(0); |
| 169 | + } |
| 170 | + |
| 171 | + [Test] |
| 172 | + public async Task OnApplyTemplate_WhenCalledMultipleTimes_UnsubscribesFromPreviousDeleteButton() |
| 173 | + { |
| 174 | + Chip chip = new(); |
| 175 | + chip.ApplyDefaultStyle(); |
| 176 | + |
| 177 | + var firstDeleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 178 | + await Assert.That(firstDeleteButton).IsNotNull(); |
| 179 | + |
| 180 | + // Re-apply template |
| 181 | + chip.OnApplyTemplate(); |
| 182 | + |
| 183 | + // Verify no exceptions occur and the chip still works |
| 184 | + var command = new TestCommand(); |
| 185 | + chip.DeleteCommand = command; |
| 186 | + |
| 187 | + var deleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 188 | + deleteButton!.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, deleteButton)); |
| 189 | + |
| 190 | + await Assert.That(command.ExecuteCount).IsEqualTo(1); |
| 191 | + } |
| 192 | + |
| 193 | + [Test] |
| 194 | + public async Task DeleteClick_WhenNoDeleteCommand_EventStillRaised() |
| 195 | + { |
| 196 | + Chip chip = new(); |
| 197 | + chip.ApplyDefaultStyle(); |
| 198 | + List<RoutedEventArgs> invocations = new(); |
| 199 | + |
| 200 | + chip.DeleteClick += OnDeleteClick; |
| 201 | + |
| 202 | + void OnDeleteClick(object? sender, RoutedEventArgs e) |
| 203 | + => invocations.Add(e); |
| 204 | + |
| 205 | + // Ensure no command is set |
| 206 | + chip.DeleteCommand = null; |
| 207 | + |
| 208 | + // Find and click the delete button |
| 209 | + var deleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 210 | + deleteButton!.RaiseEvent(new RoutedEventArgs(Button.ClickEvent, deleteButton)); |
| 211 | + |
| 212 | + // Event should still be raised even without a command |
| 213 | + await Assert.That(invocations.Count).IsEqualTo(1); |
| 214 | + } |
| 215 | + |
| 216 | + [Test] |
| 217 | + public async Task Content_CanBeSetAndRetrieved() |
| 218 | + { |
| 219 | + Chip chip = new(); |
| 220 | + |
| 221 | + var content = "Test Chip"; |
| 222 | + chip.Content = content; |
| 223 | + |
| 224 | + await Assert.That(chip.Content).IsEqualTo(content); |
| 225 | + } |
| 226 | + |
| 227 | + [Test] |
| 228 | + public async Task IsDeletable_WhenFalse_DeleteButtonShouldBeHidden() |
| 229 | + { |
| 230 | + Chip chip = new(); |
| 231 | + chip.ApplyDefaultStyle(); |
| 232 | + chip.IsDeletable = false; |
| 233 | + |
| 234 | + // The delete button should still exist in the template but visibility is controlled by style |
| 235 | + var deleteButton = chip.FindVisualChild<Button>(Chip.DeleteButtonPartName); |
| 236 | + await Assert.That(deleteButton).IsNotNull(); |
| 237 | + } |
| 238 | + |
| 239 | + // Helper class for testing ICommand |
| 240 | + private class TestCommand : ICommand |
| 241 | + { |
| 242 | + public int ExecuteCount { get; private set; } |
| 243 | + public object? LastParameter { get; private set; } |
| 244 | + public bool CanExecuteResult { get; set; } = true; |
| 245 | + |
| 246 | + public event EventHandler? CanExecuteChanged; |
| 247 | + |
| 248 | + public bool CanExecute(object? parameter) |
| 249 | + { |
| 250 | + return CanExecuteResult; |
| 251 | + } |
| 252 | + |
| 253 | + public void Execute(object? parameter) |
| 254 | + { |
| 255 | + ExecuteCount++; |
| 256 | + LastParameter = parameter; |
| 257 | + } |
| 258 | + |
| 259 | + public void RaiseCanExecuteChanged() |
| 260 | + { |
| 261 | + CanExecuteChanged?.Invoke(this, EventArgs.Empty); |
| 262 | + } |
| 263 | + } |
| 264 | +} |
0 commit comments