2020-09-24 16:24:05 +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.
2020-09-24 21:25:04 +08:00
using System ;
2020-09-24 21:00:13 +08:00
using System.IO ;
using System.Linq ;
2020-09-24 16:24:05 +08:00
using NUnit.Framework ;
2020-10-04 22:57:35 +08:00
using osu.Framework.Allocation ;
using osu.Framework.Screens ;
2020-09-24 21:00:13 +08:00
using osu.Framework.Testing ;
2020-09-24 16:24:05 +08:00
using osu.Game.Beatmaps ;
using osu.Game.Rulesets ;
using osu.Game.Rulesets.Osu ;
2021-09-12 21:50:41 +08:00
using osu.Game.Screens.Edit ;
2020-09-24 21:00:13 +08:00
using osu.Game.Screens.Edit.Setup ;
2022-01-25 20:25:28 +08:00
using osu.Game.Storyboards ;
2020-09-24 21:00:13 +08:00
using osu.Game.Tests.Resources ;
using SharpCompress.Archives ;
using SharpCompress.Archives.Zip ;
2020-09-24 16:24:05 +08:00
namespace osu.Game.Tests.Visual.Editing
{
public class TestSceneEditorBeatmapCreation : EditorTestScene
{
protected override Ruleset CreateEditorRuleset ( ) = > new OsuRuleset ( ) ;
2020-09-25 11:24:41 +08:00
protected override bool EditorComponentsReady = > Editor . ChildrenOfType < SetupScreen > ( ) . SingleOrDefault ( ) ? . IsLoaded = = true ;
2020-09-24 21:00:13 +08:00
2021-05-28 13:33:06 +08:00
protected override bool IsolateSavingFromDatabase = > false ;
2020-10-04 22:57:35 +08:00
[Resolved]
private BeatmapManager beatmapManager { get ; set ; }
2020-09-24 21:25:04 +08:00
public override void SetUpSteps ( )
{
base . SetUpSteps ( ) ;
2020-09-24 16:24:05 +08:00
2020-09-25 11:25:50 +08:00
// if we save a beatmap with a hash collision, things fall over.
// probably needs a more solid resolution in the future but this will do for now.
AddStep ( "make new beatmap unique" , ( ) = > EditorBeatmap . Metadata . Title = Guid . NewGuid ( ) . ToString ( ) ) ;
2020-09-25 17:40:20 +08:00
}
2020-09-25 11:25:50 +08:00
2022-01-25 20:25:28 +08:00
protected override WorkingBeatmap CreateWorkingBeatmap ( IBeatmap beatmap , Storyboard storyboard = null ) = > new DummyWorkingBeatmap ( Audio , null ) ;
2021-05-31 13:24:46 +08:00
2020-09-25 17:40:20 +08:00
[Test]
public void TestCreateNewBeatmap ( )
{
2020-09-24 16:24:05 +08:00
AddStep ( "save beatmap" , ( ) = > Editor . Save ( ) ) ;
2021-12-17 17:26:12 +08:00
AddAssert ( "new beatmap in database" , ( ) = > beatmapManager . QueryBeatmapSet ( s = > s . ID = = EditorBeatmap . BeatmapInfo . BeatmapSet . ID ) ? . Value . DeletePending = = false ) ;
2020-10-04 22:57:35 +08:00
}
[Test]
public void TestExitWithoutSave ( )
{
2021-09-12 21:50:41 +08:00
EditorBeatmap editorBeatmap = null ;
AddStep ( "store editor beatmap" , ( ) = > editorBeatmap = EditorBeatmap ) ;
2021-06-23 10:30:52 +08:00
AddStep ( "exit without save" , ( ) = >
{
Editor . Exit ( ) ;
DialogOverlay . CurrentDialog . PerformOkAction ( ) ;
} ) ;
2020-10-04 22:57:35 +08:00
AddUntilStep ( "wait for exit" , ( ) = > ! Editor . IsCurrentScreen ( ) ) ;
2021-12-17 17:26:12 +08:00
AddAssert ( "new beatmap not persisted" , ( ) = > beatmapManager . QueryBeatmapSet ( s = > s . ID = = editorBeatmap . BeatmapInfo . BeatmapSet . ID ) ? . Value . DeletePending = = true ) ;
2020-09-24 16:24:05 +08:00
}
2020-09-24 21:00:13 +08:00
[Test]
public void TestAddAudioTrack ( )
{
AddAssert ( "switch track to real track" , ( ) = >
{
var setup = Editor . ChildrenOfType < SetupScreen > ( ) . First ( ) ;
2021-10-27 12:04:41 +08:00
string temp = TestResources . GetTestBeatmapForImport ( ) ;
2020-09-24 21:00:13 +08:00
string extractedFolder = $"{temp}_extracted" ;
Directory . CreateDirectory ( extractedFolder ) ;
using ( var zip = ZipArchive . Open ( temp ) )
zip . WriteToDirectory ( extractedFolder ) ;
2020-10-06 14:17:15 +08:00
bool success = setup . ChildrenOfType < ResourcesSection > ( ) . First ( ) . ChangeAudioTrack ( Path . Combine ( extractedFolder , "03. Renatus - Soleily 192kbps.mp3" ) ) ;
2020-09-24 21:00:13 +08:00
File . Delete ( temp ) ;
Directory . Delete ( extractedFolder , true ) ;
return success ;
} ) ;
AddAssert ( "track length changed" , ( ) = > Beatmap . Value . Track . Length > 60000 ) ;
}
2022-01-24 01:34:33 +08:00
[Test]
public void TestCreateNewDifficulty ( )
{
string firstDifficultyName = Guid . NewGuid ( ) . ToString ( ) ;
string secondDifficultyName = Guid . NewGuid ( ) . ToString ( ) ;
AddStep ( "set unique difficulty name" , ( ) = > EditorBeatmap . BeatmapInfo . DifficultyName = firstDifficultyName ) ;
AddStep ( "save beatmap" , ( ) = > Editor . Save ( ) ) ;
AddAssert ( "new beatmap persisted" , ( ) = >
{
var beatmap = beatmapManager . QueryBeatmap ( b = > b . DifficultyName = = firstDifficultyName ) ;
var set = beatmapManager . QueryBeatmapSet ( s = > s . ID = = EditorBeatmap . BeatmapInfo . BeatmapSet . ID ) ;
return beatmap ! = null
& & beatmap . DifficultyName = = firstDifficultyName
& & set ! = null
& & set . PerformRead ( s = > s . Beatmaps . Single ( ) . ID = = beatmap . ID ) ;
} ) ;
2022-01-24 02:50:02 +08:00
AddAssert ( "can save again" , ( ) = > Editor . Save ( ) ) ;
2022-01-24 01:34:33 +08:00
AddStep ( "create new difficulty" , ( ) = > Editor . CreateNewDifficulty ( new OsuRuleset ( ) . RulesetInfo ) ) ;
AddUntilStep ( "wait for created" , ( ) = >
{
string difficultyName = Editor . ChildrenOfType < EditorBeatmap > ( ) . SingleOrDefault ( ) ? . BeatmapInfo . DifficultyName ;
return difficultyName ! = null & & difficultyName ! = firstDifficultyName ;
} ) ;
AddStep ( "set unique difficulty name" , ( ) = > EditorBeatmap . BeatmapInfo . DifficultyName = secondDifficultyName ) ;
AddStep ( "save beatmap" , ( ) = > Editor . Save ( ) ) ;
AddAssert ( "new beatmap persisted" , ( ) = >
{
var beatmap = beatmapManager . QueryBeatmap ( b = > b . DifficultyName = = secondDifficultyName ) ;
var set = beatmapManager . QueryBeatmapSet ( s = > s . ID = = EditorBeatmap . BeatmapInfo . BeatmapSet . ID ) ;
return beatmap ! = null
& & beatmap . DifficultyName = = secondDifficultyName
& & set ! = null
& & set . PerformRead ( s = > s . Beatmaps . Count = = 2 & & s . Beatmaps . Any ( b = > b . DifficultyName = = secondDifficultyName ) ) ;
} ) ;
}
2022-01-24 02:25:59 +08:00
[Test]
public void TestCreateNewBeatmapFailsWithBlankNamedDifficulties ( )
{
Guid setId = Guid . Empty ;
AddStep ( "retrieve set ID" , ( ) = > setId = EditorBeatmap . BeatmapInfo . BeatmapSet ! . ID ) ;
AddStep ( "save beatmap" , ( ) = > Editor . Save ( ) ) ;
AddAssert ( "new beatmap persisted" , ( ) = >
{
var set = beatmapManager . QueryBeatmapSet ( s = > s . ID = = setId ) ;
return set ! = null & & set . PerformRead ( s = > s . Beatmaps . Count = = 1 & & s . Files . Count = = 1 ) ;
} ) ;
AddStep ( "try to create new difficulty" , ( ) = > Editor . CreateNewDifficulty ( new OsuRuleset ( ) . RulesetInfo ) ) ;
AddAssert ( "beatmap set unchanged" , ( ) = >
{
var set = beatmapManager . QueryBeatmapSet ( s = > s . ID = = setId ) ;
return set ! = null & & set . PerformRead ( s = > s . Beatmaps . Count = = 1 & & s . Files . Count = = 1 ) ;
} ) ;
}
[Test]
public void TestCreateNewBeatmapFailsWithSameNamedDifficulties ( )
{
Guid setId = Guid . Empty ;
const string duplicate_difficulty_name = "duplicate" ;
AddStep ( "retrieve set ID" , ( ) = > setId = EditorBeatmap . BeatmapInfo . BeatmapSet ! . ID ) ;
AddStep ( "set difficulty name" , ( ) = > EditorBeatmap . BeatmapInfo . DifficultyName = duplicate_difficulty_name ) ;
AddStep ( "save beatmap" , ( ) = > Editor . Save ( ) ) ;
AddAssert ( "new beatmap persisted" , ( ) = >
{
var set = beatmapManager . QueryBeatmapSet ( s = > s . ID = = setId ) ;
return set ! = null & & set . PerformRead ( s = > s . Beatmaps . Count = = 1 & & s . Files . Count = = 1 ) ;
} ) ;
AddStep ( "create new difficulty" , ( ) = > Editor . CreateNewDifficulty ( new OsuRuleset ( ) . RulesetInfo ) ) ;
AddUntilStep ( "wait for created" , ( ) = >
{
string difficultyName = Editor . ChildrenOfType < EditorBeatmap > ( ) . SingleOrDefault ( ) ? . BeatmapInfo . DifficultyName ;
return difficultyName ! = null & & difficultyName ! = duplicate_difficulty_name ;
} ) ;
AddStep ( "set difficulty name" , ( ) = > EditorBeatmap . BeatmapInfo . DifficultyName = duplicate_difficulty_name ) ;
AddStep ( "try to save beatmap" , ( ) = > Editor . Save ( ) ) ;
AddAssert ( "beatmap set not corrupted" , ( ) = >
{
var set = beatmapManager . QueryBeatmapSet ( s = > s . ID = = setId ) ;
// the difficulty was already created at the point of the switch.
// what we want to check is that both difficulties do not use the same file.
return set ! = null & & set . PerformRead ( s = > s . Beatmaps . Count = = 2 & & s . Files . Count = = 2 ) ;
} ) ;
}
2020-09-24 16:24:05 +08:00
}
}