Imported Upstream version 5.8.0.22

Former-commit-id: df344e34b07851d296efb3e6604c8db42b6f7aa3
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-10-19 20:04:20 +00:00
parent 5f4a27cc8a
commit 7d05485754
5020 changed files with 114082 additions and 186061 deletions

View File

@@ -2,6 +2,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServices.WindowsRuntime.Tests", "tests\System.Runtime.InteropServices.WindowsRuntime.Tests.csproj", "{27F624C8-06E3-455D-B5D1-C0FEB343EFAA}"
ProjectSection(ProjectDependencies) = postProject
{5655C5E4-9B76-489B-87AF-42BD60570A00} = {5655C5E4-9B76-489B-87AF-42BD60570A00}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServices.WindowsRuntime", "src\System.Runtime.InteropServices.WindowsRuntime.csproj", "{5655C5E4-9B76-489B-87AF-42BD60570A00}"
ProjectSection(ProjectDependencies) = postProject
{DC485335-D0D7-4E28-9593-445B7B6BEFA7} = {DC485335-D0D7-4E28-9593-445B7B6BEFA7}
@@ -9,6 +14,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Runtime.InteropServices.WindowsRuntime", "ref\System.Runtime.InteropServices.WindowsRuntime.csproj", "{DC485335-D0D7-4E28-9593-445B7B6BEFA7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1A2F9F4A-A032-433E-B914-ADD5992BB178}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E893-4E87-987E-04EF0DCEAEFD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2EDB-464B-9DF6-380BF4789AD4}"
@@ -19,6 +26,10 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{27F624C8-06E3-455D-B5D1-C0FEB343EFAA}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU
{27F624C8-06E3-455D-B5D1-C0FEB343EFAA}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{27F624C8-06E3-455D-B5D1-C0FEB343EFAA}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
{27F624C8-06E3-455D-B5D1-C0FEB343EFAA}.Release|Any CPU.Build.0 = netcoreapp-Windows_NT-Release|Any CPU
{5655C5E4-9B76-489B-87AF-42BD60570A00}.Debug|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Debug|Any CPU
{5655C5E4-9B76-489B-87AF-42BD60570A00}.Debug|Any CPU.Build.0 = netcoreapp-Windows_NT-Debug|Any CPU
{5655C5E4-9B76-489B-87AF-42BD60570A00}.Release|Any CPU.ActiveCfg = netcoreapp-Windows_NT-Release|Any CPU
@@ -32,6 +43,7 @@ Global
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{27F624C8-06E3-455D-B5D1-C0FEB343EFAA} = {1A2F9F4A-A032-433E-B914-ADD5992BB178}
{5655C5E4-9B76-489B-87AF-42BD60570A00} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD}
{DC485335-D0D7-4E28-9593-445B7B6BEFA7} = {2E666815-2EDB-464B-9DF6-380BF4789AD4}
EndGlobalSection

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildConfigurations>
uapaot-Windows_NT;
netcoreapp-Windows_NT;
netcoreapp;
uap-Windows_NT;
</BuildConfigurations>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<AppDesignerFolder>Properties</AppDesignerFolder>
<ProjectGuid>{27F624C8-06E3-455D-B5D1-C0FEB343EFAA}</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Windows_NT-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uap-Windows_NT-Release|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uapaot-Windows_NT-Debug|AnyCPU'" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'uapaot-Windows_NT-Release|AnyCPU'" />
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\DefaultInterfaceAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\EmptyAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\EventRegistrationTokenTests.cs" />
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\EventRegistrationTokenTableTests.cs" />
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\InterfaceImplementedInVersionAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\ReturnValueNameAttributeTests.cs" />
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\WindowsRuntimeMarshalTests.cs" />
<Compile Include="$(CommonTestPath)\System\PlatformDetection.cs">
<Link>Common\System\PlatformDetection.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetsWindows)' == 'false'">
<Compile Include="System\Runtime\InteropServices\WindowsRuntime\UnixTests.cs" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>

View File

@@ -0,0 +1,20 @@
// 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;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class DefaultInterfaceAttributeTests
{
[Theory]
[InlineData(null)]
[InlineData(typeof(int))]
public void Ctor_DefaultInterface(Type defaultInterface)
{
var attribute = new ComDefaultInterfaceAttribute(defaultInterface);
Assert.Equal(defaultInterface, attribute.Value);
}
}
}

View File

