Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ppy
GitHub Repository: ppy/osu
Path: blob/master/osu.Game.Tests/Visual/UserInterface/TestSceneDrawableDate.cs
4378 views
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Testing;
using osu.Game.Graphics;
using osuTK;
using osuTK.Graphics;

namespace osu.Game.Tests.Visual.UserInterface
{
    public partial class TestSceneDrawableDate : OsuTestScene
    {
        [SetUpSteps]
        public void SetUpSteps()
        {
            AddStep("Create 7 dates", () =>
            {
                Child = new FillFlowContainer
                {
                    Direction = FillDirection.Vertical,
                    AutoSizeAxes = Axes.Both,
                    Origin = Anchor.Centre,
                    Anchor = Anchor.Centre,
                    Children = new Drawable[]
                    {
                        new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(60))),
                        new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(55))),
                        new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(50))),
                        new PokeyDrawableDate(DateTimeOffset.Now),
                        new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(60))),
                        new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(65))),
                        new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(70))),
                    }
                };
            });
        }

        [Test]
        public void TestSecondsUpdate()
        {
            AddUntilStep("4th date says \"2 seconds ago\"", () => this.ChildrenOfType<DrawableDate>().ElementAt(3).Current.Value == "2 seconds ago");
        }

        private partial class PokeyDrawableDate : CompositeDrawable
        {
            public PokeyDrawableDate(DateTimeOffset date)
            {
                const float box_size = 10;

                DrawableDate drawableDate;
                Box flash;

                AutoSizeAxes = Axes.Both;
                InternalChildren = new Drawable[]
                {
                    flash = new Box
                    {
                        Colour = Color4.Yellow,
                        Size = new Vector2(box_size),
                        Anchor = Anchor.CentreLeft,
                        Origin = Anchor.CentreLeft,
                        Alpha = 0
                    },
                    drawableDate = new DrawableDate(date)
                    {
                        X = box_size + 2,
                    }
                };

                drawableDate.Current.ValueChanged += _ => flash.FadeOutFromOne(500);
            }
        }
    }
}