1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Collections.Generic;
7 using System.Globalization;
8 using Xunit;
9 
10 namespace Windows.UI.Xaml.Media.Animation.Tests
11 {
12     public class RepeatBehaviorTests
13     {
14         [Fact]
Ctor_Default()15         public void Ctor_Default()
16         {
17             var repeatBehaviour = new RepeatBehavior();
18             Assert.True(repeatBehaviour.HasCount);
19             Assert.Equal(0, repeatBehaviour.Count);
20 
21             Assert.False(repeatBehaviour.HasDuration);
22             Assert.Equal(TimeSpan.Zero, repeatBehaviour.Duration);
23 
24             Assert.Equal(RepeatBehaviorType.Count, repeatBehaviour.Type);
25         }
26 
27         [Theory]
28         [InlineData(0)]
29         [InlineData(10)]
30         [InlineData(double.MaxValue)]
Ctor_Count(double count)31         public void Ctor_Count(double count)
32         {
33             var repeatBehaviour = new RepeatBehavior(count);
34             Assert.True(repeatBehaviour.HasCount);
35             Assert.Equal(count, repeatBehaviour.Count);
36 
37             Assert.False(repeatBehaviour.HasDuration);
38             Assert.Equal(TimeSpan.Zero, repeatBehaviour.Duration);
39 
40             Assert.Equal(RepeatBehaviorType.Count, repeatBehaviour.Type);
41             Assert.Equal(count.GetHashCode(), repeatBehaviour.GetHashCode());
42         }
43 
44         [Theory]
45         [InlineData(-1)]
46         [InlineData(double.NaN)]
47         [InlineData(double.PositiveInfinity)]
48         [InlineData(double.NegativeInfinity)]
Ctor_InvalidCount_ThrowsArgumentOutOfRangeException(double count)49         public void Ctor_InvalidCount_ThrowsArgumentOutOfRangeException(double count)
50         {
51             AssertExtensions.Throws<ArgumentOutOfRangeException>("count", () => new RepeatBehavior(count));
52         }
53 
54         [Theory]
55         [InlineData(0)]
56         [InlineData(1)]
Ctor_TimeSpan(int seconds)57         public void Ctor_TimeSpan(int seconds)
58         {
59             var repeatBehaviour = new RepeatBehavior(TimeSpan.FromSeconds(seconds));
60             Assert.False(repeatBehaviour.HasCount);
61             Assert.Equal(0, repeatBehaviour.Count);
62 
63             Assert.True(repeatBehaviour.HasDuration);
64             Assert.Equal(TimeSpan.FromSeconds(seconds), repeatBehaviour.Duration);
65 
66             Assert.Equal(RepeatBehaviorType.Duration, repeatBehaviour.Type);
67             Assert.Equal(TimeSpan.FromSeconds(seconds).GetHashCode(), repeatBehaviour.GetHashCode());
68         }
69 
70         [Fact]
Ctor_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()71         public void Ctor_NegativeTimeSpan_ThrowsArgumentOutOfRangeException()
72         {
73             AssertExtensions.Throws<ArgumentOutOfRangeException>("duration", () => new RepeatBehavior(TimeSpan.FromTicks(-1)));
74         }
75 
76         [Fact]
Forever_Get_ReturnsExpected()77         public void Forever_Get_ReturnsExpected()
78         {
79             RepeatBehavior forever = RepeatBehavior.Forever;
80             Assert.False(forever.HasCount);
81             Assert.Equal(0, forever.Count);
82 
83             Assert.False(forever.HasDuration);
84             Assert.Equal(TimeSpan.Zero, forever.Duration);
85 
86             Assert.Equal(RepeatBehaviorType.Forever, forever.Type);
87 
88             Assert.Equal(int.MaxValue - 42, forever.GetHashCode());
89         }
90 
91         [Theory]
92         [InlineData(-1)]
93         [InlineData(0)]
94         [InlineData(10)]
95         [InlineData(double.MaxValue)]
96         [InlineData(double.NaN)]
97         [InlineData(double.PositiveInfinity)]
98         [InlineData(double.NegativeInfinity)]
Count_Set_GetReturnsExpected(double value)99         public void Count_Set_GetReturnsExpected(double value)
100         {
101             var repeatBehaviour = new RepeatBehavior(TimeSpan.MaxValue) { Count = value };
102             Assert.False(repeatBehaviour.HasCount);
103             Assert.Equal(value, repeatBehaviour.Count);
104 
105             // Although we set a count, the type is unchanged.
106             Assert.Equal(RepeatBehaviorType.Duration, repeatBehaviour.Type);
107         }
108 
109         [Theory]
110         [InlineData(0)]
111         [InlineData(1)]
112         [InlineData(-1)]
Duration_Set_GetReturnsExpected(int seconds)113         public void Duration_Set_GetReturnsExpected(int seconds)
114         {
115             var repeatBehaviour = new RepeatBehavior(1) { Duration = TimeSpan.FromSeconds(seconds) };
116             Assert.False(repeatBehaviour.HasDuration);
117             Assert.Equal(TimeSpan.FromSeconds(seconds), repeatBehaviour.Duration);
118 
119             // Although we set a duration, the type is unchanged.
120             Assert.Equal(RepeatBehaviorType.Count, repeatBehaviour.Type);
121         }
122 
123         [Theory]
124         [InlineData(RepeatBehaviorType.Count - 1)]
125         [InlineData(RepeatBehaviorType.Count)]
126         [InlineData(RepeatBehaviorType.Duration)]
127         [InlineData(RepeatBehaviorType.Forever)]
128         [InlineData(RepeatBehaviorType.Forever + 1)]
Type_Set_GetReturnsExpected(RepeatBehaviorType type)129         public void Type_Set_GetReturnsExpected(RepeatBehaviorType type)
130         {
131             var repeatBehaviour = new RepeatBehavior(1) { Type = type };
132             Assert.Equal(type == RepeatBehaviorType.Count, repeatBehaviour.HasCount);
133             Assert.Equal(type == RepeatBehaviorType.Duration, repeatBehaviour.HasDuration);
134         }
135 
Equals_TestData()136         public static IEnumerable<object[]> Equals_TestData()
137         {
138             yield return new object[] { new RepeatBehavior(2), new RepeatBehavior(2), true };
139             yield return new object[] { new RepeatBehavior(2), new RepeatBehavior(1), false };
140             yield return new object[] { new RepeatBehavior(2), RepeatBehavior.Forever, false };
141             yield return new object[] { new RepeatBehavior(2), new RepeatBehavior(TimeSpan.FromSeconds(2)), false };
142 
143             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), new RepeatBehavior(TimeSpan.FromSeconds(2)), true };
144             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), new RepeatBehavior(TimeSpan.FromSeconds(1)), false };
145             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), RepeatBehavior.Forever, false };
146             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), new RepeatBehavior(2), false };
147 
148             yield return new object[] { RepeatBehavior.Forever, RepeatBehavior.Forever, true };
149             yield return new object[] { RepeatBehavior.Forever, new RepeatBehavior(TimeSpan.FromSeconds(2)), false };
150             yield return new object[] { RepeatBehavior.Forever, new RepeatBehavior(2), false };
151 
152             yield return new object[] { new RepeatBehavior { Type = RepeatBehaviorType.Count - 1 }, new RepeatBehavior { Type = RepeatBehaviorType.Count - 1 }, false };
153             yield return new object[] { new RepeatBehavior { Type = RepeatBehaviorType.Forever + 1 }, new RepeatBehavior { Type = RepeatBehaviorType.Count + 1 }, false };
154             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), new object(), false };
155             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), null, false };
156         }
157 
158         [Theory]
159         [MemberData(nameof(Equals_TestData))]
Equals_Object_ReturnsExpected(RepeatBehavior repeatBehaviour, object other, bool expected)160         public void Equals_Object_ReturnsExpected(RepeatBehavior repeatBehaviour, object other, bool expected)
161         {
162             Assert.Equal(expected, repeatBehaviour.Equals(other));
163             if (other is RepeatBehavior otherRepeatBehaviour)
164             {
165                 Assert.Equal(expected, RepeatBehavior.Equals(repeatBehaviour, otherRepeatBehaviour));
166                 Assert.Equal(expected, repeatBehaviour.Equals(otherRepeatBehaviour));
167                 Assert.Equal(expected, repeatBehaviour == otherRepeatBehaviour);
168                 Assert.Equal(!expected, repeatBehaviour != otherRepeatBehaviour);
169 
170                 if (repeatBehaviour.Type >= RepeatBehaviorType.Count && repeatBehaviour.Type <= RepeatBehaviorType.Forever)
171                 {
172                     Assert.Equal(expected, repeatBehaviour.GetHashCode().Equals(otherRepeatBehaviour.GetHashCode()));
173                 }
174                 else if (repeatBehaviour.Type == otherRepeatBehaviour.Type)
175                 {
176                     Assert.Equal(repeatBehaviour.GetHashCode(), otherRepeatBehaviour.GetHashCode());
177                 }
178             }
179         }
180 
ToString_TestData()181         public static IEnumerable<object[]> ToString_TestData()
182         {
183             yield return new object[] { RepeatBehavior.Forever, null, null, "Forever" };
184             yield return new object[] { RepeatBehavior.Forever, "InvalidFormat", CultureInfo.CurrentCulture, "Forever" };
185 
186             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), null, null, TimeSpan.FromSeconds(2).ToString() };
187             yield return new object[] { new RepeatBehavior(TimeSpan.FromSeconds(2)), "InvalidFormat", CultureInfo.CurrentCulture, TimeSpan.FromSeconds(2).ToString() };
188 
189             var culture = new CultureInfo("en-US");
190             culture.NumberFormat.NumberDecimalSeparator = "|";
191             yield return new object[] { new RepeatBehavior(2.2), "abc", culture, "abcx" };
192             yield return new object[] { new RepeatBehavior(2.2), "N4", culture, "2|2000x" };
193             yield return new object[] { new RepeatBehavior(2.2), null, culture, "2|2x" };
194 
195             yield return new object[] { new RepeatBehavior(2.2), null, null, $"{2.2.ToString()}x" };
196         }
197 
198         [Theory]
199         [MemberData(nameof(ToString_TestData))]
ToString_Invoke_ReturnsExpected(RepeatBehavior keyTime, string format, IFormatProvider formatProvider, string expected)200         public void ToString_Invoke_ReturnsExpected(RepeatBehavior keyTime, string format, IFormatProvider formatProvider, string expected)
201         {
202             if (format == null)
203             {
204                 if (formatProvider == null)
205                 {
206                     Assert.Equal(expected, keyTime.ToString());
207                 }
208 
209                 Assert.Equal(expected, keyTime.ToString(formatProvider));
210             }
211 
212             Assert.Equal(expected, ((IFormattable)keyTime).ToString(format, formatProvider));
213         }
214     }
215 }