Skip to content

Commit

Permalink
Dispose controls that using in unit tests (#11751)
Browse files Browse the repository at this point in the history
* Dispose controls that using in unit tests

* Add try catch in tests and use omit type in new()

* Removing FromHandle_TestData for tests "FromHandle"

* Using TheoryData

* Split test data
  • Loading branch information
LeafShi1 authored Aug 8, 2024
1 parent 56782c0 commit 481f7e0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace System.Drawing.Design;
[CLSCompliant(false)]
public partial class ColorEditor : UITypeEditor
{
private ColorUI? _colorUI;

/// <summary>
/// Edits the given object value using the editor style provided by ColorEditor.GetEditStyle.
/// </summary>
Expand All @@ -25,7 +23,7 @@ public partial class ColorEditor : UITypeEditor
return value;
}

_colorUI ??= new ColorUI(this);
using ColorUI? _colorUI = new(this);

_colorUI.Start(editorService, value);
editorService.DropDownControl(_colorUI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,30 +223,44 @@ public void ControlAccessibleObject_Description_GetWithAccessibleDescription_Ret
Assert.Equal(accessibleDescription, accessibleObject.Description);
}

public static IEnumerable<object[]> Handle_Set_TestData()
public static TheoryData<object> Handle_Set_DataTestData()
{
yield return new object[] { IntPtr.Zero };
yield return new object[] { (IntPtr)(-1) };
yield return new object[] { (IntPtr)1 };
yield return new object[] { (IntPtr)250 };
yield return new object[] { new Control().Handle };
return new TheoryData<object>
{
null,
IntPtr.Zero,
new IntPtr(-1),
new IntPtr(1),
new IntPtr(250)
};
}

[WinFormsTheory]
[MemberData(nameof(Handle_Set_TestData))]
public void ControlAccessibleObject_Handle_Set_Success(IntPtr value)
[MemberData(nameof(Handle_Set_DataTestData))]
public void ControlAccessibleObject_Handle_Set_Success(object testValue)
{
IntPtr value;
if (testValue is null)
{
using Control control = new();
value = control.Handle;
}
else
{
value = (IntPtr)testValue;
}

using Control ownerControl = new();
ownerControl.CreateControl();
Assert.True(ownerControl.IsHandleCreated);
var accessibleObject = new Control.ControlAccessibleObject(ownerControl);
Assert.True(ownerControl.IsHandleCreated);

// Set empty.
// Set value.
accessibleObject.Handle = value;
Assert.Equal(value, accessibleObject.Handle);

// Set same.
// Set same value.
accessibleObject.Handle = value;
Assert.Equal(value, accessibleObject.Handle);
}
Expand Down Expand Up @@ -1361,7 +1375,7 @@ public void ControlAccessibleObject_GetPropertyValue_Default_Name_ReturnsExpecte
else
{
Assert.Equal(VARIANT.Empty, controlAccessibleObject.GetPropertyValue(UIA_PROPERTY_ID.UIA_NamePropertyId));
}
}
}

public static IEnumerable<object[]> ControlAccessibleObject_GetPropertyValue_ControlTypeProperty_ReturnsCorrectValue_TestData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ public void AxHost_EndInit_Invoke_Nop()
[WinFormsFact]
public void AxHost_EndInit_InvokeWithParent_CreatesControl()
{
Control parent = new();
using Control parent = new();
using SubAxHost control = new(WebBrowserClsidString)
{
Parent = parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,34 +1119,48 @@ public void ControlCollection_Add_TopLevelValue_ThrowsArgumentException()
public void ControlCollection_Add_DifferentThreadValueOwner_ThrowsArgumentException()
{
Control owner = null;
Thread thread = new(() =>
try
{
owner = new Control();
Assert.NotEqual(IntPtr.Zero, owner.Handle);
});
thread.Start();
thread.Join();
Thread thread = new(() =>
{
owner = new Control();
Assert.NotEqual(IntPtr.Zero, owner.Handle);
});
thread.Start();
thread.Join();

using Control control = new();
var collection = new Control.ControlCollection(owner);
Assert.Throws<ArgumentException>(() => collection.Add(control));
using Control control = new();
var collection = new Control.ControlCollection(owner);
Assert.Throws<ArgumentException>(() => collection.Add(control));
}
finally
{
owner?.Dispose();
}
}

[Fact] // cross-thread access
public void ControlCollection_Add_DifferentThreadValueControl_ThrowsArgumentException()
{
Control control = null;
Thread thread = new(() =>
try
{
control = new Control();
Assert.NotEqual(IntPtr.Zero, control.Handle);
});
thread.Start();
thread.Join();
Thread thread = new(() =>
{
control = new Control();
Assert.NotEqual(IntPtr.Zero, control.Handle);
});
thread.Start();
thread.Join();

using Control owner = new();
var collection = new Control.ControlCollection(owner);
Assert.Throws<ArgumentException>(() => collection.Add(control));
using Control owner = new();
var collection = new Control.ControlCollection(owner);
Assert.Throws<ArgumentException>(() => collection.Add(control));
}
finally
{
control?.Dispose();
}
}

[WinFormsFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ public void Control_AccessibilityObject_GetWithHandle_ReturnsExpected()
Assert.Same(control, accessibleObject.Owner);
}

public static IEnumerable<object[]> AccessibilityObject_CustomCreateAccessibilityInstance_TestData()
public static TheoryData<AccessibleObject, AccessibleObject> AccessibilityObject_CustomCreateAccessibilityInstance_TestData()
{
yield return new object[] { null, null };

AccessibleObject accessibleObject = new();
yield return new object[] { accessibleObject, accessibleObject };

var controlAccessibleObject = new Control.ControlAccessibleObject(new Control());
yield return new object[] { controlAccessibleObject, controlAccessibleObject };
return new TheoryData<AccessibleObject, AccessibleObject>
{
{ null, null },
{ accessibleObject, accessibleObject },
};
}

[WinFormsTheory]
Expand All @@ -79,6 +78,24 @@ public void Control_AccessibilityObject_GetCustomCreateAccessibilityInstance_Ret
}
}

[WinFormsFact]
public void Control_AccessibilityObject_GetCustomCreateAccessibilityInstance_WithRealControlAccessibleObject_ReturnsExpected()
{
using Control control1 = new();
var controlAccessibleObject = new Control.ControlAccessibleObject(control1);

using (new NoAssertContext())
{
using CustomCreateAccessibilityInstanceControl control = new()
{
CreateAccessibilityResult = controlAccessibleObject
};
Assert.Same(controlAccessibleObject, control.AccessibilityObject);
Assert.Same(control.AccessibilityObject, control.AccessibilityObject);
Assert.False(control.IsHandleCreated);
}
}

private class CustomCreateAccessibilityInstanceControl : Control
{
public AccessibleObject CreateAccessibilityResult { get; set; }
Expand Down Expand Up @@ -10506,7 +10523,7 @@ public static TheoryData<int, bool> Get_Control_ShowFocusCues_GetWithHandleMessa
}

[WinFormsTheory]
[CommonMemberData(typeof(ControlTests), nameof(ControlTests.Get_Control_ShowFocusCues_GetWithHandleMessageSent_ReturnsExpected))]
[CommonMemberData(typeof(ControlTests), nameof(Get_Control_ShowFocusCues_GetWithHandleMessageSent_ReturnsExpected))]
public void Control_ShowFocusCues_GetWithHandleMessageSent_ReturnsExpected(int wParam, bool expected)
{
using SubControl control = new();
Expand Down Expand Up @@ -10565,7 +10582,7 @@ public static TheoryData<int, bool> Get_Control_ShowKeyboardCues_GetWithHandleMe
}

[WinFormsTheory]
[CommonMemberData(typeof(ControlTests), nameof(ControlTests.Get_Control_ShowKeyboardCues_GetWithHandleMessageSent_ReturnsExpected))]
[CommonMemberData(typeof(ControlTests), nameof(Get_Control_ShowKeyboardCues_GetWithHandleMessageSent_ReturnsExpected))]
public void Control_ShowKeyboardCues_GetWithHandleMessageSent_ReturnsExpected(int wParam, bool expected)
{
using SubControl control = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,28 @@ public void Screen_FromControl_NullControl_ThrowsArgumentNullException()
Assert.Throws<ArgumentNullException>("control", () => Screen.FromControl(null));
}

public static IEnumerable<object[]> FromHandle_TestData()
[Fact]
public void Screen_FromHandle_ZeroHandle_ReturnsExpected()
{
yield return new object[] { IntPtr.Zero };
yield return new object[] { new Control().Handle };
Screen screen = Screen.FromHandle(IntPtr.Zero);
Assert.NotNull(screen);
VerifyScreen(screen);
}

[Theory]
[MemberData(nameof(FromHandle_TestData))]
public void Screen_FromHandle_Invoke_ReturnsExpected(IntPtr handle)
[WinFormsFact]
public void Screen_FromHandle_RealHandle_ReturnsExpected()
{
Screen screen = Screen.FromHandle(handle);
Assert.NotNull(screen);
VerifyScreen(screen);
Control control = new();
try
{
Screen screen = Screen.FromHandle(control.Handle);
Assert.NotNull(screen);
VerifyScreen(screen);
}
finally
{
control?.Dispose();
}
}

public static IEnumerable<object[]> FromPoint_TestData()
Expand Down

0 comments on commit 481f7e0

Please sign in to comment.