1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 21:17:33 +08:00
osu-lazer/osu.Game/Tests/Visual/EditorTestScene.cs
Dean Herbert 5a7d339cc8 Centralise and harden editor-ready-for-use check
Not only does this combine the check into one location, but it also adds
a check on the global `WorkingBeatmap` being updated, which is the only
way I can see the following failure happening:

```csharp
05:19:07     osu.Game.Tests: osu.Game.Tests.Visual.Editing.TestSceneEditorBeatmapCreation.TestAddAudioTrack
05:19:07       Failed TestAddAudioTrack [161 ms]
05:19:07       Error Message:
05:19:07        TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
05:19:07       Stack Trace:
05:19:07       --TearDown
05:19:07        at osu.Game.Database.ModelManager`1.<>c__DisplayClass7_0.<AddFile>b__0(TModel managed) in C:\BuildAgent\work\ecd860037212ac52\osu.Game\Database\ModelManager.cs:line 36
05:19:07        at osu.Game.Database.ModelManager`1.<>c__DisplayClass8_0.<performFileOperation>b__0(Realm realm) in C:\BuildAgent\work\ecd860037212ac52\osu.Game\Database\ModelManager.cs:line 49
05:19:07        at osu.Game.Database.RealmExtensions.Write(Realm realm, Action`1 function) in C:\BuildAgent\work\ecd860037212ac52\osu.Game\Database\RealmExtensions.cs:line 14
05:19:07        at osu.Game.Database.ModelManager`1.performFileOperation(TModel item, Action`1 operation) in C:\BuildAgent\work\ecd860037212ac52\osu.Game\Database\ModelManager.cs:line 46
05:19:07        at osu.Game.Database.ModelManager`1.AddFile(TModel item, Stream contents, String filename) in C:\BuildAgent\work\ecd860037212ac52\osu.Game\Database\ModelManager.cs:line 36
05:19:07        at osu.Game.Screens.Edit.Setup.ResourcesSection.ChangeAudioTrack(FileInfo source) in C:\BuildAgent\work\ecd860037212ac52\osu.Game\Screens\Edit\Setup\ResourcesSection.cs:line 115
05:19:07        at osu.Game.Tests.Visual.Editing.TestSceneEditorBeatmapCreation.<TestAddAudioTrack>b__13_0() in C:\BuildAgent\work\ecd860037212ac52\osu.Game.Tests\Visual\Editing\TestSceneEditorBeatmapCreation.cs:line 101
05:19:07        at osu.Framework.Testing.Drawables.Steps.AssertButton.checkAssert()
05:19:07        at osu.Framework.Testing.Drawables.Steps.StepButton.PerformStep(Boolean userTriggered)
05:19:07        at osu.Framework.Testing.TestScene.runNextStep(Action onCompletion, Action`1 onError, Func`2 stopCondition)
```
2022-06-27 16:22:01 +09:00

184 lines
6.9 KiB
C#

