Skip to content

Commit

Permalink
fix(datepicker): Make sure DatePicker field ordering is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
carldebilly committed Feb 25, 2021
1 parent 7dd5a7e commit 91408f4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,111 +4,137 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Private.Infrastructure;
using Windows.UI.Xaml.Controls;
using FluentAssertions;
using FluentAssertions.Execution;
using Uno.Disposables;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls
{
[TestClass]
public class Given_DatePicker
{
public class Given_DatePicker
{
[TestInitialize]
public async Task Setup()
{
TestServices.WindowHelper.WindowContent = null;

await TestServices.WindowHelper.WaitForIdle();
}

[TestMethod]
[RunsOnUIThread]
public async Task When_United_States_Culture_Column_Order()
{
var originalCulture = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("en-US");
var datePicker = new DatePicker();
using var _ = new AssertionScope();
using var lang = SetAmbiantLanguage("en-US");

TestServices.WindowHelper.WindowContent = datePicker;
var datePicker = new DatePicker();

await TestServices.WindowHelper.WaitForIdle();
TestServices.WindowHelper.WindowContent = datePicker;

// US uses mm/dd/yyyy
await TestServices.WindowHelper.WaitForIdle();

var monthTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "MonthTextBlock");
Assert.AreEqual(0, (int)monthTextBlock.GetValue(Grid.ColumnProperty));
var dayTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "DayTextBlock");
Assert.AreEqual(2, (int)dayTextBlock.GetValue(Grid.ColumnProperty));
var yearTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "YearTextBlock");
Assert.AreEqual(4, (int)yearTextBlock.GetValue(Grid.ColumnProperty));
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
// US uses mm/dd/yyyy
CheckDateTimeTextBlockPartPosition(datePicker, "YearTextBlock", expectedColumn: 4);
CheckDateTimeTextBlockPartPosition(datePicker, "MonthTextBlock", expectedColumn: 0);
CheckDateTimeTextBlockPartPosition(datePicker, "DayTextBlock", expectedColumn: 2);
}

[TestMethod]
[RunsOnUIThread]
public async Task When_CanadaEnglish_Culture_Column_Order()
{
using var _ = new AssertionScope();
using var lang = SetAmbiantLanguage("en-CA");

TestServices.WindowHelper.WindowContent = null;
var datePicker = new DatePicker();

await TestServices.WindowHelper.WaitForIdle();
}
TestServices.WindowHelper.WindowContent = datePicker;

await TestServices.WindowHelper.WaitForIdle();

// en-CA uses yyyy/mm/dd
CheckDateTimeTextBlockPartPosition(datePicker, "YearTextBlock", expectedColumn: 0);
CheckDateTimeTextBlockPartPosition(datePicker, "MonthTextBlock", expectedColumn: 2);
CheckDateTimeTextBlockPartPosition(datePicker, "DayTextBlock", expectedColumn: 4);
}

[TestMethod]
[RunsOnUIThread]
public async Task When_Czech_Culture_Column_Order()
public async Task When_CanadaFrench_Culture_Column_Order()
{
var originalCulture = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("cs-CZ");
var datePicker = new DatePicker();
using var _ = new AssertionScope();
using var lang = SetAmbiantLanguage("fr-CA");

TestServices.WindowHelper.WindowContent = datePicker;
var datePicker = new DatePicker();

await TestServices.WindowHelper.WaitForIdle();
TestServices.WindowHelper.WindowContent = datePicker;

// CZ uses mm/dd/yyyy
await TestServices.WindowHelper.WaitForIdle();

var dayTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "DayTextBlock");
Assert.AreEqual(0, (int)dayTextBlock.GetValue(Grid.ColumnProperty));
var monthTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "MonthTextBlock");
Assert.AreEqual(2, (int)monthTextBlock.GetValue(Grid.ColumnProperty));
var yearTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "YearTextBlock");
Assert.AreEqual(4, (int)yearTextBlock.GetValue(Grid.ColumnProperty));
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
// fr-CA uses yyyy/mm/dd
CheckDateTimeTextBlockPartPosition(datePicker, "YearTextBlock", expectedColumn: 0);
CheckDateTimeTextBlockPartPosition(datePicker, "MonthTextBlock", expectedColumn: 2);
CheckDateTimeTextBlockPartPosition(datePicker, "DayTextBlock", expectedColumn: 4);
}

