1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-31 01:23:22 +08:00
osu-lazer/osu.Game.Tests/Visual/Settings/TestSceneKeyBindingPanel.cs

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

285 lines
10 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Diagnostics;
using System.Linq;
2018-03-02 14:34:31 +08:00
using NUnit.Framework;
using osu.Framework.Testing;
using osu.Framework.Threading;
2021-05-24 20:56:47 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
2017-09-18 21:32:49 +08:00
using osu.Game.Overlays;
using osu.Game.Overlays.Settings.Sections.Input;
using osuTK.Input;
2018-04-13 17:19:50 +08:00
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.Settings
2017-09-18 21:32:49 +08:00
{
2018-03-02 14:34:31 +08:00
[TestFixture]
public class TestSceneKeyBindingPanel : OsuManualInputManagerTestScene
2017-09-18 21:32:49 +08:00
{
private readonly KeyBindingPanel panel;
2018-04-13 17:19:50 +08:00
public TestSceneKeyBindingPanel()
2017-09-18 21:32:49 +08:00
{
Child = panel = new KeyBindingPanel();
2017-09-18 21:32:49 +08:00
}
2018-04-13 17:19:50 +08:00
2017-09-18 21:32:49 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
panel.Show();
2017-09-18 21:32:49 +08:00
}
2021-05-24 20:56:47 +08:00
[SetUpSteps]
public void SetUpSteps()
{
2021-08-17 15:37:18 +08:00
AddUntilStep("wait for load", () => panel.ChildrenOfType<GlobalKeyBindingsSection>().Any());
2021-05-24 20:56:47 +08:00
AddStep("Scroll to top", () => panel.ChildrenOfType<SettingsPanel.SettingsSectionsContainer>().First().ScrollToTop());
AddWaitStep("wait for scroll", 5);
}
[Test]
public void TestBindingMouseWheelToNonGameplay()
{
scrollToAndStartBinding("Increase volume");
AddStep("press k", () => InputManager.Key(Key.K));
checkBinding("Increase volume", "K");
AddStep("click again", () => InputManager.Click(MouseButton.Left));
AddStep("scroll mouse wheel", () => InputManager.ScrollVerticalBy(1));
checkBinding("Increase volume", "Wheel Up");
}
[Test]
public void TestBindingMouseWheelToGameplay()
{
scrollToAndStartBinding("Left button");
AddStep("press k", () => InputManager.Key(Key.Z));
checkBinding("Left button", "Z");
AddStep("click again", () => InputManager.Click(MouseButton.Left));
AddStep("scroll mouse wheel", () => InputManager.ScrollVerticalBy(1));
checkBinding("Left button", "Z");
}
[Test]
public void TestClickTwiceOnClearButton()
{
2021-05-25 19:08:40 +08:00
KeyBindingRow firstRow = null;
AddStep("click first row", () =>
{
2021-05-25 19:08:40 +08:00
firstRow = panel.ChildrenOfType<KeyBindingRow>().First();
2021-05-15 09:24:08 +08:00
InputManager.MoveMouseTo(firstRow);
InputManager.Click(MouseButton.Left);
});
AddStep("schedule button clicks", () =>
{
2021-05-25 19:08:40 +08:00
var clearButton = firstRow.ChildrenOfType<KeyBindingRow.ClearButton>().Single();
InputManager.MoveMouseTo(clearButton);
int buttonClicks = 0;
ScheduledDelegate clickDelegate = null;
clickDelegate = Scheduler.AddDelayed(() =>
{
2020-11-05 22:41:56 +08:00
InputManager.Click(MouseButton.Left);
if (++buttonClicks == 2)
{
// ReSharper disable once AccessToModifiedClosure
Debug.Assert(clickDelegate != null);
// ReSharper disable once AccessToModifiedClosure
clickDelegate.Cancel();
}
}, 0, true);
});
}
2020-08-03 03:41:35 +08:00
[Test]
public void TestClearButtonOnBindings()
{
2021-05-25 19:08:40 +08:00
KeyBindingRow multiBindingRow = null;
2020-08-03 03:41:35 +08:00
AddStep("click first row with two bindings", () =>
2020-08-03 03:41:35 +08:00
{
2021-05-25 19:08:40 +08:00
multiBindingRow = panel.ChildrenOfType<KeyBindingRow>().First(row => row.Defaults.Count() > 1);
InputManager.MoveMouseTo(multiBindingRow);
2020-08-03 03:41:35 +08:00
InputManager.Click(MouseButton.Left);
});
clickClearButton();
2021-05-25 19:08:40 +08:00
AddAssert("first binding cleared", () => string.IsNullOrEmpty(multiBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().First().Text.Text.ToString()));
2020-08-03 03:41:35 +08:00
AddStep("click second binding", () =>
{
2021-05-25 19:08:40 +08:00
var target = multiBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(1);
2020-08-03 03:41:35 +08:00
InputManager.MoveMouseTo(target);
InputManager.Click(MouseButton.Left);
});
clickClearButton();
2021-05-25 19:08:40 +08:00
AddAssert("second binding cleared", () => string.IsNullOrEmpty(multiBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(1).Text.Text.ToString()));
2020-08-03 03:41:35 +08:00
void clickClearButton()
{
AddStep("click clear button", () =>
{
2021-05-25 19:08:40 +08:00
var clearButton = multiBindingRow.ChildrenOfType<KeyBindingRow.ClearButton>().Single();
2020-08-03 03:41:35 +08:00
InputManager.MoveMouseTo(clearButton);
InputManager.Click(MouseButton.Left);
});
}
}
[Test]
public void TestSingleBindingResetButton()
{
KeyBindingRow settingsKeyBindingRow = null;
AddStep("click first row", () =>
{
settingsKeyBindingRow = panel.ChildrenOfType<KeyBindingRow>().First();
InputManager.MoveMouseTo(settingsKeyBindingRow);
InputManager.Click(MouseButton.Left);
InputManager.PressKey(Key.P);
InputManager.ReleaseKey(Key.P);
});
2021-05-15 09:24:08 +08:00
AddUntilStep("restore button shown", () => settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First().Alpha > 0);
2021-05-15 09:24:08 +08:00
AddStep("click reset button for bindings", () =>
{
var resetButton = settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First();
2021-08-04 16:27:44 +08:00
resetButton.TriggerClick();
2021-05-15 09:24:08 +08:00
});
2021-05-15 09:24:08 +08:00
AddUntilStep("restore button hidden", () => settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First().Alpha == 0);
AddAssert("binding cleared", () => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
}
[Test]
public void TestResetAllBindingsButton()
{
KeyBindingRow settingsKeyBindingRow = null;
AddStep("click first row", () =>
{
settingsKeyBindingRow = panel.ChildrenOfType<KeyBindingRow>().First();
InputManager.MoveMouseTo(settingsKeyBindingRow);
InputManager.Click(MouseButton.Left);
InputManager.PressKey(Key.P);
InputManager.ReleaseKey(Key.P);
});
AddUntilStep("restore button shown", () => settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First().Alpha > 0);
AddStep("click reset button for bindings", () =>
{
var resetButton = panel.ChildrenOfType<ResetButton>().First();
2021-08-04 16:27:44 +08:00
resetButton.TriggerClick();
2021-05-15 09:24:08 +08:00
});
AddUntilStep("restore button hidden", () => settingsKeyBindingRow.ChildrenOfType<RestoreDefaultValueButton<bool>>().First().Alpha == 0);
AddAssert("binding cleared", () => settingsKeyBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(0).KeyBinding.KeyCombination.Equals(settingsKeyBindingRow.Defaults.ElementAt(0)));
}
[Test]
public void TestClickRowSelectsFirstBinding()
{
2021-05-25 19:08:40 +08:00
KeyBindingRow multiBindingRow = null;
AddStep("click first row with two bindings", () =>
{
2021-05-25 19:08:40 +08:00
multiBindingRow = panel.ChildrenOfType<KeyBindingRow>().First(row => row.Defaults.Count() > 1);
InputManager.MoveMouseTo(multiBindingRow);
InputManager.Click(MouseButton.Left);
});
2021-05-25 19:08:40 +08:00
AddAssert("first binding selected", () => multiBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().First().IsBinding);
AddStep("click second binding", () =>
{
2021-05-25 19:08:40 +08:00
var target = multiBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().ElementAt(1);
InputManager.MoveMouseTo(target);
InputManager.Click(MouseButton.Left);
});
AddStep("click back binding row", () =>
{
2021-05-25 19:08:40 +08:00
multiBindingRow = panel.ChildrenOfType<KeyBindingRow>().ElementAt(10);
InputManager.MoveMouseTo(multiBindingRow);
InputManager.Click(MouseButton.Left);
});
2021-05-25 19:08:40 +08:00
AddAssert("first binding selected", () => multiBindingRow.ChildrenOfType<KeyBindingRow.KeyButton>().First().IsBinding);
}
2021-05-24 20:56:47 +08:00
[Test]
public void TestFilteringHidesResetSectionButtons()
{
SearchTextBox searchTextBox = null;
AddStep("add any search term", () =>
{
searchTextBox = panel.ChildrenOfType<SearchTextBox>().Single();
searchTextBox.Current.Value = "chat";
});
AddUntilStep("all reset section bindings buttons hidden", () => panel.ChildrenOfType<ResetButton>().All(button => button.Alpha == 0));
AddStep("clear search term", () => searchTextBox.Current.Value = string.Empty);
AddUntilStep("all reset section bindings buttons shown", () => panel.ChildrenOfType<ResetButton>().All(button => button.Alpha == 1));
}
2021-05-24 20:56:47 +08:00
private void checkBinding(string name, string keyName)
{
AddAssert($"Check {name} is bound to {keyName}", () =>
{
2021-09-16 17:08:19 +08:00
var firstRow = panel.ChildrenOfType<KeyBindingRow>().First(r => r.ChildrenOfType<OsuSpriteText>().Any(s => s.Text.ToString() == name));
2021-05-24 20:56:47 +08:00
var firstButton = firstRow.ChildrenOfType<KeyBindingRow.KeyButton>().First();
return firstButton.Text.Text == keyName;
});
}
private void scrollToAndStartBinding(string name)
{
KeyBindingRow.KeyButton firstButton = null;
AddStep($"Scroll to {name}", () =>
{
2021-09-16 17:08:19 +08:00
var firstRow = panel.ChildrenOfType<KeyBindingRow>().First(r => r.ChildrenOfType<OsuSpriteText>().Any(s => s.Text.ToString() == name));
2021-05-24 20:56:47 +08:00
firstButton = firstRow.ChildrenOfType<KeyBindingRow.KeyButton>().First();
panel.ChildrenOfType<SettingsPanel.SettingsSectionsContainer>().First().ScrollTo(firstButton);
});
AddWaitStep("wait for scroll", 5);
AddStep("click to bind", () =>
{
InputManager.MoveMouseTo(firstButton);
InputManager.Click(MouseButton.Left);
});
}
2017-09-18 21:32:49 +08:00
}
}