Example #1
0
        public void Title_GetAndSetProperly()
        {
            string title = string.Empty;
            var    api   = new StubbedNativeCalls
            {
                GetConsoleTitle       = () => title,
                SetConsoleTitleString = s => title = s
            };

            using var controller = new StubbedConsoleController();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);

            sut.Title.Should().Be(string.Empty);
            sut.Title = "hello";
            title.Should().Be("hello");
            sut.Title.Should().Be("hello");
            sut.Title = "world";
            title.Should().Be("world");
            sut.Title.Should().Be("world");

            sut.Title = null !;
            title.Should().BeEmpty();
            sut.Title.Should().BeEmpty();
        }
Example #2
0
        public void FrameCharSets_SetOnlyOnChange()
        {
            var  api = new StubbedNativeCalls();
            var  graphicsProvider  = new StubbedGraphicsProvider();
            bool graphicsRequested = false;

            graphicsProvider.ProvideConsoleOutputHandleINativeCallsSizeFrameCharSets = (handle, calls, arg3, arg4) =>
            {
                graphicsRequested = true;
                return(graphicsProvider.Graphics);
            };

            using var sut = new ConControls.Controls.ConsoleWindow(api, new StubbedConsoleController(), graphicsProvider);

            sut.FrameCharSets.Should().BeOfType <FrameCharSets>();

            var fremeCharSets = new FrameCharSets();

            graphicsRequested = false;
            sut.FrameCharSets = fremeCharSets;
            graphicsRequested.Should().BeTrue();
            sut.FrameCharSets.Should().BeSameAs(fremeCharSets);
            graphicsRequested = false;
            sut.FrameCharSets = fremeCharSets;
            graphicsRequested.Should().BeFalse();
            sut.FrameCharSets = new FrameCharSets();
            graphicsRequested.Should().BeTrue();

            sut.Invoking(s => s.FrameCharSets = null !).Should().Throw <ArgumentNullException>();
        }
        public void DefaultForegroundColor_SetAndDrawn()
        {
            const ConsoleColor color = ConsoleColor.DarkMagenta;
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            bool graphicsProvided = false;
            var  graphicsProvider = new StubbedGraphicsProvider();

            graphicsProvider.ProvideConsoleOutputHandleINativeCallsSizeFrameCharSets = (handle, calls, arg3, arg4) =>
            {
                graphicsProvided = true;
                return(graphicsProvider.Graphics);
            };
            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);
            var suti = (IControlContainer)sut;

            sut.DefaultForegroundColor.Should().Be(ConsoleColor.Gray);
            suti.ForegroundColor.Should().Be(ConsoleColor.Gray);
            graphicsProvided     = false;
            suti.ForegroundColor = color;
            graphicsProvided.Should().BeTrue();
            sut.DefaultForegroundColor.Should().Be(color);

            graphicsProvided     = false;
            suti.ForegroundColor = null;
            graphicsProvided.Should().BeFalse();
            sut.DefaultForegroundColor.Should().Be(color);
        }
        public void ControlManagement_ControlAreaChanged_Redrawn()
        {
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            var  graphicsProvider = new StubbedGraphicsProvider();
            bool provided         = false;

            graphicsProvider.ProvideConsoleOutputHandleINativeCallsSizeFrameCharSets = (handle, calls, arg3, arg4) =>
            {
                provided = true;
                return(graphicsProvider.Graphics);
            };

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);
            using var c   = new Panel(sut)
                  {
                      Area = (5, 5, 10, 10).Rect(), Parent = sut
                  };
            provided = false;
            c.Area   = (4, 4, 6, 6).Rect();
            provided.Should().BeTrue();

            sut.Controls.Remove(c);
            provided = false;
            c.Area   = (1, 2, 3, 4).Rect();
            provided.Should().BeFalse();
        }
    }
