From 26cbd509cc9348c58dad5087fc850265375fffca Mon Sep 17 00:00:00 2001 From: "Zheng Li (BEYONDSOFT CONSULTING INC)" Date: Tue, 22 Apr 2025 16:03:15 +0800 Subject: [PATCH 1/2] Add code coverage for DesignerUtils --- .../Forms/Design/DesignerUtilsTests.cs | 162 ++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs new file mode 100644 index 00000000000..7fdd054cf95 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs @@ -0,0 +1,162 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Drawing; +using System.Drawing.Imaging; + +namespace System.Windows.Forms.Design.Tests; + +public class DesignerUtilsTests +{ + public static Image BoxImage => DesignerUtils.BoxImage; + public static Brush HoverBrush => DesignerUtils.HoverBrush; + + [Fact] + public void BoxImage_ShouldReturnNonNullImage() => + BoxImage.Should().NotBeNull(); + + [Fact] + public void BoxImage_ShouldHaveExpectedDimensions() + { + BoxImage.Width.Should().Be(DesignerUtils.s_boxImageSize); + BoxImage.Height.Should().Be(DesignerUtils.s_boxImageSize); + } + + [Fact] + public void BoxImage_ShouldHaveExpectedPixelFormat() => + ((Bitmap)BoxImage).PixelFormat.Should().Be(PixelFormat.Format32bppPArgb); + + [Fact] + public void BoxImage_ShouldBeCached() + { + Image firstCall = DesignerUtils.BoxImage; + Image secondCall = DesignerUtils.BoxImage; + firstCall.Should().BeSameAs(secondCall); + } + + [Fact] + public void HoverBrush_ShouldReturnNonNullBrush() => + HoverBrush.Should().NotBeNull(); + + [Fact] + public void HoverBrush_ShouldBeSolidBrush() => + HoverBrush.Should().BeOfType(); + + [Fact] + public void HoverBrush_ShouldHaveExpectedColor() => + ((SolidBrush)HoverBrush).Color.Should().Be(Color.FromArgb(50, SystemColors.Highlight)); + + [Fact] + public void MinDragSize_ShouldReturnNonEmptySize() + { + Size minDragSize = DesignerUtils.MinDragSize; + minDragSize.Should().NotBe(Size.Empty); + } + + [Fact] + public void MinDragSize_ShouldBeCached() + { + Size firstCall = DesignerUtils.MinDragSize; + Size secondCall = DesignerUtils.MinDragSize; + firstCall.Should().Be(secondCall); + } + + [Theory] + [MemberData(nameof(ResizeBorderTestData))] + public void DrawResizeBorder_ShouldUseCorrectBrushBasedOnBackColor(int width, int height, Color backColor, Color expectedColor) + { + using Bitmap bitmap = new(width, height); + using Graphics graphics = Graphics.FromImage(bitmap); + using Region region = new(new Rectangle(0, 0, width, height)); + DesignerUtils.DrawResizeBorder(graphics, region, backColor); + Color pixelColor = bitmap.GetPixel(width / 2, height / 2); + pixelColor.ToArgb().Should().Be(expectedColor.ToArgb()); + } + + public static TheoryData ResizeBorderTestData => new() + { + { 10, 10, Color.White, SystemColors.ControlDarkDark }, + { 10, 10, Color.Black, SystemColors.ControlLight } + }; + + [Fact] + public void DrawGrabHandle_ShouldNotThrow_WhenCalledWithValidParameters() + { + using Bitmap bitmap = new(10, 10); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(2, 2, 6, 6); + Exception exception = Record.Exception(() => DesignerUtils.DrawGrabHandle(graphics, bounds, isPrimary: true)); + exception.Should().BeNull(); + } + + [Theory] + [BoolData] + public void DrawGrabHandle_ShouldDrawHandle_BasedOnSelectionType(bool isPrimary) + { + using Bitmap bitmap = new(10, 10); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(2, 2, 6, 6); + + DesignerUtils.DrawGrabHandle(graphics, bounds, isPrimary); + + Color pixelColor = bitmap.GetPixel(3, 3); + pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); + } + + [Theory] + [BoolData] + public void DrawLockedHandle_ShouldDrawHandle_BasedOnSelectionType(bool isPrimary) + { + using Bitmap bitmap = new(20, 20); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(5, 5, 10, 10); + + DesignerUtils.DrawLockedHandle(graphics, bounds, isPrimary); + + Color pixelColor = bitmap.GetPixel(6, 6); + pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); + } + + [Fact] + public void DrawLockedHandle_ShouldDrawUpperRect_WhenCalledWithPrimarySelection() + { + using Bitmap bitmap = new(20, 20); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(5, 5, 10, 10); + DesignerUtils.DrawLockedHandle(graphics, bounds, isPrimary: true); + Color pixelColor = bitmap.GetPixel(bounds.Left + 1, bounds.Top + 1); + pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); + } + + [Fact] + public void DrawLockedHandle_ShouldDrawLowerRect_WhenCalledWithNonPrimarySelection() + { + using Bitmap bitmap = new(20, 20); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(5, 5, 10, 10); + DesignerUtils.DrawLockedHandle(graphics, bounds, isPrimary: false); + Color pixelColor = bitmap.GetPixel(bounds.Left + 1, bounds.Top + DesignerUtils.s_lockedHandleLowerOffset + 1); + pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); + } + + [Fact] + public void DrawSelectionBorder_ShouldNotThrow_WhenCalledWithValidParameters() + { + using Bitmap bitmap = new(20, 20); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(5, 5, 10, 10); + Exception exception = Record.Exception(() => DesignerUtils.DrawSelectionBorder(graphics, bounds)); + exception.Should().BeNull(); + } + + [Fact] + public void DrawSelectionBorder_ShouldFillRectangle_WhenCalled() + { + using Bitmap bitmap = new(20, 20); + using Graphics graphics = Graphics.FromImage(bitmap); + Rectangle bounds = new(5, 5, 10, 10); + DesignerUtils.DrawSelectionBorder(graphics, bounds); + Color pixelColor = bitmap.GetPixel(6, 6); + pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); + } +} From 45a7b9cd8b0f5b107b78bdc0dda26cc66273b0ee Mon Sep 17 00:00:00 2001 From: "Zheng Li (BEYONDSOFT CONSULTING INC)" Date: Wed, 23 Apr 2025 15:50:57 +0800 Subject: [PATCH 2/2] Address the FeedBacks --- .../Forms/Design/DesignerUtilsTests.cs | 123 ++++++++---------- 1 file changed, 54 insertions(+), 69 deletions(-) diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs index 7fdd054cf95..35dfb41b217 100644 --- a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerUtilsTests.cs @@ -6,27 +6,41 @@ namespace System.Windows.Forms.Design.Tests; -public class DesignerUtilsTests +public class DesignerUtilsTests :IDisposable { - public static Image BoxImage => DesignerUtils.BoxImage; - public static Brush HoverBrush => DesignerUtils.HoverBrush; + private readonly Bitmap _bitmap; + private readonly Graphics _graphics; + private readonly Rectangle _bounds; - [Fact] + public DesignerUtilsTests() + { + _bitmap = new(20, 20); + _graphics = Graphics.FromImage(_bitmap); + _bounds = new(5, 5, 20, 20); + } + + public void Dispose() + { + _graphics.Dispose(); + _bitmap.Dispose(); + } + + [WinFormsFact] public void BoxImage_ShouldReturnNonNullImage() => - BoxImage.Should().NotBeNull(); + DesignerUtils.BoxImage.Should().BeOfType(); - [Fact] + [WinFormsFact] public void BoxImage_ShouldHaveExpectedDimensions() { - BoxImage.Width.Should().Be(DesignerUtils.s_boxImageSize); - BoxImage.Height.Should().Be(DesignerUtils.s_boxImageSize); + DesignerUtils.BoxImage.Width.Should().Be(DesignerUtils.s_boxImageSize); + DesignerUtils.BoxImage.Height.Should().Be(DesignerUtils.s_boxImageSize); } - [Fact] + [WinFormsFact] public void BoxImage_ShouldHaveExpectedPixelFormat() => - ((Bitmap)BoxImage).PixelFormat.Should().Be(PixelFormat.Format32bppPArgb); + ((Bitmap)DesignerUtils.BoxImage).PixelFormat.Should().Be(PixelFormat.Format32bppPArgb); - [Fact] + [WinFormsFact] public void BoxImage_ShouldBeCached() { Image firstCall = DesignerUtils.BoxImage; @@ -34,17 +48,13 @@ public void BoxImage_ShouldBeCached() firstCall.Should().BeSameAs(secondCall); } - [Fact] - public void HoverBrush_ShouldReturnNonNullBrush() => - HoverBrush.Should().NotBeNull(); - [Fact] public void HoverBrush_ShouldBeSolidBrush() => - HoverBrush.Should().BeOfType(); + DesignerUtils.HoverBrush.Should().BeOfType(); [Fact] public void HoverBrush_ShouldHaveExpectedColor() => - ((SolidBrush)HoverBrush).Color.Should().Be(Color.FromArgb(50, SystemColors.Highlight)); + ((SolidBrush)DesignerUtils.HoverBrush).Color.Should().Be(Color.FromArgb(50, SystemColors.Highlight)); [Fact] public void MinDragSize_ShouldReturnNonEmptySize() @@ -61,102 +71,77 @@ public void MinDragSize_ShouldBeCached() firstCall.Should().Be(secondCall); } - [Theory] + [WinFormsTheory] [MemberData(nameof(ResizeBorderTestData))] - public void DrawResizeBorder_ShouldUseCorrectBrushBasedOnBackColor(int width, int height, Color backColor, Color expectedColor) + public void DrawResizeBorder_ShouldUseCorrectBrushBasedOnBackColor(Color backColor, Color expectedColor) { - using Bitmap bitmap = new(width, height); - using Graphics graphics = Graphics.FromImage(bitmap); - using Region region = new(new Rectangle(0, 0, width, height)); - DesignerUtils.DrawResizeBorder(graphics, region, backColor); - Color pixelColor = bitmap.GetPixel(width / 2, height / 2); + using Region region = new(new Rectangle(0, 0, _bounds.Width, _bounds.Height)); + DesignerUtils.DrawResizeBorder(_graphics, region, backColor); + Color pixelColor = _bitmap.GetPixel(_bounds.Width / 2, _bounds.Height / 2); pixelColor.ToArgb().Should().Be(expectedColor.ToArgb()); } - public static TheoryData ResizeBorderTestData => new() + public static TheoryData ResizeBorderTestData => new() { - { 10, 10, Color.White, SystemColors.ControlDarkDark }, - { 10, 10, Color.Black, SystemColors.ControlLight } + { Color.White, SystemColors.ControlDarkDark }, + { Color.Black, SystemColors.ControlLight } }; - [Fact] + [WinFormsFact] public void DrawGrabHandle_ShouldNotThrow_WhenCalledWithValidParameters() { - using Bitmap bitmap = new(10, 10); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(2, 2, 6, 6); - Exception exception = Record.Exception(() => DesignerUtils.DrawGrabHandle(graphics, bounds, isPrimary: true)); + Exception exception = Record.Exception(() => DesignerUtils.DrawGrabHandle(_graphics, _bounds, isPrimary: true)); exception.Should().BeNull(); } - [Theory] + [WinFormsTheory] [BoolData] public void DrawGrabHandle_ShouldDrawHandle_BasedOnSelectionType(bool isPrimary) { - using Bitmap bitmap = new(10, 10); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(2, 2, 6, 6); + DesignerUtils.DrawGrabHandle(_graphics, _bounds, isPrimary); - DesignerUtils.DrawGrabHandle(graphics, bounds, isPrimary); - - Color pixelColor = bitmap.GetPixel(3, 3); + Color pixelColor = _bitmap.GetPixel(6, 6); pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); } - [Theory] + [WinFormsTheory] [BoolData] public void DrawLockedHandle_ShouldDrawHandle_BasedOnSelectionType(bool isPrimary) { - using Bitmap bitmap = new(20, 20); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(5, 5, 10, 10); - - DesignerUtils.DrawLockedHandle(graphics, bounds, isPrimary); + DesignerUtils.DrawLockedHandle(_graphics, _bounds, isPrimary); - Color pixelColor = bitmap.GetPixel(6, 6); + Color pixelColor = _bitmap.GetPixel(6, 6); pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); } - [Fact] + [WinFormsFact] public void DrawLockedHandle_ShouldDrawUpperRect_WhenCalledWithPrimarySelection() { - using Bitmap bitmap = new(20, 20); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(5, 5, 10, 10); - DesignerUtils.DrawLockedHandle(graphics, bounds, isPrimary: true); - Color pixelColor = bitmap.GetPixel(bounds.Left + 1, bounds.Top + 1); + DesignerUtils.DrawLockedHandle(_graphics, _bounds, isPrimary: true); + Color pixelColor = _bitmap.GetPixel(_bounds.Left + 1, _bounds.Top + 1); pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); } - [Fact] + [WinFormsFact] public void DrawLockedHandle_ShouldDrawLowerRect_WhenCalledWithNonPrimarySelection() { - using Bitmap bitmap = new(20, 20); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(5, 5, 10, 10); - DesignerUtils.DrawLockedHandle(graphics, bounds, isPrimary: false); - Color pixelColor = bitmap.GetPixel(bounds.Left + 1, bounds.Top + DesignerUtils.s_lockedHandleLowerOffset + 1); + DesignerUtils.DrawLockedHandle(_graphics, _bounds, isPrimary: false); + Color pixelColor = _bitmap.GetPixel(_bounds.Left + 1, _bounds.Top + DesignerUtils.s_lockedHandleLowerOffset + 1); pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); } - [Fact] + [WinFormsFact] public void DrawSelectionBorder_ShouldNotThrow_WhenCalledWithValidParameters() { - using Bitmap bitmap = new(20, 20); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(5, 5, 10, 10); - Exception exception = Record.Exception(() => DesignerUtils.DrawSelectionBorder(graphics, bounds)); + Exception exception = Record.Exception(() => DesignerUtils.DrawSelectionBorder(_graphics, _bounds)); exception.Should().BeNull(); } - [Fact] + [WinFormsFact] public void DrawSelectionBorder_ShouldFillRectangle_WhenCalled() { - using Bitmap bitmap = new(20, 20); - using Graphics graphics = Graphics.FromImage(bitmap); - Rectangle bounds = new(5, 5, 10, 10); - DesignerUtils.DrawSelectionBorder(graphics, bounds); - Color pixelColor = bitmap.GetPixel(6, 6); + DesignerUtils.DrawSelectionBorder(_graphics, _bounds); + Color pixelColor = _bitmap.GetPixel(6, 6); pixelColor.ToArgb().Should().NotBe(Color.Empty.ToArgb()); } }