Skip to content

Commit 721af24

Browse files
committed
refactor: make tests a little bit faster
1 parent 3eb667c commit 721af24

7 files changed

+472
-422
lines changed

src/Mahapps.Metro.Tests/Tests/ButtonTests.cs

+59-36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Generic;
56
using System.Threading.Tasks;
67
using System.Windows;
78
using System.Windows.Controls;
@@ -15,24 +16,55 @@ namespace MahApps.Metro.Tests.Tests
1516
[TestFixture]
1617
public class ButtonTests
1718
{
19+
private ButtonWindow? window;
20+
21+
[OneTimeSetUp]
22+
public async Task OneTimeSetUp()
23+
{
24+
this.window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
25+
}
26+
27+
[OneTimeTearDown]
28+
public void OneTimeTearDown()
29+
{
30+
this.window?.Close();
31+
this.window = null;
32+
}
33+
34+
[SetUp]
35+
public void SetUp()
36+
{
37+
this.PreparePropertiesForTest([
38+
ControlsHelper.ContentCharacterCasingProperty.Name,
39+
UIElement.IsEnabledProperty.Name
40+
]);
41+
}
42+
43+
private void PreparePropertiesForTest(IList<string>? properties = null)
44+
{
45+
this.window?.DefaultButton.ClearDependencyProperties(properties);
46+
this.window?.SquareButton.ClearDependencyProperties(properties);
47+
this.window?.TheDropDownButton.ClearDependencyProperties(properties);
48+
this.window?.TheSplitButton.ClearDependencyProperties(properties);
49+
}
50+
1851
[Test]
19-
public async Task DefaultButtonTextIsUpperCase()
52+
public void DefaultButtonTextIsUpperCase()
2053
{
21-
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
22-
var presenter = window.DefaultButton.FindChild<ContentPresenter>("PART_ContentPresenter");
54+
Assert.That(this.window, Is.Not.Null);
55+
56+
var presenter = this.window.DefaultButton.FindChild<ContentPresenter>("PART_ContentPresenter");
2357

2458
Assert.That(presenter, Is.Not.Null);
2559
Assert.That(presenter.Content, Is.EqualTo("SOMETEXT"));
26-
27-
window.Close();
2860
}
2961

3062
[Test]
31-
public async Task DefaultButtonRespectsControlsHelperContentCharacterCasing()
63+
public void DefaultButtonRespectsControlsHelperContentCharacterCasing()
3264
{
33-
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
65+
Assert.That(this.window, Is.Not.Null);
3466

35-
Button defaultButton = window.DefaultButton;
67+
Button defaultButton = this.window.DefaultButton;
3668
Assert.That(defaultButton, Is.Not.Null);
3769

3870
var presenter = defaultButton.FindChild<ContentPresenter>("PART_ContentPresenter");
@@ -46,28 +78,25 @@ public async Task DefaultButtonRespectsControlsHelperContentCharacterCasing()
4678

4779
defaultButton.SetValue(ControlsHelper.ContentCharacterCasingProperty, CharacterCasing.Upper);
4880
Assert.That(presenter.Content, Is.EqualTo("SOMETEXT"));
49-
50-
window.Close();
5181
}
5282

5383
[Test]
54-
public async Task SquareButtonButtonTextIsLowerCase()
84+
public void SquareButtonButtonTextIsLowerCase()
5585
{
56-
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
57-
var presenter = window.SquareButton.FindChild<ContentPresenter>("PART_ContentPresenter");
86+
Assert.That(this.window, Is.Not.Null);
87+
88+
var presenter = this.window.SquareButton.FindChild<ContentPresenter>("PART_ContentPresenter");
5889

5990
Assert.That(presenter, Is.Not.Null);
6091
Assert.That(presenter.Content, Is.EqualTo("sometext"));
61-
62-
window.Close();
6392
}
6493

6594
[Test]
66-
public async Task SquareButtonRespectsButtonHelperContentCharacterCasing()
95+
public void SquareButtonRespectsButtonHelperContentCharacterCasing()
6796
{
68-
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
97+
Assert.That(this.window, Is.Not.Null);
6998

70-
Button squareButton = window.SquareButton;
99+
Button squareButton = this.window.SquareButton;
71100
Assert.That(squareButton, Is.Not.Null);
72101

73102
var presenter = squareButton.FindChild<ContentPresenter>("PART_ContentPresenter");
@@ -81,36 +110,30 @@ public async Task SquareButtonRespectsButtonHelperContentCharacterCasing()
81110

82111
squareButton.SetValue(ControlsHelper.ContentCharacterCasingProperty, CharacterCasing.Upper);
83112
Assert.That(presenter.Content, Is.EqualTo("SOMETEXT"));
84-
85-
window.Close();
86113
}
87114

88115
[Test]
89-
public async Task DropDownButtonShouldRespectParentIsEnabledProperty()
116+
public void DropDownButtonShouldRespectParentIsEnabledProperty()
90117
{
91-
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
118+
Assert.That(this.window, Is.Not.Null);
92119

93-
window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
94-
Assert.That(window.TheDropDownButton.IsEnabled, Is.False);
120+
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
121+
Assert.That(this.window.TheDropDownButton.IsEnabled, Is.False);
95122

96-
window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
97-
Assert.That(window.TheDropDownButton.IsEnabled, Is.True);
98-
99-
window.Close();
123+
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
124+
Assert.That(this.window.TheDropDownButton.IsEnabled, Is.True);
100125
}
101126

102127
[Test]
103-
public async Task SplitButtonShouldRespectParentIsEnabledProperty()
128+
public void SplitButtonShouldRespectParentIsEnabledProperty()
104129
{
105-
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
106-
107-
window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
108-
Assert.That(window.TheSplitButton.IsEnabled, Is.False);
130+
Assert.That(this.window, Is.Not.Null);
109131

110-
window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
111-
Assert.That(window.TheSplitButton.IsEnabled, Is.True);
132+
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
133+
Assert.That(this.window.TheSplitButton.IsEnabled, Is.False);
112134

113-
window.Close();
135+
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
136+
Assert.That(this.window.TheSplitButton.IsEnabled, Is.True);
114137
}
115138
}
116139
}

