Don't wrap Context for Android Hybrid Composition (flutter/engine#36756)

This commit is contained in:
stuartmorgan 2022-10-14 16:58:06 -04:00 committed by GitHub
parent ee4b501e02
commit 5d6387fb32
2 changed files with 56 additions and 4 deletions

View File

@ -161,7 +161,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
ensureValidAndroidVersion(19);
ensureValidRequest(request);
final PlatformView platformView = createPlatformView(request);
final PlatformView platformView = createPlatformView(request, false);
configureForHybridComposition(platformView, request);
// New code should be added to configureForHybridComposition, not here, unless it is
@ -189,7 +189,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
+ viewId);
}
final PlatformView platformView = createPlatformView(request);
final PlatformView platformView = createPlatformView(request, true);
final View embeddedView = platformView.getView();
if (embeddedView.getParent() != null) {
@ -477,7 +477,8 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
// all display modes, and adds it to `platformViews`.
@TargetApi(19)
private PlatformView createPlatformView(
@NonNull PlatformViewsChannel.PlatformViewCreationRequest request) {
@NonNull PlatformViewsChannel.PlatformViewCreationRequest request,
boolean wrapContext) {
final PlatformViewFactory viewFactory = registry.getFactory(request.viewType);
if (viewFactory == null) {
throw new IllegalStateException(
@ -490,7 +491,9 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega
}
// In some display modes, the context needs to be modified during display.
final Context mutableContext = new MutableContextWrapper(context);
// TODO(stuartmorgan): Make this wrapping unconditional if possible; for context see
// https://github.com/flutter/flutter/issues/113449
final Context mutableContext = wrapContext ? new MutableContextWrapper(context) : context;
final PlatformView platformView =
viewFactory.create(mutableContext, request.viewId, createParams);

View File

@ -312,6 +312,55 @@ public class PlatformViewsControllerTest {
verify(viewFactory, times(1)).create(any(), eq(platformViewId), any());
}
@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage__wrapsContextForVirtualDisplay() {
PlatformViewsController platformViewsController = new PlatformViewsController();
int platformViewId = 0;
assertNull(platformViewsController.getPlatformViewById(platformViewId));
PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);
when(platformView.getView()).thenReturn(mock(View.class));
ArgumentCaptor<Context> passedContext = ArgumentCaptor.forClass(Context.class);
when(viewFactory.create(passedContext.capture(), eq(platformViewId), any()))
.thenReturn(platformView);
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
FlutterJNI jni = new FlutterJNI();
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(
jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ false);
assertTrue(passedContext.getValue() instanceof MutableContextWrapper);
}
@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage__doesNotWrapContextForHybrid() {
PlatformViewsController platformViewsController = new PlatformViewsController();
int platformViewId = 0;
assertNull(platformViewsController.getPlatformViewById(platformViewId));
PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);
when(platformView.getView()).thenReturn(mock(View.class));
ArgumentCaptor<Context> passedContext = ArgumentCaptor.forClass(Context.class);
when(viewFactory.create(passedContext.capture(), eq(platformViewId), any()))
.thenReturn(platformView);
platformViewsController.getRegistry().registerViewFactory("testType", viewFactory);
FlutterJNI jni = new FlutterJNI();
attach(jni, platformViewsController);
// Simulate create call from the framework.
createPlatformView(jni, platformViewsController, platformViewId, "testType", /* hybrid=*/ true);
assertFalse(passedContext.getValue() instanceof MutableContextWrapper);
}
@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage__setsAndroidViewLayoutDirection() {