@@ -0,0 +1,18 @@
// 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;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class EmptyAttributeTests
{
[Fact]
public void EmptyAttributes_Ctor_Success()
{
new ReadOnlyArrayAttribute();
new WriteOnlyArrayAttribute();
}
}
}

View File

@@ -0,0 +1,178 @@
// 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;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class EventRegistrationTokenTableTests
{
[Fact]
public void Ctor_Default()
{
var tokenTable = new EventRegistrationTokenTable<Delegate>();
Assert.Null(tokenTable.InvocationList);
}
[Fact]
public void Ctor_NonDelegateType_ThrowsInvalidOperationException()
{
Assert.Throws<InvalidOperationException>(() => new EventRegistrationTokenTable<string>());
}
[Fact]
public void AddEventHandler_SingleInvocationList_AddsSingleDelegateToInvocationList()
{
EventHandler handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token = tokenTable.AddEventHandler(handler);
Assert.NotEqual(0, token.GetHashCode());
Assert.Equal(new Delegate[] { handler }, tokenTable.InvocationList.GetInvocationList());
}
[Fact]
public void AddEventHandler_MultipleInvocationList_AddsAllDelegateToInvocationLists()
{
EventHandler handler1 = new EventHandler(EventHandlerMethod1);
EventHandler handler2 = new EventHandler(EventHandlerMethod2);
EventHandler combinedHandler = (EventHandler)Delegate.Combine(handler1, handler2);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token = tokenTable.AddEventHandler(combinedHandler);
Assert.NotEqual(0, token.GetHashCode());
Assert.Equal(new Delegate[] { handler1, handler2 }, tokenTable.InvocationList.GetInvocationList());
}
[Fact]
public void AddEventHandler_MultipleTimes_AddsEachDelegateToInvocationList()
{
EventHandler handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token1 = tokenTable.AddEventHandler(handler);
Assert.NotEqual(0, token1.GetHashCode());
EventRegistrationToken token2 = tokenTable.AddEventHandler(handler);
Assert.NotEqual(token1.GetHashCode(), token2.GetHashCode());
Assert.Equal(new Delegate[] { handler, handler }, tokenTable.InvocationList.GetInvocationList());
}
[Fact]
public void AddEventHandler_Null_ReturnsZeroToken()
{
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token = tokenTable.AddEventHandler(null);
Assert.Equal(0, token.GetHashCode());
// Removing this token should be a nop.
tokenTable.RemoveEventHandler(token);
}
[Fact]
public void RemoveEventHandler_Token_RemovesFromTable()
{
EventHandler handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token = tokenTable.AddEventHandler(handler);
tokenTable.RemoveEventHandler(token);
Assert.Null(tokenTable.InvocationList);
// Calls to RemoveEventHandler after removal are nops.
tokenTable.RemoveEventHandler(token);
}
[Fact]
public void RemoveEventHandler_Delegate_RemovesFromTable()
{
EventHandler handler1 = new EventHandler(EventHandlerMethod1);
EventHandler handler2 = new EventHandler(EventHandlerMethod2);
EventHandler combinedHandler = (EventHandler)Delegate.Combine(handler1, handler2);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
tokenTable.AddEventHandler(combinedHandler);
tokenTable.RemoveEventHandler(handler1);
tokenTable.RemoveEventHandler(handler2);
Assert.Equal(new Delegate[] { handler1, handler2 }, tokenTable.InvocationList.GetInvocationList());
tokenTable.RemoveEventHandler(combinedHandler);
Assert.Null(tokenTable.InvocationList);
// Calls to RemoveEventHandler after removal are nops.
tokenTable.RemoveEventHandler(combinedHandler);
}
[Fact]
public void RemoveEventHandler_MultipleTimes_RemovesSingleDelegateFromTable()
{
EventHandler handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
tokenTable.AddEventHandler(handler);
tokenTable.AddEventHandler(handler);
tokenTable.RemoveEventHandler(handler);
Assert.Equal(new Delegate[] { handler }, tokenTable.InvocationList.GetInvocationList());
tokenTable.RemoveEventHandler(handler);
Assert.Null(tokenTable.InvocationList);
}
[Fact]
public void RemoveEventHandler_NullDelegate_Nop()
{
var tokenTable = new EventRegistrationTokenTable<Delegate>();
tokenTable.RemoveEventHandler(null);
}
[Fact]
public void GetOrCreateEventRegistrationTokenTable_NullTable_ReturnsNewTable()
{
EventRegistrationTokenTable<Delegate> tokenTable = null;
EventRegistrationTokenTable<Delegate> result = EventRegistrationTokenTable<Delegate>.GetOrCreateEventRegistrationTokenTable(ref tokenTable);
Assert.Null(result.InvocationList);
}
[Fact]
public void GetOrCreateEventRegistrationTokenTable_NonNullTable_ReturnsEventTable()
{
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationTokenTable<Delegate> result = EventRegistrationTokenTable<Delegate>.GetOrCreateEventRegistrationTokenTable(ref tokenTable);
Assert.Same(tokenTable, result);
}
[Fact]
public void GetOrCreateEventRegistrationTokenTable_NonDelegateType_ThrowsInvalidOperationException()
{
EventRegistrationTokenTable<string> tokenTable = null;
Assert.Throws<InvalidOperationException>(() => EventRegistrationTokenTable<string>.GetOrCreateEventRegistrationTokenTable(ref tokenTable));
}
[Fact]
public void InvocationList_SetNull_GetReturnsNull()
{
var tokenTable = new EventRegistrationTokenTable<Delegate>();
tokenTable.AddEventHandler(new EventHandler(EventHandlerMethod1));
tokenTable.InvocationList = null;
Assert.Null(tokenTable.InvocationList);
}
[Fact]
public void InvocationList_SetDelegate_GetReturnsExpected()
{
Delegate invocationList = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>() { InvocationList = invocationList };
Assert.Equal(new Delegate[] { invocationList }, tokenTable.InvocationList.GetInvocationList());
}
private static void EventHandlerMethod1(object sender, EventArgs e) { }
private static void EventHandlerMethod2(object sender, EventArgs e) { }
}
}

