using System; using System.Runtime.InteropServices; using Annulus; using Azimuth; using Blacklight.Core.Cameras; using Blacklight.Core.Keyboard; namespace Blacklight.Core.UnitTests.Keyboard { public class ImeWindowTests : UnitTestSharp.TestFixture { [DllImport("User32.dll")] private static extern IntPtr SendMessage(IntPtr window, uint message, IntPtr wParam, IntPtr lParam); public static IntPtr Send(WinFormsInputTests.TestControl control, int message, uint flags = 0) { return SendMessage(control.Handle, (uint)message, IntPtr.Zero, ImeMessagesTests.Flags(flags)); } public static (WinFormsInputTests.TestControl, WinFormsInput, WinFormsInputTests.RecordingTextConsumer) MakeTextInput() { var (control, _, input) = WinFormsInputTests.MakeInput(); var text = new WinFormsInputTests.RecordingTextConsumer(consumes: false); input.TextConsumer = text; return (control, input, text); } public class WndProcTests : UnitTestSharp.TestFixture { public void ACompositionWithNoFlags_ReportsItCleared() { var (control, _, text) = MakeTextInput(); Send(control, ImeMessages.Composition, 0); CheckEqual(new[] { "|0" }, text.Compositions); } public void ACompositionStringThatChanged_ReportsWhatTheContextHolds() { var (control, _, text) = MakeTextInput(); Send(control, ImeMessages.Composition, NativeMethods.GCS_COMPSTR); CheckEqual(new[] { "|0" }, text.Compositions); CheckEqual(new[] { "0-0" }, text.Targets); } public void CommittedTextTheContextDoesNotHold_ReportsNoText() { var (control, _, text) = MakeTextInput(); Send(control, ImeMessages.Composition, NativeMethods.GCS_RESULTSTR); CheckEqual(0, text.Texts.Count); CheckEqual(0, text.Compositions.Count); } public void OnlyTheCursorMoving_ReportsNothing() { var (control, _, text) = MakeTextInput(); Send(control, ImeMessages.Composition, NativeMethods.GCS_CURSORPOS); CheckEqual(0, text.Compositions.Count); } public void TheEndOfTheComposition_IsReported() { var (control, _, text) = MakeTextInput(); Send(control, ImeMessages.EndComposition); CheckEqual(1, text.CompositionEndCount); } public void TheStartOfTheComposition_IsAnsweredAndReportsNothing() { var (control, _, text) = MakeTextInput(); IntPtr answer = Send(control, ImeMessages.StartComposition); CheckEqual(IntPtr.Zero, answer); CheckEqual(0, text.Compositions.Count); CheckEqual(0, text.CompositionEndCount); } public void SetContext_ReachesTheDefaultHandlingAndReportsNothing() { var (control, _, text) = MakeTextInput(); Send(control, ImeMessages.SetContext, 0xC000000F); CheckEqual(0, text.Compositions.Count); CheckEqual(0, text.CompositionEndCount); } public void WithNothingTakingText_IsNotReported() { var (control, input, text) = MakeTextInput(); input.TextConsumer = null; Send(control, ImeMessages.Composition, 0); Send(control, ImeMessages.EndComposition); Send(control, ImeMessages.StartComposition); Send(control, ImeMessages.SetContext, 0xC000000F); CheckEqual(0, text.Compositions.Count); CheckEqual(0, text.CompositionEndCount); } public void AfterTheWindowIsMadeAgain_IsStillReported() { var (control, _, text) = MakeTextInput(); control.Recreate(); Send(control, ImeMessages.Composition, 0); CheckEqual(new[] { "|0" }, text.Compositions); } public void AfterDispose_IsNoLongerReported() { var (control, input, text) = MakeTextInput(); input.Dispose(); Send(control, ImeMessages.Composition, 0); CheckEqual(0, text.Compositions.Count); } } public class PlaceTests : UnitTestSharp.TestFixture { public void WithNoCaret_DoesNothing() { var (control, _, _) = MakeTextInput(); ImeWindow.Place(control.Handle, null); } public void WithNoWindow_DoesNothing() { ImeWindow.Place(IntPtr.Zero, Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4))); } public void ByACaret_Works() { var (control, _, _) = MakeTextInput(); ImeWindow.Place(control.Handle, Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4))); } public void ForAWindowWithNoInputContext_DoesNothing() { var (control, input, _) = MakeTextInput(); input.TextConsumer = null; ImeWindow.Place(control.Handle, Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4))); } } public class CaretBoundsTests : UnitTestSharp.TestFixture { public void StartsNull() { var (_, input, _) = MakeTextInput(); CheckNull(input.CaretBounds); } public void HoldsWhatWasSet() { var (_, input, _) = MakeTextInput(); Rect caret = Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4)); input.CaretBounds = caret; CheckEqual(caret, input.CaretBounds); } public void SetWithNothingTakingText_IsHeldWithoutPlacingAnything() { var (_, input, _) = MakeTextInput(); input.TextConsumer = null; Rect caret = Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4)); input.CaretBounds = caret; CheckEqual(caret, input.CaretBounds); } public void SetBeforeTheWindowHasAHandle_IsHeld() { var control = new WinFormsInputTests.TestControl(); var input = new WinFormsInput(control, new WinFormsInputTests.RecordingConsumer()); input.TextConsumer = new WinFormsInputTests.RecordingTextConsumer(consumes: false); Rect caret = Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4)); input.CaretBounds = caret; CheckEqual(caret, input.CaretBounds); } public void ATextConsumerSetLater_IsGivenTheHeldCaret() { var (_, input, _) = MakeTextInput(); input.TextConsumer = null; input.CaretBounds = Rect.FromPositionAndSize(new Vector(1, 2), new Vector(3, 4)); input.TextConsumer = new WinFormsInputTests.RecordingTextConsumer(consumes: false); } } public class FinishCompositionTests : UnitTestSharp.TestFixture { public void WithNothingComposed_ReportsNothing() { var (_, input, text) = MakeTextInput(); input.FinishComposition(); CheckEqual(0, text.Texts.Count); CheckEqual(0, text.Compositions.Count); } public void BeforeTheWindowHasAHandle_DoesNothing() { var control = new WinFormsInputTests.TestControl(); var input = new WinFormsInput(control, new WinFormsInputTests.RecordingConsumer()); input.FinishComposition(); } public void ForAWindowWithNoInputContext_DoesNothing() { var (_, input, _) = MakeTextInput(); input.TextConsumer = null; input.FinishComposition(); } } } }