Example #5
0
        public void KeyEvent_SwitchBuffersKey_BuffersSwitched()
        {
            bool active = true;

            using var consoleController = new StubbedConsoleController
                  {
                      ActiveScreenGet        = () => active,
                      ActiveScreenSetBoolean = b => active = b
                  };
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider)
                  {
                      SwitchConsoleBuffersKey = ConControls.Controls.KeyCombination.F11
                  };

            var e = new ConsoleKeyEventArgs(new KEY_EVENT_RECORD
            {
                KeyDown        = 1,
                VirtualKeyCode = VirtualKey.F11
            });

            consoleController.KeyEventEvent(consoleController, e);
            active.Should().BeFalse();
            consoleController.KeyEventEvent(consoleController, e);
            active.Should().BeTrue();
            consoleController.KeyEventEvent(consoleController, e);
            active.Should().BeFalse();
        }
Example #6
0
        public void KeyEvent_Handled_Ignored()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);
            bool raised = false;

            sut.KeyEvent += (sender, e) =>
            {
                sender.Should().Be(sut);
                raised    = true;
                e.Handled = true;
            };

            _ = new StubbedTextControl(sut);
            consoleController.KeyEventEvent(consoleController,
                                            new ConsoleKeyEventArgs(new KEY_EVENT_RECORD
            {
                ControlKeys = ControlKeyStates.None, KeyDown = 1, VirtualKeyCode = VirtualKey.Tab
            }));
            raised.Should().BeTrue();
            sut.FocusedControl.Should().BeNull();
        }
Example #7
0
        public void FocusNext_Cousins_CorrectOrder()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);

            var c0 = new Panel(sut)
            {
                Parent = sut
            };
            var f00 = new ConControls.Controls.TextBlock(sut)
            {
                Parent = c0
            };
            var c1 = new Panel(sut)
            {
                Parent = sut
            };
            var f10 = new ConControls.Controls.TextBlock(sut)
            {
                Parent = c1
            };

            sut.FocusedControl.Should().BeNull();
            sut.FocusNext().Should().Be(f00);
            sut.FocusNext().Should().Be(f10);
            sut.FocusNext().Should().Be(f00);
            sut.FocusNext().Should().Be(f10);
        }
Example #8
0
        public async Task InputEvents_InvalidRecordType_Logged()
        {
            var record = new INPUT_RECORD
            {
                EventType = (InputEventType)0xFFFF
            };
            var records = new[] { record };
            var tcs     = new TaskCompletionSource <int>();

            using var api = new StubbedNativeCalls();
            api.ReadConsoleInputConsoleInputHandleInt32 = (handle, size) =>
            {
                handle.Should().Be(api.StdIn);
                return(records);
            };
            using var sut    = new ConControls.ConsoleApi.ConsoleController(api);
            using var logger = new TestLogger(CheckInputLogForRecord);
            api.StdInEvent.Set();
            (await Task.WhenAny(tcs.Task, Task.Delay(2000)))
            .Should()
            .Be(tcs.Task, "event should be processed in less than 2 seconds!");

            void CheckInputLogForRecord(string msg)
            {
                if (msg.Contains("Unkown input record type "))
                {
                    tcs.SetResult(0);
                }
            }
        }
 public void Constructor_CantCreateScreenBuffer_Win32Exception()
 {
     using var api = new StubbedNativeCalls
           {
               CreateConsoleScreenBuffer = () => new ConsoleOutputHandle(IntPtr.Zero)
           };
     api.Invoking(a => new ConControls.ConsoleApi.ConsoleController(a)).Should().Throw <Win32Exception>().WithInnerException <Win32Exception>();
 }
Example #10
0
        public async Task WaitForCloseAsync_Works()
        {
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            using var sut        = new ConControls.Controls.ConsoleWindow(api, controller, new StubbedGraphicsProvider());
            sut.Close(12);
            (await sut.WaitForCloseAsync()).Should().Be(12);
        }