View File

@@ -0,0 +1,43 @@
// 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 System.Collections.Generic;
using Xunit;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class EventRegistrationTokenTests
{
public static IEnumerable<object[]> Equals_TestData()
{
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token = tokenTable.AddEventHandler(new EventHandler(EventHandlerMethod1));
EventRegistrationToken emptyToken = tokenTable.AddEventHandler(null);
yield return new object[] { token, token, true };
yield return new object[] { token, emptyToken, false };
yield return new object[] { emptyToken, emptyToken, true };
yield return new object[] { emptyToken, new EventRegistrationToken(), true };
yield return new object[] { token, new object(), false };
yield return new object[] { token, null, false };
}
[Theory]
[MemberData(nameof(Equals_TestData))]
public void Equals_Object_ReturnsExpected(EventRegistrationToken token, object other, bool expected)
{
Assert.Equal(expected, token.Equals(other));
if (other is EventRegistrationToken otherToken)
{
Assert.Equal(expected, token == otherToken);
Assert.Equal(!expected, token != otherToken);
Assert.Equal(expected, token.GetHashCode().Equals(other.GetHashCode()));
}
}
private static void EventHandlerMethod1(object sender, EventArgs e) { }
private static void EventHandlerMethod2(object sender, EventArgs e) { }
}
}

View File

@@ -0,0 +1,24 @@
// 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;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class InterfaceImplementedInVersionAttributeTests
{
[Theory]
[InlineData(null, 0, 0, 0, 0)]
[InlineData(typeof(int), 255, 255, 255, 255)]
public void Ctor_DefaultInterface(Type interfaceType, byte majorVersion, byte minorVersion, byte buildVersion, byte revisionVersion)
{
var attribute = new InterfaceImplementedInVersionAttribute(interfaceType, majorVersion, minorVersion, buildVersion, revisionVersion);
Assert.Equal(interfaceType, attribute.InterfaceType);
Assert.Equal(majorVersion, attribute.MajorVersion);
Assert.Equal(minorVersion, attribute.MinorVersion);
Assert.Equal(buildVersion, attribute.BuildVersion);
Assert.Equal(revisionVersion, attribute.RevisionVersion);
}
}
}

View File

@@ -0,0 +1,20 @@
// 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;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class ReturnValueNameAttributeTests
{
[Theory]
[InlineData(null)]
[InlineData("Name")]
public void Ctor_Name(string name)
{
var attribute = new ReturnValueNameAttribute(name);
Assert.Equal(name, attribute.Name);
}
}
}

View File

