Skip to content

Commit

Permalink
Merge pull request #19532 from unoplatform/mergify/bp/release/stable/…
Browse files Browse the repository at this point in the history
…5.6/pr-19519

Default `Date`/`Calendar` picker dates (backport #19519)
  • Loading branch information
jeromelaban authored Feb 17, 2025
2 parents 4b1f65e + 270c5a8 commit 242f4f8
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
using static Private.Infrastructure.TestServices;
using Private.Infrastructure;
using Uno.UI.RuntimeTests.MUX.Helpers;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls;

Expand Down Expand Up @@ -125,5 +127,28 @@ public async Task When_Theme_Changes()
}
}
}

#if HAS_UNO
[TestMethod]
public async Task When_Default_Flyout_Date()
{
var now = DateTimeOffset.UtcNow;
var datePicker = new Microsoft.UI.Xaml.Controls.CalendarDatePicker();

TestServices.WindowHelper.WindowContent = datePicker;

await TestServices.WindowHelper.WaitForLoaded(datePicker);

datePicker.IsCalendarOpen = true;

await WindowHelper.WaitFor(() => VisualTreeHelper.GetOpenPopupsForXamlRoot(datePicker.XamlRoot).Count > 0);
var popup = VisualTreeHelper.GetOpenPopupsForXamlRoot(datePicker.XamlRoot).First();
var child = (FlyoutPresenter)popup.Child;
var calendarView = (CalendarView)child.Content;
Assert.AreEqual(now.Day, calendarView.m_lastDisplayedDate.Day);
Assert.AreEqual(now.Month, calendarView.m_lastDisplayedDate.Month);
Assert.AreEqual(now.Year, calendarView.m_lastDisplayedDate.Year);
}
#endif
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Automation.Provider;
using Microsoft.UI.Xaml.Controls;
Expand Down Expand Up @@ -346,6 +345,41 @@ private async Task When_Flyout_Closed_FlyoutBase_Closed_Invoked(bool useNative)
#endif
}

#if __ANDROID__ || __IOS__
[TestMethod]
public async Task When_Default_Flyout_Date_Native()
{
var now = DateTimeOffset.UtcNow;
var datePicker = new Microsoft.UI.Xaml.Controls.DatePicker();
datePicker.UseNativeStyle = true;

TestServices.WindowHelper.WindowContent = datePicker;

await TestServices.WindowHelper.WaitForLoaded(datePicker);

await DateTimePickerHelper.OpenDateTimePicker(datePicker);

var openFlyouts = FlyoutBase.OpenFlyouts;
Assert.AreEqual(1, openFlyouts.Count);
var associatedFlyout = openFlyouts[0];
Assert.IsInstanceOfType(associatedFlyout, typeof(Microsoft.UI.Xaml.Controls.NativeDatePickerFlyout));

var datePickerFlyout = (NativeDatePickerFlyout)associatedFlyout;

try
{
Assert.AreEqual(DatePicker.NullDateSentinelValue, datePickerFlyout.Date);
Assert.AreEqual(now.Day, datePickerFlyout.NativeDialogDate.Day);
Assert.AreEqual(now.Month, datePickerFlyout.NativeDialogDate.Month);
Assert.AreEqual(now.Year, datePickerFlyout.NativeDialogDate.Year);
}
finally
{
datePickerFlyout.Close();
}
}
#endif

#if __IOS__
[TestMethod]
[UnoWorkItem("https://github.com/unoplatform/uno/issues/15263")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public partial class CalendarView : Control
// 1. display Date if it is requested, if it is not requested, then
// 2. Today, if Today is not in given min/max range, then
// 3. the closest date to Today (i.e. the coerced date of Today)
DateTime m_lastDisplayedDate;
internal DateTime m_lastDisplayedDate;

DateTime m_today;
// m_minDate and m_maxDate are effective min/max dates, which could be different
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/Controls/DatePicker/DatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ void RefreshFlyoutButtonAutomationName()

/* static */

private static DateTimeOffset NullDateSentinelValue { get; } =
internal static DateTimeOffset NullDateSentinelValue { get; } =
default(WindowsFoundationDateTime);

void GetTodaysDate(out DateTimeOffset? todaysDate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public NativeDatePickerFlyout()

internal bool IsNativeDialogOpen => _dialog?.IsShowing ?? false;

internal DateTimeOffset NativeDialogDate => _dialog.DatePicker.DateTime;

protected internal override void Open()
{
var date = Date;
// If we're setting the date to the null sentinel value,
// we'll instead set it to the current date for the purposes
// of where to place the user's position in the looping selectors.
if (date.Ticks == DatePicker.DEFAULT_DATE_TICKS)
if (date == DatePicker.NullDateSentinelValue)
{
var temp = new global::Windows.Globalization.Calendar();
var calendar = new global::Windows.Globalization.Calendar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ private NativeDatePickerFlyoutPresenter _presenter

internal bool IsNativeDialogOpen { get; private set; }

internal DateTimeOffset NativeDialogDate => _selector.Date;

public static DependencyProperty UseNativeMinMaxDatesProperty { get; } = DependencyProperty.Register(
"UseNativeMinMaxDates",
typeof(bool),
Expand Down Expand Up @@ -145,7 +147,7 @@ private void DatePickerFlyout_Opening(object sender, object e)
// If we're setting the date to the null sentinel value,
// we'll instead set it to the current date for the purposes
// of where to place the user's position in the looping selectors.
if (date.Ticks == DatePicker.DEFAULT_DATE_TICKS)
if (date == DatePicker.NullDateSentinelValue)
{
var temp = new global::Windows.Globalization.Calendar();
var calendar = new global::Windows.Globalization.Calendar(
Expand Down

0 comments on commit 242f4f8

Please sign in to comment.