Example #11
0
        public void CopyCharacters_AreaTooLarge_ClippedCorrectly()
        {
            Size size       = new Size(4, 4);
            var  mainBuffer = Enumerable.Repeat(cc, 16).ToArray();

            char[] characters =
            {
                cc1, cc2, cc3, cc4, cc1,
                cc2, cc3, cc4, cc1, cc2,
                cc3, cc4, cc1, cc2, cc3,
                cc4, cc1, cc2, cc3, cc4,
                cc1, cc2, cc3, cc4, cc1
            };
            var expectedBuffer = new[]
            {
                cA, cB, cC, cD,
                cB, cC, cD, cA,
                cC, cD, cA, cB,
                cD, cA, cB, cC
            };

            bool written = false, successful = false;

            using var stubbedApi = new StubbedNativeCalls();
            stubbedApi.ReadConsoleOutputConsoleOutputHandleRectangle = (handle, rectangle) =>
            {
                rectangle.Size.Should().Be(size);
                handle.Should().Be(stubbedApi.ScreenHandle);
                return(mainBuffer);
            };
            stubbedApi.WriteConsoleOutputConsoleOutputHandleCHAR_INFOArrayRectangle = (handle, buffer, area) =>
            {
                written = true;
                handle.Should().Be(stubbedApi.ScreenHandle);
                area.Size.Should().Be(size);
                buffer.Should().Equal(expectedBuffer);
                successful = true;
            };
            var sut = new ConControls.Controls.Drawing.ConsoleGraphics(stubbedApi.ScreenHandle, stubbedApi, size,
                                                                       new ConControls.Controls.Drawing.FrameCharSets());

            sut.CopyCharacters(
                background: background,
                foreColor: foreground,
                topLeft: Point.Empty,
                characters: characters,
                arraySize: new Size(5, 5));

            written.Should().BeFalse();
            mainBuffer.Should().Equal(expectedBuffer);
            sut.Flush();
            written.Should().BeTrue();
            successful.Should().BeTrue();
        }
Example #12
0
        public void ActiveScreen_CorrectBehaviour()
        {
            var api = new StubbedNativeCalls();
            var sut = new ConControls.ConsoleApi.ConsoleController(api);

            sut.ActiveScreen.Should().BeTrue();

            bool set = false;
            ConsoleOutputHandle currentHandle = api.ScreenHandle;

            api.SetActiveConsoleScreenBufferConsoleOutputHandle = SetSuccess;

            sut.ActiveScreen = true;
            set.Should().BeFalse();
            sut.ActiveScreen.Should().BeTrue();
            sut.ActiveScreen = false;
            set.Should().BeTrue();
            sut.ActiveScreen.Should().BeFalse();
            currentHandle.Should().Be(api.StdOut);
            set = false;
            sut.ActiveScreen = false;
            set.Should().BeFalse();
            sut.ActiveScreen.Should().BeFalse();

            api.SetActiveConsoleScreenBufferConsoleOutputHandle = SetFail;
            sut.ActiveScreen = true;
            set.Should().BeTrue();
            sut.ActiveScreen.Should().BeFalse();

            api.SetActiveConsoleScreenBufferConsoleOutputHandle = SetSuccess;
            sut.ActiveScreen = true;
            sut.ActiveScreen.Should().BeTrue();
            currentHandle.Should().Be(api.ScreenHandle);

            set = false;
            api.SetActiveConsoleScreenBufferConsoleOutputHandle = SetFail;
            sut.ActiveScreen = false;
            set.Should().BeTrue();
            sut.ActiveScreen.Should().BeTrue();

            bool SetFail(ConsoleOutputHandle handle)
            {
                set           = true;
                currentHandle = handle;
                return(false);
            }

            bool SetSuccess(ConsoleOutputHandle handle)
            {
                set           = true;
                currentHandle = handle;
                return(true);
            }
        }
Example #13
0
        public void FocusNext_Empty_Null()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);

            sut.FocusedControl.Should().BeNull();
            sut.FocusNext().Should().BeNull();
            sut.FocusNext().Should().BeNull();
        }
Example #14
0
        public void PointToConsole_Identity()
        {
            var consoleListener = new StubbedConsoleController();

            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleListener, graphicsProvider);
            Point p = new Point(12, 34);

            sut.PointToConsole(p).Should().Be(p);
        }
