Skip to content

Commit

Permalink
test fixed and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahwilliams committed Nov 25, 2024
1 parent ec0f7b1 commit 5810e72
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,10 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
this.flutterEngine
.getPlatformViewsController()
.attachToFlutterRenderer(this.flutterEngine.getRenderer());
this.flutterEngine.getPlatformViewsController2().attachAccessibilityBridge(accessibilityBridge);
this.flutterEngine
.getPlatformViewsController2()
.attachToFlutterRenderer(this.flutterEngine.getRenderer());

// Inform the Android framework that it should retrieve a new InputConnection
// now that an engine is attached.
Expand All @@ -1187,6 +1191,7 @@ public void attachToFlutterEngine(@NonNull FlutterEngine flutterEngine) {
sendViewportMetricsToFlutter();

flutterEngine.getPlatformViewsController().attachToView(this);
flutterEngine.getPlatformViewsController2().attachToView(this);

// Notify engine attachment listeners of the attachment.
for (FlutterEngineAttachmentListener listener : flutterEngineAttachmentListeners) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.MotionEvent.PointerCoords;
Expand Down Expand Up @@ -587,12 +586,6 @@ public void destroyOverlaySurface() {
@Override
public void createPlatformView(
@NonNull PlatformViewsChannel2.PlatformViewCreationRequest request) {
if (Build.VERSION.SDK_INT < 34) {
throw new IllegalStateException(
"Trying to use platform views with API "
+ Build.VERSION.SDK_INT
+ ", required API level is 34.");
}
createFlutterPlatformView(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import io.flutter.embedding.android.FlutterImageView;
Expand Down Expand Up @@ -49,7 +47,6 @@
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
Expand Down Expand Up @@ -331,62 +328,6 @@ public void createPlatformViewMessage_setsAndroidViewLayoutDirection() {
verify(androidView, times(1)).setLayoutDirection(0);
}

@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_setsAndroidViewSize() {
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();

int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));

PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);

View androidView = mock(View.class);
when(platformView.getView()).thenReturn(androidView);
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
PlatformViewsController2.getRegistry().registerViewFactory("testType", viewFactory);

FlutterJNI jni = new FlutterJNI();
attach(jni, PlatformViewsController2);

// Simulate create call from the framework.
createPlatformView(jni, PlatformViewsController2, platformViewId, "testType");

ArgumentCaptor<FrameLayout.LayoutParams> layoutParamsCaptor =
ArgumentCaptor.forClass(FrameLayout.LayoutParams.class);
verify(androidView, times(2)).setLayoutParams(layoutParamsCaptor.capture());

List<FrameLayout.LayoutParams> capturedLayoutParams = layoutParamsCaptor.getAllValues();
assertEquals(capturedLayoutParams.get(0).width, 1);
assertEquals(capturedLayoutParams.get(0).height, 1);
}

@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_disablesAccessibility() {
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();

int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));

PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);

View androidView = mock(View.class);
when(platformView.getView()).thenReturn(androidView);
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
PlatformViewsController2.getRegistry().registerViewFactory("testType", viewFactory);

FlutterJNI jni = new FlutterJNI();
attach(jni, PlatformViewsController2);

// Simulate create call from the framework.
createPlatformView(jni, PlatformViewsController2, platformViewId, "testType");
verify(androidView, times(1))
.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
}

@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_throwsIfViewIsNull() {
Expand Down Expand Up @@ -435,36 +376,6 @@ public void createHybridPlatformViewMessage_throwsIfViewIsNull() {
assertFalse(PlatformViewsController2.initializePlatformViewIfNeeded(platformViewId));
}

@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void createPlatformViewMessage_throwsIfViewHasParent() {
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();

int platformViewId = 0;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));

PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);
View androidView = mock(View.class);
when(androidView.getParent()).thenReturn(mock(ViewParent.class));
when(platformView.getView()).thenReturn(androidView);
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
PlatformViewsController2.getRegistry().registerViewFactory("testType", viewFactory);

FlutterJNI jni = new FlutterJNI();
attach(jni, PlatformViewsController2);

// Simulate create call from the framework.
createPlatformView(jni, PlatformViewsController2, platformViewId, "testType");
assertEquals(ShadowFlutterJNI.getResponses().size(), 1);

assertThrows(
IllegalStateException.class,
() -> {
PlatformViewsController2.initializePlatformViewIfNeeded(platformViewId);
});
}

@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void setPlatformViewDirection_throwIfPlatformViewNotFound() {
Expand Down Expand Up @@ -570,36 +481,6 @@ public void disposeNullAndroidView() {
verify(platformView, times(1)).dispose();
}

@Test
@Config(shadows = {ShadowFlutterJNI.class, ShadowPlatformTaskQueue.class})
public void reattachToFlutterView() {
PlatformViewsController2 PlatformViewsController2 = new PlatformViewsController2();

int platformViewId = 100;
assertNull(PlatformViewsController2.getPlatformViewById(platformViewId));

PlatformViewFactory viewFactory = mock(PlatformViewFactory.class);
PlatformView platformView = mock(PlatformView.class);
View androidView = mock(View.class);
when(platformView.getView()).thenReturn(androidView);
when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView);
PlatformViewsController2.getRegistry().registerViewFactory("testType", viewFactory);

FlutterJNI jni = new FlutterJNI();
FlutterView initFlutterView = mock(FlutterView.class);
attachToFlutterView(jni, PlatformViewsController2, initFlutterView);

createPlatformView(jni, PlatformViewsController2, platformViewId, "testType");
verify(initFlutterView, times(1)).addView(any(PlatformViewWrapper.class));

PlatformViewsController2.detachFromView();
verify(initFlutterView, times(1)).removeView(any(PlatformViewWrapper.class));

FlutterView newFlutterView = mock(FlutterView.class);
PlatformViewsController2.attachToView(newFlutterView);
verify(newFlutterView, times(1)).addView(any(PlatformViewWrapper.class));
}

private static ByteBuffer encodeMethodCall(MethodCall call) {
final ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call);
buffer.rewind();
Expand Down

0 comments on commit 5810e72

Please sign in to comment.