Skip to content

Commit 999c08e

Browse files
authored
Merge pull request #33725 from peppy/fix-random-button-crash
Fix crash when random and rewind are run on the same frame
2 parents 1741b02 + 10c07fd commit 999c08e

File tree

3 files changed

+134
-66
lines changed

3 files changed

+134
-66
lines changed

osu.Game.Tests/Visual/SongSelectV2/TestSceneBeatmapCarouselRandom.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,28 @@ public void TestRewindOverMultipleIterations()
121121
}
122122
}
123123

124+
[Test]
125+
public void TestRandomThenRewindSameFrame()
126+
{
127+
AddBeatmaps(10, 3, true);
128+
WaitForDrawablePanels();
129+
130+
BeatmapInfo? originalSelected = null;
131+
132+
nextRandom();
133+
134+
CheckHasSelection();
135+
AddStep("store selection", () => originalSelected = (BeatmapInfo)Carousel.CurrentSelection!);
136+
137+
AddStep("random then rewind", () =>
138+
{
139+
Carousel.NextRandom();
140+
Carousel.PreviousRandom();
141+
});
142+
143+
AddAssert("selection not changed", () => Carousel.CurrentSelection, () => Is.EqualTo(originalSelected));
144+
}
145+
124146
[Test]
125147
public void TestRewindToDeletedBeatmap()
126148
{

osu.Game.Tests/Visual/SongSelectV2/TestSceneSongSelect.cs

Lines changed: 108 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using osuTK.Input;
2727
using FooterButtonMods = osu.Game.Screens.SelectV2.FooterButtonMods;
2828
using FooterButtonOptions = osu.Game.Screens.SelectV2.FooterButtonOptions;
29+
using FooterButtonRandom = osu.Game.Screens.SelectV2.FooterButtonRandom;
2930

3031
namespace osu.Game.Tests.Visual.SongSelectV2
3132
{
@@ -451,71 +452,113 @@ public void TestFooterModOverlay()
451452
AddStep("Hide", () => this.ChildrenOfType<ModSelectOverlay>().Single().Hide());
452453
}
453454

454-
// add these test cases when functionality is implemented.
455-
// [Test]
456-
// public void TestFooterRandom()
457-
// {
458-
// loadSongSelect();
459-
//
460-
// AddStep("press F2", () => InputManager.Key(Key.F2));
461-
// AddAssert("next random invoked", () => nextRandomCalled && !previousRandomCalled);
462-
// }
463-
//
464-
// [Test]
465-
// public void TestFooterRandomViaMouse()
466-
// {
467-
// loadSongSelect();
468-
//
469-
// AddStep("click button", () =>
470-
// {
471-
// InputManager.MoveMouseTo(randomButton);
472-
// InputManager.Click(MouseButton.Left);
473-
// });
474-
// AddAssert("next random invoked", () => nextRandomCalled && !previousRandomCalled);
475-
// }
476-
//
477-
// [Test]
478-
// public void TestFooterRewind()
479-
// {
480-
// loadSongSelect();
481-
//
482-
// AddStep("press Shift+F2", () =>
483-
// {
484-
// InputManager.PressKey(Key.LShift);
485-
// InputManager.PressKey(Key.F2);
486-
// InputManager.ReleaseKey(Key.F2);
487-
// InputManager.ReleaseKey(Key.LShift);
488-
// });
489-
// AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
490-
// }
491-
//
492-
// [Test]
493-
// public void TestFooterRewindViaShiftMouseLeft()
494-
// {
495-
// loadSongSelect();
496-
//
497-
// AddStep("shift + click button", () =>
498-
// {
499-
// InputManager.PressKey(Key.LShift);
500-
// InputManager.MoveMouseTo(randomButton);
501-
// InputManager.Click(MouseButton.Left);
502-
// InputManager.ReleaseKey(Key.LShift);
503-
// });
504-
// AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
505-
// }
506-
//
507-
// [Test]
508-
// public void TestFooterRewindViaMouseRight()
509-
// {
510-
// loadSongSelect();
511-
//
512-
// AddStep("right click button", () =>
513-
// {
514-
// InputManager.MoveMouseTo(randomButton);
515-
// InputManager.Click(MouseButton.Right);
516-
// });
517-
// AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
518-
// }
455+
[Test]
456+
public void TestFooterRandom()
457+
{
458+
LoadSongSelect();
459+
460+
bool nextRandomCalled = false;
461+
bool previousRandomCalled = false;
462+
AddStep("hook events", () =>
463+
{
464+
randomButton.NextRandom = () => nextRandomCalled = true;
465+
randomButton.PreviousRandom = () => previousRandomCalled = true;
466+
});
467+
468+
AddStep("press F2", () => InputManager.Key(Key.F2));
469+
AddAssert("next random invoked", () => nextRandomCalled && !previousRandomCalled);
470+
}
471+
472+
[Test]
473+
public void TestFooterRandomViaMouse()
474+
{
475+
LoadSongSelect();
476+
477+
bool nextRandomCalled = false;
478+
bool previousRandomCalled = false;
479+
AddStep("hook events", () =>
480+
{
481+
randomButton.NextRandom = () => nextRandomCalled = true;
482+
randomButton.PreviousRandom = () => previousRandomCalled = true;
483+
});
484+
485+
AddStep("click button", () =>
486+
{
487+
InputManager.MoveMouseTo(randomButton);
488+
InputManager.Click(MouseButton.Left);
489+
});
490+
AddAssert("next random invoked", () => nextRandomCalled && !previousRandomCalled);
491+
}
492+
493+
[Test]
494+
public void TestFooterRewind()
495+
{
496+
LoadSongSelect();
497+
498+
bool nextRandomCalled = false;
499+
bool previousRandomCalled = false;
500+
AddStep("hook events", () =>
501+
{
502+
randomButton.NextRandom = () => nextRandomCalled = true;
503+
randomButton.PreviousRandom = () => previousRandomCalled = true;
504+
});
505+
506+
AddStep("press Shift+F2", () =>
507+
{
508+
InputManager.PressKey(Key.LShift);
509+
InputManager.PressKey(Key.F2);
510+
InputManager.ReleaseKey(Key.F2);
511+
InputManager.ReleaseKey(Key.LShift);
512+
});
513+
514+
AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
515+
}
516+
517+
[Test]
518+
public void TestFooterRewindViaShiftMouseLeft()
519+
{
520+
LoadSongSelect();
521+
522+
bool nextRandomCalled = false;
523+
bool previousRandomCalled = false;
524+
AddStep("hook events", () =>
525+
{
526+
randomButton.NextRandom = () => nextRandomCalled = true;
527+
randomButton.PreviousRandom = () => previousRandomCalled = true;
528+
});
529+
530+
AddStep("shift + click button", () =>
531+
{
532+
InputManager.PressKey(Key.LShift);
533+
InputManager.MoveMouseTo(randomButton);
534+
InputManager.Click(MouseButton.Left);
535+
InputManager.ReleaseKey(Key.LShift);
536+
});
537+
AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
538+
}
539+
540+
[Test]
541+
public void TestFooterRewindViaMouseRight()
542+
{
543+
LoadSongSelect();
544+
545+
bool nextRandomCalled = false;
546+
bool previousRandomCalled = false;
547+
AddStep("hook events", () =>
548+
{
549+
randomButton.NextRandom = () => nextRandomCalled = true;
550+
randomButton.PreviousRandom = () => previousRandomCalled = true;
551+
});
552+
553+
AddStep("right click button", () =>
554+
{
555+
InputManager.MoveMouseTo(randomButton);
556+
InputManager.Click(MouseButton.Right);
557+
});
558+
AddAssert("previous random invoked", () => previousRandomCalled && !nextRandomCalled);
559+
}
560+
561+
private FooterButtonRandom randomButton => Footer.ChildrenOfType<FooterButtonRandom>().Single();
519562

520563
[Test]
521564
public void TestFooterOptions()

osu.Game/Screens/SelectV2/BeatmapCarousel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,10 @@ public void PreviousRandom()
691691
if (randomAlgorithm.Value == RandomSelectAlgorithm.RandomPermutation)
692692
previouslyVisitedRandomSets.Remove(beatmapInfo.BeatmapSet!);
693693

694-
playSpinSample(distanceBetween(previousBeatmapItem, CurrentSelectionItem!), carouselItems.Count);
694+
if (CurrentSelectionItem == null)
695+
playSpinSample(0, carouselItems.Count);
696+
else
697+
playSpinSample(distanceBetween(previousBeatmapItem, CurrentSelectionItem), carouselItems.Count);
695698
}
696699

697700
RequestSelection(previousBeatmap);

0 commit comments

Comments
 (0)