Example #15
0
        public void DrawinInhiibited_DependingOnVisibility()
        {
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            using var sut        = new ConControls.Controls.ConsoleWindow(api, controller, new StubbedGraphicsProvider());

            sut.DrawingInhibited.Should().BeFalse();
            using (sut.DeferDrawing())
                sut.DrawingInhibited.Should().BeTrue();
            sut.DrawingInhibited.Should().BeFalse();
            sut.Dispose();
            sut.DrawingInhibited.Should().BeTrue();
        }
Example #16
0
        public void Size_Get_BufferSize()
        {
            var consoleListener = new StubbedConsoleController();
            var windowSize      = new Size(12, 34);

            using var api = new StubbedNativeCalls
                  {
                      GetConsoleScreenBufferInfoConsoleOutputHandle = handle => new CONSOLE_SCREEN_BUFFER_INFOEX { Window = new SMALL_RECT(1, 2, 3, 4), BufferSize = new COORD(windowSize) }
                  };
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleListener, graphicsProvider);
            sut.Size.Should().Be(windowSize);
        }
Example #17
0
        public void FillArea_CorrectArea_FilledCorrectly()
        {
            const char character  = 'X';
            Size       size       = new Size(4, 4);
            var        mainBuffer = Enumerable.Repeat(cc, 16).ToArray();
            CHAR_INFO  c0         = new CHAR_INFO
            {
                Attributes = attributes,
                Char       = character
            };
            var expectedBuffer = new[]
            {
                cc, cc, cc, cc,
                cc, c0, c0, cc,
                cc, c0, c0, cc,
                cc, cc, cc, cc
            };

            bool written = false, successful = false;

            using var stubbedApi = new StubbedNativeCalls();
            stubbedApi.ReadConsoleOutputConsoleOutputHandleRectangle = (handle, rectangle) =>
            {
                rectangle.Size.Should().Be(size);
                handle.Should().Be(stubbedApi.ScreenHandle);
                return(mainBuffer);
            };
            stubbedApi.WriteConsoleOutputConsoleOutputHandleCHAR_INFOArrayRectangle = (handle, buffer, area) =>
            {
                written = true;
                handle.Should().Be(stubbedApi.ScreenHandle);
                area.Size.Should().Be(size);
                buffer.Should().Equal(expectedBuffer);
                successful = true;
            };
            var sut = new ConControls.Controls.Drawing.ConsoleGraphics(stubbedApi.ScreenHandle, stubbedApi, size,
                                                                       new ConControls.Controls.Drawing.FrameCharSets());

            sut.FillArea(
                background: background,
                foreColor: foreground,
                c: character,
                area: new Rectangle(1, 1, 2, 2));

            written.Should().BeFalse();
            mainBuffer.Should().Equal(expectedBuffer);
            sut.Flush();
            written.Should().BeTrue();
            successful.Should().BeTrue();
        }
Example #18
0
        public void FocusedControl_NonFocussableControl_InvalidOperationException()
        {
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);
            using var c   = new Panel(sut)
                  {
                      Parent = sut
                  };
            sut.Invoking(s => s.FocusedControl = c).Should().Throw <InvalidOperationException>();
        }
Example #19
0
        static void PerformSizeTest(Size original, Size target, Size maximum, Size expectedTempWindowSize, Size expectedFinalWindowSize)
        {
            Size currentWindowSize = original, currentBufferSize = original;
            int  windowSet = 0;

            using var api = new StubbedNativeCalls();
            var controller       = new StubbedConsoleController();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);

            api.GetConsoleScreenBufferInfoConsoleOutputHandle = handle =>
            {
                handle.Should().Be(controller.OutputHandle);
                return(new CONSOLE_SCREEN_BUFFER_INFOEX
                {
                    Window = new SMALL_RECT(currentWindowSize),
                    BufferSize = new COORD(currentBufferSize),
                    MaximumWindowSize = new COORD(maximum)
                });
            };
            api.SetConsoleWindowSizeConsoleOutputHandleSize = (handle, size) =>
            {
                handle.Should().Be(controller.OutputHandle);
                currentWindowSize = size;
                if (windowSet == 0)
                {
                    size.Should().Be(expectedTempWindowSize);
                    windowSet = 1;
                    return;
                }

                size.Should().Be(expectedFinalWindowSize);
                windowSet.Should().Be(1);
                windowSet = 2;
            };
            api.SetConsoleScreenBufferSizeConsoleOutputHandleSize = (handle, size) =>
            {
                handle.Should().Be(controller.OutputHandle);
                size.Should().Be(target);
                currentBufferSize = size;
            };
            sut.Size.Should().Be(original);

            sut.Size = target;
            currentBufferSize.Should().Be(target);
            currentWindowSize.Should().Be(expectedFinalWindowSize);
        }
