From caf160ac328370078be908d6b6473991c02c5919 Mon Sep 17 00:00:00 2001 From: BDisp Date: Mon, 28 Aug 2023 15:23:04 +0100 Subject: [PATCH] Fixes #2834. v1 Border BorderBrush and Background wrongly sets to an invalid Color enum. --- Terminal.Gui/Core/Border.cs | 18 ++++++++++-------- UnitTests/Core/BorderTests.cs | 24 ++++++++++++++++++++++++ UnitTests/ReflectionTools.cs | 8 ++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/Terminal.Gui/Core/Border.cs b/Terminal.Gui/Core/Border.cs index abf4438ce7..ab310cf935 100644 --- a/Terminal.Gui/Core/Border.cs +++ b/Terminal.Gui/Core/Border.cs @@ -375,8 +375,10 @@ public Thickness BorderThickness { public Color BorderBrush { get => borderBrush != null ? (Color)borderBrush : (Color)(-1); set { - borderBrush = value; - OnBorderChanged (); + if (Enum.IsDefined (typeof (Color), value)) { + borderBrush = value; + OnBorderChanged (); + } } } @@ -386,8 +388,10 @@ public Color BorderBrush { public Color Background { get => background != null ? (Color)background : (Color)(-1); set { - background = value; - OnBorderChanged (); + if (Enum.IsDefined (typeof (Color), value)) { + background = value; + OnBorderChanged (); + } } } @@ -445,12 +449,10 @@ public View Child { private void Parent_Removed (View obj) { - if (borderBrush != null) - { + if (borderBrush != null) { BorderBrush = default; } - if (background != null) - { + if (background != null) { Background = default; } child.Removed -= Parent_Removed; diff --git a/UnitTests/Core/BorderTests.cs b/UnitTests/Core/BorderTests.cs index 89c67f5aa2..97ce9c805e 100644 --- a/UnitTests/Core/BorderTests.cs +++ b/UnitTests/Core/BorderTests.cs @@ -619,5 +619,29 @@ public void BorderStyle_And_DrawMarginFrame_Gets_Sets () ████████████████████ At 0,4 ", output); } + + [Fact] + public void BorderBrush_Background_Only_Is_Set_To_Valid_Color_Enum () + { + var border = new Border (); + Assert.Equal ((Color)(-1), border.BorderBrush); + Assert.Equal ((Color)(-1), border.Background); + Assert.Null (border.GetFieldValue ("borderBrush")); + Assert.Null (border.GetFieldValue ("background")); + + border.BorderBrush = (Color)(-1); + border.Background = (Color)(-1); + Assert.Equal ((Color)(-1), border.BorderBrush); + Assert.Equal ((Color)(-1), border.Background); + Assert.Null (border.GetFieldValue ("borderBrush")); + Assert.Null (border.GetFieldValue ("background")); + + border.BorderBrush = Color.Blue; + border.Background = Color.White; + Assert.Equal (Color.Blue, border.BorderBrush); + Assert.Equal (Color.White, border.Background); + Assert.Equal (Color.Blue, border.GetFieldValue ("borderBrush")); + Assert.Equal (Color.White, border.GetFieldValue ("background")); + } } } diff --git a/UnitTests/ReflectionTools.cs b/UnitTests/ReflectionTools.cs index 3f661bc5f2..54a021fa21 100644 --- a/UnitTests/ReflectionTools.cs +++ b/UnitTests/ReflectionTools.cs @@ -32,4 +32,12 @@ public static Object InvokePrivate (Type typeOfObjectUnderTest, string method, p } return null; } + + public static T GetFieldValue (this object obj, string name) + { + // Set the flags so that private and public fields from instances will be found + var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; + var field = obj.GetType ().GetField (name, bindingFlags); + return (T)field?.GetValue (obj); + } }