Skip to content

Commit

Permalink
Adds code coverage for FormatStringDialog
Browse files Browse the repository at this point in the history
Related dotnet#10773

## Proposed changes

- Adds code coverage for `FormatStringDialog`.

## Customer Impact

- None

## Regression?

- No

## Risk

- Minimal

## Test methodology

- Unit tests

## Test environment(s)

- 9.0.100-rc.1.24452.12
  • Loading branch information
Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box) committed Sep 24, 2024
1 parent 34dc978 commit c85431f
Showing 1 changed file with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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 Moq;

namespace System.Windows.Forms.Design.Tests;

public class FormatStringDialogTests : IDisposable
{
private readonly FormatStringDialog _formatStringDialog;

private readonly Mock<ITypeDescriptorContext> _context = new(MockBehavior.Strict);

public FormatStringDialogTests() => _formatStringDialog = new(_context.Object);

public void Dispose() => _formatStringDialog.Dispose();

[Fact]
public void Ctor_WithContext_DoesNotThrow()
{
Action action = () => new FormatStringDialog(_context.Object);

action.Should().NotThrow();
}

[Fact]
public void FormatStringDialog_HasProperRTLConfiguration()
{
if (SR.RTL == "RTL_False")
{
_formatStringDialog!.RightToLeft.Should().Be(RightToLeft.No);
_formatStringDialog!.RightToLeftLayout.Should().Be(false);
}
else
{
_formatStringDialog!.RightToLeft.Should().Be(RightToLeft.Yes);
_formatStringDialog!.RightToLeftLayout.Should().Be(true);
}
}

[Fact]
public void SetsDataGridViewCellStyle_DoesNotThrow()
{
Action action = () => _formatStringDialog.DataGridViewCellStyle = new();

action.Should().NotThrow();
}

[Fact]
public void Dirty_ReturnsDefault() => _formatStringDialog.Dirty.Should().Be(false);

[Fact]
public void SetsListControl_DoesNotThrow()
{
Action action = () =>
{
using ListBox listControl = new();
_formatStringDialog.ListControl = listControl;
};
action.Should().NotThrow();
}

[Fact]
public void End_DoesNotThrow()
{
Action action = FormatStringDialog.End;

action.Should().NotThrow();
}

[Fact]
public void FormatControlFinishedLoading_AdjustsButtonPositionsCorrectly()
{
using dynamic? okButtonField = _formatStringDialog.TestAccessor().Dynamic._okButton;
using dynamic? cancelButtonField = _formatStringDialog.TestAccessor().Dynamic._cancelButton;
using dynamic? formatControlField = _formatStringDialog.TestAccessor().Dynamic._formatControl1;

int okButtonLeftOriginalState = okButtonField.Left;
int cancelButtonLeftOriginalState = cancelButtonField.Left;

int GetRightSideOffset(Control ctl)
{
int result = ctl.Width;
Control? control = ctl;
while (control is not null)
{
result += control.Left;
control = control.Parent;
}

return result;
}

int formatControlRightSideOffset = GetRightSideOffset(formatControlField);
int cancelButtonRightSideOffset = GetRightSideOffset(cancelButtonField);

_formatStringDialog.FormatControlFinishedLoading();

((object)okButtonField.Top).Should().Be(formatControlField.Bottom + 5);
((object)cancelButtonField.Top).Should().Be(formatControlField.Bottom + 5);
((object)okButtonField.Left).Should().Be(okButtonLeftOriginalState + formatControlRightSideOffset - cancelButtonRightSideOffset);
((object)cancelButtonField.Left).Should().Be(cancelButtonLeftOriginalState + formatControlRightSideOffset - cancelButtonRightSideOffset);
}
}

0 comments on commit c85431f

Please sign in to comment.