public void AddSpecificIsNotWeak() { var collection = new MauiServiceCollection(); var services = new MauiFactory(collection); var context = new MauiContext(services); DoAdd(context); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.NotNull(context.Services.GetService <TestThing>());
static IServiceProvider CreateCompatServiceProvider() { var collection = new MauiServiceCollection(); collection.AddSingleton <ILoggerFactory, FallbackLoggerFactory>(); collection.AddSingleton <IFontRegistrar>(svc => new FontRegistrar(_embeddedFontLoader, svc.CreateLogger <FontRegistrar>())); collection.AddSingleton <IFontManager>(svc => new FontManager(svc.GetRequiredService <IFontRegistrar>(), svc.CreateLogger <FontManager>())); var provider = new MauiServiceProvider(collection, false); return(provider); }
public void CloneIncludeSameServices() { var obj = new TestThing(); var collection = new MauiServiceCollection(); collection.AddSingleton(obj); var services = new MauiFactory(collection); var first = new MauiContext(services); var second = new MauiContext(first.Services); Assert.Same(obj, second.Services.GetService <TestThing>()); }
public void AddSpecificInstanceOverridesBase() { var baseObj = new TestThing(); var collection = new MauiServiceCollection(); collection.AddSingleton(baseObj); var services = new MauiFactory(collection); var specificObj = new TestThing(); var context = new MauiContext(services); context.AddSpecific <TestThing>(specificObj); Assert.Same(specificObj, context.Services.GetService <TestThing>()); }
public void GetRequiredServiceRetrievesService() { HandlerStub handlerStub = new HandlerStub(); var collection = new MauiServiceCollection(); collection.TryAddSingleton <IMauiHandlersFactory>(new MauiHandlersFactory(null)); collection.TryAddSingleton <IFooService, FooService>(); var provider = new MauiFactory(collection); handlerStub.SetMauiContext(new HandlersContextStub(provider)); Assert.NotNull(handlerStub.MauiContext); Assert.NotNull(handlerStub.MauiContext.Services); var foo = handlerStub.GetRequiredService <IFooService>(); Assert.IsType <FooService>(foo); }