Example #20
0
        public void Area_PointEmptyAndSize()
        {
            Size windowSize = (5, 7).Sz();
            var  api        = new StubbedNativeCalls
            {
                GetConsoleScreenBufferInfoConsoleOutputHandle = handle => new CONSOLE_SCREEN_BUFFER_INFOEX
                {
                    BufferSize = new COORD(windowSize)
                }
            };

            using var controller = new StubbedConsoleController();
            using var sut        = new ConControls.Controls.ConsoleWindow(api, controller, new StubbedGraphicsProvider());

            sut.Area.Should().Be((Point.Empty, windowSize).Rect());
        }
Example #21
0
        public void ControlManagement_CursorUpdate_Updated()
        {
            bool  cursorVisible  = false;
            int   cursorSize     = 0;
            Point cursorPosition = Point.Empty;

            var api = new StubbedNativeCalls
            {
                SetCursorInfoConsoleOutputHandleBooleanInt32Point = (handle, visible, size, position) =>
                {
                    cursorVisible  = visible;
                    cursorSize     = size;
                    cursorPosition = position;
                }
            };

            using var controller = new StubbedConsoleController();
            var graphicsProvider = new StubbedGraphicsProvider();
            var textController   = new StubbedConsoleTextController
            {
                BufferLineCountGet = () => 20,
                MaxLineLengthGet   = () => 20,
                EndCaretGet        = () => (20, 20).Pt(),
                GetLineLengthInt32 = l => 20
            };

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);
            using var c   = new ConControls.Controls.TextBlock(sut, textController)
                  {
                      Area = (5, 5, 10, 10).Rect(), Parent = sut, CursorSize = 12, CursorVisible = false, CursorPosition = (1, 2).Pt()
                  };

            sut.FocusedControl = c;
            cursorVisible.Should().BeFalse();
            cursorSize.Should().Be(12);
            cursorPosition.Should().Be((6, 7).Pt());

            c.CursorPosition = (5, 6).Pt();
            cursorPosition.Should().Be((10, 11).Pt());

            c.CursorVisible = true;
            cursorVisible.Should().BeTrue();

            c.CursorSize = 23;
            cursorSize.Should().Be(23);
        }
Example #22
0
        public void KeyEvent_UnhandledKeyUp_Ignored()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);

            _ = new StubbedTextControl(sut);
            consoleController.KeyEventEvent(consoleController,
                                            new ConsoleKeyEventArgs(new KEY_EVENT_RECORD
            {
                ControlKeys    = ControlKeyStates.None,
                KeyDown        = 0,
                VirtualKeyCode = VirtualKey.Tab
            }));
            sut.FocusedControl.Should().BeNull();
        }
Example #23
0
        public void FocusNext_SingleThenDisabled_Null()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);

            var f0 = new ConControls.Controls.TextBlock(sut)
            {
                Parent = sut
            };

            sut.FocusedControl.Should().BeNull();
            sut.FocusNext().Should().Be(f0);
            f0.Enabled = false;
            sut.FocusNext().Should().BeNull();
        }
