From c561016423a579024ba5a55cc98a67dcb47f10e0 Mon Sep 17 00:00:00 2001 From: BDisp Date: Tue, 17 May 2022 22:23:04 +0100 Subject: [PATCH 1/2] Prevents updating button before initialization. --- Terminal.Gui/Views/Button.cs | 3 ++- UnitTests/TextFormatterTests.cs | 37 +++++++++++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index ca204fa13..81328a958 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -137,7 +137,8 @@ namespace Terminal.Gui { if (hotKey != hk) { HotKey = hk; } - Update (); + if (IsInitialized) + Update (); } } diff --git a/UnitTests/TextFormatterTests.cs b/UnitTests/TextFormatterTests.cs index 3a66c26dd..cfc932693 100644 --- a/UnitTests/TextFormatterTests.cs +++ b/UnitTests/TextFormatterTests.cs @@ -2065,7 +2065,8 @@ namespace Terminal.Gui.Core { } var label = new Label (breakLines) { TextDirection = TextDirection.TopBottom_LeftRight, - Width = Dim.Fill (), Height = Dim.Fill () + Width = Dim.Fill (), + Height = Dim.Fill () }; var frame = new FrameView () { Width = Dim.Fill (), Height = Dim.Fill () }; @@ -3153,7 +3154,7 @@ e [Fact] public void GetSumMaxCharWidth_List_Simple_And_Wide_Runes () { - List text =new List() { "Hello", "World" }; + List text = new List () { "Hello", "World" }; Assert.Equal (2, TextFormatter.GetSumMaxCharWidth (text)); Assert.Equal (1, TextFormatter.GetSumMaxCharWidth (text, 1, 1)); text = new List () { "こんにちは", "世界" }; @@ -3177,6 +3178,38 @@ e Assert.Equal (6, TextFormatter.GetMaxLengthForWidth (runes, 6)); runes = ustring.Make ("こんにちは 世界").ToRuneList (); Assert.Equal (3, TextFormatter.GetMaxLengthForWidth (runes, 6)); + runes = ustring.Make ("[ Say Hello 你 ]").ToRuneList (); + Assert.Equal (15, TextFormatter.GetMaxLengthForWidth (runes, 16)); + } + + [Fact, AutoInitShutdown] + public void GetMaxLengthForWidth_On_Button () + { + var btn = new Button ("Say Hello 你") { + X = Pos.Center (), + Y = Pos.Center () + }; + var win = new Window ("Test Demo 你") { + Width = Dim.Fill (), + Height = Dim.Fill () + }; + win.Add (btn); + Application.Top.Add (win); + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (30, 5); + + Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds); + + var expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ [ Say Hello 你 ] │ +│ │ +└────────────────────────────┘ +"; + + var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + Assert.Equal (new Rect (0, 0, 30, 5), pos); } [Fact] From d99bc42f397345591120a217d4944de8d47573f6 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 18 May 2022 13:47:04 +0100 Subject: [PATCH 2/2] Preventing more Update execution before Initialize. --- Terminal.Gui/Views/Button.cs | 6 +++-- UnitTests/ButtonTests.cs | 48 ++++++++++++++++++++++++++++++--- UnitTests/TextFormatterTests.cs | 30 --------------------- 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index 81328a958..4300c4e59 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -150,7 +150,8 @@ namespace Terminal.Gui { get => is_default; set { is_default = value; - Update (); + if (IsInitialized) + Update (); } } @@ -187,7 +188,8 @@ namespace Terminal.Gui { get => base.AutoSize; set { base.AutoSize = value; - Update (); + if (IsInitialized) + Update (); } } diff --git a/UnitTests/ButtonTests.cs b/UnitTests/ButtonTests.cs index 153e7ef33..d7224438a 100644 --- a/UnitTests/ButtonTests.cs +++ b/UnitTests/ButtonTests.cs @@ -1,12 +1,16 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using Xunit; +using Xunit.Abstractions; namespace Terminal.Gui.Views { public class ButtonTests { + readonly ITestOutputHelper output; + + public ButtonTests (ITestOutputHelper output) + { + this.output = output; + } + [Fact, AutoInitShutdown] public void Constructors_Defaults () { @@ -185,5 +189,41 @@ namespace Terminal.Gui.Views { Assert.Equal ("Te_st", btn.Text); Assert.Equal (Key.S, btn.HotKey); } + + [Fact, AutoInitShutdown] + public void Update_Only_On_Or_After_Initialize () + { + var btn = new Button ("Say Hello 你") { + X = Pos.Center (), + Y = Pos.Center () + }; + var win = new Window ("Test Demo 你") { + Width = Dim.Fill (), + Height = Dim.Fill () + }; + win.Add (btn); + Application.Top.Add (win); + + Assert.False (btn.IsInitialized); + + Application.Begin (Application.Top); + ((FakeDriver)Application.Driver).SetBufferSize (30, 5); + + Assert.True (btn.IsInitialized); + Assert.Equal ("Say Hello 你", btn.Text); + Assert.Equal ("[ Say Hello 你 ]", btn.TextFormatter.Text); + Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds); + + var expected = @" +┌ Test Demo 你 ──────────────┐ +│ │ +│ [ Say Hello 你 ] │ +│ │ +└────────────────────────────┘ +"; + + var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); + Assert.Equal (new Rect (0, 0, 30, 5), pos); + } } } diff --git a/UnitTests/TextFormatterTests.cs b/UnitTests/TextFormatterTests.cs index cfc932693..05f6a88c8 100644 --- a/UnitTests/TextFormatterTests.cs +++ b/UnitTests/TextFormatterTests.cs @@ -3182,36 +3182,6 @@ e Assert.Equal (15, TextFormatter.GetMaxLengthForWidth (runes, 16)); } - [Fact, AutoInitShutdown] - public void GetMaxLengthForWidth_On_Button () - { - var btn = new Button ("Say Hello 你") { - X = Pos.Center (), - Y = Pos.Center () - }; - var win = new Window ("Test Demo 你") { - Width = Dim.Fill (), - Height = Dim.Fill () - }; - win.Add (btn); - Application.Top.Add (win); - Application.Begin (Application.Top); - ((FakeDriver)Application.Driver).SetBufferSize (30, 5); - - Assert.Equal (new Rect (0, 0, 16, 1), btn.Bounds); - - var expected = @" -┌ Test Demo 你 ──────────────┐ -│ │ -│ [ Say Hello 你 ] │ -│ │ -└────────────────────────────┘ -"; - - var pos = GraphViewTests.AssertDriverContentsWithFrameAre (expected, output); - Assert.Equal (new Rect (0, 0, 30, 5), pos); - } - [Fact] public void Format_Truncate_Simple_And_Wide_Runes () {