Make LocalisationManager IDisposable to allow reuse in tests

This commit is contained in:
Susko3
2022-08-08 15:23:43 +02:00
parent fb3ce9c572
commit 44f5d1251a
7 changed files with 45 additions and 1 deletions

View File

@@ -25,6 +25,7 @@ namespace osu.Framework.Benchmarks
[GlobalCleanup]
public void GlobalCleanup()
{
manager.Dispose();
storage.Dispose();
}

View File

@@ -31,10 +31,18 @@ namespace osu.Framework.Tests.Localisation
manager.AddLanguage("en", new FakeStorage("en"));
}
[TearDown]
public void Teardown()
{
manager?.Dispose();
config?.Dispose();
}
[Test]
public void TestNoLanguagesAdded()
{
// reinitialise without the default language
manager.Dispose();
manager = new LocalisationManager(config);
var localisedText = manager.GetLocalisedBindableString(new TranslatableString(FakeStorage.LOCALISABLE_STRING_EN, FakeStorage.LOCALISABLE_STRING_EN));

View File

@@ -74,6 +74,12 @@ namespace osu.Framework.Tests.Visual.Localisation
}));
}
protected override void Dispose(bool isDisposing)
{
manager?.Dispose();
base.Dispose(isDisposing);
}
[Test]
public void TestTextFlowLocalisation()
{

View File

@@ -259,6 +259,12 @@ namespace osu.Framework.Tests.Visual.Sprites
config.SetValue(FrameworkSetting.Locale, "ja");
}
protected override void Dispose(bool isDisposing)
{
localisation?.Dispose();
base.Dispose(isDisposing);
}
}
private class FakeFrameworkConfigManager : FrameworkConfigManager

View File

@@ -56,6 +56,12 @@ namespace osu.Framework.Tests.Visual.UserInterface
return dependencies;
}
protected override void Dispose(bool isDisposing)
{
manager?.Dispose();
base.Dispose(isDisposing);
}
private const string goodbye = "goodbye";
[SetUp]

View File

@@ -447,6 +447,9 @@ namespace osu.Framework
localFonts?.Dispose();
localFonts = null;
Localisation?.Dispose();
Localisation = null;
}
}
}

View File

@@ -1,6 +1,7 @@
// 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.
using System;
using System.Collections.Generic;
using System.Globalization;
using osu.Framework.Bindables;
@@ -8,7 +9,7 @@ using osu.Framework.Configuration;
namespace osu.Framework.Localisation
{
public partial class LocalisationManager
public partial class LocalisationManager : IDisposable
{
public IBindable<LocalisationParameters> CurrentParameters => currentParameters;
@@ -119,5 +120,18 @@ namespace osu.Framework.Localisation
/// </remarks>
/// <returns>The resultant <see cref="LocalisationParameters"/>.</returns>
protected virtual LocalisationParameters CreateLocalisationParameters() => new LocalisationParameters(currentLocale?.Storage, configPreferUnicode.Value);
protected virtual void Dispose(bool disposing)
{
currentParameters.UnbindAll();
configLocale.UnbindAll();
configPreferUnicode.UnbindAll();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}