using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Web.Script.Serialization; using System.Windows.Forms; // Test fixture only: sends scan codes solely while its own textbox has focus. // It never changes the system/default IME, launches D3RO, or inspects user documents. internal static class Program { [STAThread] private static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); string output = args.Length > 0 ? Path.GetFullPath(args[0]) : Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "current-fixture.json")); Application.Run(new Fixture(output)); } } internal sealed class Fixture : Form { private readonly ObservedTextBox input = new ObservedTextBox(); private readonly Label status = new Label(); private readonly Button start = new Button(); private readonly Button stop = new Button(); private readonly CheckBox stress = new CheckBox(); private readonly ComboBox mode = new ComboBox(); private readonly TextBox runLabel = new TextBox(); private readonly string output; private InputLanguage originalLanguage; private CancellationTokenSource cancellation; private RunReport report; private CaseReport current; private Stopwatch clock = new Stopwatch(); private readonly System.Windows.Forms.Timer sampler = new System.Windows.Forms.Timer(); private ulong lastIdle, lastKernel, lastUser; private long lastCpuTicks, lastWallTicks; private int ownPid; public Fixture(string reportPath) { output = reportPath; ownPid = Process.GetCurrentProcess().Id; Text = "D3RO Typing Verification — isolated input fixture"; Width = 940; Height = 540; StartPosition = FormStartPosition.CenterScreen; var panel = new FlowLayoutPanel { Dock = DockStyle.Top, Height = 90, Padding = new Padding(8) }; start.Text = "Start verification"; start.Width = 145; stop.Text = "Stop"; stop.Enabled = false; stop.Width = 70; stress.Text = "Include CPU load"; stress.Checked = true; stress.Width = 145; mode.DropDownStyle = ComboBoxStyle.DropDownList; mode.Items.AddRange(new object[] { "Hangul IME", "Latin layout correction" }); mode.SelectedIndex = 0; mode.Width = 165; runLabel.Text = "label-current-app-state"; runLabel.Width = 210; panel.Controls.Add(start); panel.Controls.Add(stop); panel.Controls.Add(stress); panel.Controls.Add(mode); panel.Controls.Add(runLabel); panel.Controls.Add(new Label { Text = "The test types only into the box below. Changing focus aborts. No clipboard or Unicode text injection.", AutoSize = true }); input.Multiline = true; input.AcceptsReturn = true; input.ScrollBars = ScrollBars.Vertical; input.Dock = DockStyle.Fill; input.Font = new Font("Malgun Gothic", 20); input.AccessibleName = "Verification input only"; input.ImeMode = ImeMode.Hangul; status.Dock = DockStyle.Bottom; status.Height = 75; status.Text = "Ready. Select the app-state label, then Start verification.\r\nReport: " + output; Controls.Add(input); Controls.Add(panel); Controls.Add(status); start.Click += async delegate { await Run(); }; stop.Click += delegate { if (cancellation != null) cancellation.Cancel(); }; input.KeyDown += delegate { if (current != null) current.ObservedKeyDowns++; }; input.KeyUp += delegate { if (current != null) current.ObservedKeyUps++; }; input.TextChanged += delegate { if (current != null) current.Changes.Add(new TextObservation { ElapsedMs = clock.ElapsedMilliseconds, Text = input.Text }); }; input.ImeComposition += delegate(bool committed) { if (current != null) { current.ImeCompositionMessages++; if (committed) current.ImeResultMessages++; } }; sampler.Interval = 500; sampler.Tick += delegate { SampleCpu(); }; FormClosing += delegate { if (cancellation != null) cancellation.Cancel(); RestoreLanguage(); }; } private async Task Run() { if (cancellation != null) return; cancellation = new CancellationTokenSource(); var token = cancellation.Token; start.Enabled = false; stop.Enabled = true; mode.Enabled = false; stress.Enabled = false; runLabel.Enabled = false; bool layout = mode.SelectedIndex == 1; report = new RunReport { StartedAtUtc = DateTime.UtcNow.ToString("o"), Label = runLabel.Text, ProcessId = ownPid, WindowHandle = Handle.ToInt64(), LogicalProcessors = Environment.ProcessorCount, Mode = layout ? "latin-layout-correction" : "hangul-ime", ReportPath = output, Status = "running", Injection = "SendInput KEYEVENTF_SCANCODE key-down/key-up pairs", Limitations = "Synthetic scan codes exercise Windows IME but do not prove hardware keyboard delivery. CPU workers are bounded; compare observed CPU samples. D3RO version/config is not inferred by this fixture." }; clock.Restart(); InitializeCpu(); sampler.Start(); Save(); try { originalLanguage = InputLanguage.CurrentInputLanguage; InputLanguage korean = InputLanguage.InstalledInputLanguages.Cast().FirstOrDefault(l => l.Culture.TwoLetterISOLanguageName == "ko"); if (korean == null) throw new InvalidOperationException("No installed Korean input language; the fixture will not install or change system settings."); InputLanguage.CurrentInputLanguage = korean; input.ImeMode = ImeMode.Hangul; input.Focus(); await Task.Delay(500, token); report.KeyboardLayout = Native.GetKeyboardLayout(0).ToInt64().ToString("X"); // Calibration always proves IME composition independently of the layout-correction mode. CaseReport calibration = await TypeCase("calibration", "dkssudgktpdy ", "안녕하세요 ", 60, false, token); report.CalibrationPassed = calibration.ExactMatch && calibration.ImeCompositionMessages > 0; if (!report.CalibrationPassed) throw new InvalidOperationException("Hangul calibration needs exact text and native IME composition messages. Result is not accepted as IME typing proof."); if (layout) input.ImeMode = ImeMode.Off; foreach (bool cpu in stress.Checked ? new bool[] { false, true } : new bool[] { false }) { foreach (int pace in new int[] { 60, 30, 15 }) { token.ThrowIfCancellationRequested(); string keys = Repeat("dkssudgktpdy rkatkgkqslek qksrkqtmqslek ", 3); string expected = Repeat("안녕하세요 감사합니다 반갑습니다 ", 3); await TypeCase((cpu ? "cpu" : "idle") + "-" + pace, keys, expected, pace, cpu, token); } } report.Status = report.Cases.All(c => c.ExactMatch) ? "passed" : "failed"; } catch (OperationCanceledException) { report.Status = "aborted"; report.Error = "Cancelled or foreground/focus changed; input was stopped."; } catch (Exception error) { report.Status = "failed"; report.Error = error.GetType().Name + ": " + error.Message; } finally { sampler.Stop(); current = null; report.FinishedAtUtc = DateTime.UtcNow.ToString("o"); report.ElapsedMs = clock.ElapsedMilliseconds; Save(); RestoreLanguage(); cancellation.Dispose(); cancellation = null; start.Enabled = true; stop.Enabled = false; mode.Enabled = true; stress.Enabled = true; runLabel.Enabled = true; status.Text = report.Status.ToUpperInvariant() + ": " + report.Cases.Count(c => c.ExactMatch) + "/" + report.Cases.Count + " exact text matches.\r\n" + (report.Error ?? "") + "\r\n" + output; } } private async Task TypeCase(string name, string keys, string expected, int pace, bool load, CancellationToken token) { AssertOwnFocus(token); current = null; input.Clear(); // Only the fixture's own test field; never another app. await Task.Delay(200, token); current = new CaseReport { Name = name, PaceMs = pace, Expected = expected, Keys = keys, StartedAtUtc = DateTime.UtcNow.ToString("o"), StartedElapsedMs = clock.ElapsedMilliseconds }; report.Cases.Add(current); var item = current; using (var workers = new CpuWorkers(load ? Math.Max(1, Environment.ProcessorCount - 1) : 0, 20000)) { item.CpuWorkers = workers.Count; if (load) await Task.Delay(900, token); status.Text = "Running " + name + ". Keep focus inside the test input.\r\n" + output; try { foreach (char key in keys) { AssertOwnFocus(token); long requested = clock.ElapsedMilliseconds; uint sent = Native.SendScanPair(key); item.SentEvents += (int)sent; item.RequestedEvents += 2; item.KeyTimesMs.Add(requested); if (sent != 2) throw new InvalidOperationException("SendInput inserted " + sent + "/2 events; no retry."); await Task.Delay(pace, token); } await Task.Delay(1500, token); AssertOwnFocus(token); item.Actual = input.Text; item.ExactMatch = string.Equals(item.Actual, item.Expected, StringComparison.Ordinal); item.FirstMismatchIndex = FirstMismatch(item.Expected, item.Actual); } finally { item.Actual = input.Text; item.ElapsedMs = clock.ElapsedMilliseconds - item.StartedElapsedMs; item.FinishedAtUtc = DateTime.UtcNow.ToString("o"); Save(); current = null; } } return item; } private void AssertOwnFocus(CancellationToken token) { token.ThrowIfCancellationRequested(); if (Native.GetForegroundWindow() != Handle || Native.GetFocus() != input.Handle) { cancellation.Cancel(); throw new OperationCanceledException("Fixture textbox is not the active focused receiver."); } } private void InitializeCpu() { Native.ReadSystemTimes(out lastIdle, out lastKernel, out lastUser); lastCpuTicks = Process.GetCurrentProcess().TotalProcessorTime.Ticks; lastWallTicks = Stopwatch.GetTimestamp(); } private void SampleCpu() { ulong idle, kernel, user; Native.ReadSystemTimes(out idle, out kernel, out user); ulong total = kernel - lastKernel + user - lastUser; double system = total > 0 ? 100.0 * (total - Math.Min(total, idle - lastIdle)) / total : 0; long cpu = Process.GetCurrentProcess().TotalProcessorTime.Ticks; long now = Stopwatch.GetTimestamp(); double seconds = (double)(now - lastWallTicks) / Stopwatch.Frequency; double process = seconds > 0 ? 100.0 * (cpu - lastCpuTicks) / TimeSpan.TicksPerSecond / seconds / Environment.ProcessorCount : 0; report.CpuSamples.Add(new CpuSample { ElapsedMs = clock.ElapsedMilliseconds, Case = current == null ? "between" : current.Name, SystemPercent = system, FixturePercent = process }); lastIdle = idle; lastKernel = kernel; lastUser = user; lastCpuTicks = cpu; lastWallTicks = now; } private void Save() { Directory.CreateDirectory(Path.GetDirectoryName(output)); var json = new JavaScriptSerializer { MaxJsonLength = 16 * 1024 * 1024 }.Serialize(report); File.WriteAllText(output, json, new UTF8Encoding(false)); } private void RestoreLanguage() { if (originalLanguage == null) return; try { InputLanguage.CurrentInputLanguage = originalLanguage; } catch { } originalLanguage = null; } private static string Repeat(string value, int count) { return string.Concat(Enumerable.Repeat(value, count)); } private static int FirstMismatch(string expected, string actual) { for (int i = 0; i < Math.Min(expected.Length, actual.Length); i++) if (expected[i] != actual[i]) return i; return expected.Length == actual.Length ? -1 : Math.Min(expected.Length, actual.Length); } } internal sealed class CpuWorkers : IDisposable { private readonly CancellationTokenSource stop = new CancellationTokenSource(); private readonly List threads = new List(); public int Count { get { return threads.Count; } } public CpuWorkers(int count, int maximumMs) { for (int i = 0; i < count; i++) { var thread = new Thread(delegate() { var timer = Stopwatch.StartNew(); while (!stop.IsCancellationRequested && timer.ElapsedMilliseconds < maximumMs) Thread.SpinWait(10000); }); thread.IsBackground = true; thread.Name = "D3RO fixture bounded CPU worker"; threads.Add(thread); thread.Start(); } } public void Dispose() { stop.Cancel(); foreach (Thread thread in threads) thread.Join(1000); stop.Dispose(); } } internal sealed class ObservedTextBox : TextBox { internal event Action ImeComposition; protected override void WndProc(ref Message message) { if (message.Msg == 0x10f && ImeComposition != null) { ImeComposition((message.LParam.ToInt64() & 0x800) != 0); } base.WndProc(ref message); } } internal static class Native { [StructLayout(LayoutKind.Sequential)] private struct KeyboardInput { public ushort Vk, Scan; public uint Flags, Time; public IntPtr Extra; } [StructLayout(LayoutKind.Explicit, Size = 40)] private struct Input { [FieldOffset(0)] public uint Type; [FieldOffset(8)] public KeyboardInput Keyboard; } [StructLayout(LayoutKind.Sequential)] private struct FileTime { public uint Low, High; public ulong Value { get { return ((ulong)High << 32) | Low; } } } [DllImport("user32.dll")] internal static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] internal static extern IntPtr GetFocus(); [DllImport("user32.dll")] internal static extern IntPtr GetKeyboardLayout(uint thread); [DllImport("user32.dll", SetLastError = true)] private static extern uint SendInput(uint count, Input[] inputs, int size); [DllImport("kernel32.dll")] private static extern bool GetSystemTimes(out FileTime idle, out FileTime kernel, out FileTime user); internal static void ReadSystemTimes(out ulong idle, out ulong kernel, out ulong user) { FileTime i, k, u; if (!GetSystemTimes(out i, out k, out u)) throw new InvalidOperationException("GetSystemTimes failed"); idle = i.Value; kernel = k.Value; user = u.Value; } internal static uint SendScanPair(char key) { ushort scan; if (key == ' ') scan = 0x39; else { string[] rows = { "qwertyuiop", "asdfghjkl", "zxcvbnm" }; int[] starts = { 0x10, 0x1e, 0x2c }; scan = 0; for (int row = 0; row < rows.Length; row++) { int index = rows[row].IndexOf(key); if (index >= 0) { scan = (ushort)(starts[row] + index); break; } } if (scan == 0) throw new InvalidOperationException("Unsupported synthetic corpus key"); } var pair = new Input[2]; pair[0].Type = pair[1].Type = 1; pair[0].Keyboard.Scan = pair[1].Keyboard.Scan = scan; pair[0].Keyboard.Flags = 8; pair[1].Keyboard.Flags = 8 | 2; return SendInput(2, pair, Marshal.SizeOf(typeof(Input))); } } internal sealed class RunReport { public string StartedAtUtc, FinishedAtUtc, Label, Mode, ReportPath, Status, Error, Injection, Limitations, KeyboardLayout; public int ProcessId, LogicalProcessors; public long WindowHandle, ElapsedMs; public bool CalibrationPassed; public List Cases = new List(); public List CpuSamples = new List(); } internal sealed class CaseReport { public string Name, Expected, Actual, Keys, StartedAtUtc, FinishedAtUtc; public int PaceMs, CpuWorkers, SentEvents, RequestedEvents, ObservedKeyDowns, ObservedKeyUps, FirstMismatchIndex, ImeCompositionMessages, ImeResultMessages; public long StartedElapsedMs, ElapsedMs; public bool ExactMatch; public List KeyTimesMs = new List(); public List Changes = new List(); } internal sealed class TextObservation { public long ElapsedMs; public string Text; } internal sealed class CpuSample { public long ElapsedMs; public string Case; public double SystemPercent, FixturePercent; }