2022-07-21 09:15:21 +00: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.
2024-01-08 20:51:01 +00:00
using System ;
2022-07-21 09:15:21 +00:00
using System.Linq ;
using NUnit.Framework ;
using osu.Framework.Allocation ;
using osu.Framework.Bindables ;
using osu.Framework.Extensions ;
using osu.Framework.Testing ;
using osu.Game.Beatmaps ;
2024-01-22 11:48:29 +00:00
using osu.Game.Database ;
2023-07-26 07:22:21 +00:00
using osu.Game.Rulesets ;
using osu.Game.Scoring ;
using osu.Game.Scoring.Legacy ;
2022-07-21 09:15:21 +00:00
using osu.Game.Screens.Play ;
using osu.Game.Tests.Beatmaps.IO ;
using osu.Game.Tests.Visual ;
namespace osu.Game.Tests.Database
{
[HeadlessTest]
2023-07-26 07:07:45 +00:00
public partial class BackgroundDataStoreProcessorTests : OsuTestScene , ILocalUserPlayInfo
2022-07-21 09:15:21 +00:00
{
2024-10-01 10:55:46 +00:00
public IBindable < LocalUserPlayingState > PlayingState = > isPlaying ;
2022-07-21 09:15:21 +00:00
2024-10-01 10:55:46 +00:00
private readonly Bindable < LocalUserPlayingState > isPlaying = new Bindable < LocalUserPlayingState > ( ) ;
2022-07-21 09:15:21 +00:00
private BeatmapSetInfo importedSet = null ! ;
[BackgroundDependencyLoader]
private void load ( OsuGameBase osu )
{
importedSet = BeatmapImportHelper . LoadQuickOszIntoOsu ( osu ) . GetResultSafely ( ) ;
}
[SetUpSteps]
public void SetUpSteps ( )
{
2024-10-01 10:55:46 +00:00
AddStep ( "Set not playing" , ( ) = > isPlaying . Value = LocalUserPlayingState . NotPlaying ) ;
2022-07-21 09:15:21 +00:00
}
[Test]
public void TestDifficultyProcessing ( )
{
AddAssert ( "Difficulty is initially set" , ( ) = >
{
return Realm . Run ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
return beatmapSetInfo . Beatmaps . All ( b = > b . StarRating > 0 ) ;
} ) ;
} ) ;
AddStep ( "Reset difficulty" , ( ) = >
{
Realm . Write ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
foreach ( var b in beatmapSetInfo . Beatmaps )
b . StarRating = - 1 ;
} ) ;
} ) ;
AddStep ( "Run background processor" , ( ) = >
{
2023-07-26 07:07:45 +00:00
Add ( new TestBackgroundDataStoreProcessor ( ) ) ;
2022-07-21 09:15:21 +00:00
} ) ;
AddUntilStep ( "wait for difficulties repopulated" , ( ) = >
{
return Realm . Run ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
return beatmapSetInfo . Beatmaps . All ( b = > b . StarRating > 0 ) ;
} ) ;
} ) ;
}
[Test]
public void TestDifficultyProcessingWhilePlaying ( )
{
AddAssert ( "Difficulty is initially set" , ( ) = >
{
return Realm . Run ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
return beatmapSetInfo . Beatmaps . All ( b = > b . StarRating > 0 ) ;
} ) ;
} ) ;
2024-10-01 10:55:46 +00:00
AddStep ( "Set playing" , ( ) = > isPlaying . Value = LocalUserPlayingState . Playing ) ;
2022-07-21 09:15:21 +00:00
AddStep ( "Reset difficulty" , ( ) = >
{
Realm . Write ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
foreach ( var b in beatmapSetInfo . Beatmaps )
b . StarRating = - 1 ;
} ) ;
} ) ;
AddStep ( "Run background processor" , ( ) = >
{
2023-07-26 07:07:45 +00:00
Add ( new TestBackgroundDataStoreProcessor ( ) ) ;
2022-07-21 09:15:21 +00:00
} ) ;
AddWaitStep ( "wait some" , 500 ) ;
AddAssert ( "Difficulty still not populated" , ( ) = >
{
return Realm . Run ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
return beatmapSetInfo . Beatmaps . All ( b = > b . StarRating = = - 1 ) ;
} ) ;
} ) ;
2024-10-01 10:55:46 +00:00
AddStep ( "Set not playing" , ( ) = > isPlaying . Value = LocalUserPlayingState . NotPlaying ) ;
2022-07-21 09:15:21 +00:00
AddUntilStep ( "wait for difficulties repopulated" , ( ) = >
{
return Realm . Run ( r = >
{
2023-07-06 04:37:42 +00:00
var beatmapSetInfo = r . Find < BeatmapSetInfo > ( importedSet . ID ) ! ;
2022-07-21 09:15:21 +00:00
return beatmapSetInfo . Beatmaps . All ( b = > b . StarRating > 0 ) ;
} ) ;
} ) ;
}
2023-12-18 21:23:58 +00:00
[TestCase(30000001)]
2023-10-02 07:54:46 +00:00
[TestCase(30000002)]
[TestCase(30000003)]
2023-12-18 21:23:58 +00:00
[TestCase(30000004)]
[TestCase(30000005)]
2023-10-02 07:54:46 +00:00
public void TestScoreUpgradeSuccess ( int scoreVersion )
2023-07-26 07:28:38 +00:00
{
ScoreInfo scoreInfo = null ! ;
2023-08-21 10:35:04 +00:00
AddStep ( "Add score which requires upgrade (and has beatmap)" , ( ) = >
2023-07-26 07:28:38 +00:00
{
Realm . Write ( r = >
{
r . Add ( scoreInfo = new ScoreInfo ( ruleset : r . All < RulesetInfo > ( ) . First ( ) , beatmap : r . All < BeatmapInfo > ( ) . First ( ) )
{
2023-10-02 07:54:46 +00:00
TotalScoreVersion = scoreVersion ,
2023-07-26 07:28:38 +00:00
LegacyTotalScore = 123456 ,
IsLegacyScore = true ,
} ) ;
} ) ;
} ) ;
AddStep ( "Run background processor" , ( ) = > Add ( new TestBackgroundDataStoreProcessor ( ) ) ) ;
AddUntilStep ( "Score version upgraded" , ( ) = > Realm . Run ( r = > r . Find < ScoreInfo > ( scoreInfo . ID ) ! . TotalScoreVersion ) , ( ) = > Is . EqualTo ( LegacyScoreEncoder . LATEST_VERSION ) ) ;
2023-08-21 10:36:22 +00:00
AddAssert ( "Score not marked as failed" , ( ) = > Realm . Run ( r = > r . Find < ScoreInfo > ( scoreInfo . ID ) ! . BackgroundReprocessingFailed ) , ( ) = > Is . False ) ;
2023-07-26 07:28:38 +00:00
}
2024-07-16 12:03:33 +00:00
[TestCase(30000002)]
[TestCase(30000013)]
public void TestScoreUpgradeFailed ( int scoreVersion )
2023-07-26 07:22:21 +00:00
{
ScoreInfo scoreInfo = null ! ;
AddStep ( "Add score which requires upgrade (but has no beatmap)" , ( ) = >
{
Realm . Write ( r = >
{
r . Add ( scoreInfo = new ScoreInfo ( ruleset : r . All < RulesetInfo > ( ) . First ( ) , beatmap : new BeatmapInfo
{
BeatmapSet = new BeatmapSetInfo ( ) ,
Ruleset = r . All < RulesetInfo > ( ) . First ( ) ,
} )
{
2024-07-16 12:03:33 +00:00
TotalScoreVersion = scoreVersion ,
2023-07-26 07:22:21 +00:00
IsLegacyScore = true ,
} ) ;
} ) ;
} ) ;
2024-07-16 12:01:33 +00:00
TestBackgroundDataStoreProcessor processor = null ! ;
AddStep ( "Run background processor" , ( ) = > Add ( processor = new TestBackgroundDataStoreProcessor ( ) ) ) ;
AddUntilStep ( "Wait for completion" , ( ) = > processor . Completed ) ;
2023-07-26 07:22:21 +00:00
2023-08-21 10:36:22 +00:00
AddUntilStep ( "Score marked as failed" , ( ) = > Realm . Run ( r = > r . Find < ScoreInfo > ( scoreInfo . ID ) ! . BackgroundReprocessingFailed ) , ( ) = > Is . True ) ;
2024-07-16 12:03:33 +00:00
AddAssert ( "Score version not upgraded" , ( ) = > Realm . Run ( r = > r . Find < ScoreInfo > ( scoreInfo . ID ) ! . TotalScoreVersion ) , ( ) = > Is . EqualTo ( scoreVersion ) ) ;
2023-07-26 07:22:21 +00:00
}
2024-01-08 20:51:01 +00:00
[Test]
public void TestCustomRulesetScoreNotSubjectToUpgrades ( [ Values ] bool available )
{
RulesetInfo rulesetInfo = null ! ;
ScoreInfo scoreInfo = null ! ;
TestBackgroundDataStoreProcessor processor = null ! ;
AddStep ( "Add unavailable ruleset" , ( ) = > Realm . Write ( r = > r . Add ( rulesetInfo = new RulesetInfo
{
ShortName = Guid . NewGuid ( ) . ToString ( ) ,
Available = available
} ) ) ) ;
AddStep ( "Add score for unavailable ruleset" , ( ) = > Realm . Write ( r = > r . Add ( scoreInfo = new ScoreInfo (
ruleset : rulesetInfo ,
beatmap : r . All < BeatmapInfo > ( ) . First ( ) )
{
TotalScoreVersion = 30000001
} ) ) ) ;
AddStep ( "Run background processor" , ( ) = > Add ( processor = new TestBackgroundDataStoreProcessor ( ) ) ) ;
AddUntilStep ( "Wait for completion" , ( ) = > processor . Completed ) ;
AddAssert ( "Score not marked as failed" , ( ) = > Realm . Run ( r = > r . Find < ScoreInfo > ( scoreInfo . ID ) ! . BackgroundReprocessingFailed ) , ( ) = > Is . False ) ;
AddAssert ( "Score version not upgraded" , ( ) = > Realm . Run ( r = > r . Find < ScoreInfo > ( scoreInfo . ID ) ! . TotalScoreVersion ) , ( ) = > Is . EqualTo ( 30000001 ) ) ;
}
2023-07-26 07:07:45 +00:00
public partial class TestBackgroundDataStoreProcessor : BackgroundDataStoreProcessor
2022-07-21 09:15:21 +00:00
{
protected override int TimeToSleepDuringGameplay = > 10 ;
2024-01-08 20:51:01 +00:00
public bool Completed = > ProcessingTask . IsCompleted ;
2022-07-21 09:15:21 +00:00
}
}
}