src/Mahapps.Metro.Tests/Tests/DateTimePickerTests.cs

+39-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
67
using System.Threading.Tasks;
78
using System.Windows.Controls;
89
using System.Windows.Controls.Primitives;
@@ -18,10 +19,36 @@ namespace MahApps.Metro.Tests.Tests
1819
[TestFixture]
1920
public class DateTimePickerTests
2021
{
22+
private DateAndTimePickerWindow? window;
23+
24+
[OneTimeSetUp]
25+
public async Task OneTimeSetUp()
26+
{
27+
this.window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
28+
}
29+
30+
[OneTimeTearDown]
31+
public void OneTimeTearDown()
32+
{
33+
this.window?.Close();
34+
this.window = null;
35+
}
36+
37+
[SetUp]
38+
public void SetUp()
39+
{
40+
this.PreparePropertiesForTest();
41+
}
42+
43+
private void PreparePropertiesForTest(IList<string>? properties = null)
44+
{
45+
// nothing to do here
46+
}
47+
2148
[Test]
22-
public async Task DateTimePickerSetCulture()
49+
public void DateTimePickerSetCulture()
2350
{
24-
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
51+
Assert.That(this.window, Is.Not.Null);
2552

2653
Assert.That(window.TheDateTimePicker.SelectedDateTime, Is.Not.Null);
2754
Assert.That(window.TheDateTimePicker.Culture, Is.Not.Null);
@@ -30,14 +57,12 @@ public async Task DateTimePickerSetCulture()
3057
var datePickerTextBox = window.TheDateTimePicker.FindChild<DatePickerTextBox>(string.Empty);
3158
Assert.That(datePickerTextBox, Is.Not.Null);
3259
Assert.That(datePickerTextBox?.Text, Is.EqualTo("31/08/2016 14:00:01"));
33-
34-
window.Close();
3560
}
3661

3762
[Test]
38-
public async Task TimePickerCultureDeTest()
63+
public void TimePickerCultureDeTest()
3964
{
40-
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
65+
Assert.That(this.window, Is.Not.Null);
4166

4267
Assert.That(window.TheTimePickerDe.SelectedDateTime, Is.Not.Null);
4368
Assert.That(window.TheTimePickerDe.Culture, Is.Not.Null);
@@ -46,14 +71,12 @@ public async Task TimePickerCultureDeTest()
4671
var datePickerTextBox = window.TheTimePickerDe.FindChild<DatePickerTextBox>(string.Empty);
4772
Assert.That(datePickerTextBox, Is.Not.Null);
4873
Assert.That(datePickerTextBox?.Text, Is.EqualTo("14:00:01"));
49-
50-
window.Close();
5174
}
5275

5376
[Test]
54-
public async Task TimePickerCultureUsTest()
77+
public void TimePickerCultureUsTest()
5578
{
56-
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
79+
Assert.That(this.window, Is.Not.Null);
5780

5881
Assert.That(window.TheTimePickerUs.SelectedDateTime, Is.Not.Null);
5982
Assert.That(window.TheTimePickerUs.Culture, Is.Not.Null);
@@ -62,14 +85,12 @@ public async Task TimePickerCultureUsTest()
6285
var datePickerTextBox = window.TheTimePickerUs.FindChild<DatePickerTextBox>(string.Empty);
6386
Assert.That(datePickerTextBox, Is.Not.Null);
6487
Assert.That(datePickerTextBox?.Text, Is.EqualTo("2:00:01 PM"));
65-
66-
window.Close();
6788
}
6889

6990
[Test]
70-
public async Task TheTimePickerCsCzTest()
91+
public void TheTimePickerCsCzTest()
7192
{
72-
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
93+
Assert.That(this.window, Is.Not.Null);
7394

7495
Assert.That(window.TheTimePickerCsCz.SelectedDateTime, Is.Not.Null);
7596
Assert.That(window.TheTimePickerCsCz.Culture, Is.Not.Null);
@@ -78,14 +99,12 @@ public async Task TheTimePickerCsCzTest()
7899
var datePickerTextBox = window.TheTimePickerCsCz.FindChild<DatePickerTextBox>(string.Empty);
79100
Assert.That(datePickerTextBox, Is.Not.Null);
80101
Assert.That(datePickerTextBox?.Text, Is.EqualTo("22:23:24"));
81-
82-
window.Close();
83102
}
84103

85104
[Test]
86-
public async Task TimePickerTimeFormat()
105+
public void TimePickerTimeFormat()
87106
{
88-
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
107+
Assert.That(this.window, Is.Not.Null);
89108

90109
Assert.That(window.TheDateTimeFormatPicker.Culture, Is.Not.Null);
91110
Assert.That(window.TheDateTimeFormatPicker.Culture.IetfLanguageTag, Is.EqualTo("it-IT"));
@@ -105,14 +124,12 @@ public async Task TimePickerTimeFormat()
105124

106125
window.TheDateTimeFormatPicker.SetCurrentValue(TimePickerBase.SelectedTimeFormatProperty, TimePickerFormat.Short);
107126
Assert.That(datePickerTextBox.Text, Is.EqualTo("mercoledì 31 agosto 2016 14:00"));
108-
109-
window.Close();
110127
}
111128

112129
[Test]
113-
public async Task MilitaryTimeShouldBeConvertedToDateTime()
130+
public void MilitaryTimeShouldBeConvertedToDateTime()
114131
{
115-
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
132+
Assert.That(this.window, Is.Not.Null);
116133

117134
var datePickerTextBox = window.EmptyTimePicker?.FindChild<DatePickerTextBox>(string.Empty);
118135
Assert.That(datePickerTextBox, Is.Not.Null);
@@ -130,8 +147,6 @@ public async Task MilitaryTimeShouldBeConvertedToDateTime()
130147
);
131148

132149
Assert.That((window.EmptyTimePicker).SelectedDateTime, Is.EqualTo(default(DateTime) + new TimeSpan(14, 42, 12)));
133-
134-
window.Close();
135150
}
136151
}
137152
}

0 commit comments

Comments
 (0)