提交 cbbd43d6 编写于 作者: X xiaoy312

test: stabilize When_RepeatForever_ShouldLoop

上级 54a6d5d8
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
...@@ -121,10 +121,20 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Animation ...@@ -121,10 +121,20 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Animation
[RunsOnUIThread] [RunsOnUIThread]
public async Task When_RepeatForever_ShouldLoop() public async Task When_RepeatForever_ShouldLoop()
{ {
// On CI, the measurement at 100ms seem to be too unreliable on Android & MacOS.
// Stretch the test by 5x greatly improve the stability. When testing locally, we can used 1x to save time (5s vs 25s).
const int TimeResolutionScaling =
#if !DEBUG && (__ANDROID__ || __MACOS__)
5;
#else
1;
#endif
var target = new Windows.UI.Xaml.Shapes.Rectangle var target = new Windows.UI.Xaml.Shapes.Rectangle
{ {
Stretch = Stretch.Fill, Stretch = Stretch.Fill,
Fill = new SolidColorBrush(Colors.SkyBlue), Fill = new SolidColorBrush(Colors.SkyBlue),
Width = 50,
Height = 50, Height = 50,
}; };
WindowHelper.WindowContent = target; WindowHelper.WindowContent = target;
...@@ -137,19 +147,16 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Animation ...@@ -137,19 +147,16 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Animation
From = 0, From = 0,
To = 50, To = 50,
RepeatBehavior = RepeatBehavior.Forever, RepeatBehavior = RepeatBehavior.Forever,
Duration = TimeSpan.FromMilliseconds(500), Duration = TimeSpan.FromMilliseconds(500 * TimeResolutionScaling),
}; }.BindTo(target, nameof(Rectangle.Width));
Storyboard.SetTarget(animation, target); animation.Begin();
Storyboard.SetTargetProperty(animation, nameof(Rectangle.Width));
var storyboard = new Storyboard { Children = { animation } };
storyboard.Begin();
// In an ideal world, the measurements would be [0 or 50,10,20,30,40] repeated 10 times.
var list = new List<double>(); var list = new List<double>();
for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
{ {
list.Add(target.Width); list.Add(NanToZero(target.Width));
await Task.Delay(100); await Task.Delay(100 * TimeResolutionScaling);
} }
var delta = list.Zip(list.Skip(1), (a, b) => b - a).ToArray(); var delta = list.Zip(list.Skip(1), (a, b) => b - a).ToArray();
...@@ -160,10 +167,20 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Animation ...@@ -160,10 +167,20 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Animation
.ToArray(); .ToArray();
var incrementSizes = drops.Zip(drops.Skip(1), (a, b) => b - a - 1).ToArray(); // -1 to exclude the drop itself var incrementSizes = drops.Zip(drops.Skip(1), (a, b) => b - a - 1).ToArray(); // -1 to exclude the drop itself
var context = new StringBuilder()
.AppendLine("list: " + string.Join(", ", list.Select(x => x.ToString("0.#"))))
.AppendLine("delta: " + string.Join(", ", delta.Select(x => x.ToString("+0.#;-0.#;0"))))
.AppendLine("averageIncrement: " + averageIncrement)
.AppendLine("drops: " + string.Join(", ", drops.Select(x => x.ToString("0.#"))))
.AppendLine("incrementSizes: " + string.Join(", ", incrementSizes.Select(x => x.ToString("0.#"))))
.ToString();
// This 500ms animation is expected to climb from 0 to 50, reset to 0 instantly, and repeat forever. // This 500ms animation is expected to climb from 0 to 50, reset to 0 instantly, and repeat forever.
// Given that we are taking 5measurements per cycle, we can expect the followings: // Given that we are taking 5measurements per cycle, we can expect the followings:
Assert.AreEqual(10d, averageIncrement, 1.5, "an rough average of increment (exluding the drop) of 10 (+-15% error margin)"); Assert.AreEqual(10d, averageIncrement, 2.5, $"Expected an rough average of increment (excluding the drop) of 10 (+-25% error margin).\n" + context);
Assert.IsTrue(incrementSizes.Count(x => x > 3) > 8, $"at least 10 (-2 error margin: might miss first and/or last) sets of continuous increments that size of 4 (+-1 error margin: sliding slot): {string.Join(",", incrementSizes)}"); Assert.IsTrue(incrementSizes.Count(x => x >= 3) >= 8, $"Expected at least 10sets (-2 error margin: might miss first and/or last) of continuous increments in size of 4 (+-1 error margin: sliding slot).\n" + context);
double NanToZero(double value) => double.IsNaN(value) ? 0 : value;
} }
[TestMethod] [TestMethod]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册