|
| 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 | + |
| 4 | +using System.Linq; |
| 5 | +using NUnit.Framework; |
| 6 | +using osu.Framework.Testing; |
| 7 | +using osu.Game.Beatmaps; |
| 8 | +using osu.Game.Screens.SelectV2; |
| 9 | +using osuTK.Input; |
| 10 | + |
| 11 | +namespace osu.Game.Tests.Visual.SongSelect |
| 12 | +{ |
| 13 | + [TestFixture] |
| 14 | + public partial class TestSceneBeatmapCarouselV2Selection : BeatmapCarouselV2TestScene |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Keyboard selection via up and down arrows doesn't actually change the selection until |
| 18 | + /// the select key is pressed. |
| 19 | + /// </summary> |
| 20 | + [Test] |
| 21 | + public void TestKeyboardSelectionKeyRepeat() |
| 22 | + { |
| 23 | + AddBeatmaps(10); |
| 24 | + WaitForDrawablePanels(); |
| 25 | + checkNoSelection(); |
| 26 | + |
| 27 | + select(); |
| 28 | + checkNoSelection(); |
| 29 | + |
| 30 | + AddStep("press down arrow", () => InputManager.PressKey(Key.Down)); |
| 31 | + checkSelectionIterating(false); |
| 32 | + |
| 33 | + AddStep("press up arrow", () => InputManager.PressKey(Key.Up)); |
| 34 | + checkSelectionIterating(false); |
| 35 | + |
| 36 | + AddStep("release down arrow", () => InputManager.ReleaseKey(Key.Down)); |
| 37 | + checkSelectionIterating(false); |
| 38 | + |
| 39 | + AddStep("release up arrow", () => InputManager.ReleaseKey(Key.Up)); |
| 40 | + checkSelectionIterating(false); |
| 41 | + |
| 42 | + select(); |
| 43 | + checkHasSelection(); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Keyboard selection via left and right arrows moves between groups, updating the selection |
| 48 | + /// immediately. |
| 49 | + /// </summary> |
| 50 | + [Test] |
| 51 | + public void TestGroupSelectionKeyRepeat() |
| 52 | + { |
| 53 | + AddBeatmaps(10); |
| 54 | + WaitForDrawablePanels(); |
| 55 | + checkNoSelection(); |
| 56 | + |
| 57 | + AddStep("press right arrow", () => InputManager.PressKey(Key.Right)); |
| 58 | + checkSelectionIterating(true); |
| 59 | + |
| 60 | + AddStep("press left arrow", () => InputManager.PressKey(Key.Left)); |
| 61 | + checkSelectionIterating(true); |
| 62 | + |
| 63 | + AddStep("release right arrow", () => InputManager.ReleaseKey(Key.Right)); |
| 64 | + checkSelectionIterating(true); |
| 65 | + |
| 66 | + AddStep("release left arrow", () => InputManager.ReleaseKey(Key.Left)); |
| 67 | + checkSelectionIterating(false); |
| 68 | + } |
| 69 | + |
| 70 | + [Test] |
| 71 | + public void TestCarouselRemembersSelection() |
| 72 | + { |
| 73 | + AddBeatmaps(10); |
| 74 | + WaitForDrawablePanels(); |
| 75 | + |
| 76 | + selectNextGroup(); |
| 77 | + |
| 78 | + object? selection = null; |
| 79 | + |
| 80 | + AddStep("store drawable selection", () => selection = getSelectedPanel()?.Item?.Model); |
| 81 | + |
| 82 | + checkHasSelection(); |
| 83 | + AddAssert("drawable selection non-null", () => selection, () => Is.Not.Null); |
| 84 | + AddAssert("drawable selection matches carousel selection", () => selection, () => Is.EqualTo(Carousel.CurrentSelection)); |
| 85 | + |
| 86 | + RemoveAllBeatmaps(); |
| 87 | + AddUntilStep("no drawable selection", getSelectedPanel, () => Is.Null); |
| 88 | + |
| 89 | + AddBeatmaps(10); |
| 90 | + WaitForDrawablePanels(); |
| 91 | + |
| 92 | + checkHasSelection(); |
| 93 | + AddAssert("no drawable selection", getSelectedPanel, () => Is.Null); |
| 94 | + |
| 95 | + AddStep("add previous selection", () => BeatmapSets.Add(((BeatmapInfo)selection!).BeatmapSet!)); |
| 96 | + |
| 97 | + AddUntilStep("drawable selection restored", () => getSelectedPanel()?.Item?.Model, () => Is.EqualTo(selection)); |
| 98 | + AddAssert("drawable selection matches carousel selection", () => selection, () => Is.EqualTo(Carousel.CurrentSelection)); |
| 99 | + |
| 100 | + BeatmapCarouselPanel? getSelectedPanel() => Carousel.ChildrenOfType<BeatmapCarouselPanel>().SingleOrDefault(p => p.Selected.Value); |
| 101 | + } |
| 102 | + |
| 103 | + [Test] |
| 104 | + public void TestTraversalBeyondStart() |
| 105 | + { |
| 106 | + const int total_set_count = 200; |
| 107 | + |
| 108 | + AddBeatmaps(total_set_count); |
| 109 | + WaitForDrawablePanels(); |
| 110 | + |
| 111 | + selectNextGroup(); |
| 112 | + waitForSelection(0, 0); |
| 113 | + selectPrevGroup(); |
| 114 | + waitForSelection(total_set_count - 1, 0); |
| 115 | + } |
| 116 | + |
| 117 | + [Test] |
| 118 | + public void TestTraversalBeyondEnd() |
| 119 | + { |
| 120 | + const int total_set_count = 200; |
| 121 | + |
| 122 | + AddBeatmaps(total_set_count); |
| 123 | + WaitForDrawablePanels(); |
| 124 | + |
| 125 | + selectPrevGroup(); |
| 126 | + waitForSelection(total_set_count - 1, 0); |
| 127 | + selectNextGroup(); |
| 128 | + waitForSelection(0, 0); |
| 129 | + } |
| 130 | + |
| 131 | + [Test] |
| 132 | + public void TestKeyboardSelection() |
| 133 | + { |
| 134 | + AddBeatmaps(10, 3); |
| 135 | + WaitForDrawablePanels(); |
| 136 | + |
| 137 | + selectNextPanel(); |
| 138 | + selectNextPanel(); |
| 139 | + selectNextPanel(); |
| 140 | + selectNextPanel(); |
| 141 | + checkNoSelection(); |
| 142 | + |
| 143 | + select(); |
| 144 | + waitForSelection(3, 0); |
| 145 | + |
| 146 | + selectNextPanel(); |
| 147 | + waitForSelection(3, 0); |
| 148 | + |
| 149 | + select(); |
| 150 | + waitForSelection(3, 1); |
| 151 | + |
| 152 | + selectNextPanel(); |
| 153 | + waitForSelection(3, 1); |
| 154 | + |
| 155 | + select(); |
| 156 | + waitForSelection(3, 2); |
| 157 | + |
| 158 | + selectNextPanel(); |
| 159 | + waitForSelection(3, 2); |
| 160 | + |
| 161 | + select(); |
| 162 | + waitForSelection(4, 0); |
| 163 | + } |
| 164 | + |
| 165 | + [Test] |
| 166 | + public void TestEmptyTraversal() |
| 167 | + { |
| 168 | + selectNextPanel(); |
| 169 | + checkNoSelection(); |
| 170 | + |
| 171 | + selectNextGroup(); |
| 172 | + checkNoSelection(); |
| 173 | + |
| 174 | + selectPrevPanel(); |
| 175 | + checkNoSelection(); |
| 176 | + |
| 177 | + selectPrevGroup(); |
| 178 | + checkNoSelection(); |
| 179 | + } |
| 180 | + |
| 181 | + private void waitForSelection(int set, int? diff = null) |
| 182 | + { |
| 183 | + AddUntilStep($"selected is set{set}{(diff.HasValue ? $" diff{diff.Value}" : "")}", () => |
| 184 | + { |
| 185 | + if (diff != null) |
| 186 | + return ReferenceEquals(Carousel.CurrentSelection, BeatmapSets[set].Beatmaps[diff.Value]); |
| 187 | + |
| 188 | + return BeatmapSets[set].Beatmaps.Contains(Carousel.CurrentSelection); |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + private void selectNextPanel() => AddStep("select next panel", () => InputManager.Key(Key.Down)); |
| 193 | + private void selectPrevPanel() => AddStep("select prev panel", () => InputManager.Key(Key.Up)); |
| 194 | + private void selectNextGroup() => AddStep("select next group", () => InputManager.Key(Key.Right)); |
| 195 | + private void selectPrevGroup() => AddStep("select prev group", () => InputManager.Key(Key.Left)); |
| 196 | + |
| 197 | + private void select() => AddStep("select", () => InputManager.Key(Key.Enter)); |
| 198 | + |
| 199 | + private void checkNoSelection() => AddAssert("has no selection", () => Carousel.CurrentSelection, () => Is.Null); |
| 200 | + private void checkHasSelection() => AddAssert("has selection", () => Carousel.CurrentSelection, () => Is.Not.Null); |
| 201 | + |
| 202 | + private void checkSelectionIterating(bool isIterating) |
| 203 | + { |
| 204 | + object? selection = null; |
| 205 | + |
| 206 | + for (int i = 0; i < 3; i++) |
| 207 | + { |
| 208 | + AddStep("store selection", () => selection = Carousel.CurrentSelection); |
| 209 | + if (isIterating) |
| 210 | + AddUntilStep("selection changed", () => Carousel.CurrentSelection != selection); |
| 211 | + else |
| 212 | + AddUntilStep("selection not changed", () => Carousel.CurrentSelection == selection); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments