Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue31124.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 31124, "DatePicker should maintain today's date when set to null", PlatformAffected.macOS)]
public class Issue31124 : ContentPage
{
DatePicker _datePicker;
Button _setNullButton;
Label _dateLabel;

public Issue31124()
{
InitializeComponent();
}

void InitializeComponent()
{
Title = "DatePicker Null Test";

_datePicker = new DatePicker
{
AutomationId = "TestDatePicker",
Date = DateTime.Today
};

_setNullButton = new Button
{
Text = "Set Date to Null",
AutomationId = "SetNullButton"
};
_setNullButton.Clicked += Button_Clicked;

_dateLabel = new Label
{
Text = $"Current Date: {_datePicker.Date:d}",
AutomationId = "DateLabel"
};

// Listen for date changes to update the label
_datePicker.DateSelected += (s, e) =>
{
_dateLabel.Text = $"Current Date: {e.NewDate:d}";
};

Content = new VerticalStackLayout
{
Padding = new Thickness(20),
Spacing = 20,
Children =
{
new Label { Text = "Test: Setting DatePicker.Date to null should remove today's date" },
_datePicker,
_setNullButton,
_dateLabel,
new Label
{
Text = "Expected: After clicking button, date should be null",
FontSize = 12,
TextColor = Colors.Gray
}
}
};
}

void Button_Clicked(object sender, EventArgs e)
{
// Set datePicker.Date to null
_datePicker.Date = null;

// Update the label to show the current date
_dateLabel.Text = $"Current Date: {_datePicker.Date:d}";
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line will throw a NullReferenceException when _datePicker.Date is null. The format specifier :d cannot be applied to a null DateTime?. Consider using conditional formatting like _datePicker.Date?.ToString("d") ?? "(null)" or similar null-safe approach.

Suggested change
_dateLabel.Text = $"Current Date: {_datePicker.Date:d}";
_dateLabel.Text = $"Current Date: {_datePicker.Date?.ToString("d") ?? "(null)"}";

Copilot uses AI. Check for mistakes.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Text.RegularExpressions;
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The System.Text.RegularExpressions namespace is imported but never used in this file. Consider removing this unused import.

Suggested change
using System.Text.RegularExpressions;

Copilot uses AI. Check for mistakes.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue31124 : _IssuesUITest
{
public Issue31124(TestDevice testDevice) : base(testDevice)
{
}

public override string Issue => "DatePicker should maintain today's date when set to null";

[Test]
[Category(UITestCategories.DatePicker)]
public void DatePickerShouldMaintainTodaysDateWhenSetToNull()
{
// Find the initial date label text, should show today's date
var initialDateLabel = App.WaitForElement("DateLabel").GetText();
Assert.That(ContainsValidDate(initialDateLabel), Is.True,
"DatePicker should initially show today's date");

// Click the button to set date to null
App.WaitForElement("SetNullButton").Click();

// Verify the date is empty after setting to null
var dateAfterNull = App.WaitForElement("DateLabel").GetText();
Assert.That(ContainsValidDate(dateAfterNull), Is.False,
"DatePicker should be empty after being set to null");
}

static bool IsValidDate(string? input)
{
return !string.IsNullOrWhiteSpace(input) && DateTime.TryParse(input, out _);
}

static bool ContainsValidDate(string? labelText)
{
if (string.IsNullOrWhiteSpace(labelText))
return false;

// Extract the date part after "Current Date: "
var prefix = "Current Date: ";
if (labelText.StartsWith(prefix))
{
var datePart = labelText.Substring(prefix.Length);
return IsValidDate(datePart);
}

return false;
}
}
Loading