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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 4 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,112 @@
// 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
{
[Fact]
public void DesignerFrame_Constructor_SetsProperties()
{
Mock<ISite> mockSite = new();
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved
Mock<IUIService> mockUIService = new();
mockUIService.Setup(ui => ui.Styles["ArtboardBackground"]).Returns(Color.Red);
mockSite.Setup(site => site.GetService(typeof(IUIService))).Returns(mockUIService.Object);

DesignerFrame designerFrame = new(mockSite.Object);
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved

designerFrame.Text.Should().Be("DesignerFrame");
designerFrame.BackColor.Should().Be(Color.Red);

designerFrame.Controls.Cast<Control>().Should().HaveCount(1);
}

[Fact]
public void DesignerFrame_Initialize_SetsDesignerProperties()
{
Mock<ISite> mockSite = new();
Control control = new();
DesignerFrame designerFrame = new(mockSite.Object);
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved

designerFrame.Initialize(control);

List<Control> controls = designerFrame.Controls.Cast<Control>().ToList();
Control? designerRegion = controls.FirstOrDefault(c => c is ScrollableControl);

designerRegion.Should().NotBeNull();

Control? designer = designerRegion?.Controls.Cast<Control>().FirstOrDefault();
designer.Should().Be(control);
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved

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

designerFrame.Controls.Cast<Control>().Should().NotContain(control);
}

[Fact]
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved
public void DesignerFrame_ProcessDialogKey_ReturnsExpectedResult()
{
Mock<ISite> mockSite = new();
using DesignerFrame designerFrame = new(mockSite.Object);

bool result = designerFrame.TestAccessor().Dynamic.ProcessDialogKey(Keys.Enter);

result.Should().BeFalse();
}

[Fact]
public void DesignerFrame_Constructor_SetsSiteProperty()
{
Mock<ISite> mockSite = new();

using DesignerFrame designerFrame = new(mockSite.Object);

designerFrame.Site ??= mockSite.Object;

designerFrame.Should().NotBeNull();
designerFrame.Site.Should().Be(mockSite.Object);
}

[Fact]
public void DesignerFrame_AddControl_AddsControlToFrame()
{
Mock<ISite> mockSite = new();
using DesignerFrame designerFrame = new(mockSite.Object);
Control control = new();
Nora-Zhou01 marked this conversation as resolved.
Show resolved Hide resolved

designerFrame.Controls.Add(control);

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

[Fact]
public void DesignerFrame_Resize_TriggersExpectedBehavior()
{
Mock<ISite> mockSite = new();
using DesignerFrame designerFrame = new(mockSite.Object);

designerFrame.Resize += (sender, args) => { /* Handle resize if needed */ };

designerFrame.Width = 500;

designerFrame.Width.Should().Be(500);
}

[Fact]
public void Designer_ProcessDialogKey()
{
Mock<ISite> mockSite = new();
using DesignerFrame designerFrame = new(mockSite.Object);

bool result = designerFrame.TestAccessor().Dynamic.ProcessDialogKey(Keys.Enter);

result.Should().BeFalse();
}
}