Skip to content

Commit

Permalink
Merge pull request #2835 from BDisp/v1_border-borderBrush-background-…
Browse files Browse the repository at this point in the history
…fix_2834

Fixes #2834. v1 Border BorderBrush and Background wrongly sets to an invalid Color enum.
  • Loading branch information
tig authored Aug 30, 2023
2 parents d4a2549 + ee9948a commit 67b8523
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Terminal.Gui/Core/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
}
}

Expand All @@ -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 ();
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions UnitTests/Core/BorderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> ("borderBrush"));
Assert.Null (border.GetFieldValue<string> ("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<Color?> ("borderBrush"));
Assert.Null (border.GetFieldValue<Color?> ("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<Color> ("borderBrush"));
Assert.Equal (Color.White, border.GetFieldValue<Color?> ("background"));
}
}
}
8 changes: 8 additions & 0 deletions UnitTests/ReflectionTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ public static Object InvokePrivate (Type typeOfObjectUnderTest, string method, p
}
return null;
}

public static T GetFieldValue<T> (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);
}
}

0 comments on commit 67b8523

Please sign in to comment.