Repository navigation
[trimming] Context-aware handler activation #40721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -213,17 +213,14 @@ internal static void RegisterHandlerReplacement(string oldHandlerType, Type newH | |
| } | ||
| else | ||
| { | ||
| var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | ||
| var handlerType = assemblies.Select(x => x.GetType(oldHandlerType)).FirstOrDefault(x => x != null); | ||
|
|
||
| views = handlerType is null | ||
| ? new List<KeyValuePair<Type, Type>>() | ||
| : HandlerService | ||
| .Select(x => new KeyValuePair<Type, Type?>(x.ServiceType, GetRegisteredHandlerType(x))) | ||
| .Where(x => x.Value == handlerType) | ||
| .Select(x => new KeyValuePair<Type, Type>(x.Key, x.Value!)) | ||
| .Distinct() | ||
| .ToList(); | ||
| // Match by name, like the replacement request itself. Looking the name up in the loaded | ||
| // assemblies can find a different Type when an assembly is loaded more than once. | ||
| views = HandlerService | ||
| .Select(x => new KeyValuePair<Type, Type?>(x.ServiceType, GetRegisteredHandlerType(x))) | ||
| .Where(x => x.Value?.FullName == oldHandlerType) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [moderate] Regression Prevention and Test Coverage - The replacement-matching change has no discriminating negative test for an unrelated registered handler. HotReloadReplacesHandlerAfterConstructorInjection registers only the handler being replaced, and the existing pending-replacement tests likewise register only their target. Broadening this predicate to accept every non-null registered handler type would therefore satisfy their assertions while replacing unrelated controls' handlers in an app with multiple registrations. Register and activate a second handler with a different FullName, replace the first, and assert that the second registration and subsequent activation remain unchanged. |
||
| .Select(x => new KeyValuePair<Type, Type>(x.Key, x.Value!)) | ||
| .Distinct() | ||
| .ToList(); | ||
|
|
||
| if (views.Count > 0) | ||
| replacedHandlers[oldHandlerType] = views.ToList(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| using Android.Content; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Maui.DeviceTests.Stubs; | ||
| using Microsoft.Maui.Handlers; | ||
| using Microsoft.Maui.Hosting; | ||
| using Microsoft.Maui.Platform; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Maui.DeviceTests | ||
| { | ||
| [Category(TestCategory.MauiContext)] | ||
| public class HandlerActivationTests | ||
| { | ||
| [Theory(DisplayName = "Injected Handler Captures The Current Android Context")] | ||
| [InlineData(false)] | ||
| [InlineData(true)] | ||
| public void InjectedHandlerCapturesCurrentAndroidContextAndDependency(bool singleton) | ||
| { | ||
| using var app = MauiApp.CreateBuilder() | ||
| .ConfigureMauiHandlers(handlers => | ||
| { | ||
| handlers.AddHandler<ContextElementStub, ContextCapturingHandler>(); | ||
| if (singleton) | ||
| handlers.Add(ServiceDescriptor.Singleton(typeof(ContextElementStub), typeof(ContextCapturingHandler))); | ||
| }) | ||
| .Build(); | ||
| var firstAndroidContext = new ContextWrapper(MauiProgram.DefaultContext); | ||
| var secondAndroidContext = new ContextWrapper(MauiProgram.DefaultContext); | ||
| var firstContext = new MauiContext(app.Services, firstAndroidContext); | ||
| var secondContext = new MauiContext(app.Services, secondAndroidContext); | ||
| var firstDependency = new ContextDependency(); | ||
| var secondDependency = new ContextDependency(); | ||
| firstContext.AddSpecific(firstDependency); | ||
| secondContext.AddSpecific(secondDependency); | ||
|
|
||
| var first = Assert.IsType<ContextCapturingHandler>(new ContextElementStub().ToHandler(firstContext)); | ||
| var again = Assert.IsType<ContextCapturingHandler>(new ContextElementStub().ToHandler(firstContext)); | ||
| var second = Assert.IsType<ContextCapturingHandler>(new ContextElementStub().ToHandler(secondContext)); | ||
|
|
||
| Assert.NotSame(first, again); | ||
| Assert.Same(firstAndroidContext, first.AndroidContext); | ||
| Assert.Same(firstDependency, first.Dependency); | ||
| Assert.Same(firstAndroidContext, again.AndroidContext); | ||
| Assert.Same(secondAndroidContext, second.AndroidContext); | ||
| Assert.Same(secondDependency, second.Dependency); | ||
| } | ||
|
|
||
| class ContextElementStub : ElementStub | ||
| { | ||
| } | ||
|
|
||
| class ContextDependency | ||
| { | ||
| } | ||
|
|
||
| class ContextCapturingHandler : ElementHandler<ContextElementStub, object> | ||
| { | ||
| public ContextCapturingHandler(Context androidContext, ContextDependency dependency) | ||
| : base(ElementHandler.ElementMapper) | ||
| { | ||
| AndroidContext = androidContext; | ||
| Dependency = dependency; | ||
| } | ||
|
|
||
| public Context AndroidContext { get; } | ||
|
|
||
| public ContextDependency Dependency { get; } | ||
|
|
||
| protected override object CreatePlatformElement() => new object(); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.