@@ -0,0 +1,89 @@
// 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;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class UnixTests
{
[Fact]
public void DefaultInterfaceAttribute_Ctor_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => new DefaultInterfaceAttribute(typeof(int)));
}
[Fact]
public void InterfaceImplementedInVersionAttribute_Ctor_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => new InterfaceImplementedInVersionAttribute(typeof(int), 1, 2, 3, 4));
}
[Fact]
public void ReadOnlyArrayAttribute_Ctor_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => new ReadOnlyArrayAttribute());
}
[Fact]
public void ReturnValueNameAttribute_Ctor_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => new ReturnValueNameAttribute("Name"));
}
[Fact]
public void WriteOnlyArrayAttribute_Ctor_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => new WriteOnlyArrayAttribute());
}
[Fact]
public void EventRegistrationTokenTake_Ctor_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => new EventRegistrationTokenTable<Delegate>());
}
[Fact]
public void WindowsRuntimeMarshal_AddEventHandler_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.AddEventHandler(null, null, 0));
}
[Fact]
public void WindowsRuntimeMarshal_RemoveEventHandler_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.RemoveEventHandler(null, 0));
}
[Fact]
public void WindowsRuntimeMarshal_RemoveAllEventHandlers_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.RemoveAllEventHandlers(null));
}
[Fact]
public void WindowsRuntimeMarshal_StringToHString_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.StringToHString(null));
}
[Fact]
public void WindowsRuntimeMarshal_PtrToStringHString_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.PtrToStringHString(IntPtr.Zero));
}
[Fact]
public void WindowsRuntimeMarshal_FreeHString_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.FreeHString(IntPtr.Zero));
}
[Fact]
public void WindowsRuntimeMarshal_GetActivationFactory_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.GetActivationFactory(null));
}
}
}

View File

