From bc36c331f92faae8d51cd25453747848ba564a4a Mon Sep 17 00:00:00 2001 From: Nora Zhou Date: Wed, 4 Sep 2024 02:30:55 +0000 Subject: [PATCH 1/4] Add code coverage for comboBox --- .../System/Windows/Forms/ComboBoxTests.cs | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs index 252168a65b5..4e54b3b61fe 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs @@ -305,6 +305,92 @@ public void ComboBox_BackColor_ShouldSerializeValue_Success() Assert.False(property.ShouldSerializeValue(control)); } + [WinFormsFact] + public void ComboBox_MeasureItem_AddHandler() + { + using ComboBox control = new(); + int initialItemHeight = control.ItemHeight; + + MeasureItemEventHandler handler = (sender, e) => { }; + control.MeasureItem += handler; + + control.ItemHeight.Should().NotBe(initialItemHeight); + } + + [WinFormsFact] + public void ComboBox_MeasureItem_RemoveHandler() + { + using ComboBox control = new(); + int handlerCallCount = 0; + MeasureItemEventHandler handler = (sender, e) => { handlerCallCount++; }; + + control.MeasureItem += handler; + control.MeasureItem -= handler; + + handlerCallCount.Should().Be(0, "The MeasureItem event handler was not removed as expected."); + } + + [WinFormsFact] + public void ComboBox_MeasureItem_AddAndRemoveHandler() + { + using ComboBox control = new(); + + MeasureItemEventHandler handler = (sender, e) => { }; + control.MeasureItem += handler; + control.MeasureItem -= handler; + } + + public class TestableComboBox : ComboBox + { + public void InvokeOnPaint(PaintEventArgs e) + { + base.OnPaint(e); + } + } + + [WinFormsFact] + public void ComboBox_Paint_AddHandler_ShouldSubscribeEvent() + { + using TestableComboBox comboBox = new(); + int callCount = 0; + + PaintEventHandler handler = (sender, e) => callCount++; + + comboBox.Paint += handler; + comboBox.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + + callCount.Should().Be(1); + } + + [WinFormsFact] + public void ComboBox_Paint_RemoveHandler_ShouldUnsubscribeEvent() + { + using TestableComboBox comboBox = new(); + int callCount = 0; + + PaintEventHandler handler = (sender, e) => callCount++; + + comboBox.Paint += handler; + comboBox.Paint -= handler; + comboBox.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + + callCount.Should().Be(0); + } + + [WinFormsFact] + public void ComboBox_Paint_EventHandlerCalledOnPaint() + { + using TestableComboBox comboBox = new(); + Bitmap bitmap = new Bitmap(100, 100); + Graphics graphics = Graphics.FromImage(bitmap); + bool handlerCalled = false; + + comboBox.Paint += (sender, e) => handlerCalled = true; + comboBox.InvokeOnPaint(new PaintEventArgs(graphics, new Rectangle(0, 0, 100, 100))); + + handlerCalled.Should().BeTrue(); + } + [WinFormsFact] public void VerifyAutoCompleteEntries() { From 23edf996e833e5a64334679ec769f609bfc8975d Mon Sep 17 00:00:00 2001 From: Nora Zhou Date: Thu, 5 Sep 2024 08:51:37 +0000 Subject: [PATCH 2/4] Update --- .../System/Windows/Forms/ComboBoxTests.cs | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs index 4e54b3b61fe..c116dedcce7 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs @@ -317,27 +317,32 @@ public void ComboBox_MeasureItem_AddHandler() control.ItemHeight.Should().NotBe(initialItemHeight); } + public class TestComboBox : ComboBox + { + public void TriggerMeasureItem(MeasureItemEventArgs e) + { + base.OnMeasureItem(e); + } + } + [WinFormsFact] public void ComboBox_MeasureItem_RemoveHandler() { - using ComboBox control = new(); + using TestComboBox control = new(); int handlerCallCount = 0; MeasureItemEventHandler handler = (sender, e) => { handlerCallCount++; }; control.MeasureItem += handler; - control.MeasureItem -= handler; - handlerCallCount.Should().Be(0, "The MeasureItem event handler was not removed as expected."); - } + // Simulate the MeasureItem event + control.TriggerMeasureItem(new MeasureItemEventArgs(Graphics.FromHwnd(IntPtr.Zero), 0, 0)); - [WinFormsFact] - public void ComboBox_MeasureItem_AddAndRemoveHandler() - { - using ComboBox control = new(); - - MeasureItemEventHandler handler = (sender, e) => { }; - control.MeasureItem += handler; control.MeasureItem -= handler; + + // Simulate the MeasureItem event again to ensure the handler was removed + control.TriggerMeasureItem(new MeasureItemEventArgs(Graphics.FromHwnd(IntPtr.Zero), 0, 0)); + + handlerCallCount.Should().Be(1, "The MeasureItem event handler was not removed as expected."); } public class TestableComboBox : ComboBox @@ -357,7 +362,7 @@ public void ComboBox_Paint_AddHandler_ShouldSubscribeEvent() PaintEventHandler handler = (sender, e) => callCount++; comboBox.Paint += handler; - comboBox.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + comboBox.TestAccessor().Dynamic.OnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); callCount.Should().Be(1); } @@ -381,7 +386,7 @@ public void ComboBox_Paint_RemoveHandler_ShouldUnsubscribeEvent() public void ComboBox_Paint_EventHandlerCalledOnPaint() { using TestableComboBox comboBox = new(); - Bitmap bitmap = new Bitmap(100, 100); + Bitmap bitmap = new(100, 100); Graphics graphics = Graphics.FromImage(bitmap); bool handlerCalled = false; From 13b23deee22fe20b9795c6c6be3c5bdfc842384b Mon Sep 17 00:00:00 2001 From: Nora Zhou Date: Fri, 6 Sep 2024 01:39:25 +0000 Subject: [PATCH 3/4] Update --- .../System/Windows/Forms/ComboBoxTests.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs index c116dedcce7..7894772f94f 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs @@ -317,12 +317,10 @@ public void ComboBox_MeasureItem_AddHandler() control.ItemHeight.Should().NotBe(initialItemHeight); } - public class TestComboBox : ComboBox + private class TestComboBox : ComboBox { - public void TriggerMeasureItem(MeasureItemEventArgs e) - { + public void TriggerMeasureItem(MeasureItemEventArgs e) => base.OnMeasureItem(e); - } } [WinFormsFact] @@ -345,12 +343,9 @@ public void ComboBox_MeasureItem_RemoveHandler() handlerCallCount.Should().Be(1, "The MeasureItem event handler was not removed as expected."); } - public class TestableComboBox : ComboBox + private class TestableComboBox : ComboBox { - public void InvokeOnPaint(PaintEventArgs e) - { - base.OnPaint(e); - } + public void InvokeOnPaint(PaintEventArgs e) => base.OnPaint(e); } [WinFormsFact] @@ -386,7 +381,7 @@ public void ComboBox_Paint_RemoveHandler_ShouldUnsubscribeEvent() public void ComboBox_Paint_EventHandlerCalledOnPaint() { using TestableComboBox comboBox = new(); - Bitmap bitmap = new(100, 100); + using Bitmap bitmap = new(100, 100); Graphics graphics = Graphics.FromImage(bitmap); bool handlerCalled = false; From 534b7cb4caa86f9bbceeec77ea36cc1f5b1bb57a Mon Sep 17 00:00:00 2001 From: Nora Zhou Date: Mon, 9 Sep 2024 01:37:21 +0000 Subject: [PATCH 4/4] Update --- .../tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs index 7894772f94f..9bb6e2ead73 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs @@ -369,12 +369,13 @@ public void ComboBox_Paint_RemoveHandler_ShouldUnsubscribeEvent() int callCount = 0; PaintEventHandler handler = (sender, e) => callCount++; - comboBox.Paint += handler; - comboBox.Paint -= handler; comboBox.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + callCount.Should().Be(1); - callCount.Should().Be(0); + comboBox.Paint -= handler; + comboBox.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + callCount.Should().Be(1); } [WinFormsFact]