1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 04:02:57 +08:00

Prepare tests and general structure to support omission of pages

This commit is contained in:
Dean Herbert 2022-01-04 18:46:44 +09:00
parent e75c9519f3
commit 86f72b71b1
3 changed files with 52 additions and 64 deletions

View File

@ -1,9 +1,11 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Game.Graphics.UserInterface.PageSelector; using osu.Game.Graphics.UserInterface.PageSelector;
using osu.Game.Overlays; using osu.Game.Overlays;
@ -28,36 +30,38 @@ namespace osu.Game.Tests.Visual.UserInterface
}); });
} }
[Test]
public void TestOmittedPages()
{
setAvailablePages(100);
AddAssert("Correct page buttons", () => pageSelector.ChildrenOfType<PageSelectorPageButton>().Select(p => p.PageNumber).SequenceEqual(new[] { 1, 2, 3, 100 }));
}
[Test] [Test]
public void TestResetCurrentPage() public void TestResetCurrentPage()
{ {
AddStep("Set 10 pages", () => setMaxPages(10)); setAvailablePages(10);
AddStep("Select page 5", () => setCurrentPage(5)); selectPage(6);
AddStep("Set 11 pages", () => setMaxPages(11)); setAvailablePages(11);
AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 1); AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 0);
} }
[Test] [Test]
public void TestOutOfBoundsSelection() public void TestOutOfBoundsSelection()
{ {
AddStep("Set 10 pages", () => setMaxPages(10)); setAvailablePages(10);
AddStep("Select page 11", () => setCurrentPage(11)); selectPage(11);
AddAssert("Page 10 is current", () => pageSelector.CurrentPage.Value == pageSelector.MaxPages.Value); AddAssert("Page 10 is current", () => pageSelector.CurrentPage.Value == pageSelector.AvailablePages.Value - 1);
AddStep("Select page -1", () => setCurrentPage(-1)); selectPage(-1);
AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 1); AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 0);
} }
[Test] private void selectPage(int pageIndex) =>
public void TestNegativeMaxPages() AddStep($"Select page {pageIndex}", () => pageSelector.CurrentPage.Value = pageIndex);
{
AddStep("Set -10 pages", () => setMaxPages(-10));
AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 1);
AddAssert("Max is 1", () => pageSelector.MaxPages.Value == 1);
}
private void setMaxPages(int maxPages) => pageSelector.MaxPages.Value = maxPages; private void setAvailablePages(int availablePages) =>
AddStep($"Set available pages to {availablePages}", () => pageSelector.AvailablePages.Value = availablePages);
private void setCurrentPage(int currentPage) => pageSelector.CurrentPage.Value = currentPage;
} }
} }

View File

@ -1,19 +1,20 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Linq;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Graphics.UserInterface.PageSelector namespace osu.Game.Graphics.UserInterface.PageSelector
{ {
public class PageSelector : CompositeDrawable public class PageSelector : CompositeDrawable
{ {
public readonly BindableInt CurrentPage = new BindableInt(1); public readonly BindableInt CurrentPage = new BindableInt { MinValue = 0, };
public readonly BindableInt MaxPages = new BindableInt(1);
private readonly FillFlowContainer<PageSelectorPageButton> itemsFlow; public readonly BindableInt AvailablePages = new BindableInt(1) { MinValue = 1, };
private readonly FillFlowContainer itemsFlow;
private readonly PageSelectorPrevNextButton previousPageButton; private readonly PageSelectorPrevNextButton previousPageButton;
private readonly PageSelectorPrevNextButton nextPageButton; private readonly PageSelectorPrevNextButton nextPageButton;
@ -32,7 +33,7 @@ namespace osu.Game.Graphics.UserInterface.PageSelector
{ {
Action = () => CurrentPage.Value -= 1, Action = () => CurrentPage.Value -= 1,
}, },
itemsFlow = new FillFlowContainer<PageSelectorPageButton> itemsFlow = new FillFlowContainer
{ {
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
@ -49,60 +50,43 @@ namespace osu.Game.Graphics.UserInterface.PageSelector
{ {
base.LoadComplete(); base.LoadComplete();
MaxPages.BindValueChanged(_ => redraw()); CurrentPage.BindValueChanged(onCurrentPageChanged);
CurrentPage.BindValueChanged(page => onCurrentPageChanged(page.NewValue)); AvailablePages.BindValueChanged(_ => redraw(), true);
redraw();
} }
private void onCurrentPageChanged(int newPage) private void onCurrentPageChanged(ValueChangedEvent<int> currentPage)
{ {
if (newPage < 1) if (currentPage.NewValue >= AvailablePages.Value)
{ {
CurrentPage.Value = 1; CurrentPage.Value = AvailablePages.Value - 1;
return; return;
} }
if (newPage > MaxPages.Value) foreach (var page in itemsFlow.OfType<PageSelectorPageButton>())
{ page.Selected = page.PageNumber == currentPage.NewValue + 1;
CurrentPage.Value = MaxPages.Value;
return;
}
itemsFlow.ForEach(page => page.Selected = page.Page == newPage); previousPageButton.Enabled.Value = currentPage.NewValue != 0;
updateButtonsState(); nextPageButton.Enabled.Value = currentPage.NewValue < AvailablePages.Value - 1;
} }
private void redraw() private void redraw()
{ {
itemsFlow.Clear(); itemsFlow.Clear();
if (MaxPages.Value < 1) for (int i = 0; i < AvailablePages.Value; i++)
{ {
MaxPages.Value = 1; int pageIndex = i;
return;
itemsFlow.Add(new PageSelectorPageButton(pageIndex + 1)
{
Action = () => CurrentPage.Value = pageIndex,
});
} }
for (int i = 1; i <= MaxPages.Value; i++) if (CurrentPage.Value != 0)
addDrawablePage(i); CurrentPage.Value = 0;
if (CurrentPage.Value == 1)
CurrentPage.TriggerChange();
else else
CurrentPage.Value = 1; CurrentPage.TriggerChange();
} }
private void updateButtonsState()
{
int newPage = CurrentPage.Value;
int maxPages = MaxPages.Value;
previousPageButton.Enabled.Value = newPage != 1;
nextPageButton.Enabled.Value = newPage != maxPages;
}
private void addDrawablePage(int page) => itemsFlow.Add(new PageSelectorPageButton(page)
{
Action = () => CurrentPage.Value = page,
});
} }
} }

View File

@ -17,13 +17,13 @@ namespace osu.Game.Graphics.UserInterface.PageSelector
set => selected.Value = value; set => selected.Value = value;
} }
public int Page { get; } public int PageNumber { get; }
private OsuSpriteText text; private OsuSpriteText text;
public PageSelectorPageButton(int page) public PageSelectorPageButton(int pageNumber)
{ {
Page = page; PageNumber = pageNumber;
Action = () => Action = () =>
{ {
@ -35,7 +35,7 @@ namespace osu.Game.Graphics.UserInterface.PageSelector
protected override Drawable CreateContent() => text = new OsuSpriteText protected override Drawable CreateContent() => text = new OsuSpriteText
{ {
Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold), Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold),
Text = Page.ToString(), Text = PageNumber.ToString(),
}; };
[BackgroundDependencyLoader] [BackgroundDependencyLoader]