mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-30 09:47:58 +01:00
* Initial plan * Add ScreenChanged event, SetScreenSize method, and fix FakeDriver buffer initialization Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add comprehensive tests for ScreenChanged event and buffer integrity Co-authored-by: tig <585482+tig@users.noreply.github.com> * Replace obsolete SizeChanged usage with ScreenChanged in core and tests Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor terminal size event handling Replaced `ScreenChanged` with `SizeChanged` across the codebase to standardize naming and improve clarity. Updated event handling logic, including subscriptions, unsubscriptions, and raising methods. Removed deprecated `ScreenChanged` event and backward compatibility code. Refactored driver initialization to handle legacy `IConsoleDriver` types separately. Updated tests and mock implementations to align with the new `SizeChanged` event. Improved documentation and comments to reflect these changes. These updates enhance maintainability, consistency, and modernize the architecture. * Refactor & Code Cleanup: Replace IWindowSizeMonitor with IConsoleSizeMonitor Renamed `IWindowSizeMonitor` to `IConsoleSizeMonitor` across the codebase, updating all references, method signatures, and event handlers. Replaced the `WindowSizeMonitor` class with the new `ConsoleSizeMonitor` implementation, which includes improved terminal size change handling via the `Poll` method. Enabled nullable reference types in several files to enhance code safety. Updated test cases to reflect the new `IConsoleSizeMonitor` interface. Removed redundant code, simplified null checks, and corrected minor typos in comments. Streamlined the codebase by removing the obsolete `WindowSizeMonitor` class and its interface. * Code cleanup - Refactor and enhance ShadowView and FakeDriverTests Updated ShadowView.cs to use null-conditional operators and added null checks for safer access to `ScreenContents`. Refined XML documentation in View.Layout.cs for clarity and consistency. Refactored FakeDriverTests.cs to leverage modern C# features, including shorthand object instantiation, inline lambdas, and tuple-like syntax for `Size` and `Rectangle`. Removed redundant tests and improved test readability and reliability. Enhanced error handling with null checks and ensured backward compatibility for deprecated events. Improved test coverage for resizing, clipboard operations, and invalid coordinates. Verified buffer integrity and screen updates after resizing. General improvements include replacing explicit type declarations with `var`, removing unused imports, and aligning code formatting for better readability. Refactor and improve code quality and test coverage Updated `ShadowView` for null safety using null-conditional operators. Simplified object initializations and modernized syntax across the codebase, including shorthand initializations and inline lambdas. Enhanced event handling logic and ensured compatibility with obsolete members. Refactored `FakeDriverTests` by removing redundant code, standardizing formatting, and improving test setup. Suppressed obsolete warnings where necessary. Improved XML documentation in `View.Layout.cs` for clarity and removed outdated references. Performed general cleanup, including removing unused namespaces, redundant comments, and ensuring consistent formatting. These changes enhance readability, maintainability, and runtime safety. * Code cleanup Refactor TimedEventsTests for readability and consistency Improved code readability and maintainability: - Enabled nullable reference types with `#nullable enable`. - Removed unused `using System.Diagnostics;`. - Updated namespace to `UnitTests.ApplicationTests`. - Replaced `Terminal.Gui.App.TimedEvents` with `TimedEvents`. - Reformatted XML documentation comments for alignment. - Used `var` and target-typed new expressions for consistency. - Reformatted `Parallel.For` loops and lambdas for clarity. - Added `Thread.Sleep(10)` to prevent excessive CPU usage in tests. - Improved assertions and event handler formatting in tests. Aligned with modern C# coding practices. * Code Cleanup - No more driver warnings. Refactor codebase and introduce FakeClipboard - Adjusted `.editorconfig` to change severity levels for CS0612, CS0618, and CS0672 diagnostics. - Replaced `FakeDriver.FakeClipboard` with a new `FakeClipboard` class for testing purposes, supporting exception handling and clipboard data manipulation. - Removed redundant methods (`MakeColor`, `MapKey`) and unused classes (`MockConsoleDriver`) to streamline the codebase. - Refactored `ConsoleDriverFacade` and `FakeDriver` to simplify logic and improve maintainability. - Updated tests to use `CreateFakeDriver` and removed or commented out obsolete tests. - Reformatted and cleaned up code for readability across multiple files. * Refactor FakeDriver - Code Cleanup Standardized console size management by replacing `WindowSizeMonitor` with `ConsoleSizeMonitor` across the codebase. Updated methods `GetWindowSize` and `SetWindowSize` to `GetSize` and `SetSize` for consistency. Refactored `FakeDriver` to use `SetScreenSize` and removed redundant methods. Simplified driver initialization by removing legacy `InternalInit` logic. Standardized ANSI escape sequences by replacing `CSI_ReportTerminalSizeInChars` with `CSI_ReportWindowSizeInChars`. Updated test cases to align with the new `ConsoleSizeMonitor` and `SetScreenSize` methods. Removed obsolete test utilities like `FakeSizeMonitor` and `FakeWindowSizeMonitor`. Performed general code cleanup, including removing unused classes, redundant code, and improving formatting. Fixed resizing logic issues and improved exception handling in driver methods. * Update Terminal.Gui/Drivers/OutputBuffer.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/Drivers/MouseButtonStateEx.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/App/MainLoop/IApplicationMainLoop.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Tests/UnitTests/Views/ToplevelTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update Terminal.Gui/ViewBase/View.Layout.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Moved all Drawing tests to Paralleizable - proving Fakedriver works Enhanced `Ruler` and `Thickness` classes by adding an optional `IConsoleDriver? driver` parameter to decouple rendering from the default `Application.Driver`, improving flexibility and testability. Updated `DriverAssert` to support nullable drivers and added defensive checks. Refactored and expanded test cases for `Ruler`, `Thickness`, `LineCanvas`, and `StraightLineExtensions` to ensure comprehensive coverage, including zero-length intersections, line rendering, and exclusion logic. Migrated rendering-dependent tests to a parallelizable namespace. Removed redundant tests, improved naming conventions, and updated documentation for better maintainability and clarity. * Fixed Run<T> startup hang. Refactor: Simplify driver logic and update SetSize methods Removed FakeDriver safeguard in unit tests to simplify CreateDriver logic. Updated SetSize methods in NetOutput, UnixOutput, and WindowsOutput to do nothing instead of throwing NotImplementedException. Modified SizeChanged event in ConsoleDriverFacade to call SetScreenSize directly. Commented out unnecessary debug validation in DimAuto. These changes improve maintainability and reduce complexity. * Fixed intermittent unit test bug. Refactored `_cachedRunStateToplevel` to `CachedRunStateToplevel` as an internal static property, delegating its management to `ApplicationImpl` for improved encapsulation. Updated all references to use the new property and centralized its handling in `ApplicationImpl`. Removed the `MouseGrabHandler` property from `ApplicationImpl` and simplified driver-related assignments by replacing `Application.ForceDriver` and `Application.Screen` with direct references. Reset `CachedRunStateToplevel` during cleanup to ensure proper state management. Updated the `Invoke` method to use `Top` directly, aligning with the refactored design. Improved debug assertions and performed general cleanup to enhance code readability and maintainability. * Fixed intermittent bug an massive code cleanup of warnings. Refactor and enhance codebase for maintainability - Applied null-conditional operator (`!`) to improve null-safety. - Refactored tests for clarity, added new cases, and removed redundancies. - Introduced `FakeApplicationFactory` and `FakeSizeMonitor` for testing. - Removed unused code, including legacy `DecodeEscSeq` logic. - Reformatted code for readability and consistency. - Updated assertions for more accurate validation in tests. - Fixed potential null reference issues across multiple files. - Improved event handling with proper null checks. - Enhanced documentation for new classes and methods. - Modernized code with C# features like `record struct` and `nullable enable`. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com> Co-authored-by: Tig <tig@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
906 lines
35 KiB
C#
906 lines
35 KiB
C#
namespace UnitTests.ViewsTests;
|
|
|
|
public class ToplevelTests
|
|
{
|
|
[Fact]
|
|
public void Constructor_Default ()
|
|
{
|
|
var top = new Toplevel ();
|
|
|
|
Assert.Equal ("Toplevel", top.SchemeName);
|
|
Assert.Equal ("Fill(Absolute(0))", top.Width.ToString ());
|
|
Assert.Equal ("Fill(Absolute(0))", top.Height.ToString ());
|
|
Assert.False (top.Running);
|
|
Assert.False (top.Modal);
|
|
Assert.Null (top.MenuBar);
|
|
|
|
//Assert.Null (top.StatusBar);
|
|
}
|
|
|
|
[Fact]
|
|
public void Arrangement_Default_Is_Overlapped ()
|
|
{
|
|
var top = new Toplevel ();
|
|
Assert.Equal (ViewArrangement.Overlapped, top.Arrangement);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Internal_Tests ()
|
|
{
|
|
var top = new Toplevel ();
|
|
|
|
var eventInvoked = "";
|
|
|
|
top.Loaded += (s, e) => eventInvoked = "Loaded";
|
|
top.OnLoaded ();
|
|
Assert.Equal ("Loaded", eventInvoked);
|
|
top.Ready += (s, e) => eventInvoked = "Ready";
|
|
top.OnReady ();
|
|
Assert.Equal ("Ready", eventInvoked);
|
|
top.Unloaded += (s, e) => eventInvoked = "Unloaded";
|
|
top.OnUnloaded ();
|
|
Assert.Equal ("Unloaded", eventInvoked);
|
|
|
|
top.Add (new MenuBar ());
|
|
Assert.NotNull (top.MenuBar);
|
|
|
|
//top.Add (new StatusBar ());
|
|
//Assert.NotNull (top.StatusBar);
|
|
MenuBar menuBar = top.MenuBar;
|
|
top.Remove (top.MenuBar);
|
|
Assert.Null (top.MenuBar);
|
|
Assert.NotNull (menuBar);
|
|
|
|
//var statusBar = top.StatusBar;
|
|
//top.Remove (top.StatusBar);
|
|
//Assert.Null (top.StatusBar);
|
|
//Assert.NotNull (statusBar);
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.False (menuBar.WasDisposed);
|
|
|
|
//Assert.False (statusBar.WasDisposed);
|
|
menuBar.Dispose ();
|
|
|
|
//statusBar.Dispose ();
|
|
Assert.True (menuBar.WasDisposed);
|
|
|
|
//Assert.True (statusBar.WasDisposed);
|
|
#endif
|
|
|
|
Application.Begin (top);
|
|
Assert.Equal (top, Application.Top);
|
|
|
|
// Application.Top without menu and status bar.
|
|
View supView = View.GetLocationEnsuringFullVisibility (top, 2, 2, out int nx, out int ny /*, out StatusBar sb*/);
|
|
Assert.Equal (Application.Top, supView);
|
|
Assert.Equal (0, nx);
|
|
Assert.Equal (0, ny);
|
|
|
|
//Assert.Null (sb);
|
|
|
|
top.Add (new MenuBar ());
|
|
Assert.NotNull (top.MenuBar);
|
|
|
|
// Application.Top with a menu and without status bar.
|
|
View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
Assert.Equal (1, ny);
|
|
|
|
//Assert.Null (sb);
|
|
|
|
//top.Add (new StatusBar ());
|
|
//Assert.NotNull (top.StatusBar);
|
|
|
|
// Application.Top with a menu and status bar.
|
|
View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
|
|
// The available height is lower than the Application.Top height minus
|
|
// the menu bar and status bar, then the top can go beyond the bottom
|
|
// Assert.Equal (2, ny);
|
|
//Assert.NotNull (sb);
|
|
|
|
menuBar = top.MenuBar;
|
|
top.Remove (top.MenuBar);
|
|
Assert.Null (top.MenuBar);
|
|
Assert.NotNull (menuBar);
|
|
|
|
// Application.Top without a menu and with a status bar.
|
|
View.GetLocationEnsuringFullVisibility (top, 2, 2, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
|
|
// The available height is lower than the Application.Top height minus
|
|
// the status bar, then the top can go beyond the bottom
|
|
// Assert.Equal (2, ny);
|
|
//Assert.NotNull (sb);
|
|
|
|
//statusBar = top.StatusBar;
|
|
//top.Remove (top.StatusBar);
|
|
//Assert.Null (top.StatusBar);
|
|
//Assert.NotNull (statusBar);
|
|
Assert.Null (top.MenuBar);
|
|
|
|
var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
|
|
top.Add (win);
|
|
top.LayoutSubViews ();
|
|
|
|
// The SuperView is always the same regardless of the caller.
|
|
supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (Application.Top, supView);
|
|
supView = View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (Application.Top, supView);
|
|
|
|
// Application.Top without menu and status bar.
|
|
View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
Assert.Equal (0, ny);
|
|
|
|
//Assert.Null (sb);
|
|
|
|
top.Add (new MenuBar ());
|
|
Assert.NotNull (top.MenuBar);
|
|
|
|
// Application.Top with a menu and without status bar.
|
|
View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
Assert.Equal (1, ny);
|
|
|
|
//Assert.Null (sb);
|
|
|
|
top.Add (new StatusBar ());
|
|
|
|
//Assert.NotNull (top.StatusBar);
|
|
|
|
// Application.Top with a menu and status bar.
|
|
View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
|
|
// The available height is lower than the Application.Top height minus
|
|
// the menu bar and status bar, then the top can go beyond the bottom
|
|
//Assert.Equal (20, ny);
|
|
//Assert.NotNull (sb);
|
|
|
|
menuBar = top.MenuBar;
|
|
|
|
//statusBar = top.StatusBar;
|
|
top.Remove (top.MenuBar);
|
|
Assert.Null (top.MenuBar);
|
|
Assert.NotNull (menuBar);
|
|
|
|
//top.Remove (top.StatusBar);
|
|
//Assert.Null (top.StatusBar);
|
|
//Assert.NotNull (statusBar);
|
|
|
|
top.Remove (win);
|
|
|
|
win = new () { Width = 60, Height = 15 };
|
|
top.Add (win);
|
|
|
|
// Application.Top without menu and status bar.
|
|
View.GetLocationEnsuringFullVisibility (win, 0, 0, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (0, nx);
|
|
Assert.Equal (0, ny);
|
|
|
|
//Assert.Null (sb);
|
|
|
|
top.Add (new MenuBar ());
|
|
Assert.NotNull (top.MenuBar);
|
|
|
|
// Application.Top with a menu and without status bar.
|
|
View.GetLocationEnsuringFullVisibility (win, 2, 2, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (2, nx);
|
|
Assert.Equal (2, ny);
|
|
|
|
//Assert.Null (sb);
|
|
|
|
top.Add (new StatusBar ());
|
|
|
|
//Assert.NotNull (top.StatusBar);
|
|
|
|
// Application.Top with a menu and status bar.
|
|
View.GetLocationEnsuringFullVisibility (win, 30, 20, out nx, out ny /*, out sb*/);
|
|
Assert.Equal (20, nx); // 20+60=80
|
|
|
|
//Assert.Equal (9, ny); // 9+15+1(mb)=25
|
|
//Assert.NotNull (sb);
|
|
|
|
//Assert.Null (Toplevel._dragPosition);
|
|
win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
|
|
|
|
// Assert.Equal (new Point (6, 0), Toplevel._dragPosition);
|
|
win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Released });
|
|
|
|
//Assert.Null (Toplevel._dragPosition);
|
|
win.CanFocus = false;
|
|
win.NewMouseEvent (new () { Position = new (6, 0), Flags = MouseFlags.Button1Pressed });
|
|
|
|
//Assert.Null (Toplevel._dragPosition);
|
|
#if DEBUG_IDISPOSABLE
|
|
|
|
Assert.False (top.MenuBar.WasDisposed);
|
|
|
|
//Assert.False (top.StatusBar.WasDisposed);
|
|
#endif
|
|
menuBar = top.MenuBar;
|
|
|
|
//statusBar = top.StatusBar;
|
|
top.Dispose ();
|
|
Assert.Null (top.MenuBar);
|
|
|
|
//Assert.Null (top.StatusBar);
|
|
Assert.NotNull (menuBar);
|
|
|
|
//Assert.NotNull (statusBar);
|
|
#if DEBUG_IDISPOSABLE
|
|
Assert.True (menuBar.WasDisposed);
|
|
|
|
//Assert.True (statusBar.WasDisposed);
|
|
#endif
|
|
}
|
|
|
|
[Fact]
|
|
public void SuperViewChanged_Should_Not_Be_Used_To_Initialize_Toplevel_Events ()
|
|
{
|
|
var wasAdded = false;
|
|
|
|
var view = new View ();
|
|
view.SuperViewChanged += SuperViewChanged;
|
|
|
|
var win = new Window ();
|
|
win.Add (view);
|
|
Application.Init (new FakeDriver ());
|
|
Toplevel top = new ();
|
|
top.Add (win);
|
|
|
|
Assert.True (wasAdded);
|
|
|
|
Application.Shutdown ();
|
|
|
|
return;
|
|
|
|
void SuperViewChanged (object sender, SuperViewChangedEventArgs _)
|
|
{
|
|
Assert.False (wasAdded);
|
|
wasAdded = true;
|
|
view.SuperViewChanged -= SuperViewChanged;
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Mouse_Drag_On_Top_With_Superview_Null ()
|
|
{
|
|
var win = new Window ();
|
|
Toplevel top = new ();
|
|
top.Add (win);
|
|
int iterations = -1;
|
|
Window testWindow;
|
|
|
|
Application.Iteration += (s, a) =>
|
|
{
|
|
iterations++;
|
|
|
|
if (iterations == 0)
|
|
{
|
|
Application.Driver?.SetScreenSize (15, 7);
|
|
|
|
// Don't use MessageBox here; it's too complicated for this unit test; just use Window
|
|
testWindow = new ()
|
|
{
|
|
Text = "Hello",
|
|
X = 2,
|
|
Y = 2,
|
|
Width = 10,
|
|
Height = 3,
|
|
Arrangement = ViewArrangement.Movable
|
|
};
|
|
Application.Run (testWindow);
|
|
}
|
|
else if (iterations == 1)
|
|
{
|
|
Assert.Equal (new (2, 2), Application.Top!.Frame.Location);
|
|
}
|
|
else if (iterations == 2)
|
|
{
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Grab the mouse
|
|
Application.RaiseMouseEvent (new () { ScreenPosition = new (3, 2), Flags = MouseFlags.Button1Pressed });
|
|
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (2, 2, 10, 3), Application.Top.Frame);
|
|
}
|
|
else if (iterations == 3)
|
|
{
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
|
|
// Drag to left
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (2, 2), Flags = MouseFlags.Button1Pressed
|
|
| MouseFlags.ReportMousePosition
|
|
});
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
|
|
Assert.Equal (Application.Top.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (1, 2, 10, 3), Application.Top.Frame);
|
|
}
|
|
else if (iterations == 4)
|
|
{
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (1, 2), Application.Top.Frame.Location);
|
|
|
|
Assert.Equal (Application.Top.Border, Application.Mouse.MouseGrabView);
|
|
}
|
|
else if (iterations == 5)
|
|
{
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
|
|
// Drag up
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (2, 1),
|
|
Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
|
|
}
|
|
else if (iterations == 6)
|
|
{
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (1, 1), Application.Top.Frame.Location);
|
|
|
|
Assert.Equal (Application.Top.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (1, 1, 10, 3), Application.Top.Frame);
|
|
}
|
|
else if (iterations == 7)
|
|
{
|
|
Assert.Equal (Application.Top!.Border, Application.Mouse.MouseGrabView);
|
|
|
|
// Ungrab the mouse
|
|
Application.RaiseMouseEvent (new () { ScreenPosition = new (2, 1), Flags = MouseFlags.Button1Released });
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
}
|
|
else if (iterations == 8)
|
|
{
|
|
Application.RequestStop ();
|
|
}
|
|
else if (iterations == 9)
|
|
{
|
|
Application.RequestStop ();
|
|
}
|
|
};
|
|
|
|
Application.Run (top);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Mouse_Drag_On_Top_With_Superview_Not_Null ()
|
|
{
|
|
var win = new Window { X = 3, Y = 2, Width = 10, Height = 5, Arrangement = ViewArrangement.Movable };
|
|
Toplevel top = new ();
|
|
top.Add (win);
|
|
|
|
int iterations = -1;
|
|
|
|
var movex = 0;
|
|
var movey = 0;
|
|
|
|
var location = new Rectangle (win.Frame.X, win.Frame.Y, 7, 3);
|
|
|
|
Application.Iteration += (s, a) =>
|
|
{
|
|
iterations++;
|
|
|
|
if (iterations == 0)
|
|
{
|
|
Application.Driver?.SetScreenSize (30, 10);
|
|
}
|
|
else if (iterations == 1)
|
|
{
|
|
location = win.Frame;
|
|
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
// Grab the mouse
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (win.Frame.X, win.Frame.Y), Flags = MouseFlags.Button1Pressed
|
|
});
|
|
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
}
|
|
else if (iterations == 2)
|
|
{
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
|
|
// Drag to left
|
|
movex = 1;
|
|
movey = 0;
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
|
|
MouseFlags.Button1Pressed
|
|
| MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
}
|
|
else if (iterations == 3)
|
|
{
|
|
// we should have moved +1, +0
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
location.Offset (movex, movey);
|
|
}
|
|
else if (iterations == 4)
|
|
{
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
|
|
// Drag up
|
|
movex = 0;
|
|
movey = -1;
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey), Flags =
|
|
MouseFlags.Button1Pressed
|
|
| MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
}
|
|
else if (iterations == 5)
|
|
{
|
|
// we should have moved +0, -1
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
location.Offset (movex, movey);
|
|
Assert.Equal (location, win.Frame);
|
|
}
|
|
else if (iterations == 6)
|
|
{
|
|
Assert.Equal (win.Border, Application.Mouse.MouseGrabView);
|
|
|
|
// Ungrab the mouse
|
|
movex = 0;
|
|
movey = 0;
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (win.Frame.X + movex, win.Frame.Y + movey),
|
|
Flags = MouseFlags.Button1Released
|
|
});
|
|
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
}
|
|
else if (iterations == 7)
|
|
{
|
|
Application.RequestStop ();
|
|
}
|
|
};
|
|
|
|
Application.Run (top);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[SetupFakeDriver]
|
|
public void GetLocationThatFits_With_Border_Null_Not_Throws ()
|
|
{
|
|
var top = new Toplevel ();
|
|
top.BeginInit ();
|
|
top.EndInit ();
|
|
|
|
Exception exception = Record.Exception (() => Application.Driver!.SetScreenSize (0, 10));
|
|
Assert.Null (exception);
|
|
|
|
exception = Record.Exception (() => Application.Driver!.SetScreenSize (10, 0));
|
|
Assert.Null (exception);
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void PositionCursor_SetCursorVisibility_To_Invisible_If_Focused_Is_Null ()
|
|
{
|
|
var tf = new TextField { Width = 5, Text = "test" };
|
|
var view = new View { Width = 10, Height = 10, CanFocus = true };
|
|
view.Add (tf);
|
|
var top = new Toplevel ();
|
|
top.Add (view);
|
|
Application.Begin (top);
|
|
|
|
Assert.True (tf.HasFocus);
|
|
Application.PositionCursor ();
|
|
Application.Driver!.GetCursorVisibility (out CursorVisibility cursor);
|
|
Assert.Equal (CursorVisibility.Default, cursor);
|
|
|
|
view.Enabled = false;
|
|
Assert.False (tf.HasFocus);
|
|
Application.PositionCursor ();
|
|
Application.Driver!.GetCursorVisibility (out cursor);
|
|
Assert.Equal (CursorVisibility.Invisible, cursor);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void IsLoaded_Application_Begin ()
|
|
{
|
|
Toplevel top = new ();
|
|
Assert.False (top.IsLoaded);
|
|
|
|
Application.Begin (top);
|
|
Assert.True (top.IsLoaded);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void IsLoaded_With_Sub_Toplevel_Application_Begin_NeedDisplay ()
|
|
{
|
|
Toplevel top = new ();
|
|
var subTop = new Toplevel ();
|
|
var view = new View { Frame = new (0, 0, 20, 10) };
|
|
subTop.Add (view);
|
|
top.Add (subTop);
|
|
|
|
Assert.False (top.IsLoaded);
|
|
Assert.False (subTop.IsLoaded);
|
|
Assert.Equal (new (0, 0, 20, 10), view.Frame);
|
|
|
|
view.SubViewLayout += ViewLayoutStarted;
|
|
|
|
void ViewLayoutStarted (object sender, LayoutEventArgs e)
|
|
{
|
|
Assert.Equal (new (0, 0, 20, 10), view.NeedsDrawRect);
|
|
view.SubViewLayout -= ViewLayoutStarted;
|
|
}
|
|
|
|
Application.Begin (top);
|
|
|
|
Assert.True (top.IsLoaded);
|
|
Assert.True (subTop.IsLoaded);
|
|
Assert.Equal (new (0, 0, 20, 10), view.Frame);
|
|
|
|
view.Frame = new (1, 3, 10, 5);
|
|
Assert.Equal (new (1, 3, 10, 5), view.Frame);
|
|
Assert.Equal (new (0, 0, 10, 5), view.NeedsDrawRect);
|
|
|
|
view.Frame = new (1, 3, 10, 5);
|
|
top.Layout ();
|
|
Assert.Equal (new (1, 3, 10, 5), view.Frame);
|
|
Assert.Equal (new (0, 0, 10, 5), view.NeedsDrawRect);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Window_Viewport_Bigger_Than_Driver_Cols_And_Rows_Allow_Drag_Beyond_Left_Right_And_Bottom ()
|
|
{
|
|
Toplevel top = new ();
|
|
var window = new Window { Width = 20, Height = 3, Arrangement = ViewArrangement.Movable };
|
|
RunState rsTop = Application.Begin (top);
|
|
Application.Driver?.SetScreenSize (40, 10);
|
|
|
|
RunState rsWindow = Application.Begin (window);
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (new (0, 0, 40, 10), top.Frame);
|
|
Assert.Equal (new (0, 0, 20, 3), window.Frame);
|
|
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
|
|
Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
|
|
|
|
Assert.Equal (window.Border, Application.Mouse.MouseGrabView);
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (-11, -4), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (new (0, 0, 40, 10), top.Frame);
|
|
Assert.Equal (new (-11, -4, 20, 3), window.Frame);
|
|
|
|
// Changes Top size to same size as Dialog more menu and scroll bar
|
|
Application.Driver?.SetScreenSize (20, 3);
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (new (0, 0, 20, 3), top.Frame);
|
|
Assert.Equal (new (-1, -1, 20, 3), window.Frame);
|
|
|
|
// Changes Top size smaller than Dialog size
|
|
Application.Driver?.SetScreenSize (19, 2);
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (-1, -1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (new (0, 0, 19, 2), top.Frame);
|
|
Assert.Equal (new (-1, -1, 20, 3), window.Frame);
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (18, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (new (0, 0, 19, 2), top.Frame);
|
|
Assert.Equal (new (18, 1, 20, 3), window.Frame);
|
|
|
|
// On a real app we can't go beyond the SuperView bounds
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (19, 2), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (new (0, 0, 19, 2), top.Frame);
|
|
Assert.Equal (new (19, 2, 20, 3), window.Frame);
|
|
|
|
//DriverAsserts.AssertDriverContentsWithFrameAre (@"", output);
|
|
|
|
Application.End (rsWindow);
|
|
Application.End (rsTop);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Modal_As_Top_Will_Drag_Cleanly ()
|
|
{
|
|
// Don't use Dialog as a Top, use a Window instead - dialog has complex layout behavior that is not needed here.
|
|
var window = new Window { Width = 10, Height = 3, Arrangement = ViewArrangement.Movable };
|
|
|
|
window.Add (
|
|
new Label
|
|
{
|
|
X = Pos.Center (),
|
|
Y = Pos.Center (),
|
|
Width = Dim.Fill (),
|
|
Height = Dim.Fill (),
|
|
TextAlignment = Alignment.Center,
|
|
VerticalTextAlignment = Alignment.Center,
|
|
Text = "Test"
|
|
}
|
|
);
|
|
|
|
RunState rs = Application.Begin (window);
|
|
|
|
Assert.Null (Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (0, 0, 10, 3), window.Frame);
|
|
|
|
Application.RaiseMouseEvent (new () { ScreenPosition = new (0, 0), Flags = MouseFlags.Button1Pressed });
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (window.Border, Application.Mouse.MouseGrabView);
|
|
|
|
Assert.Equal (new (0, 0, 10, 3), window.Frame);
|
|
|
|
Application.RaiseMouseEvent (
|
|
new ()
|
|
{
|
|
ScreenPosition = new (1, 1), Flags = MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition
|
|
});
|
|
|
|
AutoInitShutdownAttribute.RunIteration ();
|
|
Assert.Equal (window.Border, Application.Mouse.MouseGrabView);
|
|
Assert.Equal (new (1, 1, 10, 3), window.Frame);
|
|
|
|
Application.End (rs);
|
|
window.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
[AutoInitShutdown]
|
|
public void Activating_MenuBar_By_Alt_Key_Does_Not_Throw ()
|
|
{
|
|
var menu = new MenuBar
|
|
{
|
|
Menus =
|
|
[
|
|
new ("Child", new MenuItem [] { new ("_Create Child", "", null) })
|
|
]
|
|
};
|
|
var topChild = new Toplevel ();
|
|
topChild.Add (menu);
|
|
var top = new Toplevel ();
|
|
top.Add (topChild);
|
|
Application.Begin (top);
|
|
|
|
Exception exception = Record.Exception (() => topChild.NewKeyDownEvent (KeyCode.AltMask));
|
|
Assert.Null (exception);
|
|
top.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Multi_Thread_Toplevels ()
|
|
{
|
|
Application.Init (new FakeDriver ());
|
|
|
|
Toplevel t = new ();
|
|
var w = new Window ();
|
|
t.Add (w);
|
|
|
|
int count = 0, count1 = 0, count2 = 0;
|
|
bool log = false, log1 = false, log2 = false;
|
|
var fromTopStillKnowFirstIsRunning = false;
|
|
var fromTopStillKnowSecondIsRunning = false;
|
|
var fromFirstStillKnowSecondIsRunning = false;
|
|
|
|
Application.AddTimeout (
|
|
TimeSpan.FromMilliseconds (100),
|
|
() =>
|
|
{
|
|
count++;
|
|
|
|
if (count1 == 5)
|
|
{
|
|
log1 = true;
|
|
}
|
|
|
|
if (count1 == 14 && count2 == 10 && count == 15)
|
|
{
|
|
// count2 is already stopped
|
|
fromTopStillKnowFirstIsRunning = true;
|
|
}
|
|
|
|
if (count1 == 7 && count2 == 7 && count == 8)
|
|
{
|
|
fromTopStillKnowSecondIsRunning = true;
|
|
}
|
|
|
|
if (count == 30)
|
|
{
|
|
Assert.Equal (30, count);
|
|
Assert.Equal (20, count1);
|
|
Assert.Equal (10, count2);
|
|
|
|
Assert.True (log);
|
|
Assert.True (log1);
|
|
Assert.True (log2);
|
|
|
|
Assert.True (fromTopStillKnowFirstIsRunning);
|
|
Assert.True (fromTopStillKnowSecondIsRunning);
|
|
Assert.True (fromFirstStillKnowSecondIsRunning);
|
|
|
|
Application.RequestStop ();
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
);
|
|
|
|
t.Ready += FirstWindow;
|
|
|
|
void FirstWindow (object sender, EventArgs args)
|
|
{
|
|
var firstWindow = new Window ();
|
|
firstWindow.Ready += SecondWindow;
|
|
|
|
Application.AddTimeout (
|
|
TimeSpan.FromMilliseconds (100),
|
|
() =>
|
|
{
|
|
count1++;
|
|
|
|
if (count2 == 5)
|
|
{
|
|
log2 = true;
|
|
}
|
|
|
|
if (count2 == 4 && count1 == 5 && count == 5)
|
|
{
|
|
fromFirstStillKnowSecondIsRunning = true;
|
|
}
|
|
|
|
if (count1 == 20)
|
|
{
|
|
Assert.Equal (20, count1);
|
|
Application.RequestStop ();
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
);
|
|
|
|
Application.Run (firstWindow);
|
|
firstWindow.Dispose ();
|
|
}
|
|
|
|
void SecondWindow (object sender, EventArgs args)
|
|
{
|
|
var testWindow = new Window ();
|
|
|
|
Application.AddTimeout (
|
|
TimeSpan.FromMilliseconds (100),
|
|
() =>
|
|
{
|
|
count2++;
|
|
|
|
if (count < 30)
|
|
{
|
|
log = true;
|
|
}
|
|
|
|
if (count2 == 10)
|
|
{
|
|
Assert.Equal (10, count2);
|
|
Application.RequestStop ();
|
|
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
);
|
|
|
|
Application.Run (testWindow);
|
|
testWindow.Dispose ();
|
|
}
|
|
|
|
Application.Run (t);
|
|
t.Dispose ();
|
|
Application.Shutdown ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Remove_Do_Not_Dispose_MenuBar_Or_StatusBar ()
|
|
{
|
|
var mb = new MenuBar ();
|
|
var sb = new StatusBar ();
|
|
var tl = new Toplevel ();
|
|
|
|
#if DEBUG
|
|
Assert.False (mb.WasDisposed);
|
|
Assert.False (sb.WasDisposed);
|
|
#endif
|
|
tl.Add (mb, sb);
|
|
Assert.NotNull (tl.MenuBar);
|
|
|
|
//Assert.NotNull (tl.StatusBar);
|
|
#if DEBUG
|
|
Assert.False (mb.WasDisposed);
|
|
Assert.False (sb.WasDisposed);
|
|
#endif
|
|
tl.RemoveAll ();
|
|
Assert.Null (tl.MenuBar);
|
|
|
|
//Assert.Null (tl.StatusBar);
|
|
#if DEBUG
|
|
Assert.False (mb.WasDisposed);
|
|
Assert.False (sb.WasDisposed);
|
|
#endif
|
|
}
|
|
}
|