Skip to content

Commit 4c97c6d

Browse files
Vignesh-SF3580rmarinho
authored andcommitted
added tests
1 parent 0406137 commit 4c97c6d

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed

src/Controls/src/Core/BindableProperty.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public sealed class BindableProperty
4747
{ typeof(Maui.Graphics.Color), new ColorTypeConverter() },
4848
{ typeof(ImageSource), new ImageSourceConverter() },
4949
#if NET6_0_OR_GREATER
50+
{ typeof(DateTime), new DateTimeTypeConverter() },
5051
{ typeof(DateTime?), new DateTimeTypeConverter() },
52+
{ typeof(TimeSpan), new TimeSpanTypeConverter() },
5153
{ typeof(TimeSpan?), new TimeSpanTypeConverter() }
5254
#endif
5355
};
@@ -229,6 +231,11 @@ internal bool TryConvert(ref object value)
229231
return true;
230232
}
231233
if (KnownTypeConverters.TryGetValue(returnType, out TypeConverter typeConverterTo) && typeConverterTo.CanConvertFrom(valueType))
234+
{
235+
value = typeConverterTo.ConvertFrom(value);
236+
return true;
237+
}
238+
if (KnownTypeConverters.TryGetValue(returnType, out typeConverterTo) && typeConverterTo.CanConvertFrom(typeof(string)))
232239
{
233240
value = typeConverterTo.ConvertFromInvariantString(value.ToString());
234241
return true;

src/Controls/tests/Core.UnitTests/DateOnlyTypeConverterTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ public void DateOnlyToDatePickerBinding()
5050
Assert.Equal(expectedDateTime, datePicker.Date);
5151
}
5252

53+
[Fact]
54+
public void DateOnlyToNonNullableBinding()
55+
{
56+
var dateProperty = BindableProperty.Create("Date", typeof(DateTime), typeof(DatePicker), null, BindingMode.TwoWay);
57+
var source = new Issue20438DatePickerViewModel
58+
{
59+
SelectedDate = new DateOnly(2025, 3, 15)
60+
};
61+
var bo = new MockBindable { BindingContext = source };
62+
63+
bo.SetBinding(dateProperty, "SelectedDate");
64+
var expectedDateTime = new DateTime(2025, 3, 15);
65+
Assert.Equal(expectedDateTime, bo.GetValue(dateProperty));
66+
}
67+
5368
public class Issue20438DatePickerViewModel
5469
{
5570
public DateOnly SelectedDate { get; set; }

src/Controls/tests/Core.UnitTests/TimeOnlyTypeConverterTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ public void TimeOnlyToTimePickerBinding()
4949
Assert.Equal(expectedTimeSpan, timePicker.Time);
5050
}
5151

52+
[Fact]
53+
public void TimeOnlyToNonNullableBinding()
54+
{
55+
var timeProperty = BindableProperty.Create("Time", typeof(TimeSpan), typeof(TimePicker), null, BindingMode.TwoWay);
56+
var source = new Issue20438TimePickerViewModel
57+
{
58+
SelectedTime = new TimeOnly(14, 30, 0)
59+
};
60+
var bo = new MockBindable { BindingContext = source };
61+
62+
bo.SetBinding(timeProperty, "SelectedTime");
63+
var expectedTimeSpan = new TimeSpan(14, 30, 0);
64+
Assert.Equal(expectedTimeSpan, bo.GetValue(timeProperty));
65+
}
66+
5267
public class Issue20438TimePickerViewModel
5368
{
5469
public TimeOnly SelectedTime { get; set; }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Issue20438"
5+
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
6+
x:Name="page">
7+
<ContentPage.BindingContext>
8+
<local:Issue20438ViewModel/>
9+
</ContentPage.BindingContext>
10+
<StackLayout>
11+
<DatePicker x:Name="datePicker"
12+
Date="{Binding SelectedDate}"/>
13+
<TimePicker x:Name="timePicker"
14+
Time="{Binding SelectedTime}"/>
15+
<local:Issue20438CustomDatePicker x:Name="customDatePicker"
16+
NonNullableDateTime="{Binding SelectedDate}"/>
17+
<local:Issue20438CustomTimePicker x:Name="customTimePicker"
18+
NonNullableTimeSpan="{Binding SelectedTime}"/>
19+
</StackLayout>
20+
</ContentPage>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#if NET6_0_OR_GREATER
2+
using System;
3+
using System.ComponentModel;
4+
using NUnit.Framework;
5+
6+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
7+
8+
[XamlProcessing(XamlInflator.Default, true)]
9+
public partial class Issue20438 : ContentPage
10+
{
11+
public Issue20438()
12+
{
13+
InitializeComponent();
14+
}
15+
16+
[TestFixture]
17+
public class Tests
18+
{
19+
[Test]
20+
public void DateOnlyBinding([Values] XamlInflator inflator)
21+
{
22+
var page = new Issue20438(inflator);
23+
Assert.That(page.datePicker.Date, Is.EqualTo(new DateTime(2025, 3, 15)));
24+
}
25+
26+
[Test]
27+
public void TimeOnlyBinding([Values] XamlInflator inflator)
28+
{
29+
var page = new Issue20438(inflator);
30+
Assert.That(page.timePicker.Time, Is.EqualTo(new TimeSpan(14, 30, 0)));
31+
}
32+
33+
[Test]
34+
public void DateOnlyToNonNullableDateTime([Values] XamlInflator inflator)
35+
{
36+
var page = new Issue20438(inflator);
37+
Assert.That(page.customDatePicker.NonNullableDateTime, Is.EqualTo(new DateTime(2025, 3, 15)));
38+
}
39+
40+
[Test]
41+
public void TimeOnlyToNonNullableTimeSpan([Values] XamlInflator inflator)
42+
{
43+
var page = new Issue20438(inflator);
44+
Assert.That(page.customTimePicker.NonNullableTimeSpan, Is.EqualTo(new TimeSpan(14, 30, 0)));
45+
}
46+
}
47+
}
48+
49+
public class Issue20438CustomDatePicker : DatePicker
50+
{
51+
public static readonly BindableProperty NonNullableDateTimeProperty =
52+
BindableProperty.Create(nameof(NonNullableDateTime), typeof(DateTime), typeof(Issue20438CustomDatePicker), default(DateTime));
53+
54+
[TypeConverter(typeof(DateTimeTypeConverter))]
55+
public DateTime NonNullableDateTime
56+
{
57+
get => (DateTime)GetValue(NonNullableDateTimeProperty);
58+
set => SetValue(NonNullableDateTimeProperty, value);
59+
}
60+
}
61+
62+
public class Issue20438CustomTimePicker : TimePicker
63+
{
64+
public static readonly BindableProperty NonNullableTimeSpanProperty =
65+
BindableProperty.Create(nameof(NonNullableTimeSpan), typeof(TimeSpan), typeof(Issue20438CustomTimePicker), default(TimeSpan));
66+
67+
[TypeConverter(typeof(TimeSpanTypeConverter))]
68+
public TimeSpan NonNullableTimeSpan
69+
{
70+
get => (TimeSpan)GetValue(NonNullableTimeSpanProperty);
71+
set => SetValue(NonNullableTimeSpanProperty, value);
72+
}
73+
}
74+
75+
public class Issue20438ViewModel
76+
{
77+
public DateOnly SelectedDate { get; set; } = new DateOnly(2025, 3, 15);
78+
public TimeOnly SelectedTime { get; set; } = new TimeOnly(14, 30, 0);
79+
}
80+
#endif

0 commit comments

Comments
 (0)