1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 20:38:33 +08:00
osu-lazer/osu.Game.Tests/Visual/SongSelect/TestScenePlaySongSelect.cs

287 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Audio;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Extensions;
using osu.Framework.MathUtils;
using osu.Framework.Platform;
2019-02-15 16:01:06 +08:00
using osu.Framework.Screens;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
2018-08-14 13:18:46 +08:00
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Osu;
2018-08-14 13:18:46 +08:00
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Rulesets.Taiko;
2018-04-13 17:19:50 +08:00
using osu.Game.Screens.Select;
using osu.Game.Screens.Select.Carousel;
using osu.Game.Screens.Select.Filter;
2019-03-25 00:02:36 +08:00
namespace osu.Game.Tests.Visual.SongSelect
2018-04-13 17:19:50 +08:00
{
[TestFixture]
public class TestScenePlaySongSelect : ScreenTestScene
2018-04-13 17:19:50 +08:00
{
private BeatmapManager manager;
private RulesetStore rulesets;
private WorkingBeatmap defaultBeatmap;
public override IReadOnlyList<Type> RequiredTypes => new[]
{
2019-03-25 00:02:36 +08:00
typeof(Screens.Select.SongSelect),
2018-04-13 17:19:50 +08:00
typeof(BeatmapCarousel),
typeof(CarouselItem),
typeof(CarouselGroup),
typeof(CarouselGroupEagerSelect),
typeof(CarouselBeatmap),
typeof(CarouselBeatmapSet),
typeof(DrawableCarouselItem),
typeof(CarouselItemState),
typeof(DrawableCarouselBeatmap),
typeof(DrawableCarouselBeatmapSet),
};
private class TestSongSelect : PlaySongSelect
{
2019-01-10 14:25:07 +08:00
public Action StartRequested;
2018-08-14 13:18:46 +08:00
public new Bindable<RulesetInfo> Ruleset => base.Ruleset;
2018-04-13 17:19:50 +08:00
public WorkingBeatmap CurrentBeatmap => Beatmap.Value;
public WorkingBeatmap CurrentBeatmapDetailsBeatmap => BeatmapDetails.Beatmap;
public new BeatmapCarousel Carousel => base.Carousel;
2019-01-10 14:25:07 +08:00
protected override bool OnStart()
{
StartRequested?.Invoke();
return base.OnStart();
}
2018-04-13 17:19:50 +08:00
}
2018-07-18 11:58:45 +08:00
private TestSongSelect songSelect;
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio)
2018-04-13 17:19:50 +08:00
{
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
Dependencies.Cache(manager = new BeatmapManager(LocalStorage, ContextFactory, rulesets, null, audio, host, defaultBeatmap = Beatmap.Default));
2018-04-13 17:19:50 +08:00
2018-07-18 11:58:45 +08:00
Beatmap.SetDefault();
}
2018-04-13 17:19:50 +08:00
2018-07-18 11:58:45 +08:00
[SetUp]
public virtual void SetUp() => Schedule(() =>
{
Ruleset.Value = new OsuRuleset().RulesetInfo;
manager?.Delete(manager.GetAllUsableBeatmapSets());
});
2018-04-13 17:19:50 +08:00
[Test]
2018-07-18 11:58:45 +08:00
public void TestDummy()
{
2019-02-15 20:50:40 +08:00
createSongSelect();
2018-04-13 17:19:50 +08:00
AddAssert("dummy selected", () => songSelect.CurrentBeatmap == defaultBeatmap);
2019-03-19 16:24:26 +08:00
AddUntilStep("dummy shown on wedge", () => songSelect.CurrentBeatmapDetailsBeatmap == defaultBeatmap);
2018-04-13 17:19:50 +08:00
2018-07-18 11:58:45 +08:00
addManyTestMaps();
2019-03-19 16:24:26 +08:00
AddWaitStep("wait for select", 3);
2018-07-18 11:58:45 +08:00
2018-04-13 17:19:50 +08:00
AddAssert("random map selected", () => songSelect.CurrentBeatmap != defaultBeatmap);
2018-07-18 11:58:45 +08:00
}
2018-04-13 17:19:50 +08:00
[Test]
2018-07-18 11:58:45 +08:00
public void TestSorting()
{
2019-02-15 20:50:40 +08:00
createSongSelect();
2018-07-18 11:58:45 +08:00
addManyTestMaps();
2019-03-19 16:24:26 +08:00
AddWaitStep("wait for add", 3);
2018-07-18 11:58:45 +08:00
2018-04-13 17:19:50 +08:00
AddAssert("random map selected", () => songSelect.CurrentBeatmap != defaultBeatmap);
AddStep(@"Sort by Artist", delegate { songSelect.FilterControl.Sort = SortMode.Artist; });
AddStep(@"Sort by Title", delegate { songSelect.FilterControl.Sort = SortMode.Title; });
AddStep(@"Sort by Author", delegate { songSelect.FilterControl.Sort = SortMode.Author; });
2019-07-07 23:27:12 +08:00
AddStep(@"Sort by DateAdded", delegate { songSelect.FilterControl.Sort = SortMode.DateAdded; });
AddStep(@"Sort by BPM", delegate { songSelect.FilterControl.Sort = SortMode.BPM; });
AddStep(@"Sort by Length", delegate { songSelect.FilterControl.Sort = SortMode.Length; });
2018-04-13 17:19:50 +08:00
AddStep(@"Sort by Difficulty", delegate { songSelect.FilterControl.Sort = SortMode.Difficulty; });
}
2018-07-18 11:58:45 +08:00
[Test]
2018-07-25 22:45:07 +08:00
[Ignore("needs fixing")]
2018-08-14 13:19:26 +08:00
public void TestImportUnderDifferentRuleset()
2018-04-13 17:19:50 +08:00
{
2019-02-15 20:50:40 +08:00
createSongSelect();
changeRuleset(2);
2019-06-10 17:34:24 +08:00
addRulesetImportStep(0);
2019-03-19 16:24:26 +08:00
AddUntilStep("no selection", () => songSelect.Carousel.SelectedBeatmap == null);
}
2018-07-19 17:51:08 +08:00
[Test]
2018-08-14 13:19:26 +08:00
public void TestImportUnderCurrentRuleset()
{
2019-02-15 20:50:40 +08:00
createSongSelect();
changeRuleset(2);
2019-06-10 17:34:24 +08:00
addRulesetImportStep(2);
addRulesetImportStep(1);
2019-03-19 16:24:26 +08:00
AddUntilStep("has selection", () => songSelect.Carousel.SelectedBeatmap.RulesetID == 2);
2018-07-19 17:51:08 +08:00
changeRuleset(1);
2019-03-19 16:24:26 +08:00
AddUntilStep("has selection", () => songSelect.Carousel.SelectedBeatmap.RulesetID == 1);
2018-07-19 17:51:08 +08:00
changeRuleset(0);
2019-03-19 16:24:26 +08:00
AddUntilStep("no selection", () => songSelect.Carousel.SelectedBeatmap == null);
2018-07-18 11:58:45 +08:00
}
2018-08-14 13:18:46 +08:00
[Test]
public void TestRulesetChangeResetsMods()
{
2019-02-15 20:50:40 +08:00
createSongSelect();
2018-08-14 13:18:46 +08:00
changeRuleset(0);
changeMods(new OsuModHardRock());
int actionIndex = 0;
int modChangeIndex = 0;
int rulesetChangeIndex = 0;
AddStep("change ruleset", () =>
{
2019-04-10 11:03:57 +08:00
Mods.ValueChanged += onModChange;
2018-08-14 13:18:46 +08:00
songSelect.Ruleset.ValueChanged += onRulesetChange;
Ruleset.Value = new TaikoRuleset().RulesetInfo;
2019-04-10 11:03:57 +08:00
Mods.ValueChanged -= onModChange;
2018-08-14 13:18:46 +08:00
songSelect.Ruleset.ValueChanged -= onRulesetChange;
});
AddAssert("mods changed before ruleset", () => modChangeIndex < rulesetChangeIndex);
2019-04-10 11:03:57 +08:00
AddAssert("empty mods", () => !Mods.Value.Any());
2018-08-14 13:18:46 +08:00
void onModChange(ValueChangedEvent<IReadOnlyList<Mod>> e) => modChangeIndex = actionIndex++;
void onRulesetChange(ValueChangedEvent<RulesetInfo> e) => rulesetChangeIndex = actionIndex++;
2018-08-14 13:18:46 +08:00
}
2019-01-10 14:25:07 +08:00
[Test]
public void TestStartAfterUnMatchingFilterDoesNotStart()
{
2019-02-15 20:50:40 +08:00
createSongSelect();
2019-01-10 14:25:07 +08:00
addManyTestMaps();
2019-03-19 16:24:26 +08:00
AddUntilStep("has selection", () => songSelect.Carousel.SelectedBeatmap != null);
2019-01-10 14:25:07 +08:00
bool startRequested = false;
AddStep("set filter and finalize", () =>
{
songSelect.StartRequested = () => startRequested = true;
songSelect.Carousel.Filter(new FilterCriteria { SearchText = "somestringthatshouldn'tbematchable" });
songSelect.FinaliseSelection();
songSelect.StartRequested = null;
});
AddAssert("start not requested", () => !startRequested);
}
[Test]
public void TestHideSetSelectsCorrectBeatmap()
{
int? previousID = null;
createSongSelect();
addRulesetImportStep(0);
AddStep("Move to last difficulty", () => songSelect.Carousel.SelectBeatmap(songSelect.Carousel.BeatmapSets.First().Beatmaps.Last()));
AddStep("Store current ID", () => previousID = songSelect.Carousel.SelectedBeatmap.ID);
AddStep("Hide first beatmap", () => manager.Hide(songSelect.Carousel.SelectedBeatmapSet.Beatmaps.First()));
AddAssert("Selected beatmap has not changed", () => songSelect.Carousel.SelectedBeatmap.ID == previousID);
}
private void addRulesetImportStep(int id) => AddStep($"import test map for ruleset {id}", () => importForRuleset(id));
private void importForRuleset(int id) => manager.Import(createTestBeatmapSet(getImportId(), rulesets.AvailableRulesets.Where(r => r.ID == id).ToArray())).Wait();
private static int importId;
private int getImportId() => ++importId;
2019-04-10 11:03:57 +08:00
private void changeMods(params Mod[] mods) => AddStep($"change mods to {string.Join(", ", mods.Select(m => m.Acronym))}", () => Mods.Value = mods);
2018-08-14 13:18:46 +08:00
private void changeRuleset(int id) => AddStep($"change ruleset to {id}", () => Ruleset.Value = rulesets.AvailableRulesets.First(r => r.ID == id));
2019-02-15 20:50:40 +08:00
private void createSongSelect()
{
AddStep("create song select", () => LoadScreen(songSelect = new TestSongSelect()));
2019-03-19 16:24:26 +08:00
AddUntilStep("wait for present", () => songSelect.IsCurrentScreen());
2019-02-15 20:50:40 +08:00
}
2018-07-18 11:58:45 +08:00
private void addManyTestMaps()
{
AddStep("import test maps", () =>
{
var usableRulesets = rulesets.AvailableRulesets.Where(r => r.ID != 2).ToArray();
for (int i = 0; i < 100; i += 10)
2019-06-10 11:46:21 +08:00
manager.Import(createTestBeatmapSet(i, usableRulesets)).Wait();
2018-07-18 11:58:45 +08:00
});
}
private BeatmapSetInfo createTestBeatmapSet(int setId, RulesetInfo[] rulesets)
2018-07-18 11:58:45 +08:00
{
int j = 0;
RulesetInfo getRuleset() => rulesets[j++ % rulesets.Length];
var beatmaps = new List<BeatmapInfo>();
for (int i = 0; i < 6; i++)
{
int beatmapId = setId * 10 + i;
2019-07-08 15:43:35 +08:00
2019-07-07 23:27:12 +08:00
int length = RNG.Next(30000, 200000);
2019-07-08 15:43:35 +08:00
double bpm = RNG.NextSingle(80, 200);
2018-07-18 11:58:45 +08:00
beatmaps.Add(new BeatmapInfo
{
Ruleset = getRuleset(),
OnlineBeatmapID = beatmapId,
Path = "normal.osu",
2019-07-08 15:43:35 +08:00
Version = $"{beatmapId} (length {TimeSpan.FromMilliseconds(length):m\\:ss}, bpm {bpm:0.#})",
2019-07-08 01:25:36 +08:00
Length = length,
2019-07-08 15:43:35 +08:00
BPM = bpm,
2018-07-18 11:58:45 +08:00
BaseDifficulty = new BeatmapDifficulty
{
OverallDifficulty = 3.5f,
2019-07-07 23:27:12 +08:00
},
2018-07-18 11:58:45 +08:00
});
}
2018-04-13 17:19:50 +08:00
return new BeatmapSetInfo
{
2018-07-18 11:58:45 +08:00
OnlineBeatmapSetID = setId,
2018-04-13 17:19:50 +08:00
Hash = new MemoryStream(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())).ComputeMD5Hash(),
Metadata = new BeatmapMetadata
{
// Create random metadata, then we can check if sorting works based on these
2018-07-18 11:58:45 +08:00
Artist = "Some Artist " + RNG.Next(0, 9),
2019-07-08 15:43:35 +08:00
Title = $"Some Song (set id {setId}, max bpm {beatmaps.Max(b => b.BPM):0.#})",
2018-04-13 17:19:50 +08:00
AuthorString = "Some Guy " + RNG.Next(0, 9),
},
2019-07-07 23:27:12 +08:00
Beatmaps = beatmaps,
DateAdded = DateTimeOffset.UtcNow,
2018-04-13 17:19:50 +08:00
};
}
}
}