<commit_before>﻿// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using NUnit.Framework;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;

namespace osu.Game.Tests.Visual.UserInterface
{
    [TestFixture]
    public class TestSceneCursors : OsuManualInputManagerTestScene
    {
        private readonly GlobalCursorDisplay globalCursorDisplay;
        private readonly CustomCursorBox[] cursorBoxes = new CustomCursorBox[6];

        public TestSceneCursors()
        {
            Child = globalCursorDisplay = new GlobalCursorDisplay
            {
                RelativeSizeAxes = Axes.Both,
                Children = new[]
                {
                    // Middle user
                    cursorBoxes[0] = new CustomCursorBox(Color4.Green)
                    {
                        Anchor = Anchor.Centre,
                        Origin = Anchor.Centre,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.5f),
                    },
                    // Top-left user
                    cursorBoxes[1] = new CustomCursorBox(Color4.Blue)
                    {
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Bottom-right user
                    cursorBoxes[2] = new CustomCursorBox(Color4.Red)
                    {
                        Anchor = Anchor.BottomRight,
                        Origin = Anchor.BottomRight,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Bottom-left local
                    cursorBoxes[3] = new CustomCursorBox(Color4.Magenta, false)
                    {
                        Anchor = Anchor.BottomLeft,
                        Origin = Anchor.BottomLeft,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Top-right local
                    cursorBoxes[4] = new CustomCursorBox(Color4.Cyan, false)
                    {
                        Anchor = Anchor.TopRight,
                        Origin = Anchor.TopRight,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Left-local
                    cursorBoxes[5] = new CustomCursorBox(Color4.Yellow, false)
                    {
                        Anchor = Anchor.CentreLeft,
                        Origin = Anchor.CentreLeft,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.2f, 1),
                    },
                }
            };

            AddToggleStep("Smooth transitions", b => cursorBoxes.ForEach(box => box.SmoothTransition = b));

            testUserCursor();
            testLocalCursor();
            testUserCursorOverride();
            testMultipleLocalCursors();
        }

        /// <summary>
        /// -- Green Box --
        /// Tests whether hovering in and out of a drawable that provides the user cursor (green)
        /// results in the correct visibility state for that cursor.
        /// </summary>
        private void testUserCursor()
        {
            AddStep("Move to green area", () => InputManager.MoveMouseTo(cursorBoxes[0]));
            AddAssert("Check green cursor visible", () => checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check green cursor at mouse", () => checkAtMouse(cursorBoxes[0].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
        }

        /// <summary>
        /// -- Purple Box --
        /// Tests whether hovering in and out of a drawable that provides a local cursor (purple)
        /// results in the correct visibility and state for that cursor.
        /// </summary>
        private void testLocalCursor()
        {
            AddStep("Move to purple area", () => InputManager.MoveMouseTo(cursorBoxes[3]));
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].Cursor));
            AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
            AddAssert("Check global cursor at mouse", () => checkAtMouse(globalCursorDisplay.MenuCursor));
            AddStep("Move out", moveOut);
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
        }

        /// <summary>
        /// -- Blue-Green Box Boundary --
        /// Tests whether overriding a user cursor (green) with another user cursor (blue)
        /// results in the correct visibility and states for the cursors.
        /// </summary>
        private void testUserCursorOverride()
        {
            AddStep("Move to blue-green boundary", () => InputManager.MoveMouseTo(cursorBoxes[1].ScreenSpaceDrawQuad.BottomRight - new Vector2(10)));
            AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check blue cursor not visible", () => !checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check green cursor not visible", () => !checkVisible(cursorBoxes[0].Cursor));
        }

        /// <summary>
        /// -- Yellow-Purple Box Boundary --
        /// Tests whether multiple local cursors (purple + yellow) may be visible and at the mouse position at the same time.
        /// </summary>
        private void testMultipleLocalCursors()
        {
            AddStep("Move to yellow-purple boundary", () => InputManager.MoveMouseTo(cursorBoxes[5].ScreenSpaceDrawQuad.BottomRight - new Vector2(10)));
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
            AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
        }

        /// <summary>
        /// -- Yellow-Blue Box Boundary --
        /// Tests whether a local cursor (yellow) may be displayed along with a user cursor override (blue).
        /// </summary>
        private void testUserOverrideWithLocal()
        {
            AddStep("Move to yellow-blue boundary", () => InputManager.MoveMouseTo(cursorBoxes[5].ScreenSpaceDrawQuad.TopRight - new Vector2(10)));
            AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
            AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check blue cursor invisible", () => !checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
        }

        /// <summary>
        /// Moves the cursor to a point not covered by any cursor containers.
        /// </summary>
        private void moveOut()
            => InputManager.MoveMouseTo(new Vector2(InputManager.ScreenSpaceDrawQuad.Centre.X, InputManager.ScreenSpaceDrawQuad.TopLeft.Y));

        /// <summary>
        /// Checks if a cursor is visible.
        /// </summary>
        /// <param name="cursorContainer">The cursor to check.</param>
        private bool checkVisible(CursorContainer cursorContainer) => cursorContainer.State.Value == Visibility.Visible;

