Clean up tests some more

This commit is contained in:
Dean Herbert 2017-12-13 21:08:12 +09:00
parent 1b85952441
commit ec4f99c92e
2 changed files with 76 additions and 40 deletions

View File

@ -9,7 +9,6 @@
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.MathUtils;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Screens.Select; using osu.Game.Screens.Select;
using osu.Game.Screens.Select.Carousel; using osu.Game.Screens.Select.Carousel;
@ -18,7 +17,7 @@ namespace osu.Game.Tests.Visual
{ {
internal class TestCaseBeatmapCarousel : OsuTestCase internal class TestCaseBeatmapCarousel : OsuTestCase
{ {
private BeatmapCarousel carousel; private TestBeatmapCarousel carousel;
public override IReadOnlyList<Type> RequiredTypes => new[] public override IReadOnlyList<Type> RequiredTypes => new[]
{ {
@ -38,7 +37,7 @@ internal class TestCaseBeatmapCarousel : OsuTestCase
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
Add(carousel = new BeatmapCarousel Add(carousel = new TestBeatmapCarousel
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
}); });
@ -47,36 +46,66 @@ private void load()
{ {
carousel.Beatmaps = new[] carousel.Beatmaps = new[]
{ {
createTestBeatmapSet(0),
createTestBeatmapSet(1), createTestBeatmapSet(1),
createTestBeatmapSet(2), createTestBeatmapSet(2),
createTestBeatmapSet(3), createTestBeatmapSet(3),
createTestBeatmapSet(4),
}; };
}); });
void checkSelected(int set, int diff) =>
AddAssert($"selected is set{set} diff{diff}", () =>
carousel.SelectedBeatmap == carousel.Beatmaps.Skip(set - 1).First().Beatmaps.Skip(diff - 1).First());
void advanceSelection(bool diff, int direction = 1, int count = 1)
{
if (count == 1)
AddStep($"select {(direction > 0 ? "next" : "prev")} {(diff ? "diff" : "set")}", () =>
carousel.SelectNext(direction, !diff));
else
{
AddRepeatStep($"select {(direction > 0 ? "next" : "prev")} {(diff ? "diff" : "set")}", () =>
carousel.SelectNext(direction, !diff), count);
}
}
void checkVisibleItemCount(bool diff, int count) =>
AddAssert($"{count} {(diff ? "diff" : "set")} visible", () =>
carousel.Items.Count(s => (diff ? s.Item is CarouselBeatmap : s.Item is CarouselBeatmapSet) && s.Item.Visible) == count);
AddUntilStep(() => carousel.Beatmaps.Any(), "Wait for load"); AddUntilStep(() => carousel.Beatmaps.Any(), "Wait for load");
AddStep("SelectNext set", () => carousel.SelectNext()); advanceSelection(direction: 1, diff: false);
AddAssert("set1 diff1", () => carousel.SelectedBeatmap == carousel.Beatmaps.First().Beatmaps.First()); checkSelected(1, 1);
AddStep("SelectNext diff", () => carousel.SelectNext(1, false)); advanceSelection(direction: 1, diff: true);
AddAssert("set1 diff2", () => carousel.SelectedBeatmap == carousel.Beatmaps.First().Beatmaps.Skip(1).First()); checkSelected(1, 2);
AddStep("SelectNext backwards", () => carousel.SelectNext(-1)); advanceSelection(direction: -1, diff: false);
AddAssert("set4 diff1", () => carousel.SelectedBeatmap == carousel.Beatmaps.Last().Beatmaps.First()); checkSelected(4, 1);
AddStep("SelectNext diff backwards", () => carousel.SelectNext(-1, false)); advanceSelection(direction: -1, diff: true);
AddAssert("set3 diff3", () => carousel.SelectedBeatmap == carousel.Beatmaps.Reverse().Skip(1).First().Beatmaps.Last()); checkSelected(3, 3);
AddStep("SelectNext", () => carousel.SelectNext()); advanceSelection(diff: false);
AddStep("SelectNext", () => carousel.SelectNext()); advanceSelection(diff: false);
AddAssert("set1 diff1", () => carousel.SelectedBeatmap == carousel.Beatmaps.First().Beatmaps.First()); checkSelected(1, 1);
AddStep("SelectNext diff backwards", () => carousel.SelectNext(-1, false)); advanceSelection(direction: -1, diff: true);
AddAssert("set4 diff3", () => carousel.SelectedBeatmap == carousel.Beatmaps.Last().Beatmaps.Last()); checkSelected(4, 3);
// AddStep("Clear beatmaps", () => carousel.Beatmaps = new BeatmapSetInfo[] { }); AddStep("Filter", () => carousel.Filter(new FilterCriteria { SearchText = "set #3" }, false));
// AddStep("SelectNext (noop)", () => carousel.SelectNext()); checkVisibleItemCount(diff: false, count: 1);
checkVisibleItemCount(diff: true, count: 3);
checkSelected(3, 1);
advanceSelection(diff: true, count: 4);
checkSelected(3, 2);
AddStep("Un-filter (debounce)", () => carousel.Filter(new FilterCriteria()));
AddUntilStep(() => !carousel.PendingFilterTask, "Wait for debounce");
checkVisibleItemCount(diff: false, count: 4);
checkVisibleItemCount(diff: true, count: 3);
} }
private BeatmapSetInfo createTestBeatmapSet(int i) private BeatmapSetInfo createTestBeatmapSet(int i)
@ -89,9 +118,9 @@ private BeatmapSetInfo createTestBeatmapSet(int i)
{ {
OnlineBeatmapSetID = 1234 + i, OnlineBeatmapSetID = 1234 + i,
// Create random metadata, then we can check if sorting works based on these // Create random metadata, then we can check if sorting works based on these
Artist = "MONACA " + RNG.Next(0, 9), Artist = "peppy",
Title = "Black Song " + RNG.Next(0, 9), Title = "test set #" + i,
AuthorString = "Some Guy " + RNG.Next(0, 9), AuthorString = "peppy"
}, },
Beatmaps = new List<BeatmapInfo>(new[] Beatmaps = new List<BeatmapInfo>(new[]
{ {
@ -128,5 +157,12 @@ private BeatmapSetInfo createTestBeatmapSet(int i)
}), }),
}; };
} }
private class TestBeatmapCarousel : BeatmapCarousel
{
public new List<DrawableCarouselItem> Items => base.Items;
public bool PendingFilterTask => FilterTask != null;
}
} }
} }