@@ -0,0 +1,324 @@
// 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 System.Collections.Generic;
using Xunit;
namespace System.Runtime.InteropServices.WindowsRuntime.Tests
{
public class WindowsRuntimeMarshalTests
{
[Fact]
public void AddEventHandler_RemoveMethodHasTarget_Success()
{
bool addMethodCalled = false;
bool removeMethodCalled = false;
Delegate handler = new EventHandler(EventHandlerMethod1);
Func<Delegate, EventRegistrationToken> addMethod = eventHandler =>
{
Assert.False(addMethodCalled);
addMethodCalled = true;
Assert.Same(handler, eventHandler);
return new EventRegistrationToken();
};
WindowsRuntimeMarshal.AddEventHandler(addMethod, token => removeMethodCalled = true, handler);
Assert.True(addMethodCalled);
Assert.False(removeMethodCalled);
}
[Fact]
public void AddEventHandler_RemoveMethodHasNoTarget_Success()
{
bool removeMethodCalled = false;
ExpectedHandler = new EventHandler(EventHandlerMethod1);
WindowsRuntimeMarshal.AddEventHandler(AddMethod, token => removeMethodCalled = true, ExpectedHandler);
Assert.True(AddMethodCalled);
Assert.False(removeMethodCalled);
}
private static bool AddMethodCalled { get; set; }
private static Delegate ExpectedHandler { get; set; }
private static EventRegistrationToken AddMethod(Delegate eventHandler)
{
Assert.False(AddMethodCalled);
AddMethodCalled = true;
Assert.Same(ExpectedHandler, eventHandler);
return new EventRegistrationToken();
}
[Fact]
public void AddEventHandler_NullHandler_Nop()
{
bool addMethodCalled = false;
bool removeMethodCalled = false;
WindowsRuntimeMarshal.AddEventHandler<Delegate>(eventHandler => {
addMethodCalled = true;
return new EventRegistrationToken();
}, token => removeMethodCalled = true, null);
Assert.False(addMethodCalled);
Assert.False(removeMethodCalled);
}
[Fact]
public void AddEventHandler_NullAddMethod_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("addMethod", () => WindowsRuntimeMarshal.AddEventHandler(null, token => { }, new EventHandler(EventHandlerMethod1)));
}
[Fact]
public void AddEventHandler_NullRemoveMethod_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("removeMethod", () => WindowsRuntimeMarshal.AddEventHandler(eventHandler => new EventRegistrationToken(), null, new EventHandler(EventHandlerMethod1)));
}
[Fact]
public void RemoveEventHandler_RemoveMethodHasTarget_Success()
{
bool removeMethodCalled = false;
Delegate handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken addToken = tokenTable.AddEventHandler(handler);
Action<EventRegistrationToken> removeMethod = token =>
{
Assert.False(removeMethodCalled);
removeMethodCalled = true;
Assert.Equal(addToken, token);
};
WindowsRuntimeMarshal.AddEventHandler(eventHandler => addToken, removeMethod, handler);
// Removing with the same handler but with a different method is a nop.
WindowsRuntimeMarshal.RemoveEventHandler(token => removeMethodCalled = true, handler);
Assert.False(removeMethodCalled);
WindowsRuntimeMarshal.RemoveEventHandler(DifferentRemoveMethod, handler);
Assert.False(removeMethodCalled);
// Removing with a different handler but with the same method is a nop.
WindowsRuntimeMarshal.RemoveEventHandler(removeMethod, new EventHandler(EventHandlerMethod2));
Assert.False(removeMethodCalled);
// Removing the same handler and the same method works.
WindowsRuntimeMarshal.RemoveEventHandler(removeMethod, handler);
Assert.True(removeMethodCalled);
}
[Fact]
public void RemoveEventHandler_RemoveMethodHasNoTarget_Success()
{
Delegate handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
ExpectedRemoveToken = tokenTable.AddEventHandler(handler);
Action<EventRegistrationToken> removeMethod = RemoveMethod;
WindowsRuntimeMarshal.AddEventHandler(eventHandler => ExpectedRemoveToken, removeMethod, handler);
// Removing with the same handler but with a different method is a nop.
WindowsRuntimeMarshal.RemoveEventHandler(token => RemoveMethodCalled = true, handler);
Assert.False(RemoveMethodCalled);
WindowsRuntimeMarshal.RemoveEventHandler(DifferentRemoveMethod, handler);
Assert.False(RemoveMethodCalled);
// Removing with a different handler but with the same method is a nop.
WindowsRuntimeMarshal.RemoveEventHandler(removeMethod, new EventHandler(EventHandlerMethod2));
Assert.False(RemoveMethodCalled);
// Removing the same handler and the same method works.
WindowsRuntimeMarshal.RemoveEventHandler(removeMethod, handler);
Assert.True(RemoveMethodCalled);
}
private static EventRegistrationToken ExpectedRemoveToken { get; set; }
private static bool RemoveMethodCalled { get; set; }
private static void RemoveMethod(EventRegistrationToken token)
{
Assert.False(RemoveMethodCalled);
RemoveMethodCalled = true;
Assert.Equal(ExpectedRemoveToken, token);
}
private static void DifferentRemoveMethod(EventRegistrationToken token) => RemoveMethodCalled = true;
[Fact]
public void RemoveEventHandler_NullHandler_Nop()
{
bool removeMethodCalled = false;
Delegate handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken addToken = tokenTable.AddEventHandler(handler);
Action<EventRegistrationToken> removeMethod = token => removeMethodCalled = true;
WindowsRuntimeMarshal.AddEventHandler(eventHandler => addToken, removeMethod, handler);
WindowsRuntimeMarshal.RemoveEventHandler<Delegate>(removeMethod, null);
Assert.False(removeMethodCalled);
}
[Fact]
public void RemoveEventHandler_NullRemoveMethod_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("removeMethod", () => WindowsRuntimeMarshal.RemoveEventHandler<Delegate>(null, new EventHandler(EventHandlerMethod1)));
}
[Fact]
public void RemoveAllEventHandlers_RemoveMethodHasTarget_Success()
{
Delegate handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token1 = tokenTable.AddEventHandler(handler);
EventRegistrationToken token2 = tokenTable.AddEventHandler(handler);
EventRegistrationToken token3 = tokenTable.AddEventHandler(handler);
var removedTokens = new List<EventRegistrationToken>();
Action<EventRegistrationToken> removeMethod = token => removedTokens.Add(token);
WindowsRuntimeMarshal.AddEventHandler(eventHandler => token1, removeMethod, handler);
WindowsRuntimeMarshal.AddEventHandler(eventHandler => token2, removeMethod, handler);
bool removeMethodWithTargetCalled = false;
WindowsRuntimeMarshal.AddEventHandler(eventHandler => token3, token => removeMethodWithTargetCalled = false, handler);
// Removing with the same handler but with a different method is a nop.
WindowsRuntimeMarshal.RemoveAllEventHandlers(token => RemoveMethodCalled = true);
Assert.Empty(removedTokens);
Assert.False(DifferentRemoveAllMethodCalled);
Assert.False(removeMethodWithTargetCalled);
WindowsRuntimeMarshal.RemoveAllEventHandlers(DifferentRemoveAllMethod);
Assert.Empty(removedTokens);
Assert.False(DifferentRemoveAllMethodCalled);
Assert.False(removeMethodWithTargetCalled);
// Removing the same handler and the same method works.
WindowsRuntimeMarshal.RemoveAllEventHandlers(removeMethod);
Assert.Equal(new EventRegistrationToken[] { token1, token2 }, removedTokens);
Assert.False(DifferentRemoveAllMethodCalled);
Assert.False(removeMethodWithTargetCalled);
}
[Fact]
public void RemoveAllEventHandlers_RemoveMethodHasNoTarget_Success()
{
Delegate handler = new EventHandler(EventHandlerMethod1);
var tokenTable = new EventRegistrationTokenTable<Delegate>();
EventRegistrationToken token1 = tokenTable.AddEventHandler(handler);
EventRegistrationToken token2 = tokenTable.AddEventHandler(handler);
EventRegistrationToken token3 = tokenTable.AddEventHandler(handler);
Action<EventRegistrationToken> removeMethod = RemoveAllMethod;
WindowsRuntimeMarshal.AddEventHandler(eventHandler => token1, removeMethod, handler);
WindowsRuntimeMarshal.AddEventHandler(eventHandler => token2, removeMethod, handler);
bool removeMethodWithTargetCalled = false;
WindowsRuntimeMarshal.AddEventHandler(eventHandler => token3, token => removeMethodWithTargetCalled = false, handler);
// Removing with the same handler but with a different method is a nop.
WindowsRuntimeMarshal.RemoveAllEventHandlers(token => RemoveMethodCalled = true);
Assert.Empty(RemoveAllTokens);
Assert.False(DifferentRemoveAllMethodCalled);
Assert.False(removeMethodWithTargetCalled);
WindowsRuntimeMarshal.RemoveAllEventHandlers(DifferentRemoveAllMethod);
Assert.Empty(RemoveAllTokens);
Assert.False(DifferentRemoveAllMethodCalled);
Assert.False(removeMethodWithTargetCalled);
// Removing the same handler and the same method works.
WindowsRuntimeMarshal.RemoveAllEventHandlers(removeMethod);
Assert.Equal(new EventRegistrationToken[] { token1, token2 }, RemoveAllTokens);
Assert.False(DifferentRemoveAllMethodCalled);
Assert.False(removeMethodWithTargetCalled);
}
private static List<EventRegistrationToken> RemoveAllTokens { get; } = new List<EventRegistrationToken>();
private static void RemoveAllMethod(EventRegistrationToken token) => RemoveAllTokens.Add(token);
private static bool DifferentRemoveAllMethodCalled { get; set; }
private static void DifferentRemoveAllMethod(EventRegistrationToken token) => DifferentRemoveAllMethodCalled = true;
[Fact]
public void RemoveAllEventHandlers_NullRemoveMethod_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("removeMethod", () => WindowsRuntimeMarshal.RemoveAllEventHandlers(null));
}
[ConditionalTheory(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsWinRTSupported))]
[InlineData("")]
[InlineData("HString")]
public void StringToHString_PtrToHString_ReturnsExpected(string s)
{
IntPtr ptr = WindowsRuntimeMarshal.StringToHString(s);
try
{
if (s.Length == 0)
{
Assert.Equal(IntPtr.Zero, ptr);
}
else
{
Assert.NotEqual(IntPtr.Zero, ptr);
}
Assert.Equal(s, WindowsRuntimeMarshal.PtrToStringHString(ptr));
}
finally
{
WindowsRuntimeMarshal.FreeHString(ptr);
}
}
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsWinRTSupported))]
public void StringToHString_NullString_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("s", () => WindowsRuntimeMarshal.StringToHString(null));
}
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWinRTSupported))]
public void StringToHString_WinRTNotSupported_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.StringToHString(null));
}
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWinRTSupported))]
public void PtrToStringHString_WinRTNotSupported_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.PtrToStringHString(IntPtr.Zero));
}
[ConditionalFact(nameof(PlatformDetection) + "." + nameof(PlatformDetection.IsNotWinRTSupported))]
public void FreeHString_WinRTNotSupported_ThrowsPlatformNotSupportedException()
{
Assert.Throws<PlatformNotSupportedException>(() => WindowsRuntimeMarshal.FreeHString(IntPtr.Zero));
}
[Fact]
public void GetActivationFactory_NullType_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("type", () => WindowsRuntimeMarshal.GetActivationFactory(null));
}
[Fact]
[SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "No reliable way to check if a type is WinRT in AOT")]
public void GetActivationFactory_NotExportedType_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>("type", () => WindowsRuntimeMarshal.GetActivationFactory(typeof(int)));
}
private static void EventHandlerMethod1(object sender, EventArgs e) { }
private static void EventHandlerMethod2(object sender, EventArgs e) { }
}
}