GFramework/GFramework.Godot.Tests/Text/RichTextEffectsControllerTests.cs
GeWuYou f3d50c6361 refactor(tests): 更新测试代码中的命名空间引用
- 将 RichTextEffectsControllerTests 中的 GFramework.Godot.Text 引用替换为 Godot 相关命名空间
- 使用 Godot.Collections.Array 替代系统数组类型
- 在 GlobalUsings 中添加 GFramework.Godot.Text 的全局引用
- 修复返回类型以使用新的 Array<RichTextEffect> 结构
2026-04-18 14:59:21 +08:00

128 lines
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Godot;
using Godot.Collections;
using Array = Godot.Collections.Array;
namespace GFramework.Godot.Tests.Text;
/// <summary>
/// <see cref="RichTextEffectsController" /> 的纯托管行为测试。
/// </summary>
[TestFixture]
public sealed class RichTextEffectsControllerTests
{
/// <summary>
/// 验证启用框架效果时会开启宿主 BBCode并在 Profile 为空时回退到内置默认配置。
/// </summary>
[Test]
public void RefreshEffects_Should_Enable_Bbcode_And_Use_BuiltIn_Default_Profile()
{
var host = new FakeRichTextEffectHost();
var registry = new RecordingRegistry();
var controller = new RichTextEffectsController(
host,
() => registry,
() => null,
() => true,
() => false);
controller.RefreshEffects();
Assert.That(host.BbcodeEnabled, Is.True);
Assert.That(registry.CapturedAnimatedEffectsEnabled, Has.Count.EqualTo(1));
Assert.That(registry.CapturedAnimatedEffectsEnabled[0], Is.False);
Assert.That(registry.CapturedProfiles, Has.Count.EqualTo(1));
Assert.That(registry.CapturedProfiles[0].Effects.Select(static entry => entry.Key), Is.EqualTo(new[]
{
"green",
"red",
"gold",
"blue",
"fade_in",
"sine",
"jitter",
"fly_in"
}));
}
/// <summary>
/// 验证关闭框架效果时不会调用注册表,并会清空宿主的自定义效果集合。
/// </summary>
[Test]
public void RefreshEffects_Should_Clear_CustomEffects_When_Framework_Effects_Are_Disabled()
{
var existingEffects = new Array();
existingEffects.Add("placeholder");
var host = new FakeRichTextEffectHost
{
BbcodeEnabled = true,
CustomEffects = existingEffects
};
var registry = new RecordingRegistry();
var controller = new RichTextEffectsController(
host,
() => registry,
() => RichTextProfile.CreateBuiltInDefault(),
() => false,
() => true);
controller.RefreshEffects();
Assert.That(host.BbcodeEnabled, Is.True);
Assert.That(host.CustomEffects.Count, Is.EqualTo(0));
Assert.That(registry.CapturedProfiles, Is.Empty);
}
/// <summary>
/// 验证控制器会在每次刷新时读取最新的注册表访问器结果,避免缓存旧注册表。
/// </summary>
[Test]
public void RefreshEffects_Should_Use_The_Current_Registry_From_Accessor()
{
var host = new FakeRichTextEffectHost();
var firstRegistry = new RecordingRegistry();
var secondRegistry = new RecordingRegistry();
IRichTextEffectRegistry currentRegistry = firstRegistry;
var controller = new RichTextEffectsController(
host,
() => currentRegistry,
() => RichTextProfile.CreateBuiltInDefault(),
() => true,
() => true);
controller.RefreshEffects();
currentRegistry = secondRegistry;
controller.RefreshEffects();
Assert.That(firstRegistry.CapturedProfiles, Has.Count.EqualTo(1));
Assert.That(secondRegistry.CapturedProfiles, Has.Count.EqualTo(1));
}
private sealed class FakeRichTextEffectHost : IRichTextEffectHost
{
public bool BbcodeEnabled { get; set; }
public Array CustomEffects { get; set; } = new();
}
private sealed class RecordingRegistry : IRichTextEffectRegistry
{
public List<RichTextProfile> CapturedProfiles { get; } = [];
public List<bool> CapturedAnimatedEffectsEnabled { get; } = [];
public IReadOnlyList<RichTextEffect> CreateEffects(RichTextProfile profile, bool animatedEffectsEnabled)
{
CapturedProfiles.Add(profile);
CapturedAnimatedEffectsEnabled.Add(animatedEffectsEnabled);
return new Array<RichTextEffect>();
}
public RichTextEffect? CreateEffect(string key, bool animatedEffectsEnabled)
{
return null;
}
}
}