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

Replace EnumValidator - Unit Tests and Changing the Validator #4782

Merged
2 commits merged into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -382,10 +382,7 @@ public ContentAlignment ImageAlign
}
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (value != _imageAlign)
{
Expand Down Expand Up @@ -671,10 +668,7 @@ public virtual ContentAlignment TextAlign
}
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (value == TextAlign)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ public ContentAlignment CheckAlign
}
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (_checkAlign != value)
{
Expand Down
10 changes: 2 additions & 8 deletions src/System.Windows.Forms/src/System/Windows/Forms/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,7 @@ public ContentAlignment ImageAlign
}
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (value != ImageAlign)
{
Expand Down Expand Up @@ -724,10 +721,7 @@ public virtual ContentAlignment TextAlign
}
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (TextAlign != value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,7 @@ public ContentAlignment CheckAlign
}
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

checkAlign = value;
if (OwnerDraw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,7 @@ public ContentAlignment ControlAlign
get => _controlAlign;
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (_controlAlign != value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -982,10 +982,7 @@ public ContentAlignment ImageAlign
get => _imageAlign;
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (_imageAlign != value)
{
Expand Down Expand Up @@ -1875,10 +1872,7 @@ public virtual ContentAlignment TextAlign
get => _textAlign;
set
{
if (!WindowsFormsUtils.EnumValidator.IsValidContentAlignment(value))
{
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(ContentAlignment));
}
SourceGenerated.EnumValidator.Validate(value);

if (_textAlign != value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,6 @@ public static string GetComponentName(IComponent component, string defaultNameVa

public static class EnumValidator
{
/// <summary>
/// Valid values are 0x001,0x002,0x004, 0x010,0x020,0x040, 0x100, 0x200,0x400
/// Method for verifying
/// Verify that the number passed in has only one bit on
/// Verify that the bit that is on is a valid bit by bitwise anding it to a mask.
/// </summary>
public static bool IsValidContentAlignment(ContentAlignment contentAlign)
{
if (BitOperations.PopCount((uint)contentAlign) != 1)
{
return false;
}

// to calculate:
// foreach (int val in Enum.GetValues(typeof(ContentAlignment))) { mask |= val; }
int contentAlignmentMask = 0x777;
return ((contentAlignmentMask & (int)contentAlign) != 0);
}

/// <summary>
/// shifts off the number of bits specified by numBitsToShift
/// - makes sure the bits we've shifted off are just zeros
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms.Design;
using WinForms.Common.Tests;
using Xunit;
using static Interop;

Expand Down Expand Up @@ -242,6 +244,48 @@ public void Label_Invokes_SetToolTip_IfExternalToolTipIsSet()
Assert.True(actual);
}

[WinFormsTheory]
[CommonMemberData(nameof(CommonTestHelper.GetEnumTypeTheoryDataInvalid), typeof(ContentAlignment))]
public void Label_ImageAlign_SetInvalidValue_ThrowsInvalidEnumArgumentException(ContentAlignment value)
{
using var control = new Label();
Assert.Throws<InvalidEnumArgumentException>("value", () => control.ImageAlign = value);
}

public static IEnumerable<object[]> ImageAlign_Set_TestData()
{
foreach (bool autoSize in new bool[] { true, false })
{
foreach (ContentAlignment value in Enum.GetValues(typeof(ContentAlignment)))
{
yield return new object[] { autoSize, value };
}
}
}

[WinFormsTheory]
[MemberData(nameof(ImageAlign_Set_TestData))]
public void Label_ImageAlign_Set_GetReturnsExpected(bool autoSize, ContentAlignment value)
{
using var control = new Label
{
AutoSize = autoSize
};
int layoutCallCount = 0;
control.Layout += (sender, e) => layoutCallCount++;

control.ImageAlign = value;
Assert.Equal(value, control.ImageAlign);
Assert.Equal(0, layoutCallCount);
Assert.False(control.IsHandleCreated);

// Set same.
control.ImageAlign = value;
Assert.Equal(value, control.ImageAlign);
Assert.Equal(0, layoutCallCount);
Assert.False(control.IsHandleCreated);
}

public class SubLabel : Label
{
public new bool CanEnableIme => base.CanEnableIme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public void RadioButton_CreateParams_GetUserPaint_ReturnsExpected(bool userPaint
Assert.False(control.IsHandleCreated);
}

[WinFormsTheory]
[CommonMemberData(nameof(CommonTestHelper.GetEnumTypeTheoryDataInvalid), typeof(ContentAlignment))]
public void RadioButton_CheckAlign_SetInvalidValue_ThrowsInvalidEnumArgumentException(ContentAlignment value)
{
using var control = new RadioButton();
Assert.Throws<InvalidEnumArgumentException>("value", () => control.CheckAlign = value);
}

[WinFormsTheory]
[CommonMemberData(nameof(CommonTestHelper.GetBoolTheoryData))]
public void RadioRadioButton_TabStop_Set_GetReturnsExpected(bool value)
Expand Down