From be5fa59aba85ab655f5cb25c7f3363b4beafba41 Mon Sep 17 00:00:00 2001 From: Nora Zhou <104609169+Nora-Zhou01@users.noreply.github.com> Date: Mon, 9 Sep 2024 16:27:34 +0000 Subject: [PATCH] Add code coverage for comboBox (#12038) * Add code coverage for comboBox * Update * Update * Update --- .../System/Windows/Forms/ComboBoxTests.cs | 87 +++++++++++++++++++ 1 file changed, 87 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 429d4cad19d..4e3c826d4e8 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,93 @@ 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); + } + + private class TestComboBox : ComboBox + { + public void TriggerMeasureItem(MeasureItemEventArgs e) => + base.OnMeasureItem(e); + } + + [WinFormsFact] + public void ComboBox_MeasureItem_RemoveHandler() + { + using TestComboBox control = new(); + int handlerCallCount = 0; + MeasureItemEventHandler handler = (sender, e) => { handlerCallCount++; }; + + control.MeasureItem += handler; + + // Simulate the MeasureItem event + control.TriggerMeasureItem(new MeasureItemEventArgs(Graphics.FromHwnd(IntPtr.Zero), 0, 0)); + + 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."); + } + + private 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.TestAccessor().Dynamic.OnPaint(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.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + callCount.Should().Be(1); + + comboBox.Paint -= handler; + comboBox.InvokeOnPaint(new PaintEventArgs(Graphics.FromHwnd(comboBox.Handle), new Rectangle())); + callCount.Should().Be(1); + } + + [WinFormsFact] + public void ComboBox_Paint_EventHandlerCalledOnPaint() + { + using TestableComboBox comboBox = new(); + using Bitmap bitmap = new(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() {