Пример #1
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();
        }
Пример #2
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();
        }
Пример #3
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>();
        }
Пример #4
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();
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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();
        }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
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());
        }
Пример #11
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();
        }
Пример #12
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();
        }
Пример #13
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());
        }
    }
Пример #14
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();
        }
Пример #15
0
        public void SizeEvents_AreaChangedRaised()
        {
            var api = new StubbedNativeCalls();

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

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

            sut.AreaChanged += OnAreaChanged;
            controller.SizeEventEvent(controller, new ConsoleSizeEventArgs(Rectangle.Empty, Size.Empty));
            raised.Should().BeTrue();
            raised           = false;
            sut.AreaChanged -= OnAreaChanged;
            controller.SizeEventEvent(controller, new ConsoleSizeEventArgs(Rectangle.Empty, Size.Empty));
            raised.Should().BeFalse();

            void OnAreaChanged(object sender, EventArgs e)
            {
                sender.Should().Be(sut);
                raised = true;
            }
        }
Пример #16
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);
        }
Пример #17
0
        public void Constructor_WindowInitialized()
        {
            bool cursorSet = false, cursorReset = false;
            bool disposing = false, controllerDisposed = false;

            using var consoleController = new StubbedConsoleController
                  {
                      Dispose = () => controllerDisposed = true
                  };
            const int originalCursorSize = 10;
            Size      windowSize         = new Size(12, 42);

            using var api = new StubbedNativeCalls
                  {
                      GetConsoleScreenBufferInfoConsoleOutputHandle = handle =>
                      {
                          handle.Should().Be(consoleController.OutputHandle);
                          return(new CONSOLE_SCREEN_BUFFER_INFOEX
                    {
                        BufferSize = new COORD(windowSize)
                    });
                      },
                      GetCursorInfoConsoleOutputHandle = handle =>
                      {
                          handle.Should().Be(consoleController.OutputHandle);
                          return(true, originalCursorSize, Point.Empty);
                      },
                      SetCursorInfoConsoleOutputHandleBooleanInt32Point = (handle, visible, size, position) =>
                      {
                          handle.Should().Be(consoleController.OutputHandle);
                          if (disposing)
                          {
                              cursorSet.Should().BeTrue();
                              cursorReset.Should().BeFalse();
                              visible.Should().BeTrue();
                              size.Should().Be(originalCursorSize);
                              position.Should().Be(Point.Empty);
                              cursorReset = true;
                              return;
                          }

                          visible.Should().BeFalse();
                          size.Should().Be(originalCursorSize);
                          position.Should().Be(Point.Empty);
                          cursorSet = true;
                      }
                  };
            bool drawn    = false;
            var  graphics = new StubIConsoleGraphics
            {
                DrawBackgroundConsoleColorRectangle = (color, rectangle) =>
                {
                    drawn.Should().BeFalse();
                    color.Should().Be(ConsoleColor.Black);
                    rectangle.Should().Be(new Rectangle(Point.Empty, windowSize));
                    drawn = true;
                }
            };
            var graphicsProvider = new StubbedGraphicsProvider
            {
                ProvideConsoleOutputHandleINativeCallsSizeFrameCharSets = (handle, consoleApi, size, frameCharSets) =>
                {
                    handle.Should().Be(consoleController.OutputHandle);
                    consoleApi.Should().Be(api);
                    size.Should().Be(windowSize);
                    frameCharSets.Should().BeOfType <FrameCharSets>();
                    return(graphics);
                }
            };

            using var sut = new ConControls.Controls.ConsoleWindow(api, consoleController, graphicsProvider);
            consoleController.FocusEventEvent.Should().NotBeNull();
            consoleController.KeyEventEvent.Should().NotBeNull();
            consoleController.MenuEventEvent.Should().NotBeNull();
            consoleController.MouseEventEvent.Should().NotBeNull();
            consoleController.SizeEventEvent.Should().NotBeNull();

            sut.Controls.Should().NotBeNull();
            cursorSet.Should().BeTrue();

            drawn.Should().BeTrue();

            controllerDisposed.Should().BeFalse();
            disposing = true;
            sut.Dispose();
            consoleController.FocusEventEvent.Should().BeNull();
            consoleController.KeyEventEvent.Should().BeNull();
            consoleController.MenuEventEvent.Should().BeNull();
            consoleController.MouseEventEvent.Should().BeNull();
            consoleController.SizeEventEvent.Should().BeNull();
            cursorReset.Should().BeTrue();
            controllerDisposed.Should().BeTrue();
        }
    }