TestServices.WindowHelper.WindowContent = null;
[TestMethod]
[RunsOnUIThread]
public async Task When_Czech_Culture_Column_Order()
{
using var _ = new AssertionScope();
using var lang = SetAmbiantLanguage("cs-CZ");

await TestServices.WindowHelper.WaitForIdle();
}
var datePicker = new DatePicker();

TestServices.WindowHelper.WindowContent = datePicker;

await TestServices.WindowHelper.WaitForIdle();

// CZ uses dd/mm/yyyy
CheckDateTimeTextBlockPartPosition(datePicker, "YearTextBlock", expectedColumn: 4);
CheckDateTimeTextBlockPartPosition(datePicker, "MonthTextBlock", expectedColumn: 2);
CheckDateTimeTextBlockPartPosition(datePicker, "DayTextBlock", expectedColumn: 0);
}

[TestMethod]
[RunsOnUIThread]
public async Task When_Hungarian_Culture_Column_Order()
{
var originalCulture = CultureInfo.CurrentCulture;
try
{
CultureInfo.CurrentCulture = new CultureInfo("hu-HU");
var datePicker = new DatePicker();
using var _ = new AssertionScope();
using var lang = SetAmbiantLanguage("hu-HU");

TestServices.WindowHelper.WindowContent = datePicker;
var datePicker = new DatePicker();

await TestServices.WindowHelper.WaitForIdle();
TestServices.WindowHelper.WindowContent = datePicker;

// HU uses yyyy/mm/dd
await TestServices.WindowHelper.WaitForIdle();

var yearTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "YearTextBlock");
Assert.AreEqual(0, (int)yearTextBlock.GetValue(Grid.ColumnProperty));
var monthTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "MonthTextBlock");
Assert.AreEqual(2, (int)monthTextBlock.GetValue(Grid.ColumnProperty));
var dayTextBlock = (TextBlock)MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, "DayTextBlock");
Assert.AreEqual(4, (int)dayTextBlock.GetValue(Grid.ColumnProperty));
}
finally
{
CultureInfo.CurrentCulture = originalCulture;
// HU uses yyyy/mm/dd
CheckDateTimeTextBlockPartPosition(datePicker, "YearTextBlock", expectedColumn: 0);
CheckDateTimeTextBlockPartPosition(datePicker, "MonthTextBlock", expectedColumn: 2);
CheckDateTimeTextBlockPartPosition(datePicker, "DayTextBlock", expectedColumn: 4);
}

TestServices.WindowHelper.WindowContent = null;
private static IDisposable SetAmbiantLanguage(string language)
{
var previousLanguage = ApplicationLanguages.PrimaryLanguageOverride;
ApplicationLanguages.PrimaryLanguageOverride = language;
return Disposable.Create(() => ApplicationLanguages.PrimaryLanguageOverride = previousLanguage);
}

await TestServices.WindowHelper.WaitForIdle();
private static void CheckDateTimeTextBlockPartPosition(DatePicker datePicker, string id, int expectedColumn)
{
var textBlock = MUXControlsTestApp.Utilities.VisualTreeUtils.FindVisualChildByName(datePicker, id) as TextBlock;
textBlock.Should().NotBeNull($"TextBlock {id} not found");
if(textBlock != null)
{
var v = textBlock.GetValue(Grid.ColumnProperty);
((int) v).Should().Be(expectedColumn, $"{id} column property");
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/DatePicker/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,14 @@ void CreateNewFormatter(
// strClock, /* Clock */
// spFormatter));

spFormatter = new DateTimeFormatter(
strFormat,
spLanguages,
strGeographicRegion,
strCalendarIdentifier,
strClock
);

ppDateTimeFormatter = spFormatter;
}

Expand Down

0 comments on commit 91408f4

Please sign in to comment.