diff --git a/src/Network/NWEthernetChannel.cs b/src/Network/NWEthernetChannel.cs index b99c63b04be..f88cc1fb761 100644 --- a/src/Network/NWEthernetChannel.cs +++ b/src/Network/NWEthernetChannel.cs @@ -10,6 +10,7 @@ #nullable enable #if MONOMAC +using System.ComponentModel; using System.Text; using System.Runtime.CompilerServices; using CoreFoundation; @@ -166,8 +167,28 @@ static void TrampolineStateChangesHandler (IntPtr block, NWEthernetChannelState } } +#if !XAMCORE_5_0 [BindingImpl (BindingImplOptions.Optimizable)] + [EditorBrowsable (EditorBrowsableState.Never)] + [Obsolete ("Use the other overload.")] + [OverloadResolutionPriority (-1)] public void SetStateChangesHandler (Action handler) + { + if (handler is null) { + unsafe { + nw_ethernet_channel_set_state_changed_handler (GetCheckedHandle (), null); + } + return; + } + + SetStateChangesHandler ((NWEthernetChannelState state, NWError? error) => handler ((NWBrowserState) state, error)); + } +#endif + + /// Sets the callback invoked when the Ethernet channel state changes. + /// The callback receives the new and an associated , if any. Pass to remove the callback. + [BindingImpl (BindingImplOptions.Optimizable)] + public void SetStateChangesHandler (Action? handler) { unsafe { if (handler is null) { diff --git a/tests/cecil-tests/Documentation.KnownFailures.txt b/tests/cecil-tests/Documentation.KnownFailures.txt index 21849ae1724..79aa2efdb44 100644 --- a/tests/cecil-tests/Documentation.KnownFailures.txt +++ b/tests/cecil-tests/Documentation.KnownFailures.txt @@ -22644,7 +22644,6 @@ M:Network.NWEthernetChannel.Cancel M:Network.NWEthernetChannel.Send(System.ReadOnlySpan{System.Byte},System.UInt16,System.String,System.Action{Network.NWError}) M:Network.NWEthernetChannel.SetQueue(CoreFoundation.DispatchQueue) M:Network.NWEthernetChannel.SetReceiveHandler(Network.NWEthernetChannelReceiveDelegate) -M:Network.NWEthernetChannel.SetStateChangesHandler(System.Action{Network.NWBrowserState,Network.NWError}) M:Network.NWEthernetChannel.Start M:Network.NWFramer.CreateMessage M:Network.NWFramer.CreateOptions``1(Network.NWProtocolDefinition) diff --git a/tests/monotouch-test/Network/NWEthernetChannelTest.cs b/tests/monotouch-test/Network/NWEthernetChannelTest.cs new file mode 100644 index 00000000000..790d3e7576e --- /dev/null +++ b/tests/monotouch-test/Network/NWEthernetChannelTest.cs @@ -0,0 +1,69 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +#if MONOMAC +using System.Threading.Tasks; + +using Network; + +namespace MonoTouchFixtures.Network { + [TestFixture] + [Preserve (AllMembers = true)] + public class NWEthernetChannelTest { + [TestCase (false)] + [TestCase (true)] + public void StateChangesHandler (bool useLegacyHandler) + { + TestRuntime.AssertXcodeVersion (11, 0); + TestRuntime.IgnoreInCI ("This test requires a wired interface and the custom-protocol entitlement."); + + using var monitor = new NWPathMonitor (); + var pathUpdated = new TaskCompletionSource (); + monitor.SetQueue (DispatchQueue.DefaultGlobalQueue); + monitor.SnapshotHandler = currentPath => { + pathUpdated.TrySetResult (currentPath); + }; + monitor.Start (); + try { + Assert.That (pathUpdated.Task.Wait (30000), Is.True, "Path update"); + } finally { + monitor.Cancel (); + } + var path = pathUpdated.Task.Result; + Assert.That (path, Is.Not.Null, "Current path"); + + NWInterface? ethernetInterface = null; + path.EnumerateInterfaces (networkInterface => { + if (networkInterface.InterfaceType == NWInterfaceType.Wired) + ethernetInterface = networkInterface; + return true; + }); + if (ethernetInterface is null) + Assert.Ignore ("This test requires a wired interface."); + + using var channel = new NWEthernetChannel (0x88b5, ethernetInterface); + var stateChanged = new TaskCompletionSource (); + channel.SetQueue (DispatchQueue.DefaultGlobalQueue); + if (useLegacyHandler) { + Action legacyHandler = (currentState, error) => { + stateChanged.TrySetResult ((NWEthernetChannelState) currentState); + }; + channel.SetStateChangesHandler (legacyHandler); + } else { + channel.SetStateChangesHandler ((currentState, error) => { + stateChanged.TrySetResult (currentState); + }); + } + channel.Start (); + try { + Assert.That (stateChanged.Task.Wait (30000), Is.True, "State callback"); + Assert.That (stateChanged.Task.Result, Is.AnyOf ( + NWEthernetChannelState.Waiting, NWEthernetChannelState.Preparing, NWEthernetChannelState.Ready, + NWEthernetChannelState.Failed, NWEthernetChannelState.Cancelled), "Ethernet channel state"); + } finally { + channel.Cancel (); + } + } + } +} +#endif