Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow committed Nov 23, 2021
1 parent 8d0baff commit 9713f31
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/Core/src/CommandMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ private protected virtual void SetPropertyCore(string key, Command action)

private protected virtual void InvokeCore(string key, IElementHandler viewHandler, IElement virtualView, object? args)
{
var action = GetCommandCore(key);
var action = GetCommand(key);
action?.Invoke(viewHandler, virtualView, args);
}

private protected virtual Command? GetCommandCore(string key)
public virtual Command? GetCommand(string key)
{
if (_mapper.TryGetValue(key, out var action))
return action;
else if (Chained is not null)
return Chained.GetCommandCore(key);
return Chained.GetCommand(key);
else
return null;
}
Expand Down Expand Up @@ -75,7 +75,7 @@ public CommandMapper(CommandMapper chained)
{
get
{
var action = GetCommandCore(key) ?? throw new IndexOutOfRangeException($"Unable to find mapping for '{nameof(key)}'.");
var action = GetCommand(key) ?? throw new IndexOutOfRangeException($"Unable to find mapping for '{nameof(key)}'.");
return new Action<TViewHandler, TVirtualView, object?>((h, v, o) => action.Invoke(h, v, o));
}
set => Add(key, value);
Expand Down
67 changes: 67 additions & 0 deletions src/Core/src/CommandMapperExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;

namespace Microsoft.Maui
{
public static class CommandMapperExtensions
{
/// <summary>
/// Modify a command mapping in place.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The modified method to call when the command is updated.</param>
public static void ModifyMapping<TVirtualView, TViewHandler>(this CommandMapper<TVirtualView, TViewHandler> commandMapper,
string key, Action<TViewHandler, TVirtualView, object?, Action<IElementHandler, IElement, object?>?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
var previousMethod = commandMapper.GetCommand(key);

commandMapper.Add(key, newMethod);

void newMethod(TViewHandler handler, TVirtualView view, object? args)
{
method(handler, view, args, previousMethod);
}
}

/// <summary>
/// Specify a method to be run after an existing command mapping.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The method to call after the existing mapping is finished.</param>
public static void AppendToMapping<TVirtualView, TViewHandler>(this CommandMapper<TVirtualView, TViewHandler> commandMapper,
string key, Action<TViewHandler, TVirtualView, object?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
commandMapper.ModifyMapping(key, (handler, view, args, action) =>
{
action?.Invoke(handler, view, args);
method(handler, view, args);
});
}

/// <summary>
/// Specify a method to be run before an existing command mapping.
/// </summary>
/// <typeparam name="TVirtualView">The cross-platform type.</typeparam>
/// <typeparam name="TViewHandler">The handler type.</typeparam>
/// <param name="commandMapper">The command mapper in which to change the mapping.</param>
/// <param name="key">The name of the command.</param>
/// <param name="method">The method to call before the existing mapping begins.</param>
public static void PrependToMapping<TVirtualView, TViewHandler>(this CommandMapper<TVirtualView, TViewHandler> commandMapper,
string key, Action<TViewHandler, TVirtualView, object?> method)
where TVirtualView : IElement where TViewHandler : IElementHandler
{
commandMapper.ModifyMapping(key, (handler, view, args, action) =>
{
method(handler, view, args);
action?.Invoke(handler, view, args);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Microsoft.Maui.Handlers;
using Android.Views;
using Android.Widget;

namespace Microsoft.Maui.DeviceTests
{
public partial class ViewHandlerTests
{
protected static Thing CreateNativeView() =>

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Maui.Handlers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui.DeviceTests
{
Expand Down
28 changes: 13 additions & 15 deletions src/Core/tests/DeviceTests/Handlers/View/ViewHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Microsoft.Maui;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.View)]
public partial class ViewHandlerTests : HandlerTestBase<StubBase, StubBaseHandler>
public partial class ViewHandlerTests : HandlerTestBase<StubBaseHandler, StubBase>
{
[Fact(DisplayName = "NativeArrange triggers MapFrame")]
public Task NativeArrangeTriggersMapFrame()
public async Task NativeArrangeTriggersMapFrame()
{
var commandMapperField = typeof(ElementHandler).GetField("_commandMapper");
var didUpdateFrame = 0;

var view = new StubBase();

return InvokeOnMainThreadAsync(() =>
await InvokeOnMainThreadAsync(() =>
{
var handler = new StubBaseHandler(CreateNativeView());
var handler = new StubBaseHandler();
var viewHandler = handler as ViewHandler;
var commandMapper = commandMapperField.GetValue(handler) as CommandMapper;
handler.Com
handler.CommandMapper.AppendToMapping(nameof(IView.Frame), (h, v, a) =>
{
didUpdateFrame++;
});
InitializeViewHandler(view, handler);
var handler = CreateHandler(view);
});

Assert.Equal(1, didUpdateFrame);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Maui.Handlers;
using UIKit;

namespace Microsoft.Maui.DeviceTests
{
Expand Down
51 changes: 35 additions & 16 deletions src/Core/tests/DeviceTests/Stubs/StubBaseHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Primitives;

#if __IOS__ || MACCATALYST
using NativeView = UIKit.UIView;
Expand All @@ -18,26 +13,50 @@

namespace Microsoft.Maui.DeviceTests.Stubs
{
public class StubBaseHandler : ViewHandler<StubBase, NativeView>
public class StubBaseHandler : ViewHandler<StubBase, StubNativeView>
{
readonly NativeView _nativeView;
public static IPropertyMapper<StubBase, StubBaseHandler> StubMapper =
new PropertyMapper<StubBase, StubBaseHandler>(ViewMapper)
{
};

public static CommandMapper<StubBase, StubBaseHandler> StubCommandMapper =
new(ViewCommandMapper)
{
};

public static IPropertyMapper<StubBase, StubBaseHandler> StubMapper = new PropertyMapper<StubBase, StubBaseHandler>(ViewMapper)
public StubBaseHandler()
: this(null, null)
{
};
}

public static CommandMapper<StubBase, StubBaseHandler> StubCommandMapper = new(ViewCommandMapper)
public StubBaseHandler(IPropertyMapper? mapper = null, CommandMapper? commandMapper = null)
: base(
new PropertyMapper<StubBase, StubBaseHandler>(mapper ?? StubMapper),
new CommandMapper<StubBase, StubBaseHandler>(commandMapper ?? ViewCommandMapper))
{
};
}

public CommandMapper<StubBase, StubBaseHandler>? CommandMapper =>
_commandMapper as CommandMapper<StubBase, StubBaseHandler>;

public PropertyMapper<StubBase, StubBaseHandler>? PropertyMapper =>
_mapper as PropertyMapper<StubBase, StubBaseHandler>;

protected StubBaseHandler(NativeView nativeView, IPropertyMapper? mapper = null, CommandMapper? commandMapper = null)
: base(mapper ?? StubMapper, commandMapper ?? ViewCommandMapper)
protected override StubNativeView CreateNativeView() =>
new StubNativeView(MauiContext!);
}

public class StubNativeView : NativeView
{
public StubNativeView(IMauiContext mauiContext)
#if __ANDROID__
: base(mauiContext.Context)
#endif
{
_nativeView = nativeView;
MauiContext = mauiContext;
}

protected override NativeView CreateNativeView() =>
_nativeView;
public IMauiContext MauiContext { get; }
}
}

0 comments on commit 9713f31

Please sign in to comment.