Example #24
0
        public void MouseEvents_MouseEventRaisedWithCorrectValues()
        {
            const ControlKeyStates controlKeys = ControlKeyStates.LEFT_ALT_PRESSED | ControlKeyStates.CAPSLOCK_ON;
            const int scroll = 123;
            const MouseButtonStates buttons = MouseButtonStates.LeftButtonPressed;
            Point position             = (3, 7).Pt();
            const MouseEventFlags kind = MouseEventFlags.Wheeled;

            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            var graphicsProvider = new StubbedGraphicsProvider();

            var args = new ConsoleMouseEventArgs(new MOUSE_EVENT_RECORD
            {
                ButtonState   = buttons,
                ControlKeys   = controlKeys,
                EventFlags    = kind,
                MousePosition = new COORD(position),
                Scroll        = scroll
            });

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);
            bool raised = false;

            sut.MouseEvent += OnMouse;
            controller.MouseEventEvent(controller, args);
            raised.Should().BeTrue();
            raised          = false;
            sut.MouseEvent -= OnMouse;
            controller.MouseEventEvent(controller, args);
            raised.Should().BeFalse();

            void OnMouse(object sender, MouseEventArgs e)
            {
                sender.Should().Be(sut);
                e.ControlKeys.Should().Be(controlKeys);
                e.Scroll.Should().Be(scroll);
                e.ButtonState.Should().Be(buttons);
                e.Position.Should().Be(position);
                e.Kind.Should().Be(kind);
                raised = true;
            }
        }
        public void DrawBackground_AreaTooLarge_ClippedCorrectly()
        {
            const char expectedCharacter = default;
            Size       size       = new Size(4, 4);
            var        mainBuffer = Enumerable.Repeat(cc, 16).ToArray();
            CHAR_INFO  c0         = new CHAR_INFO
            {
                Attributes = background.ToBackgroundColor() | background.ToForegroundColor(),
                Char       = expectedCharacter
            };
            var expectedBuffer = Enumerable.Repeat(c0, 16).ToArray();

            bool written = false, successful = false;

            using var stubbedApi = new StubbedNativeCalls();
            stubbedApi.ReadConsoleOutputConsoleOutputHandleRectangle = (handle, rectangle) =>
            {
                rectangle.Size.Should().Be(size);
                handle.Should().Be(stubbedApi.ScreenHandle);
                return(mainBuffer);
            };
            stubbedApi.WriteConsoleOutputConsoleOutputHandleCHAR_INFOArrayRectangle = (handle, buffer, area) =>
            {
                written = true;
                handle.Should().Be(stubbedApi.ScreenHandle);
                area.Size.Should().Be(size);
                buffer.Should().Equal(expectedBuffer);
                successful = true;
            };
            var sut = new ConControls.Controls.Drawing.ConsoleGraphics(stubbedApi.ScreenHandle, stubbedApi, size,
                                                                       new ConControls.Controls.Drawing.FrameCharSets());

            sut.DrawBackground(
                color: background,
                area: new Rectangle(-1, -1, 7, 7));

            written.Should().BeFalse();
            mainBuffer.Should().Equal(expectedBuffer);
            sut.Flush();
            written.Should().BeTrue();
            successful.Should().BeTrue();
        }
Example #26
0
        public void KeyEvent_CloseWindowKey_WindowClosed()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider)
                  {
                      CloseWindowKey = ConControls.Controls.KeyCombination.AltF4
                  };
            consoleController.KeyEventEvent(consoleController,
                                            new ConsoleKeyEventArgs(new KEY_EVENT_RECORD
            {
                ControlKeys    = ControlKeyStates.LEFT_ALT_PRESSED,
                KeyDown        = 1,
                VirtualKeyCode = VirtualKey.F4
            }));

            sut.IsDisposed.Should().BeTrue();
        }
Example #27
0
        public void CursorVisible_IntegrationTest_RespectsCaretVisibility()
        {
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            string text = string.Join(Environment.NewLine, Enumerable.Repeat("12345678901234567890", 40));

            using var window = new ConControls.Controls.ConsoleWindow(api, controller, new StubbedGraphicsProvider());
            using var sut    = new ConControls.Controls.TextBlock(window)
                  {
                      Area     = (5, 5, 10, 10).Rect(),
                      Parent   = window,
                      Text     = text,
                      Caret    = (5, 0).Pt(),
                      CanFocus = true,
                      Focused  = true
                  };

            sut.Caret.Should().Be((5, 0).Pt());
            sut.CursorVisible.Should().BeTrue();
            sut.CursorPosition.Should().Be((5, 0).Pt());
            var e = new ConsoleMouseEventArgs(new MOUSE_EVENT_RECORD
            {
                EventFlags    = MouseEventFlags.Wheeled,
                MousePosition = new COORD(5, 5),
                Scroll        = -120
            });

            bool setCorrectly = false;

            api.SetCursorInfoConsoleOutputHandleBooleanInt32Point = (handle, visible, size, location) => setCorrectly = !visible;
            controller.MouseEventEvent(controller, e);
            setCorrectly.Should().BeTrue();
            sut.Scroll.Should().Be((0, 1).Pt());
            sut.CursorVisible.Should().BeFalse();
            sut.Caret.Should().Be((5, 0).Pt());
        }
    }
