Skip to content

Commit f4a2c65

Browse files
committed
Adds cancellation tokens to async delays
Ensures that asynchronous delay calls use cancellation tokens to allow for tests to be cancelled correctly.
1 parent 4cd3fbb commit f4a2c65

File tree

16 files changed

+110
-110
lines changed

16 files changed

+110
-110
lines changed

tests/MaterialDesignThemes.UITests/Samples/Validation/ValidationUpdates.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private async Task CauseErrors()
4646
{
4747
Error = "Some error";
4848
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Text)));
49-
await Task.Delay(100);
49+
await Task.Delay(100, TestContext.Current!.CancellationToken);
5050
Error += " + more";
5151
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(nameof(Text)));
5252
}

tests/MaterialDesignThemes.UITests/WPF/AutoSuggestBoxes/AutoSuggestTextBoxTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ await Wait.For(async () =>
6363
lastHeight = currentHeight;
6464
if (!rv)
6565
{
66-
await Task.Delay(100);
66+
await Task.Delay(100, TestContext.Current!.CancellationToken);
6767
}
6868
return rv;
6969
});
@@ -74,7 +74,7 @@ await Wait.For(async () =>
7474
await bananas.LeftClick();
7575

7676
// Wait for the text to be updated
77-
await Task.Delay(50);
77+
await Task.Delay(50, TestContext.Current!.CancellationToken);
7878

7979
var suggestBoxText = await suggestBox.GetText();
8080
//Validate that the current text is the same as the selected item
@@ -136,13 +136,13 @@ public async Task AutoSuggestBox_MovesFocusToNextElement_WhenPopupIsClosed()
136136

137137
// Act
138138
await suggestBox.MoveKeyboardFocus();
139-
await Task.Delay(50);
139+
await Task.Delay(50, TestContext.Current!.CancellationToken);
140140
await suggestBox.SendInput(new KeyboardInput("B")); // Open the popup
141-
await Task.Delay(50);
141+
await Task.Delay(50, TestContext.Current.CancellationToken);
142142
await suggestBox.SendInput(new KeyboardInput(Key.Escape)); // Close the popup
143-
await Task.Delay(50);
143+
await Task.Delay(50, TestContext.Current.CancellationToken);
144144
await suggestBox.SendInput(new KeyboardInput(Key.Tab)); // Press TAB to focus the next element
145-
await Task.Delay(50);
145+
await Task.Delay(50, TestContext.Current.CancellationToken);
146146

147147
// Assert
148148
await Assert.That(await suggestBox.GetIsFocused()).IsFalse();
@@ -151,7 +151,7 @@ public async Task AutoSuggestBox_MovesFocusToNextElement_WhenPopupIsClosed()
151151
recorder.Success();
152152
}
153153

