From c1dda6f30bf871e9a7e0cb1083da12c2e2c86ed4 Mon Sep 17 00:00:00 2001 From: Vladimir Krestov Date: Wed, 9 Feb 2022 15:54:54 +0300 Subject: [PATCH] Reworked conditional attributes to have ability to add several unsupported architectures Fixes #6653 --- .../UIIntegrationTests/ImageListTests.cs | 2 +- .../ConditionalWinFormsFactAttribute.cs | 42 +++++++++---------- .../ConditionalWinFormsTheoryAttribute.cs | 35 +++++++--------- .../System/Windows/Forms/DataGridViewTests.cs | 14 +++---- .../System/Windows/Forms/RichTextBoxTests.cs | 2 +- 5 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ImageListTests.cs b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ImageListTests.cs index a209082a0e2..804e0f7f20f 100644 --- a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ImageListTests.cs +++ b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ImageListTests.cs @@ -24,7 +24,7 @@ public ImageListTests(ITestOutputHelper testOutputHelper) } [ActiveIssue("https://github.com/dotnet/winforms/issues/6635")] - [ConditionalWinFormsFact(UnsupportedArchitecture = Architecture.X86, + [ConditionalWinFormsFact(Architecture.X86, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6635")] public void ImageList_FinalizerReleasesNativeHandle_ReturnsExpected() { diff --git a/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsFactAttribute.cs b/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsFactAttribute.cs index 8a103006600..210c5bf540f 100644 --- a/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsFactAttribute.cs +++ b/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsFactAttribute.cs @@ -7,19 +7,34 @@ namespace System { + /// + /// Extension of WinFormsFact attribute. Allows to skip specific test cases only, + /// depending on a runtime architecture. This attribute must have unsupported architectures. + /// public class ConditionalWinFormsFactAttribute : WinFormsFactAttribute { - private Architecture _unsupportedArchitecture; - private bool _isSkipRequired; - private string _skipReason; + private readonly bool _isSkipRequired; + + /// + /// Defines the architectures to skip the test on. + /// This is complementary to property. + /// + public ConditionalWinFormsFactAttribute(params Architecture[] unsupportedArchitectures) + { + if (unsupportedArchitectures.Length == 0) + { + throw new InvalidOperationException("The conditional attribute must have unsupported architectures. " + + "If you need to run a test in all artichectures, please use WinFormsFact"); + } + + _isSkipRequired = unsupportedArchitectures.Contains(RuntimeInformation.ProcessArchitecture); + } public override string Skip { get => base.Skip; set { - _skipReason = value; - if (_isSkipRequired) { base.Skip = value; @@ -28,22 +43,5 @@ public override string Skip // Don't skip a test case if the skip condition is false. } } - - /// - /// Defines the architecture to skip the test on. This is complementary to parameter. - /// - public Architecture UnsupportedArchitecture - { - get => _unsupportedArchitecture; - set - { - _unsupportedArchitecture = value; - _isSkipRequired = RuntimeInformation.ProcessArchitecture == value; - if (_isSkipRequired && _skipReason is not null) - { - Skip = _skipReason; - } - } - } } } diff --git a/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsTheoryAttribute.cs b/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsTheoryAttribute.cs index c75175c3718..2ba0a53fcf7 100644 --- a/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsTheoryAttribute.cs +++ b/src/System.Windows.Forms/tests/TestUtilities/ConditionalWinFormsTheoryAttribute.cs @@ -3,45 +3,42 @@ // See the LICENSE file in the project root for more information. using System.Runtime.InteropServices; +using System.Windows.Forms; using Xunit; namespace System { public class ConditionalWinFormsTheoryAttribute : WinFormsTheoryAttribute { - private Architecture _unsupportedArchitecture; - private bool _isSkipRequired; + private Architecture[] _unsupportedArchitectures; private string _skipReason; public override string Skip { get => base.Skip; - set - { - _skipReason = value; - - if (_isSkipRequired) - { - base.Skip = value; - } - - // Don't skip a test case if the skip condition is false. - } + set => _skipReason = value; } /// /// Defines the architecture to skip the test on. This is complementary to parameter. /// - public Architecture UnsupportedArchitecture + public Architecture[] UnsupportedArchitectures { - get => _unsupportedArchitecture; + get => _unsupportedArchitectures; set { - _unsupportedArchitecture = value; - _isSkipRequired = RuntimeInformation.ProcessArchitecture == value; - if (_isSkipRequired && _skipReason is not null) + if (value is null || value.Length == 0) + { + throw new InvalidOperationException("The conditional attribute must have unsupported architectures. " + + "If you need to run a test in all artichectures, please use WinFormsFact"); + } + + _unsupportedArchitectures = value; + + if (value.Contains(RuntimeInformation.ProcessArchitecture)) // If skipping is required { - Skip = _skipReason; + _skipReason.ThrowIfNullOrEmpty(); // Conditional attribute must have a skip reason + base.Skip = _skipReason; } } } diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewTests.cs index d0524b887ba..29407559f84 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/DataGridViewTests.cs @@ -171,7 +171,7 @@ public static IEnumerable ColumnHeadersHeight_SetWithHandle_TestData() } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [MemberData(nameof(ColumnHeadersHeight_SetWithHandle_TestData))] public void DataGridView_ColumnHeadersHeight_SetWithHandle_GetReturnsExpected(DataGridViewColumnHeadersHeightSizeMode columnHeadersWidthSizeMode, bool columnHeadersVisible, bool autoSize, int value, int expectedValue, int expectedInvalidatedCallCount) @@ -240,7 +240,7 @@ public static IEnumerable ColumnHeadersHeight_SetWithParentWithHandle_ } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [MemberData(nameof(ColumnHeadersHeight_SetWithParentWithHandle_TestData))] public void DataGridView_ColumnHeadersHeight_SetWithParentWithHandle_GetReturnsExpected(DataGridViewColumnHeadersHeightSizeMode columnHeadersWidthSizeMode, bool columnHeadersVisible, bool autoSize, int value, int expectedValue, int expectedInvalidatedCallCount, int expectedLayoutCallCount, int expectedParentLayoutCallCount) @@ -530,7 +530,7 @@ public static IEnumerable ColumnHeadersHeightSizeMode_SetWithHandle_Te } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [MemberData(nameof(ColumnHeadersHeightSizeMode_SetWithHandle_TestData))] public void DataGridView_ColumnHeadersHeightSizeMode_SetWithHandle_GetReturnsExpected(bool columnHeadersVisible, DataGridViewColumnHeadersHeightSizeMode value, int expectedColumnHeadersHeight, int expectedInvalidatedCallCount) @@ -566,7 +566,7 @@ public void DataGridView_ColumnHeadersHeightSizeMode_SetWithHandle_GetReturnsExp } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [InlineData(DataGridViewColumnHeadersHeightSizeMode.DisableResizing, DataGridViewColumnHeadersHeightSizeMode.AutoSize)] [InlineData(DataGridViewColumnHeadersHeightSizeMode.EnableResizing, DataGridViewColumnHeadersHeightSizeMode.AutoSize)] @@ -1474,7 +1474,7 @@ public static IEnumerable RowHeadersWidthSizeMode_SetWithHandle_TestDa } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [MemberData(nameof(RowHeadersWidthSizeMode_SetWithHandle_TestData))] public void DataGridView_RowHeadersWidthSizeMode_SetWithHandle_GetReturnsExpected(bool rowHeadersVisible, DataGridViewRowHeadersWidthSizeMode value, int expectedRowHeadersWidth, int expectedInvalidatedCallCount) @@ -1510,7 +1510,7 @@ public void DataGridView_RowHeadersWidthSizeMode_SetWithHandle_GetReturnsExpecte } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [InlineData(DataGridViewRowHeadersWidthSizeMode.DisableResizing, DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)] [InlineData(DataGridViewRowHeadersWidthSizeMode.DisableResizing, DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders)] @@ -2446,7 +2446,7 @@ public void DataGridView_OnRowHeadersWidthChanged_Invoke_CallsRowHeadersWidthCha } [ActiveIssue("https://github.com/dotnet/winforms/issues/6597")] - [ConditionalWinFormsTheory(UnsupportedArchitecture = Architecture.Arm64, + [ConditionalWinFormsTheory(UnsupportedArchitectures = new[] { Architecture.Arm64 }, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6597")] [MemberData(nameof(OnRowHeadersWidthChanged_TestData))] public void DataGridView_OnRowHeadersWidthChanged_InvokeWithHandle_CallsRowHeadersWidthChanged(DataGridViewRowHeadersWidthSizeMode rowHeadersWidthSizeMode, bool rowHeadersVisible, EventArgs eventArgs) diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/RichTextBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/RichTextBoxTests.cs index ffb5a25cd2c..ad7dc4f5af8 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/RichTextBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/RichTextBoxTests.cs @@ -6876,7 +6876,7 @@ public static IEnumerable RichTextBox_Text_GetWithHandle_TestData() // NOTE: do not convert this into a theory as it will run hundreds of tests // and with that will cycle through hundreds of UI controls. [ActiveIssue("https://github.com/dotnet/winforms/issues/6609")] - [ConditionalWinFormsFact(UnsupportedArchitecture = Architecture.X86, + [ConditionalWinFormsFact(Architecture.X86, Skip = "Flaky tests, see: https://github.com/dotnet/winforms/issues/6609")] public void RichTextBox_Text_GetWithHandle_ReturnsExpected() {