|
3 | 3 | import static io.flutter.embedding.engine.systemchannels.PlatformViewsChannel.PlatformViewTouch;
|
4 | 4 | import static org.junit.Assert.assertEquals;
|
5 | 5 | import static org.junit.Assert.assertNotEquals;
|
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertNull; |
6 | 8 | import static org.mockito.Matchers.eq;
|
| 9 | +import static org.mockito.Mockito.any; |
7 | 10 | import static org.mockito.Mockito.mock;
|
8 | 11 | import static org.mockito.Mockito.never;
|
9 | 12 | import static org.mockito.Mockito.times;
|
10 | 13 | import static org.mockito.Mockito.verify;
|
| 14 | +import static org.mockito.Mockito.when; |
11 | 15 |
|
| 16 | +import android.content.Context; |
| 17 | +import android.content.res.AssetManager; |
12 | 18 | import android.view.MotionEvent;
|
13 | 19 | import android.view.View;
|
| 20 | +import io.flutter.embedding.android.FlutterView; |
14 | 21 | import io.flutter.embedding.android.MotionEventTracker;
|
| 22 | +import io.flutter.embedding.engine.FlutterJNI; |
| 23 | +import io.flutter.embedding.engine.dart.DartExecutor; |
| 24 | +import io.flutter.plugin.common.MethodCall; |
| 25 | +import io.flutter.plugin.common.StandardMethodCodec; |
| 26 | +import java.nio.ByteBuffer; |
15 | 27 | import java.util.Arrays;
|
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.Map; |
16 | 30 | import org.junit.Ignore;
|
17 | 31 | import org.junit.Test;
|
18 | 32 | import org.junit.runner.RunWith;
|
@@ -186,4 +200,54 @@ public void itUsesActionEventTypeFromMotionEventForHybridPlatformViews() {
|
186 | 200 | assertNotEquals(resolvedEvent.getAction(), frameWorkTouch.action);
|
187 | 201 | assertEquals(resolvedEvent.getAction(), original.getAction());
|
188 | 202 | }
|
| 203 | + |
| 204 | + @Test |
| 205 | + public void getPlatformViewById__hybridComposition() { |
| 206 | + PlatformViewsController platformViewsController = new PlatformViewsController(); |
| 207 | + |
| 208 | + int platformViewId = 0; |
| 209 | + assertNull(platformViewsController.getPlatformViewById(platformViewId)); |
| 210 | + |
| 211 | + FlutterJNI jni = new FlutterJNI(); |
| 212 | + AssetManager assetManager = mock(AssetManager.class); |
| 213 | + Context context = RuntimeEnvironment.application.getApplicationContext(); |
| 214 | + |
| 215 | + DartExecutor executor = new DartExecutor(jni, assetManager); |
| 216 | + executor.onAttachedToJNI(); |
| 217 | + platformViewsController.attach(context, null, executor); |
| 218 | + platformViewsController.attachToView(mock(FlutterView.class)); |
| 219 | + |
| 220 | + PlatformViewFactory viewFactory = mock(PlatformViewFactory.class); |
| 221 | + PlatformView platformView = mock(PlatformView.class); |
| 222 | + View androidView = mock(View.class); |
| 223 | + when(platformView.getView()).thenReturn(androidView); |
| 224 | + when(viewFactory.create(any(), eq(platformViewId), any())).thenReturn(platformView); |
| 225 | + |
| 226 | + platformViewsController.getRegistry().registerViewFactory("testType", viewFactory); |
| 227 | + |
| 228 | + // Simulate create call from the framework. |
| 229 | + Map<String, Object> platformViewCreateArguments = new HashMap<>(); |
| 230 | + platformViewCreateArguments.put("hybrid", true); |
| 231 | + platformViewCreateArguments.put("id", platformViewId); |
| 232 | + platformViewCreateArguments.put("viewType", "testType"); |
| 233 | + platformViewCreateArguments.put("direction", 0); |
| 234 | + MethodCall platformCreateMethodCall = new MethodCall("create", platformViewCreateArguments); |
| 235 | + |
| 236 | + jni.handlePlatformMessage( |
| 237 | + "flutter/platform_views", encodeMethodCall(platformCreateMethodCall), /*replyId=*/ 0); |
| 238 | + |
| 239 | + platformViewsController.initializePlatformViewIfNeeded(platformViewId); |
| 240 | + |
| 241 | + View resultAndroidView = platformViewsController.getPlatformViewById(platformViewId); |
| 242 | + assertNotNull(resultAndroidView); |
| 243 | + assertEquals(resultAndroidView, androidView); |
| 244 | + } |
| 245 | + |
| 246 | + private static byte[] encodeMethodCall(MethodCall call) { |
| 247 | + ByteBuffer buffer = StandardMethodCodec.INSTANCE.encodeMethodCall(call); |
| 248 | + buffer.rewind(); |
| 249 | + byte[] dest = new byte[buffer.remaining()]; |
| 250 | + buffer.get(dest); |
| 251 | + return dest; |
| 252 | + } |
189 | 253 | }
|
0 commit comments