Skip to content

Commit

Permalink
feat(calendar): Ensure Calendar & DateTimeFormatter are following App…
Browse files Browse the repository at this point in the history
…licationLanguages state
  • Loading branch information
carldebilly committed Feb 25, 2021
1 parent 07bb5cc commit 7dd5a7e
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 97 deletions.
46 changes: 46 additions & 0 deletions src/Uno.UI.Tests/Windows_Globalization/Given_CalendarFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Globalization;
using System.Linq;
using Windows.Globalization.DateTimeFormatting;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Uno.UI.Tests.Windows_Globalization
{
[TestClass]
public class Given_CalendarFormatter
{
[TestMethod]
[DataRow("day month year", "en-US", "{month.numeric}/{day.integer}/{year.full}")]
[DataRow("day month year", "en-CA", "{year.full}-{month.numeric}-{day.integer(2)}")]
[DataRow("day month year", "en-GB", "{day.integer(2)}/{month.numeric}/{year.full}")]
[DataRow("day month year", "fr-CA", "{year.full}-{month.numeric}-{day.integer(2)}")]
[DataRow("day month year", "fr-FR", "{day.integer(2)}/{month.numeric}/{year.full}")]
[DataRow("day month year", "hu-HU", "{year.full}. {month.numeric}. {day.integer(2)}.")]
public void When_UsingVariousLanguages(string format, string language, string expectedPattern)
{
var sut = new DateTimeFormatter(format, new[] {language});

var firstPattern = sut.Patterns.First();

using var _ = new AssertionScope();

firstPattern.Should().Be(expectedPattern);
firstPattern.Length.Should().Be(expectedPattern.Length);
}

[TestMethod]
[DataRow("day", "en-US|fr-CA|ru-RU", "{day.integer}|{day.integer}|{day.integer}")]
[DataRow("day month year", "en-US|fr-CA", "{month.numeric}/{day.integer}/{year.full}|{year.full}-{month.numeric}-{day.integer(2)}")]
[DataRow("month year", "en-US|fr-CA", "{month.full} {year.full}|{month.full}, {year.full}")]
[DataRow("day month", "en-US|fr-CA", "{month.full} {day.integer}|{day.integer} {month.full}")]
[DataRow("hour minute second", "en-US|fr-CA", "{hour}:{minute}:{second} {period.abbreviated}|{hour}:{minute}:{second}")]
[DataRow("hour minute", "en-US|fr-CA", "{hour}:{minute} {period.abbreviated}|{hour}:{minute}")]
public void When_UsingMultipleLanguages(string format, string languages, string expectedPatterns)
{
var sut = new DateTimeFormatter(format, languages.Split('|'));

sut.Patterns.Should().BeEquivalentTo(expectedPatterns.Split('|'));
}
}
}
108 changes: 54 additions & 54 deletions src/Uno.UI.Tests/Windows_Globalization/When_Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,43 +598,43 @@ private void Validate(

using (new AssertionScope("Calendar Properties"))
{
SUT.Day.Should().Be(day, "day");
SUT.Month.Should().Be(month, "month");
SUT.Year.Should().Be(year, "year");
SUT.Hour.Should().Be(hours, "hours");
SUT.Minute.Should().Be(minutes, "minutes");
SUT.Second.Should().Be(seconds, "seconds");
SUT.Nanosecond.Should().Be(milliseconds * 1000, "milliseconds");
SUT.DayOfWeek.Should().Be(dayOfWeek, "dayOfWeek");
SUT.Era.Should().Be(era, "era");
SUT.FirstDayInThisMonth.Should().Be(firstDayInThisMonth, "firstDayInThisMonth");
SUT.FirstEra.Should().Be(firstEra, "firstEra");
SUT.FirstHourInThisPeriod.Should().Be(firstHourInThisPeriod, "firstHourInThisPeriod");
SUT.FirstMinuteInThisHour.Should().Be(firstMinuteInThisHour, "firstMinuteInThisHour");
SUT.FirstMonthInThisYear.Should().Be(firstMonthInThisYear, "firstMonthInThisYear");
SUT.FirstPeriodInThisDay.Should().Be(firstPeriodInThisDay, "firstPeriodInThisDay");
SUT.FirstSecondInThisMinute.Should().Be(firstSecondInThisMinute, "firstSecondInThisMinute");
SUT.FirstYearInThisEra.Should().Be(firstYearInThisEra, "firstYearInThisEra");
SUT.Day.Should().Be(day, nameof(day));
SUT.Month.Should().Be(month, nameof(month));
SUT.Year.Should().Be(year, nameof(year));
SUT.Hour.Should().Be(hours, nameof(hours));
SUT.Minute.Should().Be(minutes, nameof(minutes));
SUT.Second.Should().Be(seconds, nameof(seconds));
SUT.Nanosecond.Should().Be(milliseconds * 1000, nameof(milliseconds));
SUT.DayOfWeek.Should().Be(dayOfWeek, nameof(dayOfWeek));
SUT.Era.Should().Be(era, nameof(era));
SUT.FirstDayInThisMonth.Should().Be(firstDayInThisMonth, nameof(firstDayInThisMonth));
SUT.FirstEra.Should().Be(firstEra, nameof(firstEra));
SUT.FirstHourInThisPeriod.Should().Be(firstHourInThisPeriod, nameof(firstHourInThisPeriod));
SUT.FirstMinuteInThisHour.Should().Be(firstMinuteInThisHour, nameof(firstMinuteInThisHour));
SUT.FirstMonthInThisYear.Should().Be(firstMonthInThisYear, nameof(firstMonthInThisYear));
SUT.FirstPeriodInThisDay.Should().Be(firstPeriodInThisDay, nameof(firstPeriodInThisDay));
SUT.FirstSecondInThisMinute.Should().Be(firstSecondInThisMinute, nameof(firstSecondInThisMinute));
SUT.FirstYearInThisEra.Should().Be(firstYearInThisEra, nameof(firstYearInThisEra));
SUT.Languages.Should().HaveCount(1, "languages count");
SUT.Languages.Should().HaveElementAt(0, culture, "culture");
SUT.LastDayInThisMonth.Should().Be(lastDayInThisMonth, "lastDayInThisMonth");
SUT.LastEra.Should().Be(lastEra, "lastEra");
SUT.LastHourInThisPeriod.Should().Be(lastHourInThisPeriod, "lastHourInThisPeriod");
SUT.LastMinuteInThisHour.Should().Be(lastMinuteInThisHour, "lastMinuteInThisHour");
SUT.LastMonthInThisYear.Should().Be(lastMonthInThisYear, "lastMonthInThisYear");
SUT.LastPeriodInThisDay.Should().Be(lastPeriodInThisDay, "lastPeriodInThisDay");
SUT.LastSecondInThisMinute.Should().Be(lastSecondInThisMinute, "lastSecondInThisMinute");
SUT.LastYearInThisEra.Should().Be(lastYearInThisEra, "lastYearInThisEra");
SUT.NumberOfDaysInThisMonth.Should().Be(numberOfDaysInThisMonth, "numberOfDaysInThisMonth");
SUT.NumberOfEras.Should().Be(numberOfEras, "numberOfEras");
SUT.NumberOfHoursInThisPeriod.Should().Be(numberOfHoursInThisPeriod, "numberOfHoursInThisPeriod");
SUT.NumberOfMinutesInThisHour.Should().Be(numberOfMinutesInThisHour, "numberOfMinutesInThisHour");
SUT.NumberOfMonthsInThisYear.Should().Be(numberOfMonthsInThisYear, "numberOfMonthsInThisYear");
SUT.NumberOfPeriodsInThisDay.Should().Be(numberOfPeriodsInThisDay, "numberOfPeriodsInThisDay");
SUT.NumberOfSecondsInThisMinute.Should().Be(numberOfSecondsInThisMinute, "numberOfSecondsInThisMinute");
SUT.NumberOfYearsInThisEra.Should().Be(numberOfYearsInThisEra, "numberOfYearsInThisEra");
SUT.Period.Should().Be(period, "period");
SUT.ResolvedLanguage.Should().Be(culture, "culture");
SUT.Languages.Should().HaveElementAt(0, culture, nameof(culture));
SUT.LastDayInThisMonth.Should().Be(lastDayInThisMonth, nameof(lastDayInThisMonth));
SUT.LastEra.Should().Be(lastEra, nameof(lastEra));
SUT.LastHourInThisPeriod.Should().Be(lastHourInThisPeriod, nameof(lastHourInThisPeriod));
SUT.LastMinuteInThisHour.Should().Be(lastMinuteInThisHour, nameof(lastMinuteInThisHour));
SUT.LastMonthInThisYear.Should().Be(lastMonthInThisYear, nameof(lastMonthInThisYear));
SUT.LastPeriodInThisDay.Should().Be(lastPeriodInThisDay, nameof(lastPeriodInThisDay));
SUT.LastSecondInThisMinute.Should().Be(lastSecondInThisMinute, nameof(lastSecondInThisMinute));
SUT.LastYearInThisEra.Should().Be(lastYearInThisEra, nameof(lastYearInThisEra));
SUT.NumberOfDaysInThisMonth.Should().Be(numberOfDaysInThisMonth, nameof(numberOfDaysInThisMonth));
SUT.NumberOfEras.Should().Be(numberOfEras, nameof(numberOfEras));
SUT.NumberOfHoursInThisPeriod.Should().Be(numberOfHoursInThisPeriod, nameof(numberOfHoursInThisPeriod));
SUT.NumberOfMinutesInThisHour.Should().Be(numberOfMinutesInThisHour, nameof(numberOfMinutesInThisHour));
SUT.NumberOfMonthsInThisYear.Should().Be(numberOfMonthsInThisYear, nameof(numberOfMonthsInThisYear));
SUT.NumberOfPeriodsInThisDay.Should().Be(numberOfPeriodsInThisDay, nameof(numberOfPeriodsInThisDay));
SUT.NumberOfSecondsInThisMinute.Should().Be(numberOfSecondsInThisMinute, nameof(numberOfSecondsInThisMinute));
SUT.NumberOfYearsInThisEra.Should().Be(numberOfYearsInThisEra, nameof(numberOfYearsInThisEra));
SUT.Period.Should().Be(period, nameof(period));
SUT.ResolvedLanguage.Should().Be(culture, nameof(culture));

// Validation is disabled as timezone support is only using the current machine's timezone
// SUT.IsDaylightSavingTime.Should().Be(isDaylightSavingTime, "isDaylightSavingTime");
Expand Down Expand Up @@ -675,24 +675,24 @@ string dayOfWeekAsString

using (new AssertionScope("Calendar Format"))
{
SUT.YearAsPaddedString(2).Should().Be(yearAsPaddedString, "yearAsPaddedString");
SUT.YearAsString().Should().Be(yearAsString, "yearAsString");
SUT.MonthAsPaddedNumericString(2).Should().Be(monthAsPaddedNumericString, "monthAsPaddedNumericString");
SUT.MonthAsSoloString().Should().Be(monthAsSoloString, "monthAsSoloString");
SUT.MonthAsString().Should().Be(monthAsString, "monthAsString");
SUT.MonthAsNumericString().Should().Be(monthAsNumericString, "monthAsNumericString");
SUT.DayAsPaddedString(2).Should().Be(dayAsPaddedString, "dayAsPaddedString");
SUT.DayAsString().Should().Be(dayAsString, "dayAsString");
SUT.HourAsPaddedString(2).Should().Be(hourAsPaddedString, "hourAsPaddedString");
SUT.HourAsString().Should().Be(hourAsString, "hourAsString");
SUT.MinuteAsPaddedString(2).Should().Be(minuteAsPaddedString, "minuteAsPaddedString");
SUT.MinuteAsString().Should().Be(minuteAsString, "minuteAsString");
SUT.SecondAsPaddedString(2).Should().Be(secondAsPaddedString, "secondAsPaddedString");
SUT.SecondAsString().Should().Be(secondAsString, "secondAsString");
SUT.NanosecondAsPaddedString(2).Should().Be(nanosecondAsPaddedString, "nanosecondAsPaddedString");
SUT.NanosecondAsString().Should().Be(nanosecondAsString, "nanosecondAsString");
SUT.DayOfWeekAsSoloString().Should().Be(dayOfWeekAsSoloString, "dayOfWeekAsSoloString");
SUT.DayOfWeekAsString().Should().Be(dayOfWeekAsString, "dayOfWeekAsString");
SUT.YearAsPaddedString(2).Should().Be(yearAsPaddedString, nameof(yearAsPaddedString));
SUT.YearAsString().Should().Be(yearAsString, nameof(yearAsString));
SUT.MonthAsPaddedNumericString(2).Should().Be(monthAsPaddedNumericString, nameof(monthAsPaddedNumericString));
SUT.MonthAsSoloString().Should().Be(monthAsSoloString, nameof(monthAsSoloString));
SUT.MonthAsString().Should().Be(monthAsString, nameof(monthAsString));
SUT.MonthAsNumericString().Should().Be(monthAsNumericString, nameof(monthAsNumericString));
SUT.DayAsPaddedString(2).Should().Be(dayAsPaddedString, nameof(dayAsPaddedString));
SUT.DayAsString().Should().Be(dayAsString, nameof(dayAsString));
SUT.HourAsPaddedString(2).Should().Be(hourAsPaddedString, nameof(hourAsPaddedString));
SUT.HourAsString().Should().Be(hourAsString, nameof(hourAsString));
SUT.MinuteAsPaddedString(2).Should().Be(minuteAsPaddedString, nameof(minuteAsPaddedString));
SUT.MinuteAsString().Should().Be(minuteAsString, nameof(minuteAsString));
SUT.SecondAsPaddedString(2).Should().Be(secondAsPaddedString, nameof(secondAsPaddedString));
SUT.SecondAsString().Should().Be(secondAsString, nameof(secondAsString));
SUT.NanosecondAsPaddedString(2).Should().Be(nanosecondAsPaddedString, nameof(nanosecondAsPaddedString));
SUT.NanosecondAsString().Should().Be(nanosecondAsString, nameof(nanosecondAsString));
SUT.DayOfWeekAsSoloString().Should().Be(dayOfWeekAsSoloString, nameof(dayOfWeekAsSoloString));
SUT.DayOfWeekAsString().Should().Be(dayOfWeekAsString, nameof(dayOfWeekAsString));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Globalization/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static string GetClock(string clock)

public Calendar()
{
_languages = new string[1] { CultureInfo.CurrentCulture.IetfLanguageTag };
_languages = ApplicationLanguages.Languages;
_resolvedCulture = CultureInfo.CurrentCulture;
_calendar = CultureInfo.CurrentCulture.Calendar;
_timeZone = TimeZoneInfo.Local;
Expand Down
Loading

0 comments on commit 7dd5a7e

Please sign in to comment.