Skip to content

Commit

Permalink
refactor: make tests a little bit faster
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Oct 15, 2024
1 parent 3eb667c commit 721af24
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 422 deletions.
95 changes: 59 additions & 36 deletions src/Mahapps.Metro.Tests/Tests/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -15,24 +16,55 @@ namespace MahApps.Metro.Tests.Tests
[TestFixture]
public class ButtonTests
{
private ButtonWindow? window;

[OneTimeSetUp]
public async Task OneTimeSetUp()
{
this.window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
this.window?.Close();
this.window = null;
}

[SetUp]
public void SetUp()
{
this.PreparePropertiesForTest([
ControlsHelper.ContentCharacterCasingProperty.Name,
UIElement.IsEnabledProperty.Name
]);
}

private void PreparePropertiesForTest(IList<string>? properties = null)
{
this.window?.DefaultButton.ClearDependencyProperties(properties);
this.window?.SquareButton.ClearDependencyProperties(properties);
this.window?.TheDropDownButton.ClearDependencyProperties(properties);
this.window?.TheSplitButton.ClearDependencyProperties(properties);
}

[Test]
public async Task DefaultButtonTextIsUpperCase()
public void DefaultButtonTextIsUpperCase()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
var presenter = window.DefaultButton.FindChild<ContentPresenter>("PART_ContentPresenter");
Assert.That(this.window, Is.Not.Null);

var presenter = this.window.DefaultButton.FindChild<ContentPresenter>("PART_ContentPresenter");

Assert.That(presenter, Is.Not.Null);
Assert.That(presenter.Content, Is.EqualTo("SOMETEXT"));

window.Close();
}

[Test]
public async Task DefaultButtonRespectsControlsHelperContentCharacterCasing()
public void DefaultButtonRespectsControlsHelperContentCharacterCasing()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

Button defaultButton = window.DefaultButton;
Button defaultButton = this.window.DefaultButton;
Assert.That(defaultButton, Is.Not.Null);

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

defaultButton.SetValue(ControlsHelper.ContentCharacterCasingProperty, CharacterCasing.Upper);
Assert.That(presenter.Content, Is.EqualTo("SOMETEXT"));

window.Close();
}

[Test]
public async Task SquareButtonButtonTextIsLowerCase()
public void SquareButtonButtonTextIsLowerCase()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
var presenter = window.SquareButton.FindChild<ContentPresenter>("PART_ContentPresenter");
Assert.That(this.window, Is.Not.Null);

var presenter = this.window.SquareButton.FindChild<ContentPresenter>("PART_ContentPresenter");

Assert.That(presenter, Is.Not.Null);
Assert.That(presenter.Content, Is.EqualTo("sometext"));

window.Close();
}

[Test]
public async Task SquareButtonRespectsButtonHelperContentCharacterCasing()
public void SquareButtonRespectsButtonHelperContentCharacterCasing()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

Button squareButton = window.SquareButton;
Button squareButton = this.window.SquareButton;
Assert.That(squareButton, Is.Not.Null);

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

squareButton.SetValue(ControlsHelper.ContentCharacterCasingProperty, CharacterCasing.Upper);
Assert.That(presenter.Content, Is.EqualTo("SOMETEXT"));

window.Close();
}

[Test]
public async Task DropDownButtonShouldRespectParentIsEnabledProperty()
public void DropDownButtonShouldRespectParentIsEnabledProperty()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
Assert.That(window.TheDropDownButton.IsEnabled, Is.False);
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
Assert.That(this.window.TheDropDownButton.IsEnabled, Is.False);

window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
Assert.That(window.TheDropDownButton.IsEnabled, Is.True);

window.Close();
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
Assert.That(this.window.TheDropDownButton.IsEnabled, Is.True);
}

[Test]
public async Task SplitButtonShouldRespectParentIsEnabledProperty()
public void SplitButtonShouldRespectParentIsEnabledProperty()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<ButtonWindow>().ConfigureAwait(false);

window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
Assert.That(window.TheSplitButton.IsEnabled, Is.False);
Assert.That(this.window, Is.Not.Null);

window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
Assert.That(window.TheSplitButton.IsEnabled, Is.True);
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, false);
Assert.That(this.window.TheSplitButton.IsEnabled, Is.False);

