- Updated `SettingsViewModel`, `DeveloperViewModel`, and `MainWindowViewModel` to raise `PropertyChanged` for the `Localization` property when the language changes. This forces Avalonia to refresh bindings to the `Localization` indexer. - Updated `UpdateViewModel` to subscribe to language changes and manually refresh localized strings (`CurrentVersionText`) that are not directly bound to the Localization indexer. - Added `LocalizationTests.cs` to verify that PropertyChanged events are raised correctly on language change.
90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using Xunit;
|
|
using CheckHash.ViewModels;
|
|
using CheckHash.Services;
|
|
using System.ComponentModel;
|
|
using System.Threading.Tasks;
|
|
using System.Linq;
|
|
|
|
namespace CheckHash.Tests;
|
|
|
|
public class LocalizationTests
|
|
{
|
|
[Fact]
|
|
public void UpdateViewModel_ShouldUpdateProperties_WhenLanguageChanges()
|
|
{
|
|
// Arrange
|
|
var vm = new UpdateViewModel();
|
|
bool versionChanged = false;
|
|
bool localizationChanged = false;
|
|
|
|
vm.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(vm.CurrentVersionText))
|
|
{
|
|
versionChanged = true;
|
|
}
|
|
if (e.PropertyName == nameof(vm.Localization))
|
|
{
|
|
localizationChanged = true;
|
|
}
|
|
};
|
|
|
|
// Act
|
|
// Switch to a different language
|
|
var targetLangCode = LocalizationService.Instance.SelectedLanguage.Code == "vi-VN" ? "en-US" : "vi-VN";
|
|
var targetLang = LocalizationService.Instance.AvailableLanguages.First(x => x.Code == targetLangCode);
|
|
|
|
LocalizationService.Instance.SelectedLanguage = targetLang;
|
|
|
|
// Assert
|
|
Assert.True(versionChanged, "CurrentVersionText property changed event should be raised");
|
|
Assert.True(localizationChanged, "Localization property changed event should be raised");
|
|
}
|
|
|
|
[Fact]
|
|
public void SettingsViewModel_ShouldNotifyLocalizationChanged_WhenLanguageChanges()
|
|
{
|
|
// Arrange
|
|
var vm = new SettingsViewModel();
|
|
bool notified = false;
|
|
vm.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(vm.Localization))
|
|
{
|
|
notified = true;
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var targetLangCode = LocalizationService.Instance.SelectedLanguage.Code == "vi-VN" ? "en-US" : "vi-VN";
|
|
var targetLang = LocalizationService.Instance.AvailableLanguages.First(x => x.Code == targetLangCode);
|
|
LocalizationService.Instance.SelectedLanguage = targetLang;
|
|
|
|
// Assert
|
|
Assert.True(notified, "SettingsViewModel should raise PropertyChanged for 'Localization'");
|
|
}
|
|
|
|
[Fact]
|
|
public void DeveloperViewModel_ShouldNotifyLocalizationChanged_WhenLanguageChanges()
|
|
{
|
|
// Arrange
|
|
var vm = new DeveloperViewModel();
|
|
bool notified = false;
|
|
vm.PropertyChanged += (s, e) =>
|
|
{
|
|
if (e.PropertyName == nameof(vm.Localization))
|
|
{
|
|
notified = true;
|
|
}
|
|
};
|
|
|
|
// Act
|
|
var targetLangCode = LocalizationService.Instance.SelectedLanguage.Code == "vi-VN" ? "en-US" : "vi-VN";
|
|
var targetLang = LocalizationService.Instance.AvailableLanguages.First(x => x.Code == targetLangCode);
|
|
LocalizationService.Instance.SelectedLanguage = targetLang;
|
|
|
|
// Assert
|
|
Assert.True(notified, "DeveloperViewModel should raise PropertyChanged for 'Localization'");
|
|
}
|
|
}
|