mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-28 00:38:00 +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>
524 lines
17 KiB
C#
524 lines
17 KiB
C#
#nullable enable
|
|
using UnitTests.Parallelizable;
|
|
using Xunit;
|
|
|
|
namespace UnitTests_Parallelizable.ViewTests;
|
|
|
|
[Trait ("Category", "View.Scheme")]
|
|
public class SchemeTests : ParallelizableBase
|
|
{
|
|
|
|
[Fact]
|
|
public void GetScheme_Default_ReturnsBaseScheme ()
|
|
{
|
|
var view = new View ();
|
|
var baseScheme = SchemeManager.GetHardCodedSchemes ()? ["Base"];
|
|
|
|
Assert.Equal (baseScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void SetScheme_Explicitly_SetsSchemeCorrectly ()
|
|
{
|
|
var view = new View ();
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
|
|
view.SetScheme (dialogScheme);
|
|
|
|
Assert.True (view.HasScheme);
|
|
Assert.Equal (dialogScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetScheme_InheritsFromSuperView_WhenNotExplicitlySet ()
|
|
{
|
|
var superView = new View ();
|
|
var subView = new View ();
|
|
|
|
superView.Add (subView);
|
|
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
superView.SetScheme (dialogScheme);
|
|
|
|
Assert.Equal (dialogScheme, subView.GetScheme ());
|
|
Assert.False (subView.HasScheme);
|
|
|
|
subView.Dispose ();
|
|
superView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void SetSchemeName_OverridesInheritedScheme ()
|
|
{
|
|
var view = new View ();
|
|
view.SchemeName = "Dialog";
|
|
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
Assert.Equal (dialogScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttribute_ReturnsCorrectAttribute_Via_Mock ()
|
|
{
|
|
var view = new View { SchemeName = "Base" };
|
|
view.Driver = CreateFakeDriver ();
|
|
view.Driver.SetAttribute (new Attribute (Color.Red, Color.Green));
|
|
|
|
// Act
|
|
var attribute = view.GetCurrentAttribute ();
|
|
|
|
// Assert
|
|
Assert.Equal (new Attribute (Color.Red, Color.Green), attribute);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_ReturnsCorrectAttribute ()
|
|
{
|
|
var view = new View { SchemeName = "Base" };
|
|
|
|
Assert.Equal (view.GetScheme ().Normal, view.GetAttributeForRole (VisualRole.Normal));
|
|
Assert.Equal (view.GetScheme ().HotNormal, view.GetAttributeForRole (VisualRole.HotNormal));
|
|
Assert.Equal (view.GetScheme ().Focus, view.GetAttributeForRole (VisualRole.Focus));
|
|
Assert.Equal (view.GetScheme ().HotFocus, view.GetAttributeForRole (VisualRole.HotFocus));
|
|
Assert.Equal (view.GetScheme ().Disabled, view.GetAttributeForRole (VisualRole.Disabled));
|
|
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_DisabledView_ReturnsCorrectAttribute ()
|
|
{
|
|
var view = new View { SchemeName = "Base" };
|
|
|
|
view.Enabled = false;
|
|
Assert.Equal (view.GetScheme ().Disabled, view.GetAttributeForRole (VisualRole.Normal));
|
|
Assert.Equal (view.GetScheme ().Disabled, view.GetAttributeForRole (VisualRole.HotNormal));
|
|
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void SetAttributeForRole_SetsCorrectAttribute ()
|
|
{
|
|
var view = new View { SchemeName = "Base" };
|
|
view.Driver = CreateFakeDriver ();
|
|
view.Driver.SetAttribute (new Attribute (Color.Red, Color.Green));
|
|
|
|
var previousAttribute = view.SetAttributeForRole (VisualRole.Focus);
|
|
Assert.Equal (view.GetScheme ().Focus, view.GetCurrentAttribute ());
|
|
Assert.NotEqual (previousAttribute, view.GetCurrentAttribute ());
|
|
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void OnGettingScheme_Override_StopsDefaultBehavior ()
|
|
{
|
|
var view = new CustomView ();
|
|
var customScheme = SchemeManager.GetHardCodedSchemes ()? ["Error"];
|
|
|
|
Assert.Equal (customScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void OnSettingScheme_Override_PreventsSettingScheme ()
|
|
{
|
|
var view = new CustomView ();
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
|
|
view.SetScheme (dialogScheme);
|
|
|
|
Assert.NotEqual (dialogScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GettingScheme_Event_CanOverrideScheme ()
|
|
{
|
|
var view = new View ();
|
|
var customScheme = SchemeManager.GetHardCodedSchemes ()? ["Error"]! with { Normal = Attribute.Default };
|
|
|
|
Assert.NotEqual (Attribute.Default, view.GetScheme ().Normal);
|
|
|
|
view.GettingScheme += (sender, args) =>
|
|
{
|
|
args.Result = customScheme;
|
|
args.Handled = true;
|
|
};
|
|
|
|
Assert.Equal (customScheme, view.GetScheme ());
|
|
Assert.Equal (Attribute.Default, view.GetScheme ().Normal);
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void SettingScheme_Event_CanCancelSchemeChange ()
|
|
{
|
|
var view = new View ();
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
|
|
view.SchemeChanging += (sender, args) => args.Handled = true;
|
|
|
|
view.SetScheme (dialogScheme);
|
|
|
|
Assert.NotEqual (dialogScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_Event_CanOverrideAttribute ()
|
|
{
|
|
var view = new View { SchemeName = "Base" };
|
|
var customAttribute = new Attribute (Color.BrightRed, Color.BrightYellow);
|
|
|
|
view.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Focus)
|
|
{
|
|
args.Result = customAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
Assert.Equal (customAttribute, view.GetAttributeForRole (VisualRole.Focus));
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetHardCodedSchemes_ReturnsExpectedSchemes ()
|
|
{
|
|
var schemes = Scheme.GetHardCodedSchemes ();
|
|
|
|
Assert.NotNull (schemes);
|
|
Assert.Contains ("Base", schemes.Keys);
|
|
Assert.Contains ("Dialog", schemes.Keys);
|
|
Assert.Contains ("Error", schemes.Keys);
|
|
Assert.Contains ("Menu", schemes.Keys);
|
|
Assert.Contains ("Toplevel", schemes.Keys);
|
|
}
|
|
|
|
|
|
[Fact]
|
|
public void SchemeName_OverridesSuperViewScheme ()
|
|
{
|
|
var superView = new View ();
|
|
var subView = new View ();
|
|
|
|
superView.Add (subView);
|
|
|
|
subView.SchemeName = "Error";
|
|
|
|
var errorScheme = SchemeManager.GetHardCodedSchemes ()? ["Error"];
|
|
Assert.Equal (errorScheme, subView.GetScheme ());
|
|
|
|
subView.Dispose ();
|
|
superView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Scheme_DefaultsToBase_WhenNotSet ()
|
|
{
|
|
var view = new View ();
|
|
var baseScheme = SchemeManager.GetHardCodedSchemes ()? ["Base"];
|
|
|
|
Assert.Equal (baseScheme, view.GetScheme ());
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Scheme_HandlesNullSuperViewGracefully ()
|
|
{
|
|
var view = new View ();
|
|
view.SchemeName = "Dialog";
|
|
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
Assert.Equal (dialogScheme, view.GetScheme ());
|
|
|
|
view.Dispose ();
|
|
}
|
|
|
|
private class CustomView : View
|
|
{
|
|
protected override bool OnGettingScheme (out Scheme? scheme)
|
|
{
|
|
scheme = SchemeManager.GetHardCodedSchemes ()? ["Error"];
|
|
|
|
return true;
|
|
}
|
|
|
|
protected override bool OnSettingScheme (ValueChangingEventArgs<Scheme?> args)
|
|
{
|
|
return true; // Prevent setting the scheme
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_SubView_DefersToSuperView_WhenNoExplicitScheme ()
|
|
{
|
|
var parentView = new View { SchemeName = "Base" };
|
|
var childView = new View ();
|
|
parentView.Add (childView);
|
|
|
|
// Parent customizes attribute resolution
|
|
var customAttribute = new Attribute (Color.BrightMagenta, Color.BrightGreen);
|
|
parentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Normal)
|
|
{
|
|
args.Result = customAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
// Child without explicit scheme should get customized attribute from parent
|
|
Assert.Equal (customAttribute, childView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_SubView_UsesOwnScheme_WhenExplicitlySet ()
|
|
{
|
|
var parentView = new View { SchemeName = "Base" };
|
|
var childView = new View ();
|
|
parentView.Add (childView);
|
|
|
|
// Set explicit scheme on child
|
|
var childScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
childView.SetScheme (childScheme);
|
|
|
|
// Parent customizes attribute resolution
|
|
var customAttribute = new Attribute (Color.BrightMagenta, Color.BrightGreen);
|
|
parentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Normal)
|
|
{
|
|
args.Result = customAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
// Child with explicit scheme should NOT get customized attribute from parent
|
|
Assert.NotEqual (customAttribute, childView.GetAttributeForRole (VisualRole.Normal));
|
|
Assert.Equal (childScheme!.Normal, childView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_Adornment_UsesParentScheme ()
|
|
{
|
|
// Border (an Adornment) doesn't have a SuperView but should use its Parent's scheme
|
|
var view = new View { SchemeName = "Dialog" };
|
|
var border = view.Border!;
|
|
|
|
Assert.NotNull (border);
|
|
Assert.Null (border.SuperView); // Adornments don't have SuperView
|
|
Assert.NotNull (border.Parent);
|
|
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
|
|
// Border should use its Parent's scheme, not Base
|
|
Assert.Equal (dialogScheme!.Normal, border.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
view.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_SubView_UsesSchemeName_WhenSet ()
|
|
{
|
|
var parentView = new View { SchemeName = "Base" };
|
|
var childView = new View ();
|
|
parentView.Add (childView);
|
|
|
|
// Set SchemeName on child (not explicit scheme)
|
|
childView.SchemeName = "Dialog";
|
|
|
|
// Parent customizes attribute resolution
|
|
var customAttribute = new Attribute (Color.BrightMagenta, Color.BrightGreen);
|
|
parentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Normal)
|
|
{
|
|
args.Result = customAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
// Child with SchemeName should NOT get customized attribute from parent
|
|
// It should use the Dialog scheme instead
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
Assert.NotEqual (customAttribute, childView.GetAttributeForRole (VisualRole.Normal));
|
|
Assert.Equal (dialogScheme!.Normal, childView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_NestedHierarchy_DefersCorrectly ()
|
|
{
|
|
// Test: grandchild without explicit scheme defers through parent to grandparent
|
|
// Would fail without the SuperView deferral fix (commit 154ac15)
|
|
|
|
var grandparentView = new View { SchemeName = "Base" };
|
|
var parentView = new View (); // No scheme or SchemeName
|
|
var childView = new View (); // No scheme or SchemeName
|
|
|
|
grandparentView.Add (parentView);
|
|
parentView.Add (childView);
|
|
|
|
// Grandparent customizes attributes
|
|
var customAttribute = new Attribute (Color.BrightYellow, Color.BrightBlue);
|
|
grandparentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Normal)
|
|
{
|
|
args.Result = customAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
// Child should get attribute from grandparent through parent
|
|
Assert.Equal (customAttribute, childView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
// Parent should also get attribute from grandparent
|
|
Assert.Equal (customAttribute, parentView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
grandparentView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_ParentWithSchemeNameBreaksChain ()
|
|
{
|
|
// Test: parent with SchemeName stops deferral chain
|
|
// Would fail without the SchemeName check (commit 866e002)
|
|
|
|
var grandparentView = new View { SchemeName = "Base" };
|
|
var parentView = new View { SchemeName = "Dialog" }; // Sets SchemeName
|
|
var childView = new View (); // No scheme or SchemeName
|
|
|
|
grandparentView.Add (parentView);
|
|
parentView.Add (childView);
|
|
|
|
// Grandparent customizes attributes
|
|
var customAttribute = new Attribute (Color.BrightYellow, Color.BrightBlue);
|
|
grandparentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Normal)
|
|
{
|
|
args.Result = customAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
// Parent should NOT get grandparent's customization (it has SchemeName)
|
|
var dialogScheme = SchemeManager.GetHardCodedSchemes ()? ["Dialog"];
|
|
Assert.NotEqual (customAttribute, parentView.GetAttributeForRole (VisualRole.Normal));
|
|
Assert.Equal (dialogScheme!.Normal, parentView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
// Child should get parent's Dialog scheme (defers to parent, parent uses Dialog scheme)
|
|
Assert.Equal (dialogScheme!.Normal, childView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
grandparentView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_OnGettingAttributeForRole_TakesPrecedence ()
|
|
{
|
|
// Test: view's own OnGettingAttributeForRole takes precedence over parent
|
|
// This should work with or without the fix, but validates precedence
|
|
|
|
var parentView = new View { SchemeName = "Base" };
|
|
var childView = new TestViewWithAttributeOverride ();
|
|
parentView.Add (childView);
|
|
|
|
// Parent customizes attributes
|
|
var parentAttribute = new Attribute (Color.BrightYellow, Color.BrightBlue);
|
|
parentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
if (args.Role == VisualRole.Normal)
|
|
{
|
|
args.Result = parentAttribute;
|
|
args.Handled = true;
|
|
}
|
|
};
|
|
|
|
// Child's own override should take precedence
|
|
var childOverrideAttribute = new Attribute (Color.BrightRed, Color.BrightCyan);
|
|
childView.OverrideAttribute = childOverrideAttribute;
|
|
|
|
Assert.Equal (childOverrideAttribute, childView.GetAttributeForRole (VisualRole.Normal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void GetAttributeForRole_MultipleRoles_DeferCorrectly ()
|
|
{
|
|
// Test: multiple VisualRoles all defer correctly
|
|
// Would fail without the SuperView deferral fix for any role
|
|
|
|
var parentView = new View { SchemeName = "Base" };
|
|
var childView = new View ();
|
|
parentView.Add (childView);
|
|
|
|
var normalAttr = new Attribute (Color.Red, Color.Blue);
|
|
var focusAttr = new Attribute (Color.Green, Color.Yellow);
|
|
var hotNormalAttr = new Attribute (Color.Magenta, Color.Cyan);
|
|
|
|
parentView.GettingAttributeForRole += (sender, args) =>
|
|
{
|
|
switch (args.Role)
|
|
{
|
|
case VisualRole.Normal:
|
|
args.Result = normalAttr;
|
|
args.Handled = true;
|
|
break;
|
|
case VisualRole.Focus:
|
|
args.Result = focusAttr;
|
|
args.Handled = true;
|
|
break;
|
|
case VisualRole.HotNormal:
|
|
args.Result = hotNormalAttr;
|
|
args.Handled = true;
|
|
break;
|
|
}
|
|
};
|
|
|
|
// All roles should defer to parent
|
|
Assert.Equal (normalAttr, childView.GetAttributeForRole (VisualRole.Normal));
|
|
Assert.Equal (focusAttr, childView.GetAttributeForRole (VisualRole.Focus));
|
|
Assert.Equal (hotNormalAttr, childView.GetAttributeForRole (VisualRole.HotNormal));
|
|
|
|
childView.Dispose ();
|
|
parentView.Dispose ();
|
|
}
|
|
|
|
private class TestViewWithAttributeOverride : View
|
|
{
|
|
public Attribute? OverrideAttribute { get; set; }
|
|
|
|
protected override bool OnGettingAttributeForRole (in VisualRole role, ref Attribute currentAttribute)
|
|
{
|
|
if (OverrideAttribute.HasValue && role == VisualRole.Normal)
|
|
{
|
|
currentAttribute = OverrideAttribute.Value;
|
|
return true;
|
|
}
|
|
return base.OnGettingAttributeForRole (role, ref currentAttribute);
|
|
}
|
|
}
|
|
|
|
} |