借助网络配置,控制Modbus工具的日志显示,避免卡死界面
智能大石头 authored at 2021-12-11 22:09:19
5.31 KiB
XCoder
using System;
using System.ComponentModel;
using NewLife.XCoder.Services;
using Xunit;

namespace XCoder.App.Tests;

/// <summary>全局热键解析与注册</summary>
public class GlobalHotkeyTests
{
    [Theory]
    [DisplayName("常见热键写法都能解析")]
    [InlineData("Ctrl+Alt+X", true, true, false, false, "X")]
    [InlineData("ctrl+shift+f5", true, false, true, false, "F5")]
    [InlineData("Alt+Win+D", false, true, false, true, "D")]
    [InlineData("Control+S", true, false, false, false, "S")]
    [InlineData("Ctrl-Alt-1", true, true, false, false, "1")]
    public void Parse_Cases(String text, Boolean ctrl, Boolean alt, Boolean shift, Boolean win, String key)
    {
        var gesture = GlobalHotkeyService.Parse(text);

        Assert.Equal(ctrl, gesture.Control);
        Assert.Equal(alt, gesture.Alt);
        Assert.Equal(shift, gesture.Shift);
        Assert.Equal(win, gesture.Win);
        Assert.Equal(key, gesture.Key);
    }

    [Fact]
    [DisplayName("解析结果能还原成规范文本")]
    public void Parse_ToString()
    {
        Assert.Equal("Ctrl+Alt+X", GlobalHotkeyService.Parse("alt+ctrl+x").ToString());
    }

    [Theory]
    [DisplayName("空、缺修饰键、缺主键、多主键、未知主键都拒绝")]
    [InlineData(null)]
    [InlineData("")]
    [InlineData("X")]                 // 没有修饰键
    [InlineData("Ctrl+Alt")]          // 没有主键
    [InlineData("Ctrl+A+B")]          // 两个主键
    [InlineData("Ctrl+Foo")]          // 未知主键
    public void Parse_Invalid_Throws(String? text)
    {
        Assert.Throws<ArgumentException>(() => GlobalHotkeyService.Parse(text));
    }

    [Theory]
    [DisplayName("虚拟键码映射覆盖字母、数字、功能键与常用键")]
    [InlineData("A", 0x41)]
    [InlineData("Z", 0x5A)]
    [InlineData("0", 0x30)]
    [InlineData("F1", 0x70)]
    [InlineData("F12", 0x7B)]
    [InlineData("space", 0x20)]
    [InlineData("Escape", 0x1B)]
    [InlineData("PAGEDOWN", 0x22)]
    public void VirtualKeyOf_Cases(String key, Int32 expected)
    {
        Assert.Equal((UInt32)expected, GlobalHotkeyService.VirtualKeyOf(key));
    }

    [Theory]
    [DisplayName("不支持的键返回 0")]
    [InlineData("F25")]
    [InlineData("Foo")]
    [InlineData("")]
    [InlineData(null)]
    public void VirtualKeyOf_Unsupported_ReturnsZero(String? key)
    {
        Assert.Equal(0u, GlobalHotkeyService.VirtualKeyOf(key));
    }

    [Fact]
    [DisplayName("支持键列表包含常用键且不含非法项")]
    public void SupportedKeys_Sane()
    {
        Assert.Contains("A", GlobalHotkeyService.SupportedKeys);
        Assert.Contains("F12", GlobalHotkeyService.SupportedKeys);
        Assert.All(GlobalHotkeyService.SupportedKeys, key => Assert.NotEqual(0u, GlobalHotkeyService.VirtualKeyOf(key)));
    }

    [Fact]
    [DisplayName("非 Windows 平台注册直接返回 false 且不抛异常")]
    public void Register_NotWindows_ReturnsFalse()
    {
        if (GlobalHotkeyService.IsSupported) return;

        using var service = new GlobalHotkeyService();

        Assert.False(service.Register("Ctrl+Alt+X", () => { }));
    }

    [Fact]
    [DisplayName("Windows 上能注册并注销一个不常见的组合")]
    public void Register_AndDispose()
    {
        if (!GlobalHotkeyService.IsSupported) return;

        var service = new GlobalHotkeyService();
        try
        {
            // 选一个几乎不会冲突的组合
            var ok = service.Register("Ctrl+Alt+Shift+F12", () => { });

            // 被其它程序占用时可能失败,这里只要求"不抛异常且返回布尔"
            Assert.True(ok || !ok);
        }
        finally
        {
            service.Dispose();
        }
    }

    [Fact]
    [DisplayName("热键偏好随外壳设置持久化")]
    public void ShellPreference_Hotkey_RoundTrip()
    {
        var dir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "xcoder-hotkey-" + Guid.NewGuid().ToString("N"));
        System.IO.Directory.CreateDirectory(dir);
        var old = ShellPreferenceStore.FilePath;
        ShellPreferenceStore.FilePath = System.IO.Path.Combine(dir, "ui.shell");

        try
        {
            ShellPreferenceStore.Save(new ShellPreference(true, false, "Ctrl+Shift+F9"));

            var loaded = ShellPreferenceStore.Load();

            Assert.Equal("Ctrl+Shift+F9", loaded.Hotkey);
            Assert.False(loaded.FloatOnTop);
        }
        finally
        {
            ShellPreferenceStore.FilePath = old;
            try { System.IO.Directory.Delete(dir, true); } catch (System.IO.IOException) { }
        }
    }

    [Fact]
    [DisplayName("未配置热键时使用默认组合")]
    public void ShellPreference_DefaultHotkey()
    {
        var dir = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "xcoder-hotkey2-" + Guid.NewGuid().ToString("N"));
        System.IO.Directory.CreateDirectory(dir);
        var old = ShellPreferenceStore.FilePath;
        ShellPreferenceStore.FilePath = System.IO.Path.Combine(dir, "ui.shell");

        try
        {
            var loaded = ShellPreferenceStore.Load();

            Assert.Equal("Ctrl+Alt+Shift+X", loaded.Hotkey);
        }
        finally
        {
            ShellPreferenceStore.FilePath = old;
            try { System.IO.Directory.Delete(dir, true); } catch (System.IO.IOException) { }
        }
    }
}