// 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.
#nullable disable
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.IO.Stores;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Rulesets;
using osu.Game.Screens.Edit;
using osu.Game.Screens.Menu;
using osu.Game.Skinning;
namespace osu.Game.Tests.Visual
{
public abstract class EditorTestScene : ScreenTestScene
{
private TestEditorLoader editorLoader;
protected TestEditor Editor => editorLoader.Editor;
protected EditorBeatmap EditorBeatmap => Editor.ChildrenOfType<EditorBeatmap>().Single();
protected EditorClock EditorClock => Editor.ChildrenOfType<EditorClock>().Single();
/// <summary>
/// Whether any saves performed by the editor should be isolate (and not persist) to the underlying <see cref="BeatmapManager"/>.
/// </summary>
protected virtual bool IsolateSavingFromDatabase => true;
// required for screen transitions to work properly
// (see comment in EditorLoader.LogoArriving).
[Cached]
private OsuLogo logo = new OsuLogo
{
Alpha = 0
};
private TestBeatmapManager testBeatmapManager;
[BackgroundDependencyLoader]
private void load(GameHost host, AudioManager audio, RulesetStore rulesets)
{
Add(logo);
if (IsolateSavingFromDatabase)
Dependencies.CacheAs<BeatmapManager>(testBeatmapManager = new TestBeatmapManager(LocalStorage, Realm, rulesets, null, audio, Resources, host, Beatmap.Default));
}
public override void SetUpSteps()
{
base.SetUpSteps();
AddStep("load editor", LoadEditor);
AddUntilStep("wait for editor to load", () => Editor?.ReadyForUse == true);
AddUntilStep("wait for beatmap updated", () => !Beatmap.IsDefault);
}
protected virtual void LoadEditor()
{
Beatmap.Value = CreateWorkingBeatmap(Ruleset.Value);
if (testBeatmapManager != null)
testBeatmapManager.TestBeatmap = Beatmap.Value;
LoadScreen(editorLoader = new TestEditorLoader());
}
/// <summary>
/// Creates the ruleset for providing a corresponding beatmap to load the editor on.
/// </summary>
[NotNull]
protected abstract Ruleset CreateEditorRuleset();
protected sealed override Ruleset CreateRuleset() => CreateEditorRuleset();
protected class TestEditorLoader : EditorLoader
{
public TestEditor Editor { get; private set; }
protected sealed override Editor CreateEditor() => Editor = CreateTestEditor(this);
protected virtual TestEditor CreateTestEditor(EditorLoader loader) => new TestEditor(loader);
}
protected class TestEditor : Editor
{
[Resolved(canBeNull: true)]
[CanBeNull]
private IDialogOverlay dialogOverlay { get; set; }
public new void Undo() => base.Undo();
public new void Redo() => base.Redo();
public new bool Save() => base.Save();
public new void Cut() => base.Cut();
public new void Copy() => base.Copy();
public new void Paste() => base.Paste();
public new void SwitchToDifficulty(BeatmapInfo beatmapInfo) => base.SwitchToDifficulty(beatmapInfo);
public new void CreateNewDifficulty(RulesetInfo rulesetInfo) => base.CreateNewDifficulty(rulesetInfo);
public new bool HasUnsavedChanges => base.HasUnsavedChanges;
public override bool OnExiting(ScreenExitEvent e)
{
// For testing purposes allow the screen to exit without saving on second attempt.
if (!ExitConfirmed && dialogOverlay?.CurrentDialog is PromptForSaveDialog saveDialog)
{
saveDialog.PerformAction<PopupDialogDangerousButton>();
return true;
}
return base.OnExiting(e);
}
public TestEditor(EditorLoader loader = null)
: base(loader)
{
}
}
private class TestBeatmapManager : BeatmapManager
{
public WorkingBeatmap TestBeatmap;
public TestBeatmapManager(Storage storage, RealmAccess realm, RulesetStore rulesets, IAPIProvider api, [NotNull] AudioManager audioManager, IResourceStore<byte[]> resources, GameHost host, WorkingBeatmap defaultBeatmap)
: base(storage, realm, rulesets, api, audioManager, resources, host, defaultBeatmap)
{
}
protected override WorkingBeatmapCache CreateWorkingBeatmapCache(AudioManager audioManager, IResourceStore<byte[]> resources, IResourceStore<byte[]> storage, WorkingBeatmap defaultBeatmap, GameHost host)
{
return new TestWorkingBeatmapCache(this, audioManager, resources, storage, defaultBeatmap, host);
}
public override WorkingBeatmap CreateNewDifficulty(BeatmapSetInfo targetBeatmapSet, WorkingBeatmap referenceWorkingBeatmap, RulesetInfo rulesetInfo)
{
// don't actually care about properly creating a difficulty for this context.
return TestBeatmap;
}
public override WorkingBeatmap CopyExistingDifficulty(BeatmapSetInfo targetBeatmapSet, WorkingBeatmap referenceWorkingBeatmap)
{
// don't actually care about properly creating a difficulty for this context.
return TestBeatmap;
}
private class TestWorkingBeatmapCache : WorkingBeatmapCache
{
private readonly TestBeatmapManager testBeatmapManager;
public TestWorkingBeatmapCache(TestBeatmapManager testBeatmapManager, AudioManager audioManager, IResourceStore<byte[]> resourceStore, IResourceStore<byte[]> storage, WorkingBeatmap defaultBeatmap, GameHost gameHost)
: base(testBeatmapManager.BeatmapTrackStore, audioManager, resourceStore, storage, defaultBeatmap, gameHost)
{
this.testBeatmapManager = testBeatmapManager;
}
public override WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo)
=> testBeatmapManager.TestBeatmap;
}
public override void Save(BeatmapInfo info, IBeatmap beatmapContent, ISkin beatmapSkin = null)
{
// don't actually care about saving for this context.
}
}
}
}