Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/Core/tests/UnitTests/Hosting/HostBuilderHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public void HostBuilderCanRegisterAndResolveCorrespondingHandlerService(bool reg
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

var handlerService = retrieveHandlerServiceWithGenerics ? mauiHandlersFactory.GetHandler<IViewStub>() : mauiHandlersFactory.GetHandler(typeof(IViewStub));
var handlerService = retrieveHandlerServiceWithGenerics ? mauiHandlersFactory.GetHandler<IViewStub>(context) : mauiHandlersFactory.GetHandler(typeof(IViewStub), context);

Assert.NotNull(handlerService);
Assert.IsType<ViewHandlerStub>(handlerService);
Expand All @@ -73,14 +74,15 @@ public void HostBuilderResolvesLastRegisteredHandlerServiceForServiceType()
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var specificHandler = mauiHandlersFactory.GetHandler(typeof(ButtonStub));
var context = new HandlersContextStub(mauiApp.Services);
var specificHandler = mauiHandlersFactory.GetHandler(typeof(ButtonStub), context);
Assert.IsType<ButtonHandlerStub>(specificHandler);

var collection = mauiHandlersFactory.GetCollection();

collection.AddHandler<ButtonStub, AlternateButtonHandlerStub>();

var alternateHandler = mauiHandlersFactory.GetHandler(typeof(ButtonStub));
var alternateHandler = mauiHandlersFactory.GetHandler(typeof(ButtonStub), context);
Assert.IsType<AlternateButtonHandlerStub>(alternateHandler);
}

Expand All @@ -91,8 +93,9 @@ public void HostBuilderThrowsWhenNoMatchingHandlerServiceTypeIsRegistered()
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

Assert.Throws<HandlerNotFoundException>(() => mauiHandlersFactory.GetHandler(typeof(ViewStub)));
Assert.Throws<HandlerNotFoundException>(() => mauiHandlersFactory.GetHandler(typeof(ViewStub), context));
}

[Theory]
Expand All @@ -106,8 +109,9 @@ public void HostBuilderResolvesHandlerRegisteredUnderBaseInterfaceType(Type base
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

var handlerService = mauiHandlersFactory.GetHandler(typeof(MyDerivedViewStub));
var handlerService = mauiHandlersFactory.GetHandler(typeof(MyDerivedViewStub), context);

Assert.NotNull(handlerService);
Assert.IsType<ViewHandlerStub>(handlerService);
Expand All @@ -130,8 +134,9 @@ public void HostBuilderResolvesToHandlerRegisteredUnderMostDerivedBaseInterfaceT
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

var handlerService = mauiHandlersFactory.GetHandler(typeof(MyDerivedViewStub));
var handlerService = mauiHandlersFactory.GetHandler(typeof(MyDerivedViewStub), context);

Assert.NotNull(handlerService);
Assert.Same(mostDerivedInterfaceHandler, handlerService);
Expand All @@ -154,8 +159,9 @@ public void HostBuilderResolvesToHandlerRegisteredUnderConcreteTypeOverInterface
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

var handlerService = mauiHandlersFactory.GetHandler(typeof(MyDerivedViewStub));
var handlerService = mauiHandlersFactory.GetHandler(typeof(MyDerivedViewStub), context);

Assert.NotNull(handlerService);
Assert.IsType<ViewHandlerStub>(handlerService);
Expand All @@ -174,8 +180,9 @@ public void HostBuilderDoesNotResolveHandlersRegisteredUnderMoreDerivedTypes()
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

Assert.Throws<HandlerNotFoundException>(() => mauiHandlersFactory.GetHandler(typeof(ViewStub)));
Assert.Throws<HandlerNotFoundException>(() => mauiHandlersFactory.GetHandler(typeof(ViewStub), context));
}

[Theory]
Expand All @@ -194,8 +201,9 @@ public void HostBuilderResolvesClosestApplicableServiceType(Type type, Type expe
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

var handlerService = mauiHandlersFactory.GetHandler(type);
var handlerService = mauiHandlersFactory.GetHandler(type, context);

Assert.NotNull(handlerService);
Assert.IsType(expectedHandlerType, handlerService);
Expand All @@ -219,8 +227,9 @@ public void HostBuilderThrowsWhenOnlyInterfacesRelatedByInheritanceAreRegistered
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

Assert.Throws<InvalidOperationException>(() => mauiHandlersFactory.GetHandler(typeof(ChildViewStub)));
Assert.Throws<InvalidOperationException>(() => mauiHandlersFactory.GetHandler(typeof(ChildViewStub), context));
}

[Fact]
Expand All @@ -240,8 +249,9 @@ public void HostBuilderResolvesToHandlerRegisteredUnderConcreteTypeDespiteAmbigu
.Build();

var mauiHandlersFactory = mauiApp.Services.GetRequiredService<IMauiHandlersFactory>();
var context = new HandlersContextStub(mauiApp.Services);

var handlerService = mauiHandlersFactory.GetHandler(typeof(ChildViewStub));
var handlerService = mauiHandlersFactory.GetHandler(typeof(ChildViewStub), context);

Assert.NotNull(handlerService);
Assert.Same(classHandler, handlerService);
Expand Down