Skip to content

Commit

Permalink
NativeAOT tests interopservices (#73200)
Browse files Browse the repository at this point in the history
* Update InteropServices tests for NativeAOT

* Remove SetWin32ContextInIDispatchAttribute attribute and tests.
  - this was no longer in the ref assembly so just removing it.

* Remove dependency on registered COM server. This enables more
  expansive testing on all Windows SKUs.
  • Loading branch information
AaronRobinsonMSFT authored Aug 4, 2022
1 parent ec3e934 commit 9f1dd1a
Show file tree
Hide file tree
Showing 64 changed files with 491 additions and 353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ private static bool GetLinqExpressionsBuiltWithIsInterpretingOnly()

public static bool IsInContainer => GetIsInContainer();
public static bool SupportsComInterop => IsWindows && IsNotMonoRuntime && !IsNativeAot; // matches definitions in clr.featuredefines.props

#if NETCOREAPP
public static bool IsBuiltInComEnabled => SupportsComInterop
&& (AppContext.TryGetSwitch("System.Runtime.InteropServices.BuiltInComInterop.IsSupported", out bool isEnabled)
? isEnabled
: true);
#else
public static bool IsBuiltInComEnabled => SupportsComInterop;
#endif

// Automation refers to OLE Automation support. Automation support here means the OS
// and runtime provide support for the following: IDispatch, STA apartments, etc. This
// is typically available whenever COM support is enabled, but Windows Nano Server is an exception.
public static bool IsBuiltInComEnabledWithOSAutomationSupport => IsBuiltInComEnabled && IsNotWindowsNanoServer;

public static bool SupportsSsl3 => GetSsl3Support();
public static bool SupportsSsl2 => IsWindows && !PlatformDetection.IsWindows10Version1607OrGreater;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
<Compile Include="System\Runtime\InteropServices\RegistrationClassContext.cs" />
<Compile Include="System\Runtime\InteropServices\RegistrationConnectionType.cs" />
<Compile Include="System\Runtime\InteropServices\RuntimeEnvironment.cs" />
<Compile Include="System\Runtime\InteropServices\SetWin32ContextInIDispatchAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\TypeLibFuncAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\TypeLibFuncFlags.cs" />
<Compile Include="System\Runtime\InteropServices\TypeLibImportClassAttribute.cs" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@
<Compile Include="System\Runtime\InteropServices\ProgIdAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\RuntimeEnvironmentTests.cs" />
<Compile Include="System\Runtime\InteropServices\SafeBufferTests.cs" />
<Compile Include="System\Runtime\InteropServices\SetWin32ContextInIDispatchAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\StructLayoutAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\TypeIdentifierAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\UnknownWrapperTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DispAttributeClass
public void Event() { }
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_ComObjectWithoutComEventInterfaceAttribute_ThrowsInvalidOperationException()
{
var attribute = new ComAwareEventInfo(typeof(NonComObject), nameof(NonComObject.Event));
Expand All @@ -33,7 +33,7 @@ public void AddEventHandler_ComObjectWithoutComEventInterfaceAttribute_ThrowsInv
Assert.Throws<InvalidOperationException>(() => attribute.RemoveEventHandler(target, handler));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_ComObjectWithMultipleComEventInterfaceAttribute_ThrowsAmbiguousMatchException()
{
// C# doesn't let us apply multiple ComEventInterface values, so RefEmit is necessary.
Expand All @@ -57,7 +57,7 @@ public void AddEventHandler_ComObjectWithMultipleComEventInterfaceAttribute_Thro
Assert.Throws<AmbiguousMatchException>(() => attribute.RemoveEventHandler(target, handler));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_NullSourceTypeEventInterface_ThrowsNullReferenceException()
{
var attribute = new ComAwareEventInfo(typeof(NullSourceType), nameof(NullSourceType.Event));
Expand All @@ -74,7 +74,7 @@ public interface NullSourceType
event EventHandler Event;
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_NoSuchSourceTypeEventInterface_ThrowsArgumentNullException()
{
var attribute = new ComAwareEventInfo(typeof(NoSuchSourceType), nameof(NoSuchSourceType.Event));
Expand All @@ -91,7 +91,7 @@ public interface NoSuchSourceType
event EventHandler Event;
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_NoDispIdAttribute_ThrowsInvalidOperationException()
{
var attribute = new ComAwareEventInfo(typeof(NoDispAttributeInterface), nameof(NoDispAttributeInterface.Event));
Expand All @@ -102,7 +102,7 @@ public void AddEventHandler_NoDispIdAttribute_ThrowsInvalidOperationException()
Assert.Throws<InvalidOperationException>(() => attribute.RemoveEventHandler(target, handler));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void AddEventHandler_TargetNotIConnectionIConnectionPointContainer_ThrowsInvalidCastException()
{
var attribute = new ComAwareEventInfo(typeof(DispAttributeInterface), nameof(DispAttributeInterface.Event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ public void Combine_Unix_ThrowsPlatformNotSupportedException()
Assert.Throws<PlatformNotSupportedException>(() => ComEventsHelper.Combine(null, Guid.Empty, 1, null));
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Combine_NullRcw_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>(null, () => ComEventsHelper.Combine(null, Guid.Empty, 1, null));
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Combine_NotComObject_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>("obj", () => ComEventsHelper.Combine(1, Guid.Empty, 1, null));
Expand All @@ -35,15 +33,13 @@ public void Remove_Unix_ThrowPlatformNotSupportedException()
Assert.Throws<PlatformNotSupportedException>(() => ComEventsHelper.Remove(null, Guid.Empty, 1, null));
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Remove_NullRcw_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>(null, () => ComEventsHelper.Remove(null, Guid.Empty, 1, null));
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Remove_NotComObject_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>("obj", () => ComEventsHelper.Remove(1, Guid.Empty, 1, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace System.Runtime.InteropServices.Tests
[ComVisible(true)]
public class ComVisibleAttributeTests
{
[Fact]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void Exists()
{
Type type = typeof(ComVisibleAttributeTests);
ComVisibleAttribute attribute = Assert.IsType<ComVisibleAttribute>(Assert.Single(type.GetCustomAttributes(typeof(ComVisibleAttribute), inherit: false)));
Assert.True(attribute.Value);
}

[Theory]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
[InlineData(true)]
[InlineData(false)]
public void Ctor_Visible(bool visibility)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ public void Ctor_NonNull_ThrowsPlatformNotSupportedException(object value)
Assert.Throws<PlatformNotSupportedException>(() => new DispatchWrapper(value));
}

[Theory]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
[InlineData("")]
[InlineData(0)]
[PlatformSpecific(TestPlatforms.Windows)]
public void Ctor_NonDispatchObject_ThrowsInvalidCastException(object value)
{
Assert.Throws<InvalidCastException>(() => new DispatchWrapper(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static IEnumerable<object[]> ChangeWrapperHandleStrength_ComObject_TestDa
yield return new object[] { new AutoDualComObjectEmpty() };
}

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoServer))]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
[MemberData(nameof(ChangeWrapperHandleStrength_ComObject_TestData))]
public void ChangeWrapperHandleStrength_ComObject_ReturnsExpected(object o)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ public static IEnumerable<object[]> ChangeWrapperHandleStrength_TestData()
yield return new object[] { d };
}

[Theory]
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
[MemberData(nameof(ChangeWrapperHandleStrength_TestData))]
public void ChangeWrapperHandleStrength_ValidObject_Success(object otp)
{
Marshal.ChangeWrapperHandleStrength(otp, fIsWeak: true);
Marshal.ChangeWrapperHandleStrength(otp, fIsWeak: false);
}

[Fact]
[PlatformSpecific(TestPlatforms.Windows)]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))]
public void ChangeWrapperHandleStrength_NullObject_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("otp", () => Marshal.ChangeWrapperHandleStrength(null, fIsWeak: true));
Expand Down
Loading

0 comments on commit 9f1dd1a

Please sign in to comment.