|
| 1 | +namespace MaterialDesignThemes.UITests.WPF.Chips; |
| 2 | + |
| 3 | +public class ChipTests : TestBase |
| 4 | +{ |
| 5 | + [Test] |
| 6 | + public async Task Chip_OnLoad_RendersCorrectly() |
| 7 | + { |
| 8 | + await using var recorder = new TestRecorder(App); |
| 9 | + |
| 10 | + //Arrange |
| 11 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(@" |
| 12 | + <materialDesign:Chip Content=""Test Chip"" /> |
| 13 | + "); |
| 14 | + |
| 15 | + //Act |
| 16 | + string? content = await chip.GetContent(); |
| 17 | + |
| 18 | + //Assert |
| 19 | + await Assert.That(content).IsEqualTo("Test Chip"); |
| 20 | + |
| 21 | + recorder.Success(); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public async Task Chip_WithIcon_DisplaysIcon() |
| 26 | + { |
| 27 | + await using var recorder = new TestRecorder(App); |
| 28 | + |
| 29 | + //Arrange |
| 30 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(""" |
| 31 | + <materialDesign:Chip Content="Chip with Icon"> |
| 32 | + <materialDesign:Chip.Icon> |
| 33 | + <materialDesign:PackIcon x:Name="ChipIcon" Kind="Home" /> |
| 34 | + </materialDesign:Chip.Icon> |
| 35 | + </materialDesign:Chip> |
| 36 | + """); |
| 37 | + |
| 38 | + //Act |
| 39 | + IVisualElement<PackIcon> icon = await chip.GetElement<PackIcon>("ChipIcon"); |
| 40 | + |
| 41 | + //Assert |
| 42 | + await Assert.That(icon).IsNotNull(); |
| 43 | + await Wait.For(async () => await icon.GetIsVisible()); |
| 44 | + |
| 45 | + recorder.Success(); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public async Task Chip_WhenIsDeletableTrue_ShowsDeleteButton() |
| 50 | + { |
| 51 | + await using var recorder = new TestRecorder(App); |
| 52 | + |
| 53 | + //Arrange |
| 54 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(@" |
| 55 | + <materialDesign:Chip Content=""Deletable Chip"" IsDeletable=""True"" /> |
| 56 | + "); |
| 57 | + |
| 58 | + //Act |
| 59 | + IVisualElement<Button> deleteButton = await chip.GetElement<Button>("PART_DeleteButton"); |
| 60 | + |
| 61 | + //Assert |
| 62 | + await Assert.That(deleteButton).IsNotNull(); |
| 63 | + await Wait.For(async () => await deleteButton.GetIsVisible()); |
| 64 | + |
| 65 | + recorder.Success(); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public async Task Chip_WhenIsDeletableFalse_HidesDeleteButton() |
| 70 | + { |
| 71 | + await using var recorder = new TestRecorder(App); |
| 72 | + |
| 73 | + //Arrange |
| 74 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(@" |
| 75 | + <materialDesign:Chip Content=""Non-Deletable Chip"" IsDeletable=""False"" /> |
| 76 | + "); |
| 77 | + |
| 78 | + //Act |
| 79 | + IVisualElement<Button> deleteButton = await chip.GetElement<Button>("PART_DeleteButton"); |
| 80 | + bool isVisible = await deleteButton.GetIsVisible(); |
| 81 | + |
| 82 | + //Assert |
| 83 | + await Assert.That(isVisible).IsFalse(); |
| 84 | + |
| 85 | + recorder.Success(); |
| 86 | + } |
| 87 | + |
| 88 | + [Test] |
| 89 | + public async Task Chip_ClickingDeleteButton_RaisesDeleteClickEvent() |
| 90 | + { |
| 91 | + await using var recorder = new TestRecorder(App); |
| 92 | + |
| 93 | + //Arrange |
| 94 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(@" |
| 95 | + <materialDesign:Chip Content=""Deletable Chip"" IsDeletable=""True"" /> |
| 96 | + "); |
| 97 | + |
| 98 | + IEventRegistration deleteClickEvent = await chip.RegisterForEvent(nameof(Chip.DeleteClick)); |
| 99 | + IVisualElement<Button> deleteButton = await chip.GetElement<Button>("PART_DeleteButton"); |
| 100 | + |
| 101 | + //Act |
| 102 | + await deleteButton.LeftClick(); |
| 103 | + await Task.Delay(50, TestContext.Current!.CancellationToken); |
| 104 | + |
| 105 | + //Assert |
| 106 | + var invocations = await deleteClickEvent.GetInvocations(); |
| 107 | + await Assert.That(invocations.Count).IsEqualTo(1); |
| 108 | + |
| 109 | + recorder.Success(); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + public async Task Chip_WithDeleteCommand_ExecutesCommandOnDelete() |
| 114 | + { |
| 115 | + await using var recorder = new TestRecorder(App); |
| 116 | + |
| 117 | + //Arrange |
| 118 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(""" |
| 119 | + <materialDesign:Chip x:Name="TestChip" Content="Chip with Command" IsDeletable="True"> |
| 120 | + <materialDesign:Chip.DeleteCommand> |
| 121 | + <Binding Path="DeleteCommand" /> |
| 122 | + </materialDesign:Chip.DeleteCommand> |
| 123 | + </materialDesign:Chip> |
| 124 | + """); |
| 125 | + |
| 126 | + // Set up a command in the DataContext |
| 127 | + await chip.SetProperty(nameof(FrameworkElement.DataContext), new TestViewModel()); |
| 128 | + |
| 129 | + IVisualElement<Button> deleteButton = await chip.GetElement<Button>("PART_DeleteButton"); |
| 130 | + |
| 131 | + //Act |
| 132 | + await deleteButton.LeftClick(); |
| 133 | + await Task.Delay(50, TestContext.Current!.CancellationToken); |
| 134 | + |
| 135 | + //Assert |
| 136 | + var dataContext = await chip.GetProperty<TestViewModel>(nameof(FrameworkElement.DataContext)); |
| 137 | + await Assert.That(dataContext.DeleteCommandExecuted).IsTrue(); |
| 138 | + |
| 139 | + recorder.Success(); |
| 140 | + } |
| 141 | + |
| 142 | + [Test] |
| 143 | + public async Task Chip_WithIconBackground_AppliesCorrectBrush() |
| 144 | + { |
| 145 | + await using var recorder = new TestRecorder(App); |
| 146 | + |
| 147 | + //Arrange |
| 148 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(""" |
| 149 | + <materialDesign:Chip Content="Chip" IconBackground="Red"> |
| 150 | + <materialDesign:Chip.Icon> |
| 151 | + <materialDesign:PackIcon Kind="Home" /> |
| 152 | + </materialDesign:Chip.Icon> |
| 153 | + </materialDesign:Chip> |
| 154 | + """); |
| 155 | + |
| 156 | + //Act |
| 157 | + var iconBackground = await chip.GetIconBackground(); |
| 158 | + |
| 159 | + //Assert |
| 160 | + await Assert.That(iconBackground).IsNotNull(); |
| 161 | + |
| 162 | + recorder.Success(); |
| 163 | + } |
| 164 | + |
| 165 | + [Test] |
| 166 | + public async Task Chip_WithIconForeground_AppliesCorrectBrush() |
| 167 | + { |
| 168 | + await using var recorder = new TestRecorder(App); |
| 169 | + |
| 170 | + //Arrange |
| 171 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(""" |
| 172 | + <materialDesign:Chip Content="Chip" IconForeground="Blue"> |
| 173 | + <materialDesign:Chip.Icon> |
| 174 | + <materialDesign:PackIcon Kind="Home" /> |
| 175 | + </materialDesign:Chip.Icon> |
| 176 | + </materialDesign:Chip> |
| 177 | + """); |
| 178 | + |
| 179 | + //Act |
| 180 | + var iconForeground = await chip.GetIconForeground(); |
| 181 | + |
| 182 | + //Assert |
| 183 | + await Assert.That(iconForeground).IsNotNull(); |
| 184 | + |
| 185 | + recorder.Success(); |
| 186 | + } |
| 187 | + |
| 188 | + [Test] |
| 189 | + public async Task Chip_WithDeleteToolTip_ShowsToolTipOnDeleteButton() |
| 190 | + { |
| 191 | + await using var recorder = new TestRecorder(App); |
| 192 | + |
| 193 | + //Arrange |
| 194 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(@" |
| 195 | + <materialDesign:Chip Content=""Chip"" IsDeletable=""True"" DeleteToolTip=""Remove this chip"" /> |
| 196 | + "); |
| 197 | + |
| 198 | + //Act |
| 199 | + IVisualElement<Button> deleteButton = await chip.GetElement<Button>("PART_DeleteButton"); |
| 200 | + var toolTip = await deleteButton.GetToolTip(); |
| 201 | + |
| 202 | + //Assert |
| 203 | + await Assert.That(toolTip).IsEqualTo("Remove this chip"); |
| 204 | + |
| 205 | + recorder.Success(); |
| 206 | + } |
| 207 | + |
| 208 | + [Test] |
| 209 | + public async Task Chip_ClickingChip_RaisesClickEvent() |
| 210 | + { |
| 211 | + await using var recorder = new TestRecorder(App); |
| 212 | + |
| 213 | + //Arrange |
| 214 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(@" |
| 215 | + <materialDesign:Chip Content=""Clickable Chip"" /> |
| 216 | + "); |
| 217 | + |
| 218 | + IEventRegistration clickEvent = await chip.RegisterForEvent(ButtonBase.ClickEvent.Name); |
| 219 | + |
| 220 | + //Act |
| 221 | + await chip.LeftClick(); |
| 222 | + await Task.Delay(50, TestContext.Current!.CancellationToken); |
| 223 | + |
| 224 | + //Assert |
| 225 | + var invocations = await clickEvent.GetInvocations(); |
| 226 | + await Assert.That(invocations.Count).IsEqualTo(1); |
| 227 | + |
| 228 | + recorder.Success(); |
| 229 | + } |
| 230 | + |
| 231 | + [Test] |
| 232 | + public async Task Chip_WithDeleteCommandParameter_PassesParameterToCommand() |
| 233 | + { |
| 234 | + await using var recorder = new TestRecorder(App); |
| 235 | + |
| 236 | + //Arrange |
| 237 | + IVisualElement<Chip> chip = await LoadXaml<Chip>(""" |
| 238 | + <materialDesign:Chip Content="Chip" IsDeletable="True" DeleteCommandParameter="TestParameter"> |
| 239 | + <materialDesign:Chip.DeleteCommand> |
| 240 | + <Binding Path="DeleteCommand" /> |
| 241 | + </materialDesign:Chip.DeleteCommand> |
| 242 | + </materialDesign:Chip> |
| 243 | + """); |
| 244 | + |
| 245 | + await chip.SetProperty(nameof(FrameworkElement.DataContext), new TestViewModel()); |
| 246 | + IVisualElement<Button> deleteButton = await chip.GetElement<Button>("PART_DeleteButton"); |
| 247 | + |
| 248 | + //Act |
| 249 | + await deleteButton.LeftClick(); |
| 250 | + await Task.Delay(50, TestContext.Current!.CancellationToken); |
| 251 | + |
| 252 | + //Assert |
| 253 | + var dataContext = await chip.GetProperty<TestViewModel>(nameof(FrameworkElement.DataContext)); |
| 254 | + await Assert.That(dataContext.DeleteCommandParameter).IsEqualTo("TestParameter"); |
| 255 | + |
| 256 | + recorder.Success(); |
| 257 | + } |
| 258 | + |
| 259 | + // Helper class for command testing |
| 260 | + [Serializable] |
| 261 | + public class TestViewModel |
| 262 | + { |
| 263 | + public bool DeleteCommandExecuted { get; private set; } |
| 264 | + public object? DeleteCommandParameter { get; private set; } |
| 265 | + |
| 266 | + public ICommand DeleteCommand => new RelayCommand(Execute); |
| 267 | + |
| 268 | + private void Execute(object? parameter) |
| 269 | + { |
| 270 | + DeleteCommandExecuted = true; |
| 271 | + DeleteCommandParameter = parameter; |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + [Serializable] |
| 276 | + public class RelayCommand : ICommand |
| 277 | + { |
| 278 | + private readonly Action<object?> _execute; |
| 279 | + |
| 280 | + public RelayCommand(Action<object?> execute) |
| 281 | + { |
| 282 | + _execute = execute; |
| 283 | + } |
| 284 | + |
| 285 | + public event EventHandler? CanExecuteChanged; |
| 286 | + |
| 287 | + public bool CanExecute(object? parameter) => true; |
| 288 | + |
| 289 | + public void Execute(object? parameter) => _execute(parameter); |
| 290 | + } |
| 291 | +} |
0 commit comments