Example #28
0
        public async Task ThreadManagement_ThreadStartedAndStoppedCorrectly()
        {
            TaskCompletionSource <int> startTaskSource = new TaskCompletionSource <int>();
            TaskCompletionSource <int> endTaskSource = new TaskCompletionSource <int>();
            bool threadStartLogged = false, threadEndLogged = false;

            using var logger = new TestLogger(CheckLog);

            using var api = new StubbedNativeCalls();
            var sut = new ConControls.ConsoleApi.ConsoleController(api);

            (await Task.WhenAny(startTaskSource.Task, Task.Delay(2000)))
            .Should()
            .Be(startTaskSource.Task, "Thread should be started in less than 2 seconds!");
            threadStartLogged.Should().BeTrue();
            sut.Dispose();
            (await Task.WhenAny(endTaskSource.Task, Task.Delay(2000)))
            .Should()
            .Be(endTaskSource.Task, "Thread should be stopped in less than 2 seconds!");
            threadEndLogged.Should().BeTrue();
            sut.Dispose(); // should not fail

            void CheckLog(string msg)
            {
                if (msg.Contains("Starting thread."))
                {
                    threadStartLogged = true;
                    startTaskSource.SetResult(0);
                }

                if (msg.Contains("Stopping thread"))
                {
                    threadEndLogged = true;
                    endTaskSource.SetResult(0);
                }
            }
        }
Example #29
0
        public void Draw_NotDrawnWhenInhibited()
        {
            var api = new StubbedNativeCalls();

            using var controller = new StubbedConsoleController();
            var  graphicsProvider = new StubbedGraphicsProvider();
            bool provided         = false;

            graphicsProvider.ProvideConsoleOutputHandleINativeCallsSizeFrameCharSets = (handle, calls, arg3, arg4) =>
            {
                provided = true;
                return(graphicsProvider.Graphics);
            };

            using var sut = new ConControls.Controls.ConsoleWindow(api, controller, graphicsProvider);
            provided      = false;
            using (sut.DeferDrawing())
            {
                sut.Invalidate();
                provided.Should().BeFalse();
            }

            provided.Should().BeTrue();
        }
Example #30
0
        public void KeyEvent_ShiftTab_FocusChanged()
        {
            using var consoleController = new StubbedConsoleController();
            using var api = new StubbedNativeCalls();
            var graphicsProvider = new StubbedGraphicsProvider();

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);

            var t1 = new ConControls.Controls.TextBlock(sut)
            {
                Parent = sut
            };
            var t2 = new ConControls.Controls.TextBlock(sut)
            {
                Parent = sut
            };
            var eventArgs = new ConsoleKeyEventArgs(new KEY_EVENT_RECORD
            {
                ControlKeys    = ControlKeyStates.SCROLLLOCK_ON | ControlKeyStates.SHIFT_PRESSED,
                KeyDown        = 1,
                VirtualKeyCode = VirtualKey.Tab
            });

            consoleController.KeyEventEvent(consoleController,
                                            eventArgs);
            sut.FocusedControl.Should().Be(t2);
            consoleController.KeyEventEvent(consoleController,
                                            eventArgs);
            sut.FocusedControl.Should().Be(t1);
            consoleController.KeyEventEvent(consoleController,
                                            eventArgs);
            sut.FocusedControl.Should().Be(t2);
            consoleController.KeyEventEvent(consoleController,
                                            eventArgs);
            sut.FocusedControl.Should().Be(t1);
        }