        /// <summary>
        /// Checks if a cursor is at the current inputmanager screen position.
        /// </summary>
        /// <param name="cursorContainer">The cursor to check.</param>
        private bool checkAtMouse(CursorContainer cursorContainer)
            => Precision.AlmostEquals(InputManager.CurrentState.Mouse.Position, cursorContainer.ToScreenSpace(cursorContainer.ActiveCursor.DrawPosition));<commit_msg>Add test cases for hiding cursor on keyboard input<commit_after>﻿// Copyright (c) <NAME> <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using NUnit.Framework;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Utils;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
using osuTK;
using osuTK.Graphics;
using osuTK.Input;

namespace osu.Game.Tests.Visual.UserInterface
{
    [TestFixture]
    public class TestSceneCursors : OsuManualInputManagerTestScene
    {
        private readonly GlobalCursorDisplay globalCursorDisplay;
        private readonly CustomCursorBox[] cursorBoxes = new CustomCursorBox[6];

        public TestSceneCursors()
        {
            Child = globalCursorDisplay = new GlobalCursorDisplay
            {
                RelativeSizeAxes = Axes.Both,
                Children = new[]
                {
                    // Middle user
                    cursorBoxes[0] = new CustomCursorBox(Color4.Green)
                    {
                        Anchor = Anchor.Centre,
                        Origin = Anchor.Centre,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.5f),
                    },
                    // Top-left user
                    cursorBoxes[1] = new CustomCursorBox(Color4.Blue)
                    {
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Bottom-right user
                    cursorBoxes[2] = new CustomCursorBox(Color4.Red)
                    {
                        Anchor = Anchor.BottomRight,
                        Origin = Anchor.BottomRight,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Bottom-left local
                    cursorBoxes[3] = new CustomCursorBox(Color4.Magenta, false)
                    {
                        Anchor = Anchor.BottomLeft,
                        Origin = Anchor.BottomLeft,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Top-right local
                    cursorBoxes[4] = new CustomCursorBox(Color4.Cyan, false)
                    {
                        Anchor = Anchor.TopRight,
                        Origin = Anchor.TopRight,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.4f)
                    },
                    // Left-local
                    cursorBoxes[5] = new CustomCursorBox(Color4.Yellow, false)
                    {
                        Anchor = Anchor.CentreLeft,
                        Origin = Anchor.CentreLeft,
                        RelativeSizeAxes = Axes.Both,
                        Size = new Vector2(0.2f, 1),
                    },
                }
            };

            AddToggleStep("Smooth transitions", b => cursorBoxes.ForEach(box => box.SmoothTransition = b));
        }

        [SetUp]
        public void SetUp() => Schedule(moveOut);

        /// <summary>
        /// -- Green Box --
        /// Tests whether hovering in and out of a drawable that provides the user cursor (green)
        /// results in the correct visibility state for that cursor.
        /// </summary>
        [Test]
        public void TestUserCursor()
        {
            AddStep("Move to green area", () => InputManager.MoveMouseTo(cursorBoxes[0]));
            AddAssert("Check green cursor visible", () => checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check green cursor at mouse", () => checkAtMouse(cursorBoxes[0].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
        }

        /// <summary>
        /// -- Purple Box --
        /// Tests whether hovering in and out of a drawable that provides a local cursor (purple)
        /// results in the correct visibility and state for that cursor.
        /// </summary>
        [Test]
        public void TestLocalCursor()
        {
            AddStep("Move to purple area", () => InputManager.MoveMouseTo(cursorBoxes[3]));
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].Cursor));
            AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
            AddAssert("Check global cursor at mouse", () => checkAtMouse(globalCursorDisplay.MenuCursor));
            AddStep("Move out", moveOut);
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check global cursor visible", () => checkVisible(globalCursorDisplay.MenuCursor));
        }

        /// <summary>
        /// -- Blue-Green Box Boundary --
        /// Tests whether overriding a user cursor (green) with another user cursor (blue)
        /// results in the correct visibility and states for the cursors.
        /// </summary>
        [Test]
        public void TestUserCursorOverride()
        {
            AddStep("Move to blue-green boundary", () => InputManager.MoveMouseTo(cursorBoxes[1].ScreenSpaceDrawQuad.BottomRight - new Vector2(10)));
            AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check green cursor invisible", () => !checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check blue cursor not visible", () => !checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check green cursor not visible", () => !checkVisible(cursorBoxes[0].Cursor));
        }

        /// <summary>
        /// -- Yellow-Purple Box Boundary --
        /// Tests whether multiple local cursors (purple + yellow) may be visible and at the mouse position at the same time.
        /// </summary>
        [Test]
        public void TestMultipleLocalCursors()
        {
            AddStep("Move to yellow-purple boundary", () => InputManager.MoveMouseTo(cursorBoxes[5].ScreenSpaceDrawQuad.BottomRight - new Vector2(10)));
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check purple cursor at mouse", () => checkAtMouse(cursorBoxes[3].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
            AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
        }

