Skip to content

Commit 2aae986

Browse files
committed
Added unit tests for new AnimationBuilder.Start overload
1 parent e859b10 commit 2aae986

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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.Threading.Tasks;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Microsoft.Toolkit.Uwp;
8+
using Windows.UI.Xaml.Controls;
9+
using Microsoft.Toolkit.Uwp.UI.Animations;
10+
using System.Numerics;
11+
using Microsoft.Toolkit.Uwp.UI;
12+
using System;
13+
using Windows.UI.Xaml.Media;
14+
15+
namespace UnitTests.UWP.UI.Animations
16+
{
17+
[TestClass]
18+
[TestCategory("Test_AnimationBuilderStart")]
19+
public class Test_AnimationBuilderStart : VisualUITestBase
20+
{
21+
[TestMethod]
22+
public async Task Start_WithCallback_CompositionOnly()
23+
{
24+
await App.DispatcherQueue.EnqueueAsync(async () =>
25+
{
26+
var button = new Button();
27+
var grid = new Grid() { Children = { button } };
28+
29+
await SetTestContentAsync(grid);
30+
31+
var tcs = new TaskCompletionSource<object>();
32+
33+
AnimationBuilder.Create()
34+
.Scale(
35+
to: new Vector3(1.2f, 1, 1),
36+
delay: TimeSpan.FromMilliseconds(400))
37+
.Opacity(
38+
to: 0.7,
39+
duration: TimeSpan.FromSeconds(1))
40+
.Start(button, () => tcs.SetResult(null));
41+
42+
await tcs.Task;
43+
44+
// Note: we're just testing Scale and Opacity here as they're among the Visual properties that
45+
// are kept in sync on the Visual object after an animation completes, so we can use their
46+
// values below to check that the animations have run correctly. There is no particular reason
47+
// why we chose these two animations specifically other than this. For instance, checking
48+
// Visual.TransformMatrix.Translation or Visual.Offset after an animation targeting those
49+
// properties doesn't correctly report the final value and remains out of sync ¯\_(ツ)_/¯
50+
Assert.AreEqual(button.GetVisual().Scale, new Vector3(1.2f, 1, 1));
51+
Assert.AreEqual(button.GetVisual().Opacity, 0.7f);
52+
});
53+
}
54+
55+
[TestMethod]
56+
public async Task Start_WithCallback_XamlOnly()
57+
{
58+
await App.DispatcherQueue.EnqueueAsync(async () =>
59+
{
60+
var button = new Button();
61+
var grid = new Grid() { Children = { button } };
62+
63+
await SetTestContentAsync(grid);
64+
65+
var tcs = new TaskCompletionSource<object>();
66+
67+
AnimationBuilder.Create()
68+
.Translation(
69+
to: new Vector2(80, 20),
70+
layer: FrameworkLayer.Xaml)
71+
.Scale(
72+
to: new Vector2(1.2f, 1),
73+
delay: TimeSpan.FromMilliseconds(400),
74+
layer: FrameworkLayer.Xaml)
75+
.Opacity(
76+
to: 0.7,
77+
duration: TimeSpan.FromSeconds(1),
78+
layer: FrameworkLayer.Xaml)
79+
.Start(button, () => tcs.SetResult(null));
80+
81+
await tcs.Task;
82+
83+
CompositeTransform transform = button.RenderTransform as CompositeTransform;
84+
85+
Assert.IsNotNull(transform);
86+
Assert.AreEqual(transform.TranslateX, 80);
87+
Assert.AreEqual(transform.TranslateY, 20);
88+
Assert.AreEqual(transform.ScaleX, 1.2, 0.0000001);
89+
Assert.AreEqual(transform.ScaleY, 1, 0.0000001);
90+
Assert.AreEqual(button.Opacity, 0.7, 0.0000001);
91+
});
92+
}
93+
94+
[TestMethod]
95+
public async Task Start_WithCallback_CompositionAndXaml()
96+
{
97+
await App.DispatcherQueue.EnqueueAsync(async () =>
98+
{
99+
var button = new Button();
100+
var grid = new Grid() { Children = { button } };
101+
102+
await SetTestContentAsync(grid);
103+
104+
var tcs = new TaskCompletionSource<object>();
105+
106+
AnimationBuilder.Create()
107+
.Scale(
108+
to: new Vector3(1.2f, 1, 1),
109+
delay: TimeSpan.FromMilliseconds(400))
110+
.Opacity(
111+
to: 0.7,
112+
duration: TimeSpan.FromSeconds(1))
113+
.Translation(
114+
to: new Vector2(80, 20),
115+
layer: FrameworkLayer.Xaml)
116+
.Start(button, () => tcs.SetResult(null));
117+
118+
await tcs.Task;
119+
120+
CompositeTransform transform = button.RenderTransform as CompositeTransform;
121+
122+
Assert.AreEqual(button.GetVisual().Scale, new Vector3(1.2f, 1, 1));
123+
Assert.AreEqual(button.GetVisual().Opacity, 0.7f);
124+
Assert.IsNotNull(transform);
125+
Assert.AreEqual(transform.TranslateX, 80);
126+
Assert.AreEqual(transform.TranslateY, 20);
127+
});
128+
}
129+
}
130+
}

UnitTests/UnitTests.UWP/UnitTests.UWP.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
<Compile Include="PrivateType.cs" />
197197
<Compile Include="Properties\AssemblyInfo.cs" />
198198
<Compile Include="Helpers\Test_WeakEventListener.cs" />
199+
<Compile Include="UI\Animations\Test_AnimationBuilderStart.cs" />
199200
<Compile Include="UI\Controls\Test_Carousel.cs" />
200201
<Compile Include="UI\Controls\Test_BladeView.cs" />
201202
<Compile Include="UI\Controls\Test_RadialGauge.cs" />

0 commit comments

Comments
 (0)