| 1 | using System.ComponentModel; |
| 2 | using System.Reflection; |
| 3 | using System.Runtime.ExceptionServices; |
| 4 | using System.Runtime.InteropServices; |
| 5 | using System.Text.Json; |
| 6 | using Cage.Windows; |
| 7 | |
| 8 | internal static class NativeInputTests |
| 9 | { |
| 10 | |
| 11 | |
| 12 | internal static void Run() |
| 13 | { |
| 14 | Exception? failure = null; |
| 15 | var thread = new Thread(() => { |
| 16 | try { RunOnInputThread(); } |
| 17 | catch (Exception error) { failure = error; } |
| 18 | }) { IsBackground = true, Name = "Cage native input regression" }; |
| 19 | thread.SetApartmentState(ApartmentState.STA); |
| 20 | thread.Start(); |
| 21 | if (!thread.Join(TimeSpan.FromSeconds(15))) throw new TimeoutException("Native input regression did not complete"); |
| 22 | if (failure != null) ExceptionDispatchInfo.Capture(failure).Throw(); |
| 23 | } |
| 24 | |
| 25 | private static void RunOnInputThread() |
| 26 | { |
| 27 | var assertions = 0; |
| 28 | void Check(bool value, string message) |
| 29 | { |
| 30 | assertions++; |
| 31 | if (!value) throw new InvalidOperationException(message); |
| 32 | } |
| 33 | |
| 34 | var events = new List<Dictionary<string, object?>>(); |
| 35 | using var host = new Form { ShowInTaskbar = false }; |
| 36 | using var web = new Panel(); |
| 37 | using var input = new NativeInput(events.Add); |
| 38 | host.Controls.Add(web); |
| 39 | host.Controls.Add(input); |
| 40 | var owner = Guid.NewGuid().ToString(); |
| 41 | Check(input.ProcessRequest(JsonSerializer.SerializeToElement(new { |
| 42 | owner, operation = "activate", x = 0, y = 0, height = 24, |
| 43 | }), web, 1), "native input activation rejected"); |
| 44 | Check(!host.Visible, "regression fixture must remain hidden"); |
| 45 | |
| 46 | var down = typeof(NativeInput).GetMethod("OnKeyDown", BindingFlags.Instance | BindingFlags.NonPublic)!; |
| 47 | var press = typeof(NativeInput).GetMethod("OnKeyPress", BindingFlags.Instance | BindingFlags.NonPublic)!; |
| 48 | var up = typeof(NativeInput).GetMethod("OnKeyUp", BindingFlags.Instance | BindingFlags.NonPublic)!; |
| 49 | var original = new byte[256]; |
| 50 | if (!GetKeyboardState(original)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 51 | try |
| 52 | { |
| 53 | foreach (var capsLock in new[] { false, true }) |
| 54 | foreach (var shift in new[] { false, true }) |
| 55 | { |
| 56 | var state = new byte[256]; |
| 57 | state[(int)Keys.CapsLock] = (byte)(capsLock ? 1 : 0); |
| 58 | state[(int)Keys.ShiftKey] = state[(int)Keys.LShiftKey] = (byte)(shift ? 0x80 : 0); |
| 59 | if (!SetKeyboardState(state)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 60 | Check(Control.IsKeyLocked(Keys.CapsLock) == capsLock, "thread CapsLock fixture state was not applied"); |
| 61 | Check(((Control.ModifierKeys & Keys.Shift) != 0) == shift, "thread Shift fixture state was not applied"); |
| 62 | |
| 63 | void CheckEvent(int index, string kind, string key, string code, bool textKey) |
| 64 | { |
| 65 | var value = events[index]; |
| 66 | Check(Equals(value["owner"], owner), "input owner changed"); |
| 67 | Check(Equals(value["kind"], kind), "input event kind changed"); |
| 68 | Check(Equals(value["key"], key), "input character or command changed"); |
| 69 | Check(Equals(value["code"], code), "physical key code changed"); |
| 70 | Check(Equals(value["textKey"], textKey), "text key classification changed"); |
| 71 | Check(value.TryGetValue("modifierCapsLock", out var locked) && Equals(locked, capsLock), |
| 72 | $"CapsLock={capsLock}, Shift={shift}: {kind} {code} lost CapsLock state"); |
| 73 | Check(Equals(value["shiftKey"], shift), "Shift state changed"); |
| 74 | Check(Equals(value["ctrlKey"], false) && Equals(value["altKey"], false) && Equals(value["metaKey"], false), |
| 75 | "unpressed modifiers leaked into the event"); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | events.Clear(); |
| 81 | var modifiers = shift ? Keys.Shift : Keys.None; |
| 82 | var character = capsLock ^ shift ? 'A' : 'a'; |
| 83 | down.Invoke(input, [new KeyEventArgs(Keys.A | modifiers)]); |
| 84 | Check(events.Count == 0, "printable keydown must wait for the character callback"); |
| 85 | press.Invoke(input, [new KeyPressEventArgs(character)]); |
| 86 | up.Invoke(input, [new KeyEventArgs(Keys.A | modifiers)]); |
| 87 | Check(events.Count == 2, "printable key must produce one down/up pair"); |
| 88 | CheckEvent(0, "keydown", character.ToString(), "KeyA", true); |
| 89 | CheckEvent(1, "keyup", "a", "KeyA", false); |
| 90 | |
| 91 | |
| 92 | |
| 93 | foreach (var command in new[] { |
| 94 | (Keys.CapsLock, "CapsLock", "CapsLock"), |
| 95 | (Keys.Left, "ArrowLeft", "ArrowLeft"), |
| 96 | (Keys.ShiftKey, "Shift", "ShiftLeft"), |
| 97 | }) |
| 98 | { |
| 99 | events.Clear(); |
| 100 | down.Invoke(input, [new KeyEventArgs(command.Item1 | modifiers)]); |
| 101 | up.Invoke(input, [new KeyEventArgs(command.Item1 | modifiers)]); |
| 102 | Check(events.Count == 2, "command must produce one down/up pair"); |
| 103 | CheckEvent(0, "keydown", command.Item2, command.Item3, false); |
| 104 | CheckEvent(1, "keyup", command.Item2, command.Item3, false); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | finally |
| 109 | { |
| 110 | if (!SetKeyboardState(original)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 111 | } |
| 112 | var restored = new byte[256]; |
| 113 | if (!GetKeyboardState(restored)) throw new Win32Exception(Marshal.GetLastWin32Error()); |
| 114 | Check(original.SequenceEqual(restored), "test thread keyboard state was not restored"); |
| 115 | Console.WriteLine($"Windows native input: {assertions} assertions passed; CapsLock on/off, Shift combinations, printable/command down/up, thread state restored."); |
| 116 | } |
| 117 | |
| 118 | [DllImport("user32.dll", SetLastError = true)] |
| 119 | [return: MarshalAs(UnmanagedType.Bool)] |
| 120 | private static extern bool GetKeyboardState([Out] byte[] state); |
| 121 | |
| 122 | [DllImport("user32.dll", SetLastError = true)] |
| 123 | [return: MarshalAs(UnmanagedType.Bool)] |
| 124 | private static extern bool SetKeyboardState(byte[] state); |
| 125 | } |