View File

@ -51,7 +51,7 @@ public IEnumerable<BeatmapSetInfo> Beatmaps
Schedule(() => Schedule(() =>
{ {
scrollableContent.Clear(false); scrollableContent.Clear(false);
items.Clear(); Items.Clear();
carouselSets.Clear(); carouselSets.Clear();
yPositionsCache.Invalidate(); yPositionsCache.Invalidate();
}); });
@ -69,7 +69,7 @@ public IEnumerable<BeatmapSetInfo> Beatmaps
carouselSets.AddRange(newSets); carouselSets.AddRange(newSets);
root = new CarouselGroup(newSets.OfType<CarouselItem>().ToList()); root = new CarouselGroup(newSets.OfType<CarouselItem>().ToList());
items = root.Drawables.Value.ToList(); Items = root.Drawables.Value.ToList();
yPositionsCache.Invalidate(); yPositionsCache.Invalidate();
BeatmapsChanged?.Invoke(); BeatmapsChanged?.Invoke();
@ -88,7 +88,7 @@ public IEnumerable<BeatmapSetInfo> Beatmaps
private Bindable<RandomSelectAlgorithm> randomSelectAlgorithm; private Bindable<RandomSelectAlgorithm> randomSelectAlgorithm;
private readonly List<CarouselBeatmapSet> seenSets = new List<CarouselBeatmapSet>(); private readonly List<CarouselBeatmapSet> seenSets = new List<CarouselBeatmapSet>();
private List<DrawableCarouselItem> items = new List<DrawableCarouselItem>(); protected List<DrawableCarouselItem> Items = new List<DrawableCarouselItem>();
private CarouselGroup root = new CarouselGroup(); private CarouselGroup root = new CarouselGroup();
private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>(); private readonly Stack<CarouselBeatmap> randomSelectedBeatmaps = new Stack<CarouselBeatmap>();
@ -192,15 +192,15 @@ public void SelectNext(int direction = 1, bool skipDifficulties = true)
return; return;
} }
int originalIndex = items.IndexOf(selectedBeatmap?.Drawables.Value.First()); int originalIndex = Items.IndexOf(selectedBeatmap?.Drawables.Value.First());
int currentIndex = originalIndex; int currentIndex = originalIndex;
// local function to increment the index in the required direction, wrapping over extremities. // local function to increment the index in the required direction, wrapping over extremities.
int incrementIndex() => currentIndex = (currentIndex + direction + items.Count) % items.Count; int incrementIndex() => currentIndex = (currentIndex + direction + Items.Count) % Items.Count;
while (incrementIndex() != originalIndex) while (incrementIndex() != originalIndex)
{ {
var item = items[currentIndex].Item; var item = Items[currentIndex].Item;
if (item.Filtered || item.State == CarouselItemState.Selected) continue; if (item.Filtered || item.State == CarouselItemState.Selected) continue;
@ -272,13 +272,13 @@ public void SelectPreviousRandom()
private FilterCriteria criteria = new FilterCriteria(); private FilterCriteria criteria = new FilterCriteria();
private ScheduledDelegate filterTask; protected ScheduledDelegate FilterTask;
public bool AllowSelection = true; public bool AllowSelection = true;
public void FlushPendingFilters() public void FlushPendingFilters()
{ {
if (filterTask?.Completed == false) if (FilterTask?.Completed == false)
Filter(null, false); Filter(null, false);
} }
@ -289,7 +289,7 @@ public void Filter(FilterCriteria newCriteria = null, bool debounce = true)
Action perform = delegate Action perform = delegate
{ {
filterTask = null; FilterTask = null;
carouselSets.ForEach(s => s.Filter(criteria)); carouselSets.ForEach(s => s.Filter(criteria));
@ -301,11 +301,11 @@ public void Filter(FilterCriteria newCriteria = null, bool debounce = true)
select(selectedBeatmap); select(selectedBeatmap);
}; };
filterTask?.Cancel(); FilterTask?.Cancel();
filterTask = null; FilterTask = null;
if (debounce) if (debounce)
filterTask = Scheduler.AddDelayed(perform, 250); FilterTask = Scheduler.AddDelayed(perform, 250);
else else
perform(); perform();
} }
@ -362,7 +362,7 @@ private void removeBeatmapSet(CarouselBeatmapSet set)
foreach (var d in set.Drawables.Value) foreach (var d in set.Drawables.Value)
{ {
items.Remove(d); Items.Remove(d);
scrollableContent.Remove(d); scrollableContent.Remove(d);
} }
@ -385,7 +385,7 @@ private float computeYPositions(bool animated = true)
float lastSetY = 0; float lastSetY = 0;
foreach (DrawableCarouselItem d in items) foreach (DrawableCarouselItem d in Items)
{ {
switch (d) switch (d)
{ {
@ -474,7 +474,7 @@ protected override void Update()
}); });
// Find index range of all items that should be on-screen // Find index range of all items that should be on-screen
Trace.Assert(items.Count == yPositions.Count); Trace.Assert(Items.Count == yPositions.Count);
int firstIndex = yPositions.BinarySearch(Current - DrawableCarouselItem.MAX_HEIGHT); int firstIndex = yPositions.BinarySearch(Current - DrawableCarouselItem.MAX_HEIGHT);
if (firstIndex < 0) firstIndex = ~firstIndex; if (firstIndex < 0) firstIndex = ~firstIndex;
@ -484,21 +484,21 @@ protected override void Update()
lastIndex = ~lastIndex; lastIndex = ~lastIndex;
// Add the first item of the last visible beatmap group to preload its data. // Add the first item of the last visible beatmap group to preload its data.
if (lastIndex != 0 && items[lastIndex - 1] is DrawableCarouselBeatmapSet) if (lastIndex != 0 && Items[lastIndex - 1] is DrawableCarouselBeatmapSet)
lastIndex++; lastIndex++;
} }
// Add those items within the previously found index range that should be displayed. // Add those items within the previously found index range that should be displayed.
for (int i = firstIndex; i < lastIndex; ++i) for (int i = firstIndex; i < lastIndex; ++i)
{ {
DrawableCarouselItem item = items[i]; DrawableCarouselItem item = Items[i];
// Only add if we're not already part of the content. // Only add if we're not already part of the content.
if (!scrollableContent.Contains(item)) if (!scrollableContent.Contains(item))
{ {
// Makes sure headers are always _below_ items, // Makes sure headers are always _below_ items,
// and depth flows downward. // and depth flows downward.
item.Depth = i + (item is DrawableCarouselBeatmapSet ? items.Count : 0); item.Depth = i + (item is DrawableCarouselBeatmapSet ? Items.Count : 0);
switch (item.LoadState) switch (item.LoadState)
{ {