Skip to content

Commit

Permalink
Merge pull request #1121 from andy840119/add-test-case-to-the-changre…
Browse files Browse the repository at this point in the history
…-handler

Add test cases for lyric/note related change handler.
  • Loading branch information
andy840119 authored Feb 13, 2022
2 parents 4059abe + bed8634 commit 5df2006
Show file tree
Hide file tree
Showing 23 changed files with 1,745 additions and 62 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Game.Rulesets.Karaoke.Beatmaps;
using osu.Game.Screens.Edit;
using osu.Game.Tests.Visual;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.ChangeHandlers
{
/// <summary>
/// it's a base class for testing all change handler.
/// Should inherit <see cref="OsuTestScene"/> because all change handler need the injecting to get the value.
/// </summary>
[HeadlessTest]
public abstract class BaseChangeHandlerTest<TChangeHandler> : OsuTestScene where TChangeHandler : Component, new()
{
private TChangeHandler changeHandler;

private int transactionCount;

[BackgroundDependencyLoader]
private void load()
{
var beatmap = new KaraokeBeatmap
{
BeatmapInfo =
{
Ruleset = new KaraokeRuleset().RulesetInfo,
},
};
var editorBeatmap = new EditorBeatmap(beatmap);
Dependencies.Cache(editorBeatmap);
editorBeatmap.TransactionEnded += () =>
{
transactionCount++;
};

Child = changeHandler = new TChangeHandler();
}

protected void TriggerHandlerChanged(Action<TChangeHandler> c)
{
AddStep("Trigger change handler", () =>
{
// should reset transaction number in here because it will increase if load testing object.
transactionCount = 0;
c(changeHandler);
});
}

protected void AssertTransactionOnlyTriggerOnce()
{
AddStep("Should only trigger transaction once", () =>
{
Assert.AreEqual(1, transactionCount);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers;
using osu.Game.Rulesets.Objects;
using osu.Game.Screens.Edit;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.ChangeHandlers
{
public abstract class BaseHitObjectChangeHandlerTest<TChangeHandler, THitObject> : BaseChangeHandlerTest<TChangeHandler>
where TChangeHandler : HitObjectChangeHandler<THitObject>, new() where THitObject : HitObject
{
private EditorBeatmap editorBeatmap;

[BackgroundDependencyLoader]
private void load()
{
editorBeatmap = Dependencies.Get<EditorBeatmap>();
}

[SetUp]
public virtual void SetUp()
{
AddStep("Setup", () =>
{
editorBeatmap.Clear();
editorBeatmap.SelectedHitObjects.Clear();
});
}

protected void PrepareHitObject(HitObject hitObject, bool selected = true)
=> PrepareHitObjects(new[] { hitObject }, selected);

protected void PrepareHitObjects(IEnumerable<HitObject> selectedHitObjects, bool selected = true)
{
AddStep("Prepare testing hit objects", () =>
{
var hitobjects = selectedHitObjects.ToList();
editorBeatmap.AddRange(hitobjects);

if (selected)
{
editorBeatmap.SelectedHitObjects.AddRange(hitobjects);
}
});
}

protected void AssertHitObject(Action<THitObject> assert)
{
AddStep("Is result matched", () =>
{
foreach (var hitObject in editorBeatmap.HitObjects.OfType<THitObject>())
{
assert(hitObject);
}
});

// even if there's no property changed in the lyric editor, should still trigger the change handler.
// because every change handler call should cause one undo step.
// also, technically should not call the change handler if there's no possible to change the properties.
AssertTransactionOnlyTriggerOnce();
}

protected void AssertHitObjects(Action<IEnumerable<THitObject>> assert)
{
AddStep("Is result matched", () =>
{
assert(editorBeatmap.HitObjects.OfType<THitObject>());
});

// even if there's no property changed in the lyric editor, should still trigger the change handler.
// because every change handler call should cause one undo step.
// also, technically should not call the change handler if there's no possible to change the properties.
AssertTransactionOnlyTriggerOnce();
}

protected void AssertSelectedHitObject(Action<THitObject> assert)
{
AddStep("Is result matched", () =>
{
foreach (var hitObject in editorBeatmap.SelectedHitObjects.OfType<THitObject>())
{
assert(hitObject);
}
});

// even if there's no property changed in the lyric editor, should still trigger the change handler.
// because every change handler call should cause one undo step.
// also, technically should not call the change handler if there's no possible to change the properties.
AssertTransactionOnlyTriggerOnce();
}

protected void AssertSelectedHitObjects(Action<IEnumerable<THitObject>> assert)
{
AddStep("Is result matched", () =>
{
assert(editorBeatmap.SelectedHitObjects.OfType<THitObject>());
});

// even if there's no property changed in the lyric editor, should still trigger the change handler.
// because every change handler call should cause one undo step.
// also, technically should not call the change handler if there's no possible to change the properties.
AssertTransactionOnlyTriggerOnce();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Globalization;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Game.Rulesets.Karaoke.Configuration;
using osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers.Lyrics;
using osu.Game.Rulesets.Karaoke.Objects;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.ChangeHandlers.Lyrics
{
public class LyricLanguageChangeHandlerTest : BaseHitObjectChangeHandlerTest<LyricLanguageChangeHandler, Lyric>
{
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var baseDependencies = new DependencyContainer(base.CreateChildDependencies(parent));
baseDependencies.Cache(new KaraokeRulesetEditGeneratorConfigManager());
return baseDependencies;
}

[Test]
public void TestAutoGenerateSupportedLyric()
{
PrepareHitObject(new Lyric
{
Text = "カラオケ"
});

TriggerHandlerChanged(c => c.AutoGenerate());

AssertSelectedHitObject(h =>
{
Assert.AreEqual(new CultureInfo("ja"), h.Language);
});
}

[Test]
public void TestAutoGenerateNonSupportedLyric()
{
PrepareHitObject(new Lyric
{
Text = "???"
});

TriggerHandlerChanged(c => c.AutoGenerate());

AssertSelectedHitObject(h =>
{
Assert.IsNull(h.Language);
});
}

[Test]
public void TestSetLanguageToJapanese()
{
var language = new CultureInfo("ja");
PrepareHitObject(new Lyric());

TriggerHandlerChanged(c => c.SetLanguage(language));

AssertSelectedHitObject(h =>
{
Assert.AreEqual(language, h.Language);
});
}

[Test]
public void TestSetLanguageToNull()
{
PrepareHitObject(new Lyric
{
Text = "???"
});

TriggerHandlerChanged(c => c.SetLanguage(null));

AssertSelectedHitObject(h =>
{
Assert.IsNull(h.Language);
});
}
}
}
Loading

0 comments on commit 5df2006

Please sign in to comment.