public async Task Test_Continued_SimpleAnimation_1() { int dummyInt = 0; float dummyFloat = 0; using (CancellationTokenSource cancelTokenSource = new CancellationTokenSource()) { DummyAnimatableObject animateableObject = new DummyAnimatableObject(); TplBasedLoop animLoop = new TplBasedLoop(0); animLoop.Tick += (sender, eArgs) => animateableObject.Update(new UpdateState(TimeSpan.FromMilliseconds(1.0))); animLoop.RunAsync(cancelTokenSource.Token) .FireAndForget(); await animateableObject.BuildAnimationSequence() .ChangeFloatBy( () => animateableObject.DummyValue, (givenValue) => animateableObject.DummyValue = givenValue, 10f, TimeSpan.FromMilliseconds(500.0)) .WaitFinished() .CallAction(() => { dummyInt = 10; dummyFloat = animateableObject.DummyValue; }) .ApplyAsync(); Assert.True(dummyInt == 10); Assert.True(dummyFloat == 10f); } }
public async Task Test_Continued_MultipleAnimations_2() { const int COUNT_CYCLES = 1000; int dummyInt = 0; float dummyFloat = 0; DummyAnimatableObject animateableObject = new DummyAnimatableObject(); List <Task> allTasks = new List <Task>(); using (CancellationTokenSource cancelTokenSource = new CancellationTokenSource()) { TplBasedLoop animLoop = new TplBasedLoop(0); animLoop.Tick += (sender, eArgs) => animateableObject.Update(new UpdateState(TimeSpan.FromMilliseconds(1))); animLoop.RunAsync(cancelTokenSource.Token) .FireAndForget(); for (int loop = 0; loop < COUNT_CYCLES; loop++) { allTasks.Add(animateableObject.BuildAnimationSequence() .ChangeFloatBy( () => animateableObject.DummyValue, (givenValue) => animateableObject.DummyValue = givenValue, 10f, TimeSpan.FromMilliseconds(1500.0)) .WaitFinished() .CallAction(() => { Interlocked.Increment(ref dummyInt); dummyFloat = animateableObject.DummyValue; }) .ApplyAsync()); if (loop % 10 == 0) { Thread.Sleep(1); } } await Task.WhenAll(allTasks); } Assert.True(dummyInt == COUNT_CYCLES); }
public async Task Test_Continued_SecondaryAnimations_1() { const int COUNT_CYCLES = 500; int dummyInt = 0; float dummyFloat = 0; DummyAnimatableObject animateableObject = new DummyAnimatableObject(); List <Task> allTasks = new List <Task>(); TaskCanceledException cancelEx = null; using (CancellationTokenSource cancelTokenSource = new CancellationTokenSource()) { // Start dummy-loop which updates our object TplBasedLoop animLoop = new TplBasedLoop(0); animLoop.Tick += (sender, eArgs) => animateableObject.Update(new UpdateState(TimeSpan.FromMilliseconds(1))); animLoop.RunAsync(cancelTokenSource.Token) .FireAndForget(); // Start all animations for (int loop = 0; loop < COUNT_CYCLES; loop++) { allTasks.Add(animateableObject.BuildAnimationSequence() .ChangeFloatBy( () => animateableObject.DummyValue, (givenValue) => animateableObject.DummyValue = givenValue, 10f, TimeSpan.FromMilliseconds(15000.0)) .WaitFinished() .CallAction(() => { Interlocked.Increment(ref dummyInt); dummyFloat = animateableObject.DummyValue; }) .ApplyAsync()); if (loop % 10 == 0) { Thread.Sleep(1); } } // Create a secondary animation (works in parallel) await animateableObject.BuildAnimationSequence() .ChangeFloatBy( () => animateableObject.DummyValue2, (givenValue) => animateableObject.DummyValue2 = givenValue, 10f, TimeSpan.FromMilliseconds(500.0)) .ApplyAsSecondaryAsync(); // Now cancel all animations animateableObject.AnimationHandler.BeginCancelAnimation(); // Wait for all tasks try { await Task.WhenAll(allTasks); } catch (TaskCanceledException ex) { cancelEx = ex; } } Assert.True(cancelEx is TaskCanceledException, "Some unexpected exception was raised!"); Assert.True(dummyInt < COUNT_CYCLES, "Wrong count of cycles!"); Assert.True(animateableObject.DummyValue2 == 10f, "Secondary animation has not processed correctly!"); }
public async Task Test_Continued_CancelAnimations_1() { const int COUNT_CYCLES = 500; // Some variables for preparation int dummyInt = 0; float dummyFloat = 0; DummyAnimatableObject animateableObject = new DummyAnimatableObject(); List <Task> allTasks = new List <Task>(); AggregateException aggregateException = null; TaskCanceledException cancelException = null; TimeSpan animDuration = TimeSpan.MaxValue; using (CancellationTokenSource cancelTokenSource = new CancellationTokenSource()) { // Creates a dummy loop which updates our animation using a background thread TplBasedLoop animLoop = new TplBasedLoop(0); animLoop.Tick += (sender, eArgs) => animateableObject.Update(new UpdateState(TimeSpan.FromMilliseconds(1))); animLoop.RunAsync(cancelTokenSource.Token) .FireAndForget(); // Define a high count of animations for (int loop = 0; loop < COUNT_CYCLES; loop++) { allTasks.Add(animateableObject.BuildAnimationSequence() .ChangeFloatBy( () => animateableObject.DummyValue, (givenValue) => animateableObject.DummyValue = givenValue, 10f, TimeSpan.FromMilliseconds(15000.0)) .WaitFinished() .CallAction(() => { Interlocked.Increment(ref dummyInt); dummyFloat = animateableObject.DummyValue; }) .ApplyAsync()); if (loop % 10 == 0) { Thread.Sleep(1); } } animateableObject.AnimationHandler.BeginCancelAnimation(); // This has to be false here because animations get canceled Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); { try { await Task.WhenAll(allTasks); } catch (TaskCanceledException exCancel) { // This exception is expected because of the BeginCancelAnimation call cancelException = exCancel; } catch (AggregateException ex) { // This exception is expected because of the BeginCancelAnimation call aggregateException = ex; cancelException = ex.InnerException as TaskCanceledException; } } stopwatch.Stop(); animDuration = stopwatch.Elapsed; } // Checks after animation processing Assert.True(dummyInt < COUNT_CYCLES, "Wrong count of cycles!"); Assert.True(animDuration < TimeSpan.FromMilliseconds(500.0), "Animation was not cancelled correctly!"); Assert.NotNull(cancelException); }