提交 4ba972b6 编写于 作者: Y Youssef Victor

fix: Align Duration behavior with WinUI

上级 4064dd3b
...@@ -84,5 +84,78 @@ namespace Uno.UI.Tests.Animations ...@@ -84,5 +84,78 @@ namespace Uno.UI.Tests.Animations
Assert.AreEqual(new Duration(TimeSpan.FromSeconds(48)), duration.Add(new Duration(TimeSpan.FromSeconds(24)))); Assert.AreEqual(new Duration(TimeSpan.FromSeconds(48)), duration.Add(new Duration(TimeSpan.FromSeconds(24))));
Assert.AreEqual(new Duration(TimeSpan.FromSeconds(1)), duration.Subtract(new Duration(TimeSpan.FromSeconds(23)))); Assert.AreEqual(new Duration(TimeSpan.FromSeconds(1)), duration.Subtract(new Duration(TimeSpan.FromSeconds(23))));
} }
[TestMethod]
[DataRow("00:00:05", "00:00:03", "00:00:08")]
[DataRow("00:00:05", "Automatic", "Automatic")]
[DataRow("00:00:05", "Forever", "Forever")]
[DataRow("Automatic", "00:00:03", "Automatic")]
[DataRow("Automatic", "Automatic", "Automatic")]
[DataRow("Automatic", "Forever", "Automatic")]
[DataRow("Forever", "00:00:03", "Forever")]
[DataRow("Forever", "Automatic", "Automatic")]
[DataRow("Forever", "Forever", "Forever")]
public void When_Adding_Durations(string d1, string d2, string expected)
{
var duration1 = StringToDuration(d1);
var duration2 = StringToDuration(d2);
var expectedDuration = StringToDuration(expected);
Assert.AreEqual(expectedDuration, duration1 + duration2);
Assert.AreEqual(expectedDuration, duration1.Add(duration2));
Assert.AreEqual(expectedDuration, duration2.Add(duration1));
}
[TestMethod]
[DataRow("00:00:05", "00:00:03", "00:00:02")]
[DataRow("00:00:05", "Automatic", "Automatic")]
[DataRow("00:00:05", "Forever", "Automatic")]
[DataRow("Automatic", "00:00:03", "Automatic")]
[DataRow("Automatic", "Automatic", "Automatic")]
[DataRow("Automatic", "Forever", "Automatic")]
[DataRow("Forever", "00:00:03", "Forever")]
[DataRow("Forever", "Automatic", "Automatic")]
[DataRow("Forever", "Forever", "Automatic")]
public void When_Subtracting_Durations(string d1, string d2, string expected)
{
var duration1 = StringToDuration(d1);
var duration2 = StringToDuration(d2);
var expectedDuration = StringToDuration(expected);
Assert.AreEqual(expectedDuration, duration1 - duration2);
Assert.AreEqual(expectedDuration, duration1.Subtract(duration2));
}
[TestMethod]
[DataRow("00:00:05", "00:00:03", 1, true, true, false, false)]
[DataRow("00:00:03", "00:00:05", -1, false, false, true, true)]
[DataRow("00:00:03", "00:00:03", 0, false, true, false, true)]
[DataRow("00:00:05", "Automatic", 1, false, false, false, false)]
[DataRow("00:00:05", "Forever", -1, false, false, true, true)]
[DataRow("Automatic", "00:00:03", -1, false, false, false, false)]
[DataRow("Automatic", "Automatic", 0, false, true, false, true)]
[DataRow("Automatic", "Forever", -1, false, false, false, false)]
[DataRow("Forever", "00:00:03", 1, true, true, false, false)]
[DataRow("Forever", "Automatic", 1, false, false, false, false)]
[DataRow("Forever", "Forever", 0, false, true, false, true)]
public void When_Comparing_Durations(string d1, string d2, int expectedCompare, bool isGreaterThan, bool isGreaterThanOrEquals, bool isLessThan, bool isLessThanOrEquals)
{
var duration1 = StringToDuration(d1);
var duration2 = StringToDuration(d2);
Assert.AreEqual(expectedCompare, duration1.CompareTo(duration2));
Assert.AreEqual(expectedCompare, Duration.Compare(duration1, duration2));
Assert.AreEqual(isGreaterThan, duration1 > duration2);
Assert.AreEqual(isGreaterThanOrEquals, duration1 >= duration2);
Assert.AreEqual(isLessThan, duration1 < duration2);
Assert.AreEqual(isLessThanOrEquals, duration1 <= duration2);
}
private Duration StringToDuration(string s)
{
return s switch
{
"Automatic" => Duration.Automatic,
"Forever" => Duration.Forever,
_ => new Duration(TimeSpan.Parse(s)),
};
}
} }
} }
using Uno.UI.Extensions; using Uno.UI.Extensions;
using System; using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Text;
namespace Windows.UI.Xaml namespace Windows.UI.Xaml
{ {
...@@ -11,7 +9,6 @@ namespace Windows.UI.Xaml ...@@ -11,7 +9,6 @@ namespace Windows.UI.Xaml
private static readonly string __automatic = "Automatic"; private static readonly string __automatic = "Automatic";
private static readonly string __forever = "Forever"; private static readonly string __forever = "Forever";
public Duration(TimeSpan timeSpan) public Duration(TimeSpan timeSpan)
{ {
Type = DurationType.TimeSpan; Type = DurationType.TimeSpan;
...@@ -24,48 +21,67 @@ namespace Windows.UI.Xaml ...@@ -24,48 +21,67 @@ namespace Windows.UI.Xaml
public static implicit operator Duration(string timeSpan) public static implicit operator Duration(string timeSpan)
=> timeSpan != null ? new Duration(TimeSpan.Parse(timeSpan, CultureInfo.InvariantCulture)) : new Duration(TimeSpan.Zero); => timeSpan != null ? new Duration(TimeSpan.Parse(timeSpan, CultureInfo.InvariantCulture)) : new Duration(TimeSpan.Zero);
public bool HasTimeSpan public bool HasTimeSpan => Type == DurationType.TimeSpan;
{
get
{
return this.Type == DurationType.TimeSpan;
}
}
public static Duration Forever public static Duration Forever => new Duration() { Type = DurationType.Forever };
{
get
{
return new Duration() { Type = DurationType.Forever };
}
}
public static Duration Automatic public static Duration Automatic => new Duration() { Type = DurationType.Automatic };
{
get
{
return new Duration() { Type = DurationType.Automatic };
}
}
public Duration Add(Duration duration) public Duration Add(Duration duration)
{ {
// We have 9 cases:
// (1) TimeSpan + TimeSpan
// (2) TimeSpan + Automatic
// (3) TimeSpan + Forever
// (4) Automatic + TimeSpan
// (5) Automatic + Automatic
// (6) Automatic + Forever
// (7) Forever + TimeSpan
// (8) Forever + Automatic
// (9) Forever + Forever
// Case (1)
if (this.Type == DurationType.TimeSpan && duration.Type == DurationType.TimeSpan) if (this.Type == DurationType.TimeSpan && duration.Type == DurationType.TimeSpan)
{ {
return new Duration(this.TimeSpan.Add(duration.TimeSpan)); return new Duration(this.TimeSpan.Add(duration.TimeSpan));
} }
return this; // Case (2), (4), (5), (6), (8)
if (this.Type == DurationType.Automatic || duration.Type == DurationType.Automatic)
{
return Automatic;
}
// Case (3), (7), (9)
return Forever;
} }
public Duration Subtract(Duration duration) public Duration Subtract(Duration duration)
{ {
// We have 9 cases:
// (1) TimeSpan - TimeSpan ===> Subtract spans
// (2) TimeSpan - Automatic ===> Automatic
// (3) TimeSpan - Forever ===> Automatic
// (4) Automatic - TimeSpan ===> Automatic
// (5) Automatic - Automatic ===> Automatic
// (6) Automatic - Forever ===> Automatic
// (7) Forever - TimeSpan ===> Forever
// (8) Forever - Automatic ===> Automatic
// (9) Forever - Forever ===> Automatic
// Case (1)
if (this.Type == DurationType.TimeSpan && duration.Type == DurationType.TimeSpan) if (this.Type == DurationType.TimeSpan && duration.Type == DurationType.TimeSpan)
{ {
return new Duration(this.TimeSpan.Subtract(duration.TimeSpan)); return new Duration(this.TimeSpan.Subtract(duration.TimeSpan));
} }
return this; // Case (7)
if (this.Type == DurationType.Forever && duration.Type == DurationType.TimeSpan)
{
return Forever;
}
return Automatic;
} }
#region Operator overrides #region Operator overrides
...@@ -82,22 +98,67 @@ namespace Windows.UI.Xaml ...@@ -82,22 +98,67 @@ namespace Windows.UI.Xaml
public static bool operator >(Duration t1, Duration t2) public static bool operator >(Duration t1, Duration t2)
{ {
return Compare(t1, t2) > 0; if (t1.HasTimeSpan && t2.HasTimeSpan)
{
return t1.TimeSpan > t2.TimeSpan;
}
if (t1.HasTimeSpan && t2.Type == DurationType.Forever)
{
return false;
}
if (t1.Type == DurationType.Forever && t2.HasTimeSpan)
{
return true;
}
return false;
} }
public static bool operator >=(Duration t1, Duration t2) public static bool operator >=(Duration t1, Duration t2)
{ {
return Compare(t1, t2) >= 0; if (t1.Type == DurationType.Automatic && t2.Type == DurationType.Automatic)
{
return true;
}
if (t1.Type == DurationType.Automatic || t2.Type == DurationType.Automatic)
{
return false;
}
return !(t1 < t2);
} }
public static bool operator <(Duration t1, Duration t2) public static bool operator <(Duration t1, Duration t2)
{ {
return Compare(t1, t2) < 0; if (t1.HasTimeSpan && t2.HasTimeSpan)
{
return t1.TimeSpan < t2.TimeSpan;
}
if (t1.HasTimeSpan && t2.Type == DurationType.Forever)
{
return true;
}
return false;
} }
public static bool operator <=(Duration t1, Duration t2) public static bool operator <=(Duration t1, Duration t2)
{ {
return Compare(t1, t2) <= 0; if (t1.Type == DurationType.Automatic && t2.Type == DurationType.Automatic)
{
return true;
}
if (t1.Type == DurationType.Automatic || t2.Type == DurationType.Automatic)
{
return false;
}
return !(t1 > t2);
} }
public static Duration operator +(Duration t1, Duration t2) public static Duration operator +(Duration t1, Duration t2)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册