1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:07:26 +08:00
osu-lazer/osu.Game.Tests/Visual/Collections/TestSceneManageCollectionsDialog.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

309 lines
11 KiB
C#
Raw Normal View History

2020-09-07 21:47:19 +08:00
// 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.
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
2020-09-07 21:47:19 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface;
2020-09-08 16:05:43 +08:00
using osu.Framework.Platform;
2020-09-07 21:47:19 +08:00
using osu.Framework.Testing;
2020-09-08 16:05:43 +08:00
using osu.Game.Beatmaps;
2020-09-07 21:47:19 +08:00
using osu.Game.Collections;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
2020-09-08 16:05:43 +08:00
using osu.Game.Rulesets;
using osu.Game.Tests.Resources;
2020-09-07 21:47:19 +08:00
using osuTK;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Collections
{
public partial class TestSceneManageCollectionsDialog : OsuManualInputManagerTestScene
{
protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };
2020-09-07 21:47:19 +08:00
2022-07-27 14:59:36 +08:00
private DialogOverlay dialogOverlay = null!;
private BeatmapManager beatmapManager = null!;
private ManageCollectionsDialog dialog = null!;
2020-09-07 21:47:19 +08:00
[BackgroundDependencyLoader]
private void load(GameHost host)
2020-09-07 21:47:19 +08:00
{
Dependencies.Cache(new RealmRulesetStore(Realm));
Dependencies.Cache(beatmapManager = new BeatmapManager(LocalStorage, Realm, null, Audio, Resources, host, Beatmap.Default));
Dependencies.Cache(Realm);
beatmapManager.Import(TestResources.GetQuickTestBeatmapForImport()).WaitSafely();
2020-09-07 21:47:19 +08:00
base.Content.AddRange(new Drawable[]
{
Content,
2021-02-24 17:42:26 +08:00
dialogOverlay = new DialogOverlay(),
2020-09-07 21:47:19 +08:00
});
Dependencies.CacheAs<IDialogOverlay>(dialogOverlay);
2020-09-08 16:05:43 +08:00
}
2020-09-07 21:47:19 +08:00
[SetUp]
public void SetUp() => Schedule(() =>
{
Realm.Write(r => r.RemoveAll<BeatmapCollection>());
2020-09-07 21:47:19 +08:00
Child = dialog = new ManageCollectionsDialog();
});
[SetUpSteps]
public void SetUpSteps()
{
AddStep("show dialog", () => dialog.Show());
}
[Test]
public void TestHideDialog()
{
AddWaitStep("wait for animation", 3);
AddStep("hide dialog", () => dialog.Hide());
}
2020-09-08 16:05:43 +08:00
[Test]
public void TestLastItemIsPlaceholder()
{
2022-07-27 14:59:36 +08:00
AddAssert("last item is placeholder", () => !dialog.ChildrenOfType<DrawableCollectionListItem>().Last().Model.IsManaged);
2020-09-08 16:05:43 +08:00
}
2020-09-07 21:47:19 +08:00
[Test]
public void TestAddCollectionExternal()
{
AddStep("add collection", () => Realm.Write(r => r.Add(new BeatmapCollection(name: "First collection"))));
2020-09-07 21:47:19 +08:00
assertCollectionCount(1);
assertCollectionName(0, "First collection");
AddStep("add another collection", () => Realm.Write(r => r.Add(new BeatmapCollection(name: "Second collection"))));
2020-09-07 21:47:19 +08:00
assertCollectionCount(2);
assertCollectionName(1, "Second collection");
}
[Test]
2020-09-08 16:05:43 +08:00
public void TestFocusPlaceholderDoesNotCreateCollection()
2020-09-07 21:47:19 +08:00
{
2020-09-08 16:05:43 +08:00
AddStep("focus placeholder", () =>
2020-09-07 21:47:19 +08:00
{
2020-09-08 16:05:43 +08:00
InputManager.MoveMouseTo(dialog.ChildrenOfType<DrawableCollectionListItem>().Last());
2020-09-07 21:47:19 +08:00
InputManager.Click(MouseButton.Left);
});
2020-09-08 16:05:43 +08:00
assertCollectionCount(0);
}
[Test]
public void TestAddCollectionViaPlaceholder()
{
2022-07-27 14:59:36 +08:00
DrawableCollectionListItem placeholderItem = null!;
2020-09-07 21:47:19 +08:00
2020-09-08 16:05:43 +08:00
AddStep("focus placeholder", () =>
2020-09-07 21:47:19 +08:00
{
2020-09-08 16:05:43 +08:00
InputManager.MoveMouseTo(placeholderItem = dialog.ChildrenOfType<DrawableCollectionListItem>().Last());
2020-09-07 21:47:19 +08:00
InputManager.Click(MouseButton.Left);
});
assertCollectionCount(0);
AddStep("change collection name", () =>
{
placeholderItem.ChildrenOfType<TextBox>().First().Text = "test text";
InputManager.Key(Key.Enter);
});
2020-09-08 16:05:43 +08:00
assertCollectionCount(1);
2022-07-27 14:59:36 +08:00
AddAssert("last item is placeholder", () => !dialog.ChildrenOfType<DrawableCollectionListItem>().Last().Model.IsManaged);
2020-09-07 21:47:19 +08:00
}
[Test]
public void TestRemoveCollectionExternal()
{
BeatmapCollection first = null!;
2022-07-27 14:59:36 +08:00
AddStep("add two collections", () =>
2020-09-07 21:47:19 +08:00
{
2022-07-27 14:59:36 +08:00
Realm.Write(r =>
{
r.Add(new[]
{
first = new BeatmapCollection(name: "1"),
new BeatmapCollection(name: "2"),
2022-07-27 14:59:36 +08:00
});
});
});
2020-09-07 21:47:19 +08:00
AddStep("remove first collection", () => Realm.Write(r => r.Remove(first)));
2020-09-07 21:47:19 +08:00
assertCollectionCount(1);
assertCollectionName(0, "2");
}
2021-02-24 17:42:26 +08:00
[Test]
public void TestCollectionNameCollisions()
{
AddStep("add dropdown", () =>
{
2022-07-28 12:48:15 +08:00
Add(new CollectionDropdown
2022-05-04 13:43:59 +08:00
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
RelativeSizeAxes = Axes.X,
Width = 0.4f,
});
2021-02-24 17:42:26 +08:00
});
2022-07-27 14:59:36 +08:00
AddStep("add two collections with same name", () => Realm.Write(r => r.Add(new[]
2021-02-24 17:42:26 +08:00
{
new BeatmapCollection(name: "1"),
new BeatmapCollection(name: "1")
2022-07-27 14:59:36 +08:00
{
BeatmapMD5Hashes = { beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps[0].MD5Hash }
},
})));
2021-02-24 17:42:26 +08:00
}
2020-09-07 21:47:19 +08:00
[Test]
public void TestRemoveCollectionViaButton()
{
2022-07-27 14:59:36 +08:00
AddStep("add two collections", () => Realm.Write(r => r.Add(new[]
2020-09-07 21:47:19 +08:00
{
new BeatmapCollection(name: "1"),
new BeatmapCollection(name: "2")
2022-07-27 14:59:36 +08:00
{
BeatmapMD5Hashes = { beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps[0].MD5Hash }
},
})));
2020-09-07 21:47:19 +08:00
2020-09-08 12:23:50 +08:00
assertCollectionCount(2);
2020-09-07 21:47:19 +08:00
AddStep("click first delete button", () =>
{
InputManager.MoveMouseTo(dialog.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().First(), new Vector2(5, 0));
InputManager.Click(MouseButton.Left);
});
2020-09-08 16:05:43 +08:00
AddAssert("dialog not displayed", () => dialogOverlay.CurrentDialog == null);
assertCollectionCount(1);
assertCollectionName(0, "2");
AddStep("click first delete button", () =>
{
InputManager.MoveMouseTo(dialog.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().First(), new Vector2(5, 0));
InputManager.Click(MouseButton.Left);
});
2020-09-07 21:47:19 +08:00
AddAssert("dialog displayed", () => dialogOverlay.CurrentDialog is DeleteCollectionDialog);
AddStep("click confirmation", () =>
{
InputManager.MoveMouseTo(dialogOverlay.CurrentDialog.ChildrenOfType<PopupDialogButton>().First());
InputManager.PressButton(MouseButton.Left);
2020-09-07 21:47:19 +08:00
});
2020-09-08 16:05:43 +08:00
assertCollectionCount(0);
AddStep("release mouse button", () => InputManager.ReleaseButton(MouseButton.Left));
2020-09-07 21:47:19 +08:00
}
[Test]
public void TestCollectionNotRemovedWhenDialogCancelled()
{
2022-07-27 14:59:36 +08:00
AddStep("add collection", () => Realm.Write(r => r.Add(new[]
2020-09-07 21:47:19 +08:00
{
new BeatmapCollection(name: "1")
2022-07-27 14:59:36 +08:00
{
BeatmapMD5Hashes = { beatmapManager.GetAllUsableBeatmapSets().First().Beatmaps[0].MD5Hash }
},
})));
2020-09-07 21:47:19 +08:00
2020-09-08 16:05:43 +08:00
assertCollectionCount(1);
2020-09-08 12:23:50 +08:00
2020-09-07 21:47:19 +08:00
AddStep("click first delete button", () =>
{
InputManager.MoveMouseTo(dialog.ChildrenOfType<DrawableCollectionListItem.DeleteButton>().First(), new Vector2(5, 0));
InputManager.Click(MouseButton.Left);
});
AddAssert("dialog displayed", () => dialogOverlay.CurrentDialog is DeleteCollectionDialog);
2020-09-08 16:05:43 +08:00
AddStep("click cancellation", () =>
2020-09-07 21:47:19 +08:00
{
InputManager.MoveMouseTo(dialogOverlay.CurrentDialog.ChildrenOfType<PopupDialogButton>().Last());
InputManager.Click(MouseButton.Left);
});
2020-09-08 16:05:43 +08:00
assertCollectionCount(1);
2020-09-07 21:47:19 +08:00
}
[Test]
public void TestCollectionRenamedExternal()
{
BeatmapCollection first = null!;
2022-07-27 14:59:36 +08:00
AddStep("add two collections", () =>
2020-09-07 21:47:19 +08:00
{
2022-07-27 14:59:36 +08:00
Realm.Write(r =>
{
r.Add(new[]
{
first = new BeatmapCollection(name: "1"),
new BeatmapCollection(name: "2"),
2022-07-27 14:59:36 +08:00
});
});
});
2020-09-07 21:47:19 +08:00
2022-07-28 14:20:25 +08:00
assertCollectionName(0, "1");
2022-07-28 15:34:31 +08:00
assertCollectionName(1, "2");
2022-07-28 14:20:25 +08:00
2022-07-27 14:59:36 +08:00
AddStep("change first collection name", () => Realm.Write(_ => first.Name = "First"));
2020-09-07 21:47:19 +08:00
2022-07-28 14:20:25 +08:00
// Item will have moved due to alphabetical sorting.
assertCollectionName(0, "2");
assertCollectionName(1, "First");
2020-09-07 21:47:19 +08:00
}
[Test]
public void TestCollectionRenamedOnTextChange()
{
BeatmapCollection first = null!;
DrawableCollectionListItem firstItem = null!;
2022-07-27 14:59:36 +08:00
AddStep("add two collections", () =>
2020-09-07 21:47:19 +08:00
{
2022-07-27 14:59:36 +08:00
Realm.Write(r =>
{
r.Add(new[]
{
first = new BeatmapCollection(name: "1"),
new BeatmapCollection(name: "2"),
2022-07-27 14:59:36 +08:00
});
});
});
2020-09-07 21:47:19 +08:00
2020-09-08 12:23:50 +08:00
assertCollectionCount(2);
AddStep("focus first collection", () =>
{
InputManager.MoveMouseTo(firstItem = dialog.ChildrenOfType<DrawableCollectionListItem>().First());
InputManager.Click(MouseButton.Left);
});
AddStep("change first collection name", () =>
{
firstItem.ChildrenOfType<TextBox>().First().Text = "First";
InputManager.Key(Key.Enter);
});
2022-07-27 14:59:36 +08:00
AddUntilStep("collection has new name", () => first.Name == "First");
2020-09-07 21:47:19 +08:00
}
private void assertCollectionCount(int count)
=> AddUntilStep($"{count} collections shown", () => dialog.ChildrenOfType<DrawableCollectionListItem>().Count() == count + 1); // +1 for placeholder
2020-09-07 21:47:19 +08:00
private void assertCollectionName(int index, string name)
2020-09-08 12:23:50 +08:00
=> AddUntilStep($"item {index + 1} has correct name", () => dialog.ChildrenOfType<DrawableCollectionListItem>().ElementAt(index).ChildrenOfType<TextBox>().First().Text == name);
2020-09-07 21:47:19 +08:00
}
}