diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerFrameTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerFrameTests.cs new file mode 100644 index 00000000000..02f860b74c4 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DesignerFrameTests.cs @@ -0,0 +1,171 @@ +// 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; +using System.Drawing; +using Moq; + +namespace System.Windows.Forms.Design.Tests; + +public class DesignerFrameTests +{ + private readonly Mock _mockSite = new(); + + private class TestDesignerFrame : DesignerFrame + { + public TestDesignerFrame(ISite site) : base(site) { } + + public void TestSetSite(ISite site) + { + Site = site; + } + + public new bool ProcessDialogKey(Keys keyData) + { + return base.ProcessDialogKey(keyData); + } + } + + private void SetupMocks() + { + Mock mockUIService = new(); + mockUIService.Setup(ui => ui.Styles["ArtboardBackground"]).Returns(Color.Red); + _mockSite.Setup(site => site.GetService(typeof(IUIService))).Returns(mockUIService.Object); + } + + [Fact] + public void DesignerFrame_Constructor_SetsTextBackColorAndControls() + { + SetupMocks(); + + DesignerFrame designerFrame = new(_mockSite.Object); + try + { + designerFrame.Text.Should().Be("DesignerFrame"); + designerFrame.BackColor.Should().Be(Color.Red); + + designerFrame.Controls.Cast().Should().HaveCount(1); + } + finally + { + designerFrame.Dispose(); + } + } + + [Fact] + public void DesignerFrame_Initialize_SetsDesignerProperties() + { + Control control = new(); + DesignerFrame designerFrame = new(_mockSite.Object); + try + { + designerFrame.Initialize(control); + + List controls = designerFrame.Controls.Cast().ToList(); + Control? designerRegion = controls.FirstOrDefault(c => c is ScrollableControl); + + designerRegion.Should().NotBeNull(); + + Control? containedControl = designerRegion?.Controls.Cast().FirstOrDefault(); + containedControl.Should().Be(control); + + control.Visible.Should().BeTrue(); + control.Enabled.Should().BeTrue(); + + designerFrame.Controls.Cast().Should().NotContain(control); + } + finally + { + control.Dispose(); + designerFrame.Dispose(); + } + } + + [Fact] + public void DesignerFrame_ProcessDialogKey_ReturnsExpectedResult() + { + TestDesignerFrame designerFrame = new(_mockSite.Object); + try + { + bool result = designerFrame.ProcessDialogKey(Keys.Enter); + + result.Should().BeFalse(); + } + finally + { + designerFrame.Dispose(); + } + } + + [Fact] + public void DesignerFrame_Constructor_SetsSitePropertyUsingTestSetSite() + { + TestDesignerFrame designerFrame = new(_mockSite.Object); + try + { + designerFrame.TestSetSite(_mockSite.Object); + + designerFrame.Site.Should().Be(_mockSite.Object); + } + finally + { + designerFrame.Dispose(); + } + } + + [Fact] + public void DesignerFrame_AddControl_AddsControlToFrame() + { + Control control = new(); + DesignerFrame designerFrame = new(_mockSite.Object); + try + { + designerFrame.Controls.Add(control); + + designerFrame.Controls.Cast().Should().Contain(control); + } + finally + { + control.Dispose(); + designerFrame.Dispose(); + } + } + + [Fact] + public void DesignerFrame_Resize_TriggersExpectedBehavior() + { + DesignerFrame designerFrame = new(_mockSite.Object); + bool resizeEventTriggered = false; + + try + { + designerFrame.Resize += (sender, args) => resizeEventTriggered = true; + designerFrame.Width = 500; + + resizeEventTriggered.Should().BeTrue(); + designerFrame.Width.Should().Be(500); + } + finally + { + designerFrame.Dispose(); + } + } + + [Fact] + public void Designer_ProcessDialogKey() + { + TestDesignerFrame designerFrame = new(_mockSite.Object); + try + { + bool result = designerFrame.ProcessDialogKey(Keys.Enter); + + result.Should().BeFalse(); + } + finally + { + designerFrame.Dispose(); + } + } +}