window.Close();
this.window.TheStackPanel.SetCurrentValue(UIElement.IsEnabledProperty, true);
Assert.That(this.window.TheSplitButton.IsEnabled, Is.True);
}
}
}
63 changes: 39 additions & 24 deletions src/Mahapps.Metro.Tests/Tests/DateTimePickerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
Expand All @@ -18,10 +19,36 @@ namespace MahApps.Metro.Tests.Tests
[TestFixture]
public class DateTimePickerTests
{
private DateAndTimePickerWindow? window;

[OneTimeSetUp]
public async Task OneTimeSetUp()
{
this.window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
}

[OneTimeTearDown]
public void OneTimeTearDown()
{
this.window?.Close();
this.window = null;
}

[SetUp]
public void SetUp()
{
this.PreparePropertiesForTest();
}

private void PreparePropertiesForTest(IList<string>? properties = null)
{
// nothing to do here
}

[Test]
public async Task DateTimePickerSetCulture()
public void DateTimePickerSetCulture()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

Assert.That(window.TheDateTimePicker.SelectedDateTime, Is.Not.Null);
Assert.That(window.TheDateTimePicker.Culture, Is.Not.Null);
Expand All @@ -30,14 +57,12 @@ public async Task DateTimePickerSetCulture()
var datePickerTextBox = window.TheDateTimePicker.FindChild<DatePickerTextBox>(string.Empty);
Assert.That(datePickerTextBox, Is.Not.Null);
Assert.That(datePickerTextBox?.Text, Is.EqualTo("31/08/2016 14:00:01"));

window.Close();
}

[Test]
public async Task TimePickerCultureDeTest()
public void TimePickerCultureDeTest()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

Assert.That(window.TheTimePickerDe.SelectedDateTime, Is.Not.Null);
Assert.That(window.TheTimePickerDe.Culture, Is.Not.Null);
Expand All @@ -46,14 +71,12 @@ public async Task TimePickerCultureDeTest()
var datePickerTextBox = window.TheTimePickerDe.FindChild<DatePickerTextBox>(string.Empty);
Assert.That(datePickerTextBox, Is.Not.Null);
Assert.That(datePickerTextBox?.Text, Is.EqualTo("14:00:01"));

window.Close();
}

[Test]
public async Task TimePickerCultureUsTest()
public void TimePickerCultureUsTest()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

Assert.That(window.TheTimePickerUs.SelectedDateTime, Is.Not.Null);
Assert.That(window.TheTimePickerUs.Culture, Is.Not.Null);
Expand All @@ -62,14 +85,12 @@ public async Task TimePickerCultureUsTest()
var datePickerTextBox = window.TheTimePickerUs.FindChild<DatePickerTextBox>(string.Empty);
Assert.That(datePickerTextBox, Is.Not.Null);
Assert.That(datePickerTextBox?.Text, Is.EqualTo("2:00:01 PM"));

window.Close();
}

[Test]
public async Task TheTimePickerCsCzTest()
public void TheTimePickerCsCzTest()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

Assert.That(window.TheTimePickerCsCz.SelectedDateTime, Is.Not.Null);
Assert.That(window.TheTimePickerCsCz.Culture, Is.Not.Null);
Expand All @@ -78,14 +99,12 @@ public async Task TheTimePickerCsCzTest()
var datePickerTextBox = window.TheTimePickerCsCz.FindChild<DatePickerTextBox>(string.Empty);
Assert.That(datePickerTextBox, Is.Not.Null);
Assert.That(datePickerTextBox?.Text, Is.EqualTo("22:23:24"));

window.Close();
}

[Test]
public async Task TimePickerTimeFormat()
public void TimePickerTimeFormat()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

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

window.TheDateTimeFormatPicker.SetCurrentValue(TimePickerBase.SelectedTimeFormatProperty, TimePickerFormat.Short);
Assert.That(datePickerTextBox.Text, Is.EqualTo("mercoledì 31 agosto 2016 14:00"));

window.Close();
}

[Test]
public async Task MilitaryTimeShouldBeConvertedToDateTime()
public void MilitaryTimeShouldBeConvertedToDateTime()
{
var window = await WindowHelpers.CreateInvisibleWindowAsync<DateAndTimePickerWindow>().ConfigureAwait(false);
Assert.That(this.window, Is.Not.Null);

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

Assert.That((window.EmptyTimePicker).SelectedDateTime, Is.EqualTo(default(DateTime) + new TimeSpan(14, 42, 12)));

window.Close();
}
}
}
Loading

0 comments on commit 721af24

Please sign in to comment.