From 847499e049b01cdaaaa17900b9f1c0f624376ed1 Mon Sep 17 00:00:00 2001 From: v-chaowendy Date: Fri, 10 Jan 2025 20:15:02 +0000 Subject: [PATCH] add unit test for UserControlDocumentDesigner (#12710) Related #10773 Proposed changes Add unit test UserControlDocumentDesignerTests.cs for public properties and method of the UserControlDocumentDesigner.UserControlDocumentDesigner. Enable nullability in UserControlDocumentDesignerTests.cs. --- .../UserControlDocumentDesignerTests.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/UserControlDocumentDesignerTests.cs diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/UserControlDocumentDesignerTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/UserControlDocumentDesignerTests.cs new file mode 100644 index 00000000000..c6d05e7515f --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/UserControlDocumentDesignerTests.cs @@ -0,0 +1,79 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#nullable enable + +using System.ComponentModel.Design; +using System.ComponentModel.Design.Serialization; +using System.Drawing.Design; +using System.Windows.Forms.Design.Behavior; +using System.Windows.Forms.Design.Tests.Mocks; +using Moq; + +namespace System.Windows.Forms.Design.Tests; + +public class UserControlDocumentDesignerTests +{ + [WinFormsFact] + public void Constructor_ShouldSetAutoResizeHandlesToTrue() + { + using UserControlDocumentDesigner designer = new(); + using UserControl control = new(); + + Mock designerHost = new(); + Mock componentChangeService = new(MockBehavior.Strict); + designerHost + .Setup(s => s.GetService(typeof(IComponentChangeService))) + .Returns(componentChangeService.Object); + Mock selectionService = new(MockBehavior.Strict); + designerHost + .Setup(s => s.GetService(typeof(ISelectionService))) + .Returns(selectionService.Object); + designerHost + .Setup(s => s.GetService(typeof(IDesignerHost))) + .Returns(designerHost.Object); + + var mockSite = MockSite.CreateMockSiteWithDesignerHost(designerHost.Object); + mockSite + .Setup(s => s.GetService(typeof(IExtenderProviderService))) + .Returns(null!); + mockSite + .Setup(s => s.GetService(typeof(IUIService))) + .Returns(null!); + mockSite + .Setup(s => s.GetService(typeof(IOverlayService))) + .Returns(null!); + mockSite + .Setup(s => s.GetService(typeof(IMenuCommandService))) + .Returns(null!); + mockSite + .Setup(s => s.GetService(typeof(INameCreationService))) + .Returns(null!); + mockSite + .Setup(s => s.GetService(typeof(IPropertyValueUIService))) + .Returns(null!); + Mock eventHandlerService = new(MockBehavior.Strict); + mockSite + .Setup(s => s.GetService(typeof(IEventHandlerService))) + .Returns(eventHandlerService.Object); + Mock dictionaryService = new(MockBehavior.Strict); + dictionaryService + .Setup(s => s.GetValue(It.IsAny())) + .Returns(null!); + dictionaryService + .Setup(s => s.SetValue(It.IsAny(), It.IsAny())); + mockSite + .Setup(s => s.GetService(typeof(IDictionaryService))) + .Returns(dictionaryService.Object); + mockSite + .Setup(s => s.GetService(typeof(ComponentCache))) + .Returns(null!); + mockSite + .Setup(s => s.GetService(typeof(BehaviorService))) + .Returns(null!); + control.Site = mockSite.Object; + + designer.Initialize(control); + designer.AutoResizeHandles.Should().BeTrue(); + } +}