154-
[Fact]
154+
[Test]
155155
[Description("Issue 3815")]
156156
public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
157157
{
@@ -167,7 +167,7 @@ public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
167167
//Act & Assert
168168
await suggestBox.MoveKeyboardFocus();
169169
await suggestBox.SendInput(new KeyboardInput("e"));
170-
await Task.Delay(delay);
170+
await Task.Delay(delay, TestContext.Current!.CancellationToken);
171171

172172
static int? GetSuggestionCount(AutoSuggestBox autoSuggestBox)
173173
{
@@ -179,17 +179,17 @@ public async Task AutoSuggestBox_KeysUpAndDown_WrapAround()
179179

180180
//Assert that initially the first item is selected
181181
int selectedIndex = await suggestionListBox.GetSelectedIndex();
182-
Assert.Equal(0, selectedIndex);
183-
await Task.Delay(delay);
182+
await Assert.That(selectedIndex).IsEqualTo(0);
183+
await Task.Delay(delay, TestContext.Current.CancellationToken);
184184

185185
//Assert that the last item is selected after pressing ArrowUp
186186
await suggestBox.SendInput(new KeyboardInput(Key.Up));
187-
Assert.Equal(itemCount - 1, await suggestionListBox.GetSelectedIndex());
188-
await Task.Delay(delay);
187+
await Assert.That(await suggestionListBox.GetSelectedIndex()).IsEqualTo(itemCount - 1);
188+
await Task.Delay(delay, TestContext.Current.CancellationToken);
189189

190190
//Assert that the first item is selected after pressing ArrowDown
191191
await suggestBox.SendInput(new KeyboardInput(Key.Down));
192-
Assert.Equal(0, await suggestionListBox.GetSelectedIndex());
192+
await Assert.That(await suggestionListBox.GetSelectedIndex()).IsEqualTo(0);
193193
}
194194

195195
[Fact]

tests/MaterialDesignThemes.UITests/WPF/ColorPickerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task OnLostFocusIfSelectedTimeIsNull_DatePartWillBeToday()
2424
await thumb.SendInput(MouseInput.MoveRelative(xOffset: -5, yOffset: -10));
2525
await thumb.SendInput(MouseInput.LeftDown());
2626
await thumb.SendInput(MouseInput.MoveRelative(yOffset: 25));
27-
await Task.Delay(100);
27+
await Task.Delay(100, TestContext.Current!.CancellationToken);
2828
await thumb.SendInput(MouseInput.LeftUp());
2929

3030

tests/MaterialDesignThemes.UITests/WPF/ComboBoxes/ComboBoxTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ public async Task OnEditableComboBox_ClickInTextArea_FocusesTextBox()
142142

143143
// Open the ComboBox initially
144144
await comboBox.LeftClick(Position.RightCenter);
145-
await Task.Delay(50); // Allow a little time for the drop-down to open (and property to change)
145+
await Task.Delay(50, TestContext.Current!.CancellationToken); // Allow a little time for the drop-down to open (and property to change)
146146
bool wasOpenAfterClickOnToggleButton = await comboBox.GetIsDropDownOpen();
147147

148148
// Focus (i.e. click) another element
149149
await button.LeftClick();
150150

151151
// Click the editable TextBox of the ComboBox
152152
await editableTextBox.LeftClick();
153-
await Task.Delay(50); // Allow a little time for the drop-down to open (and property to change)
153+
await Task.Delay(50, TestContext.Current.CancellationToken); // Allow a little time for the drop-down to open (and property to change)
154154
bool wasOpenAfterClickOnEditableTextBox = await comboBox.GetIsDropDownOpen();
155155
bool textBoxHasFocus = await editableTextBox.GetIsFocused();
156156
bool textBoxHasKeyboardFocus = await editableTextBox.GetIsKeyboardFocused();

tests/MaterialDesignThemes.UITests/WPF/DatePickers/DatePickerTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public async Task OnDatePicker_WithClearButton_ClearsSelectedUncommittedText()
7878

7979
// Act
8080
await clearButton.LeftClick();
81-
await Task.Delay(50);
81+
await Task.Delay(50, TestContext.Current!.CancellationToken);
8282

8383
// Assert
8484
await Assert.That(await datePickerTextBox.GetText()).IsNull();
@@ -118,18 +118,18 @@ public async Task OutlinedDatePicker_RespectsActiveAndInactiveBorderThickness_Wh
118118

119119
// Act
120120
await button.MoveCursorTo();
121-
await Task.Delay(50); // Wait for the visual change
121+
await Task.Delay(50, TestContext.Current!.CancellationToken); // Wait for the visual change
122122
var inactiveBorderThickness = await textBoxOuterBorder.GetBorderThickness();
123123
await datePickerTextBox.MoveCursorTo();
124-
await Task.Delay(50); // Wait for the visual change
124+
await Task.Delay(50, TestContext.Current.CancellationToken); // Wait for the visual change
125125
var hoverBorderThickness = await textBoxOuterBorder.GetBorderThickness();
126126
await datePickerTextBox.LeftClick();
127-
await Task.Delay(50); // Wait for the visual change
127+
await Task.Delay(50, TestContext.Current.CancellationToken); // Wait for the visual change
128128
var focusedBorderThickness = await textBoxOuterBorder.GetBorderThickness();
129129

130130
// TODO: It would be cool if a validation error could be set via XAMLTest without the need for the Binding and ValidationRules elements in the XAML above.
131131
await datePicker.SetProperty(DatePicker.SelectedDateProperty, DateTime.Now);
132-
await Task.Delay(50); // Wait for the visual change
132+
await Task.Delay(50, TestContext.Current.CancellationToken); // Wait for the visual change
133133
var withErrorBorderThickness = await textBoxOuterBorder.GetBorderThickness();
134134

135135
// Assert
@@ -319,10 +319,10 @@ public async Task DatePicker_ShouldApplyIsMouseOverTriggers_WhenHoveringCalendar
319319

320320
// Act
321321
await datePickerTextBoxBorder.MoveCursorTo();
322-
await Task.Delay(50);
322+
await Task.Delay(50, TestContext.Current!.CancellationToken);
323323
var datePickerTextBoxHoverThickness = await datePickerTextBoxBorder.GetBorderThickness();
324324
await datePickerTimeButton.MoveCursorTo();
325-
await Task.Delay(50);
325+
await Task.Delay(50, TestContext.Current.CancellationToken);
326326
var datePickerCalendarButtonHoverThickness = await datePickerTextBoxBorder.GetBorderThickness();
327327

328328
// Assert

tests/MaterialDesignThemes.UITests/WPF/DialogHosts/DialogHostTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public async Task OnOpenDialog_OverlayCoversContent()
3232

3333
await testOverlayButton.LeftClick();
3434
await Wait.For(async () => await resultTextBlock.GetText() == "Clicks: 1");
35-
await Task.Delay(200);
35+
await Task.Delay(200, TestContext.Current!.CancellationToken);
3636
await closeDialogButton.LeftClick();
3737

3838
var retry = new Retry(5, TimeSpan.FromSeconds(5));
@@ -67,7 +67,7 @@ public async Task ClosingDialogWithIsOpenProperty_ShouldRaiseDialogClosingEvent(
6767

6868
await showButton.LeftClick();
6969
await Wait.For(async () => await closeButton.GetIsVisible());
70-
await Task.Delay(300);
70+
await Task.Delay(300, TestContext.Current!.CancellationToken);
7171
await closeButton.LeftClick();
7272

7373
await Wait.For(async () =>
@@ -109,7 +109,7 @@ public async Task FontSettingsShouldInheritIntoDialog()
109109

110110
await showButton1.LeftClick();
111111
await showButton2.LeftClick();
112-
await Task.Delay(300);
112+
await Task.Delay(300, TestContext.Current!.CancellationToken);
113113

114114
var text1 = await grid.GetElement<TextBlock>("TextBlock1");
115115
var text2 = await grid.GetElement<TextBlock>("TextBlock2");
@@ -373,14 +373,14 @@ public async Task DialogHost_ChangesSelectedTabItem_DoesNotPerformTabChangeWhenR
373373
// Open menu
374374
IVisualElement<MenuItem> menuItem1 = await rootGrid.GetElement<MenuItem>("MenuItem1");
375375
await menuItem1.LeftClick();
376-
await Task.Delay(1000); // Wait for menu to open
376+
await Task.Delay(1000, TestContext.Current!.CancellationToken); // Wait for menu to open
377377
IVisualElement<MenuItem> menuItem2 = await rootGrid.GetElement<MenuItem>("MenuItem2");
378378
await menuItem2.LeftClick();
379-
await Task.Delay(1000); // Wait for dialog content to show
379+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to show
380380

381381
// Click navigate button
382382
await navigateHomeButton.LeftClick();
383-
await Task.Delay(1000); // Wait for dialog content to close
383+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to close
384384

385385
await Assert.That(await tabItem1.GetIsSelected()).IsTrue();
386386
await Assert.That(await tabItem2.GetIsSelected()).IsFalse();
@@ -405,14 +405,14 @@ public async Task DialogHost_ChangesSelectedRailItem_DoesNotPerformRailChangeWhe
405405
// Open menu
406406
IVisualElement<MenuItem> menuItem1 = await rootGrid.GetElement<MenuItem>("MenuItem1");
407407
await menuItem1.LeftClick();
408-
await Task.Delay(1000); // Wait for menu to open
408+
await Task.Delay(1000, TestContext.Current!.CancellationToken); // Wait for menu to open
409409
IVisualElement<MenuItem> menuItem2 = await rootGrid.GetElement<MenuItem>("MenuItem2");
410410
await menuItem2.LeftClick();
411-
await Task.Delay(1000); // Wait for dialog content to show
411+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to show
412412

413413
// Click navigate button
414414
await navigateHomeButton.LeftClick();
415-
await Task.Delay(1000); // Wait for dialog content to close
415+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to close
416416

417417
await Assert.That(await railItem1.GetIsSelected()).IsTrue();
418418
await Assert.That(await railItem2.GetIsSelected()).IsFalse();
@@ -437,14 +437,14 @@ public async Task DialogHost_ChangesSelectedTabItem_DoesNotPerformTabChangeWhenR
437437
// Open menu
438438
IVisualElement<MenuItem> menuItem1 = await rootGrid.GetElement<MenuItem>("MenuItem1");
439439
await menuItem1.LeftClick();
440-
await Task.Delay(1000); // Wait for menu to open
440+
await Task.Delay(1000, TestContext.Current!.CancellationToken); // Wait for menu to open
441441
IVisualElement<MenuItem> menuItem2 = await rootGrid.GetElement<MenuItem>("MenuItem2");
442442
await menuItem2.LeftClick();
443-
await Task.Delay(1000); // Wait for dialog content to show
443+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to show
444444

445445
// Click navigate button
446446
await navigateHomeButton.LeftClick();
447-
await Task.Delay(1000); // Wait for dialog content to close
447+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to close
448448

449449
await Assert.That(await tabItem1.GetIsSelected()).IsTrue();
450450
await Assert.That(await tabItem2.GetIsSelected()).IsFalse();
@@ -469,14 +469,14 @@ public async Task DialogHost_ChangesSelectedRailItem_DoesNotPerformRailChangeWhe
469469
// Open menu
470470
IVisualElement<MenuItem> menuItem1 = await rootGrid.GetElement<MenuItem>("MenuItem1");
471471
await menuItem1.LeftClick();
472-
await Task.Delay(1000); // Wait for menu to open
472+
await Task.Delay(1000, TestContext.Current!.CancellationToken); // Wait for menu to open
473473
IVisualElement<MenuItem> menuItem2 = await rootGrid.GetElement<MenuItem>("MenuItem2");
474474
await menuItem2.LeftClick();
475-
await Task.Delay(1000); // Wait for dialog content to show
475+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to show
476476

477477
// Click navigate button
478478
await navigateHomeButton.LeftClick();
479-
await Task.Delay(1000); // Wait for dialog content to close
479+
await Task.Delay(1000, TestContext.Current.CancellationToken); // Wait for dialog content to close
480480

481481
await Assert.That(await railItem1.GetIsSelected()).IsTrue();
482482
await Assert.That(await railItem2.GetIsSelected()).IsFalse();
@@ -493,7 +493,7 @@ public async Task DialogHost_WithComboBox_CanSelectItem()
493493
IVisualElement dialogHost = await LoadUserControl<WithComboBox>();
494494

495495
var comboBox = await dialogHost.GetElement<ComboBox>("TargetedPlatformComboBox");
496-
await Task.Delay(500);
496+
await Task.Delay(500, TestContext.Current!.CancellationToken);
497497
await comboBox.LeftClick();
498498

499499
var item = await Wait.For(() => comboBox.GetElement<ComboBoxItem>("TargetItem"));

tests/MaterialDesignThemes.UITests/WPF/DrawerHosts/DrawerHostTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public async Task DrawerHost_OpenAndClose_RaisesEvents()
3535

3636
await toggleButton.LeftClick();
3737
//Allow for animations to start
38-
await Task.Delay(10);
38+
await Task.Delay(10, TestContext.Current!.CancellationToken);
3939

4040
await Wait.For(async () =>
4141
{
@@ -50,7 +50,7 @@ await Wait.For(async () =>
5050
});
5151

5252
//Wait before clicking so the animations have time to finish
53-
await Task.Delay(100);
53+
await Task.Delay(100, TestContext.Current.CancellationToken);
5454

5555
await drawerHost.LeftClick();
5656

@@ -63,7 +63,7 @@ await Wait.For(async () =>
6363

6464
await showButton.LeftClick();
6565
//Allow for animations to start
66-
await Task.Delay(10);
66+
await Task.Delay(10, TestContext.Current.CancellationToken);
6767

6868
await Wait.For(async () =>
6969
{
@@ -98,26 +98,26 @@ public async Task DrawerHost_CancelingClosingEvent_DrawerStaysOpen()
9898

9999
await showButton.LeftClick();
100100
//Allow open animation to finish
101-
await Task.Delay(300);
101+
await Task.Delay(300, TestContext.Current!.CancellationToken);
102102
await Wait.For(async () => (await openedEvent.GetInvocations()).Count == 1);
103103

104104
var closingEvent = await drawerHost.RegisterForEvent(nameof(DrawerHost.DrawerClosing));
105105

106106
//Attempt closing with routed command
107107
await closeButton.LeftClick();
108-
await Task.Delay(100);
108+
await Task.Delay(100, TestContext.Current.CancellationToken);
109109
await Wait.For(async () => (await closingEvent.GetInvocations()).Count == 1);
110110
await Assert.That(await drawerHost.GetIsLeftDrawerOpen()).IsTrue();
111111

112112
//Attempt closing with click away
113113
await drawerHost.LeftClick();
114-
await Task.Delay(100);
114+
await Task.Delay(100, TestContext.Current.CancellationToken);
115115
await Wait.For(async () => (await closingEvent.GetInvocations()).Count == 2);
116116
await Assert.That(await drawerHost.GetIsLeftDrawerOpen()).IsTrue();
117117

118118
//Attempt closing with DP property toggle
119119
await closeButtonDp.LeftClick();
120-
await Task.Delay(100);
120+
await Task.Delay(100, TestContext.Current.CancellationToken);
121121
await Wait.For(async () => (await closingEvent.GetInvocations()).Count == 3);
122122
await Assert.That(await drawerHost.GetIsLeftDrawerOpen()).IsTrue();
123123

tests/MaterialDesignThemes.UITests/WPF/PasswordBoxes/PasswordBoxTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public async Task RevealPasswordBox_WithBoundPasswordProperty_RespectsThreeWayBi
101101
string? clearTextPassword1 = await clearTextPasswordTextBox.GetProperty<string>(TextBox.TextProperty);
102102

103103
// Act 2 (Update in RevealPasswordTextBox updates PasswordBox and VM)
104-
await Task.Delay(50);
104+
await Task.Delay(50, TestContext.Current!.CancellationToken);
105105
await revealPasswordButton.LeftClick();
106-
await Task.Delay(50); // Wait for the "clear text TextBox" to become visible
106+
await Task.Delay(50, TestContext.Current.CancellationToken); // Wait for the "clear text TextBox" to become visible
107107
await clearTextPasswordTextBox.SendKeyboardInput($"2");
108108
string? boundText2 = await userControl.GetProperty<string>(nameof(BoundPasswordBox.ViewModelPassword));
109109
string? password2 = await passwordBox.GetProperty<string>(nameof(PasswordBox.Password));

tests/MaterialDesignThemes.UITests/WPF/SplitButtons/SplitButtonTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public async Task SplitButton_RegisterForClick_RaisesEvent()
5555

5656
//Act
5757
await leftButton.LeftClick();
58-
await Task.Delay(50);
58+
await Task.Delay(50, TestContext.Current!.CancellationToken);
5959
int leftButtonCount = (await clickEvent.GetInvocations()).Count;
6060
await popupBox.LeftClick();
61-
await Task.Delay(50);
61+
await Task.Delay(50, TestContext.Current.CancellationToken);
6262
int rightButtonCount = (await clickEvent.GetInvocations()).Count;
6363

6464
// Assert
@@ -94,7 +94,7 @@ public async Task SplitButton_WithButtonInPopup_CanBeInvoked()
9494
await Wait.For(async () => await popupContent.GetIsVisible());
9595
await Wait.For(async () => await popupContent.GetActualHeight() > 10);
9696
await popupContent.LeftClick();
97-
await Task.Delay(50);
97+
await Task.Delay(50, TestContext.Current!.CancellationToken);
9898

9999
// Assert
100100
var invocations = await clickEvent.GetInvocations();
@@ -118,7 +118,7 @@ public async Task SplitButton_RegisterCommandBinding_InvokesCommand()
118118

119119
//Act
120120
await splitButton.LeftClick();
121-
await Task.Delay(50);
121+
await Task.Delay(50, TestContext.Current!.CancellationToken);
122122

123123
// Assert
124124
await Assert.That(await userControl.GetCommandInvoked()).IsTrue();
@@ -176,7 +176,7 @@ public async Task SplitButton_ClickingPopupContent_DoesNotExecuteSplitButtonClic
176176
await Wait.For(async () => await popupContent.GetIsVisible());
177177
await Wait.For(async () => await popupContent.GetActualHeight() > 10);
178178
await popupContent.LeftClick();
179-
await Task.Delay(50);
179+
await Task.Delay(50, TestContext.Current!.CancellationToken);
180180

181181
// Assert
182182
await Assert.That(await splitButtonClickEvent.GetInvocations()).IsEmpty();

tests/MaterialDesignThemes.UITests/WPF/TabControls/TabControlTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public async Task TabItem_ShouldKeepDataContext_WhenContextMenuOpens(string hori
109109
await button.RightClick();
110110
await tabControl.MoveCursorTo();
111111
await tabControl.LeftClick(Position.TopLeft);
112-
await Task.Delay(50); // allow a little time for the disconnect to occur
112+
await Task.Delay(50, TestContext.Current!.CancellationToken); // allow a little time for the disconnect to occur
113113

114114
// Assert data context still present
115115
tabItem = await tabControl.GetElement<TabItem>();

0 commit comments

Comments
 (0)