A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using System;
5using NUnit.Framework;
6using osu.Framework.Graphics;
7using osu.Framework.Graphics.Shapes;
8using osu.Framework.Input;
9using osu.Framework.Input.Events;
10using osu.Framework.Testing;
11using osuTK;
12
13namespace osu.Framework.Tests.Input
14{
15 [HeadlessTest]
16 public class JoystickInputTest : ManualInputManagerTestScene
17 {
18 /// <summary>
19 /// Tests that if the hierarchy is changed while a joystick button is held, the <see cref="Drawable.OnJoystickRelease"/> event is
20 /// only propagated to the hierarchy that originally handled <see cref="Drawable.OnJoystickPress"/>.
21 /// </summary>
22 [Test]
23 public void TestJoystickReleaseOnlyPropagatedToOriginalTargets()
24 {
25 var receptors = new InputReceptor[3];
26
27 AddStep("create hierarchy", () =>
28 {
29 Children = new Drawable[]
30 {
31 receptors[0] = new InputReceptor
32 {
33 Size = new Vector2(100),
34 Press = () => true
35 },
36 receptors[1] = new InputReceptor { Size = new Vector2(100) }
37 };
38 });
39
40 AddStep("press a button", () => InputManager.PressJoystickButton(JoystickButton.Button1));
41 AddStep("add receptor above", () =>
42 {
43 Add(receptors[2] = new InputReceptor
44 {
45 Size = new Vector2(100),
46 Press = () => true,
47 Release = () => true
48 });
49 });
50
51 AddStep("release key", () => InputManager.ReleaseJoystickButton(JoystickButton.Button1));
52
53 AddAssert("receptor 0 handled key down", () => receptors[0].PressReceived);
54 AddAssert("receptor 0 handled key up", () => receptors[0].ReleaseReceived);
55 AddAssert("receptor 1 handled key down", () => receptors[1].PressReceived);
56 AddAssert("receptor 1 handled key up", () => receptors[1].ReleaseReceived);
57 AddAssert("receptor 2 did not handle key down", () => !receptors[2].PressReceived);
58 AddAssert("receptor 2 did not handle key up", () => !receptors[2].ReleaseReceived);
59 }
60
61 private class InputReceptor : Box
62 {
63 public bool PressReceived { get; private set; }
64 public bool ReleaseReceived { get; private set; }
65
66 public Func<bool> Press;
67 public Func<bool> Release;
68
69 protected override bool OnJoystickPress(JoystickPressEvent e)
70 {
71 PressReceived = true;
72 return Press?.Invoke() ?? false;
73 }
74
75 protected override void OnJoystickRelease(JoystickReleaseEvent e)
76 {
77 ReleaseReceived = true;
78 Release?.Invoke();
79 }
80 }
81 }
82}