Esempio n. 1
0
        public void Test_EventDriven_SimpleAnimation_2()
        {
            // Define animation parameters / objects
            DummyAnimatableObject animateableObject = new DummyAnimatableObject();
            int dummyInt = 0;

            // Define animation sequence
            //  .. nothing special, just step by step
            animateableObject.BuildAnimationSequence()
            .ChangeFloatBy(
                () => animateableObject.DummyValue,
                (givenValue) => animateableObject.DummyValue = givenValue,
                10f,
                TimeSpan.FromMilliseconds(500.0))
            .WaitFinished()
            .CallAction(() =>
            {
                dummyInt = 10;
            })
            .ChangeFloatBy(
                () => animateableObject.DummyValue,
                (givenValue) => animateableObject.DummyValue = givenValue,
                20f,
                TimeSpan.FromMilliseconds(500.0))
            .Apply();

            // Perform animation in an event-driven way
            EventDrivenPassInfo passInfo = animateableObject.AnimationHandler.CalculateEventDriven();

            // Check results
            Assert.True(passInfo.CountSteps == 2);
            Assert.True(dummyInt == 10);
            Assert.True(animateableObject.DummyValue == 30f);
            Assert.True(passInfo.TotalTime == TimeSpan.FromSeconds(1.0));
        }
Esempio n. 2
0
        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);
            }
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
        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!");
        }
Esempio n. 5
0
        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);
        }