        /// <summary>
        /// -- Yellow-Blue Box Boundary --
        /// Tests whether a local cursor (yellow) may be displayed along with a user cursor override (blue).
        /// </summary>
        [Test]
        public void TestUserOverrideWithLocal()
        {
            AddStep("Move to yellow-blue boundary", () => InputManager.MoveMouseTo(cursorBoxes[5].ScreenSpaceDrawQuad.TopRight - new Vector2(10)));
            AddAssert("Check blue cursor visible", () => checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check blue cursor at mouse", () => checkAtMouse(cursorBoxes[1].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
            AddAssert("Check yellow cursor at mouse", () => checkAtMouse(cursorBoxes[5].Cursor));
            AddStep("Move out", moveOut);
            AddAssert("Check blue cursor invisible", () => !checkVisible(cursorBoxes[1].Cursor));
            AddAssert("Check yellow cursor visible", () => checkVisible(cursorBoxes[5].Cursor));
        }

        /// <summary>
        /// Ensures non-mouse input hides global cursor on a "local cursor" area (which doesn't hide global cursor).
        /// </summary>
        [Test]
        public void TestKeyboardLocalCursor([Values] bool clickToShow)
        {
            AddStep("Move to purple area", () => InputManager.MoveMouseTo(cursorBoxes[3].ScreenSpaceDrawQuad.Centre + new Vector2(10, 0)));
            AddAssert("Check purple cursor visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddAssert("Check global cursor alpha is 1", () => globalCursorDisplay.MenuCursor.Alpha == 1);

            AddStep("Press key", () => InputManager.Key(Key.A));
            AddAssert("Check purple cursor still visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddUntilStep("Check global cursor alpha is 0", () => globalCursorDisplay.MenuCursor.ActiveCursor.Alpha == 0);

            if (clickToShow)
                AddStep("Click mouse", () => InputManager.Click(MouseButton.Left));
            else
                AddStep("Move mouse", () => InputManager.MoveMouseTo(InputManager.CurrentState.Mouse.Position + Vector2.One));

            AddAssert("Check purple cursor still visible", () => checkVisible(cursorBoxes[3].Cursor));
            AddUntilStep("Check global cursor alpha is 1", () => globalCursorDisplay.MenuCursor.ActiveCursor.Alpha == 1);
        }

        /// <summary>
        /// Ensures mouse input after non-mouse input doesn't show global cursor on a "user cursor" area (which hides global cursor).
        /// </summary>
        [Test]
        public void TestKeyboardUserCursor([Values] bool clickToShow)
        {
            AddStep("Move to green area", () => InputManager.MoveMouseTo(cursorBoxes[0]));
            AddAssert("Check green cursor visible", () => checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check global cursor alpha is 0", () => !checkVisible(globalCursorDisplay.MenuCursor) && globalCursorDisplay.MenuCursor.ActiveCursor.Alpha == 0);

            AddStep("Press key", () => InputManager.Key(Key.A));
            AddAssert("Check green cursor still visible", () => checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check global cursor alpha is still 0", () => !checkVisible(globalCursorDisplay.MenuCursor) && globalCursorDisplay.MenuCursor.ActiveCursor.Alpha == 0);

            if (clickToShow)
                AddStep("Click mouse", () => InputManager.Click(MouseButton.Left));
            else
                AddStep("Move mouse", () => InputManager.MoveMouseTo(InputManager.CurrentState.Mouse.Position + Vector2.One));

            AddAssert("Check green cursor still visible", () => checkVisible(cursorBoxes[0].Cursor));
            AddAssert("Check global cursor alpha is still 0", () => !checkVisible(globalCursorDisplay.MenuCursor) && globalCursorDisplay.MenuCursor.ActiveCursor.Alpha == 0);
        }

        /// <summary>
        /// Moves the cursor to a point not covered by any cursor containers.
        /// </summary>
        private void moveOut()
            => InputManager.MoveMouseTo(new Vector2(InputManager.ScreenSpaceDrawQuad.Centre.X, InputManager.ScreenSpaceDrawQuad.TopLeft.Y));

        /// <summary>
        /// Checks if a cursor is visible.
        /// </summary>
        /// <param name="cursorContainer">The cursor to check.</param>
        private bool checkVisible(CursorContainer cursorContainer) => cursorContainer.State.Value == Visibility.Visible;

        /// <summary>
        /// Checks if a cursor is at the current inputmanager screen position.
        /// </summary>
        /// <param name="cursorContainer">The cursor to check.</param>
        private bool checkAtMouse(CursorContainer cursorContainer)
            => Precision.AlmostEquals(InputManager.CurrentState.Mouse.Position, cursorContainer.ToScreenSpace(cursorContainer.ActiveCursor.DrawPosition));
{'fraction_non_alphanumeric': 0.11555392516507704, 'fraction_numerical': 0.007611885546588408, 'mean_word_length': 2.074288130814773, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 147, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2887917', 'n_tokens_mistral': 5527, 'n_tokens_neox': 5275, 'n_words': 1326}