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

Remove dependency on RemoteExecutor in tests #7500

Closed
Closed
Show file tree
Hide file tree
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,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using Xunit.Abstractions;

namespace System.Windows.Forms.UITests
{
public class ParkingWindowTests : ControlTestBase
{
public ParkingWindowTests(ITestOutputHelper testOutputHelper)
: base(testOutputHelper)
{
}

[WinFormsFact]
public void ParkingWindow_DoesNotThrowOnGarbageCollecting()
{
Control.CheckForIllegalCrossThreadCalls = true;

using Form form = InitFormWithControlToGarbageCollect();

try
{
// Force garbage collecting to access combobox from another (GC) thread.
GC.Collect();

GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
Assert.True(ex is null, $"Expected no exception, but got: {ex?.Message}"); // Actually need to check whether GC.Collect() does not throw exception.
}
}

private Form InitFormWithControlToGarbageCollect()
{
Form form = new Form();
ComboBox? comboBox = new ComboBox
{
DropDownStyle = ComboBoxStyle.DropDown
};

form.Controls.Add(comboBox);
form.Show();

// Park combobox handle in ParkingWindow.
comboBox.Parent = null;

// Recreate combobox handle to set parent to ParkingWindow.
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;

// Lose the reference to combobox to allow Garbage collecting combobox.
willibrandon marked this conversation as resolved.
Show resolved Hide resolved
comboBox = null;

return form;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System.Runtime.InteropServices;
using Microsoft.DotNet.RemoteExecutor;
using Xunit;
using static System.Windows.Forms.Application;
using static Interop;
Expand All @@ -12,53 +11,6 @@ namespace System.Windows.Forms.Tests
{
public class ParkingWindowTests
{
[WinFormsFact(Skip = "Crash with AbandonedMutexException. See: https://github.com/dotnet/arcade/issues/5325")]
public void ParkingWindow_DoesNotThrowOnGarbageCollecting()
{
using RemoteInvokeHandle invokerHandle = RemoteExecutor.Invoke(() =>
{
Control.CheckForIllegalCrossThreadCalls = true;

Form form = InitFormWithControlToGarbageCollect();

try
{
// Force garbage collecting to access combobox from another (GC) thread.
GC.Collect();

GC.WaitForPendingFinalizers();
}
catch (Exception ex)
{
Assert.True(ex is null, "Expected no exception, but got: " + ex.Message); // Actually need to check whether GC.Collect() does not throw exception.
}
});

// verify the remote process succeeded
Assert.Equal(RemoteExecutor.SuccessExitCode, invokerHandle.ExitCode);
}

private Form InitFormWithControlToGarbageCollect()
{
Form form = new Form();
ComboBox comboBox = new ComboBox();
comboBox.DropDownStyle = ComboBoxStyle.DropDown;

form.Controls.Add(comboBox);
form.Show();

// Park combobox handle in ParkingWindow.
comboBox.Parent = null;

// Recreate combobox handle to set parent to ParkingWindow.
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;

// Lose the reference to combobox to allow Garbage collecting combobox.
comboBox = null;

return form;
}

[WinFormsFact]
public void ParkingWindow_Unaware()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms.VisualStyles;
using Microsoft.DotNet.RemoteExecutor;
using System.Windows.Forms.TestUtilities;
using System.Windows.Forms.VisualStyles;
using Xunit;
using static Interop;

namespace System.Windows.Forms.Tests
{
Expand All @@ -23,50 +21,46 @@ public void Application_CurrentCulture_Get_ReturnsExpected()

public static IEnumerable<object[]> CurrentCulture_Set_TestData()
{
yield return new object[] { CultureInfo.InvariantCulture, 0x7Fu };
yield return new object[] { new CultureInfo("en"), 0x9u };
yield return new object[] { new CultureInfo("fr-FR"), 0x40Cu };
yield return new object[] { new CultureInfo("en-DK"), 0xC00u };
yield return new object[] { new CultureInfo("haw"), 0x00000075u };
yield return new object[] { new CultureInfo("en-US"), 0x00000409u };
yield return new object[] { new CultureInfo("de-DE_phoneb"), 0x00010407u };
yield return new object[] { new CustomLCIDCultureInfo(10), 0x409u };
yield return new object[] { new CustomLCIDCultureInfo(0), 0x409u };
yield return new object[] { new CustomLCIDCultureInfo(-1), 0x409u };
yield return new object[] { CultureInfo.InvariantCulture };
yield return new object[] { new CultureInfo("en") };
yield return new object[] { new CultureInfo("fr-FR") };
yield return new object[] { new CultureInfo("en-DK") };
yield return new object[] { new CultureInfo("haw") };
yield return new object[] { new CultureInfo("en-US") };
yield return new object[] { new CultureInfo("de-DE_phoneb") };
yield return new object[] { new CustomLCIDCultureInfo(10) };
yield return new object[] { new CustomLCIDCultureInfo(0) };
yield return new object[] { new CustomLCIDCultureInfo(-1) };
}

[WinFormsFact(Skip = "Crash with AbandonedMutexException. See: https://github.com/dotnet/arcade/issues/5325")]
[WinFormsFact]
public void Application_CurrentCulture_Set_GetReturnsExpected()
{
RemoteExecutor.Invoke(() =>
foreach (object[] testData in CurrentCulture_Set_TestData())
{
foreach (object[] testData in CurrentCulture_Set_TestData())
CultureInfo value = (CultureInfo)testData[0];

CultureInfo oldValue = Application.CurrentCulture;
try
{
CultureInfo value = (CultureInfo)testData[0];
uint expectedLcid = (uint)testData[1];

CultureInfo oldValue = Application.CurrentCulture;
try
{
Application.CurrentCulture = value;
Assert.Same(value, Application.CurrentCulture);
Assert.Same(value, Thread.CurrentThread.CurrentCulture);
Assert.Same(value, CultureInfo.CurrentCulture);
Assert.Equal(expectedLcid, Kernel32.GetThreadLocale());

// Set same.
Application.CurrentCulture = value;
Assert.Same(value, Application.CurrentCulture);
Assert.Same(value, Thread.CurrentThread.CurrentCulture);
Assert.Same(value, CultureInfo.CurrentCulture);
Assert.Equal(expectedLcid, Kernel32.GetThreadLocale());
}
finally
{
Application.CurrentCulture = oldValue;
}
Application.CurrentCulture = value;
Assert.Same(value, Application.CurrentCulture);
Assert.Same(value, Thread.CurrentThread.CurrentCulture);
Assert.Same(value, CultureInfo.CurrentCulture);
Assert.Equal(value.LCID, CultureInfo.CurrentCulture.LCID);

// Set same.
Application.CurrentCulture = value;
Assert.Same(value, Application.CurrentCulture);
Assert.Same(value, Thread.CurrentThread.CurrentCulture);
Assert.Same(value, CultureInfo.CurrentCulture);
Assert.Equal(value.LCID, CultureInfo.CurrentCulture.LCID);
}
}).Dispose();
finally
{
Application.CurrentCulture = oldValue;
}
}
}

[WinFormsFact]
Expand All @@ -75,31 +69,23 @@ public void Application_CurrentCulture_SetNull_ThrowsArgumentNullException()
Assert.Throws<ArgumentNullException>("value", () => Application.CurrentCulture = null);
}

[WinFormsFact(Skip = "Crash with AbandonedMutexException. See: https://github.com/dotnet/arcade/issues/5325")]
[WinFormsFact]
willibrandon marked this conversation as resolved.
Show resolved Hide resolved
public void Application_EnableVisualStyles_InvokeBeforeGettingRenderWithVisualStyles_Success()
{
RemoteExecutor.Invoke(() =>
{
Application.EnableVisualStyles();
Assert.True(Application.UseVisualStyles);
Assert.True(Application.RenderWithVisualStyles);
}).Dispose();
Application.EnableVisualStyles();
Assert.True(Application.UseVisualStyles);
Assert.True(Application.RenderWithVisualStyles);
}

[WinFormsFact(Skip = "Crash with AbandonedMutexException. See: https://github.com/dotnet/arcade/issues/5325")]
[WinFormsFact]
public void Application_EnableVisualStyles_InvokeAfterGettingRenderWithVisualStyles_Success()
{
// This is not a recommended scenario per https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.application.enablevisualstyles
// EnableVisualStyles should be executed before any control-related code is.
RemoteExecutor.Invoke(() =>
{
Assert.False(Application.UseVisualStyles);
Assert.False(Application.RenderWithVisualStyles);
Assert.True(Application.UseVisualStyles);
Assert.True(Application.RenderWithVisualStyles);

Application.EnableVisualStyles();
Assert.True(Application.UseVisualStyles, "New Visual Styles will not be applied on Winforms app. This is a high priority bug and must be looked into");
Assert.True(Application.RenderWithVisualStyles);
}).Dispose();
Application.EnableVisualStyles();
Assert.True(Application.UseVisualStyles, "New Visual Styles will not be applied on Winforms app. This is a high priority bug and must be looked into");
Assert.True(Application.RenderWithVisualStyles);
}

[WinFormsFact]
Expand All @@ -117,27 +103,23 @@ public void Application_VisualStyleState_Get_ReturnsExpected()
Assert.Equal(state, Application.VisualStyleState);
}

[WinFormsTheory(Skip = "Crash with AbandonedMutexException. See: https://github.com/dotnet/arcade/issues/5325")]
[WinFormsTheory]
[CommonMemberData(typeof(CommonTestHelper), nameof(CommonTestHelper.GetEnumTypeTheoryData), typeof(VisualStyleState))]
[CommonMemberData(typeof(CommonTestHelper), nameof(CommonTestHelper.GetEnumTypeTheoryDataInvalid), typeof(VisualStyleState))]
public void Application_VisualStyleState_Set_ReturnsExpected(VisualStyleState valueParam)
public void Application_VisualStyleState_Set_ReturnsExpected(VisualStyleState value)
{
// This needs to be in RemoteExecutor.Invoke because changing Application.VisualStyleState
// sends WM_THEMECHANGED to all controls, which can cause a deadlock if another test fails.
RemoteExecutor.Invoke((valueString) =>
VisualStyleState state = Application.VisualStyleState;
try
{
VisualStyleState value = Enum.Parse<VisualStyleState>(valueString);
VisualStyleState state = Application.VisualStyleState;
try
{
Application.VisualStyleState = value;
Assert.Equal(value, Application.VisualStyleState);
}
finally
{
Application.VisualStyleState = state;
}
}, valueParam.ToString());
Application.VisualStyleState = value;
Assert.Equal(value, Application.VisualStyleState);
}
finally
{
Application.VisualStyleState = state;
}
}

[Fact]
Expand Down