1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-09 09:30:08 +08:00
osu-lazer/osu.Game.Tests/Visual/UserInterface/TestSceneStatefulMenuItem.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

187 lines
5.8 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
2019-11-11 18:09:38 +08:00
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2019-11-11 18:09:38 +08:00
using osuTK.Input;
2019-11-11 17:42:32 +08:00
namespace osu.Game.Tests.Visual.UserInterface
{
public partial class TestSceneStatefulMenuItem : OsuManualInputManagerTestScene
{
2019-11-11 18:09:38 +08:00
[Test]
public void TestTernaryRadioMenuItem()
{
2019-11-11 18:09:38 +08:00
OsuMenu menu = null;
Bindable<TernaryState> state = new Bindable<TernaryState>(TernaryState.Indeterminate);
AddStep("create menu", () =>
{
state.Value = TernaryState.Indeterminate;
Child = menu = new OsuMenu(Direction.Vertical, true)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Items = new[]
{
new TernaryStateRadioMenuItem("First"),
new TernaryStateRadioMenuItem("Second") { State = { BindTarget = state } },
new TernaryStateRadioMenuItem("Third") { State = { Value = TernaryState.True } },
}
};
});
checkState(TernaryState.Indeterminate);
click();
checkState(TernaryState.True);
click();
checkState(TernaryState.True);
click();
checkState(TernaryState.True);
AddStep("change state via bindable", () => state.Value = TernaryState.True);
void click() =>
AddStep("click", () =>
{
InputManager.MoveMouseTo(menu.ScreenSpaceDrawQuad.Centre);
InputManager.Click(MouseButton.Left);
});
void checkState(TernaryState expected)
=> AddAssert($"state is {expected}", () => state.Value == expected);
}
[Test]
public void TestTernaryToggleMenuItem()
{
OsuMenu menu = null;
Bindable<TernaryState> state = new Bindable<TernaryState>(TernaryState.Indeterminate);
AddStep("create menu", () =>
{
state.Value = TernaryState.Indeterminate;
Child = menu = new OsuMenu(Direction.Vertical, true)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Items = new[]
{
new TernaryStateToggleMenuItem("First"),
new TernaryStateToggleMenuItem("Second") { State = { BindTarget = state } },
new TernaryStateToggleMenuItem("Third") { State = { Value = TernaryState.True } },
2019-11-11 18:09:38 +08:00
}
};
});
checkState(TernaryState.Indeterminate);
click();
checkState(TernaryState.True);
click();
checkState(TernaryState.False);
click();
checkState(TernaryState.True);
click();
checkState(TernaryState.False);
AddStep("change state via bindable", () => state.Value = TernaryState.True);
void click() =>
AddStep("click", () =>
{
InputManager.MoveMouseTo(menu.ScreenSpaceDrawQuad.Centre);
InputManager.Click(MouseButton.Left);
});
void checkState(TernaryState expected)
=> AddAssert($"state is {expected}", () => state.Value == expected);
}
[Test]
public void TestCustomState()
{
AddStep("create menu", () =>
{
2019-11-11 18:09:38 +08:00
Child = new OsuMenu(Direction.Vertical, true)
{
2019-11-11 18:09:38 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Items = new[]
{
new TestMenuItem("First", MenuItemType.Standard, getNextState),
new TestMenuItem("Second", MenuItemType.Standard, getNextState) { State = { Value = TestStates.State2 } },
new TestMenuItem("Third", MenuItemType.Standard, getNextState) { State = { Value = TestStates.State3 } },
}
};
});
}
private TestStates getNextState(TestStates state)
{
switch (state)
{
case TestStates.State1:
return TestStates.State2;
case TestStates.State2:
return TestStates.State3;
case TestStates.State3:
return TestStates.State1;
}
return TestStates.State1;
}
private class TestMenuItem : StatefulMenuItem<TestStates>
{
public TestMenuItem(string text, MenuItemType type, Func<TestStates, TestStates> changeStateFunc)
2019-11-08 11:55:22 +08:00
: base(text, changeStateFunc, type)
{
}
public override IconUsage? GetIconForState(TestStates state)
{
switch (state)
{
case TestStates.State1:
return FontAwesome.Solid.DiceOne;
case TestStates.State2:
return FontAwesome.Solid.DiceTwo;
case TestStates.State3:
return FontAwesome.Solid.DiceThree;
2019-11-08 12:14:23 +08:00
default:
throw new ArgumentOutOfRangeException(nameof(state), state, null);
}
}
}
private enum TestStates
{
State1,
State2,
State3
}
}
}