Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for DesignerFrame #12712

Merged
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// 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 : IDisposable
{
private readonly Mock<ISite> _mockSite = new();

public DesignerFrameTests() { }
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved

public void Dispose() { }
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved

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<IUIService> 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<Control>().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<Control> controls = designerFrame.Controls.Cast<Control>().ToList();
Control? designerRegion = controls.FirstOrDefault(c => c is ScrollableControl);

designerRegion.Should().NotBeNull();

Control? containedControl = designerRegion?.Controls.Cast<Control>().FirstOrDefault();
containedControl.Should().Be(control);

control.Visible.Should().BeTrue();
control.Enabled.Should().BeTrue();

designerFrame.Controls.Cast<Control>().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();
Tanya-Solyanik marked this conversation as resolved.
Show resolved Hide resolved
DesignerFrame designerFrame = new(_mockSite.Object);
try
{
designerFrame.Controls.Add(control);

designerFrame.Controls.Cast<Control>().Should().Contain(control);
}
finally
{
control.Dispose();
designerFrame.Dispose();
}
}

[Fact]
public void DesignerFrame_Resize_TriggersExpectedBehavior()
{
DesignerFrame designerFrame = new(_mockSite.Object);
try
{
designerFrame.Resize += (sender, args) => { /* Handle resize if needed */ };

designerFrame.Width = 500;

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();
}
}
}