using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
using NewLife.XCoder.Services;
using NewLife.XCoder.Tools.Settings;
using Xunit;
namespace XCoder.App.UiTests;
/// <summary>设置中心的交互级测试(APP-6 主题 / APP-8 设置项)</summary>
/// <remarks>
/// 直接构造设置页并触发按钮与下拉,验证「点一下真的生效并落盘」,
/// 比只看控件存在更接近用户实际操作。
/// </remarks>
public class SettingsToolViewTests
{
[Fact]
[DisplayName("点深色/浅色/跟随系统按钮会切换主题并写入偏好")]
public async Task ThemeButtons_SwitchTheme()
{
var temp = Path.Combine(Path.GetTempPath(), "xcoder-uitheme-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
var oldPath = ThemeService.FilePath;
ThemeService.FilePath = Path.Combine(temp, "ui.theme");
try
{
await UiSession.Run(() =>
{
var view = new SettingsToolView();
var dark = view.FindControl<Button>("ThemeDarkButton");
var light = view.FindControl<Button>("ThemeLightButton");
var auto = view.FindControl<Button>("ThemeAutoButton");
Assert.NotNull(dark);
Assert.NotNull(light);
Assert.NotNull(auto);
dark!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Assert.Equal("dark", ThemeService.Mode);
light!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Assert.Equal("light", ThemeService.Mode);
auto!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Assert.Equal("auto", ThemeService.Mode);
});
Assert.True(File.Exists(ThemeService.FilePath));
}
finally
{
ThemeService.FilePath = oldPath;
try { Directory.Delete(temp, true); } catch (IOException) { }
}
}
[Fact]
[DisplayName("快捷入口保存后写入配置文件,恢复默认会删除配置")]
public async Task QuickTools_SaveAndReset()
{
var temp = Path.Combine(Path.GetTempPath(), "xcoder-uiquick-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
var oldPath = QuickToolsStore.FilePath;
QuickToolsStore.FilePath = Path.Combine(temp, "quick.tools");
try
{
await UiSession.Run(() =>
{
var view = new SettingsToolView();
var box = view.FindControl<TextBox>("QuickBox");
var save = view.FindControl<Button>("SaveQuickButton");
var reset = view.FindControl<Button>("ResetQuickButton");
var hint = view.FindControl<TextBlock>("QuickText");
Assert.NotNull(box);
Assert.NotNull(save);
Assert.NotNull(reset);
box!.Text = "sql,can,md5";
save!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Assert.Equal(["sql", "can", "md5"], QuickToolsStore.Load());
Assert.Contains("已保存", hint!.Text);
reset!.RaiseEvent(new Avalonia.Interactivity.RoutedEventArgs(Button.ClickEvent));
Assert.False(File.Exists(QuickToolsStore.FilePath));
Assert.Equal(QuickToolsStore.DefaultKeys.Count, QuickToolsStore.Load().Count);
});
}
finally
{
QuickToolsStore.FilePath = oldPath;
try { Directory.Delete(temp, true); } catch (IOException) { }
}
}
[Fact]
[DisplayName("外壳行为勾选框切换后会持久化")]
public async Task ShellPreference_Toggles()
{
var temp = Path.Combine(Path.GetTempPath(), "xcoder-uishell-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
var oldPath = ShellPreferenceStore.FilePath;
ShellPreferenceStore.FilePath = Path.Combine(temp, "ui.shell");
ShellPreferenceStore.Save(new ShellPreference(true, true));
try
{
await UiSession.Run(() =>
{
var view = new SettingsToolView();
var closeToTray = view.FindControl<CheckBox>("CloseToTrayBox");
var onTop = view.FindControl<CheckBox>("FloatOnTopBox");
Assert.NotNull(closeToTray);
Assert.NotNull(onTop);
Assert.True(closeToTray!.IsChecked);
closeToTray.IsChecked = false;
onTop!.IsChecked = false;
});
var preference = ShellPreferenceStore.Load();
Assert.False(preference.CloseToTray);
Assert.False(preference.FloatOnTop);
}
finally
{
ShellPreferenceStore.FilePath = oldPath;
try { Directory.Delete(temp, true); } catch (IOException) { }
}
}
[Fact]
[DisplayName("语言下拉包含 Auto 与 11 种语言且默认停在跟随系统")]
public async Task LanguageBox_HasAllLanguages()
{
var temp = Path.Combine(Path.GetTempPath(), "xcoder-uilang-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
var oldPath = LocalizationService.FilePath;
var oldPreference = LocalizationService.Preference;
LocalizationService.FilePath = Path.Combine(temp, "ui.lang");
try
{
await UiSession.Run(() =>
{
LocalizationService.Apply(LocalizationService.Auto, false);
var view = new SettingsToolView();
var box = view.FindControl<ComboBox>("LanguageBox");
Assert.NotNull(box);
Assert.Equal(LocalizationService.Languages.Count + 1, box!.ItemCount);
Assert.Equal(12, LocalizationService.Languages.Count + 1);
Assert.Equal(0, box.SelectedIndex);
});
}
finally
{
LocalizationService.Apply(oldPreference, false);
LocalizationService.FilePath = oldPath;
try { Directory.Delete(temp, true); } catch (IOException) { }
}
}
[Fact]
[DisplayName("选择英文后设置页文案立即切换并写入偏好")]
public async Task LanguageBox_SwitchEnglish_UpdatesUi()
{
var temp = Path.Combine(Path.GetTempPath(), "xcoder-uilang-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
var oldPath = LocalizationService.FilePath;
var oldPreference = LocalizationService.Preference;
LocalizationService.FilePath = Path.Combine(temp, "ui.lang");
try
{
await UiSession.Run(() =>
{
LocalizationService.Apply(LocalizationService.Auto, false);
var view = new SettingsToolView();
var box = view.FindControl<ComboBox>("LanguageBox");
var title = view.FindControl<TextBlock>("TitleText");
var label = view.FindControl<TextBlock>("LanguageText");
Assert.NotNull(box);
Assert.NotNull(title);
Assert.NotNull(label);
var index = 1;
for (var i = 0; i < LocalizationService.Languages.Count; i++)
{
if (LocalizationService.Languages[i] == "en-US") { index = i + 1; break; }
}
box!.SelectedIndex = index;
Avalonia.Threading.Dispatcher.UIThread.RunJobs();
Assert.Equal("en-US", LocalizationService.Language);
Assert.Equal("Settings", title!.Text);
Assert.Equal("English", label!.Text);
Assert.Equal("en-US", File.ReadAllText(LocalizationService.FilePath).Trim());
});
}
finally
{
LocalizationService.Apply(oldPreference, false);
LocalizationService.FilePath = oldPath;
try { Directory.Delete(temp, true); } catch (IOException) { }
}
}
[Fact]
[DisplayName("热键框显示当前偏好值")]
public async Task HotkeyBox_ShowsPreference()
{
var temp = Path.Combine(Path.GetTempPath(), "xcoder-uihotkey-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(temp);
var oldPath = ShellPreferenceStore.FilePath;
ShellPreferenceStore.FilePath = Path.Combine(temp, "ui.shell");
try
{
ShellPreferenceStore.Save(new ShellPreference(true, true, "Ctrl+Shift+F9"));
await UiSession.Run(() =>
{
var view = new SettingsToolView();
var box = view.FindControl<TextBox>("HotkeyBox");
Assert.NotNull(box);
Assert.Equal("Ctrl+Shift+F9", box!.Text);
});
}
finally
{
ShellPreferenceStore.FilePath = oldPath;
try { Directory.Delete(temp, true); } catch (IOException) { }
}
}
}
|