7
7
8
8
import android .content .Context ;
9
9
import android .content .res .AssetManager ;
10
+ import android .graphics .SurfaceTexture ;
10
11
import android .util .SparseArray ;
11
12
import android .view .MotionEvent ;
12
13
import android .view .Surface ;
13
14
import android .view .SurfaceHolder ;
14
15
import android .view .SurfaceView ;
15
16
import android .view .View ;
16
17
import android .view .ViewParent ;
18
+ import android .widget .FrameLayout .LayoutParams ;
17
19
import io .flutter .embedding .android .FlutterImageView ;
18
20
import io .flutter .embedding .android .FlutterView ;
19
21
import io .flutter .embedding .android .MotionEventTracker ;
32
34
import io .flutter .plugin .common .MethodCall ;
33
35
import io .flutter .plugin .common .StandardMethodCodec ;
34
36
import io .flutter .plugin .localization .LocalizationPlugin ;
37
+ import io .flutter .view .TextureRegistry ;
35
38
import java .nio .ByteBuffer ;
36
39
import java .util .Arrays ;
37
40
import java .util .HashMap ;
@@ -232,7 +235,7 @@ public void getPlatformViewById__hybridComposition() {
232
235
attach (jni , platformViewsController );
233
236
234
237
// Simulate create call from the framework.
235
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
238
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
236
239
237
240
platformViewsController .initializePlatformViewIfNeeded (platformViewId );
238
241
@@ -259,7 +262,7 @@ public void createPlatformViewMessage__initializesAndroidView() {
259
262
attach (jni , platformViewsController );
260
263
261
264
// Simulate create call from the framework.
262
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
265
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
263
266
verify (viewFactory , times (1 )).create (any (), eq (platformViewId ), any ());
264
267
}
265
268
@@ -281,7 +284,7 @@ public void createPlatformViewMessage__throwsIfViewIsNull() {
281
284
attach (jni , platformViewsController );
282
285
283
286
// Simulate create call from the framework.
284
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
287
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
285
288
assertEquals (ShadowFlutterJNI .getResponses ().size (), 1 );
286
289
287
290
assertThrows (
@@ -291,6 +294,66 @@ public void createPlatformViewMessage__throwsIfViewIsNull() {
291
294
});
292
295
}
293
296
297
+ @ Test
298
+ @ Config (shadows = {ShadowFlutterJNI .class })
299
+ public void onDetachedFromJNI_clearsPlatformViewContext () {
300
+ PlatformViewsController platformViewsController = new PlatformViewsController ();
301
+
302
+ int platformViewId = 0 ;
303
+ assertNull (platformViewsController .getPlatformViewById (platformViewId ));
304
+
305
+ PlatformViewFactory viewFactory = mock (PlatformViewFactory .class );
306
+ PlatformView platformView = mock (PlatformView .class );
307
+
308
+ View pv = mock (View .class );
309
+ when (pv .getLayoutParams ()).thenReturn (new LayoutParams (1 , 1 ));
310
+
311
+ when (platformView .getView ()).thenReturn (pv );
312
+ when (viewFactory .create (any (), eq (platformViewId ), any ())).thenReturn (platformView );
313
+ platformViewsController .getRegistry ().registerViewFactory ("testType" , viewFactory );
314
+
315
+ FlutterJNI jni = new FlutterJNI ();
316
+ attach (jni , platformViewsController );
317
+
318
+ // Simulate create call from the framework.
319
+ createPlatformView (
320
+ jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ false );
321
+
322
+ assertFalse (platformViewsController .contextToPlatformView .isEmpty ());
323
+ platformViewsController .onDetachedFromJNI ();
324
+ assertTrue (platformViewsController .contextToPlatformView .isEmpty ());
325
+ }
326
+
327
+ @ Test
328
+ @ Config (shadows = {ShadowFlutterJNI .class })
329
+ public void onPreEngineRestart_clearsPlatformViewContext () {
330
+ PlatformViewsController platformViewsController = new PlatformViewsController ();
331
+
332
+ int platformViewId = 0 ;
333
+ assertNull (platformViewsController .getPlatformViewById (platformViewId ));
334
+
335
+ PlatformViewFactory viewFactory = mock (PlatformViewFactory .class );
336
+ PlatformView platformView = mock (PlatformView .class );
337
+
338
+ View pv = mock (View .class );
339
+ when (pv .getLayoutParams ()).thenReturn (new LayoutParams (1 , 1 ));
340
+
341
+ when (platformView .getView ()).thenReturn (pv );
342
+ when (viewFactory .create (any (), eq (platformViewId ), any ())).thenReturn (platformView );
343
+ platformViewsController .getRegistry ().registerViewFactory ("testType" , viewFactory );
344
+
345
+ FlutterJNI jni = new FlutterJNI ();
346
+ attach (jni , platformViewsController );
347
+
348
+ // Simulate create call from the framework.
349
+ createPlatformView (
350
+ jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ false );
351
+
352
+ assertFalse (platformViewsController .contextToPlatformView .isEmpty ());
353
+ platformViewsController .onDetachedFromJNI ();
354
+ assertTrue (platformViewsController .contextToPlatformView .isEmpty ());
355
+ }
356
+
294
357
@ Test
295
358
@ Config (shadows = {ShadowFlutterJNI .class })
296
359
public void createPlatformViewMessage__throwsIfViewHasParent () {
@@ -311,7 +374,7 @@ public void createPlatformViewMessage__throwsIfViewHasParent() {
311
374
attach (jni , platformViewsController );
312
375
313
376
// Simulate create call from the framework.
314
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
377
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
315
378
assertEquals (ShadowFlutterJNI .getResponses ().size (), 1 );
316
379
317
380
assertThrows (
@@ -343,7 +406,7 @@ public void disposeAndroidView__hybridComposition() {
343
406
attach (jni , platformViewsController );
344
407
345
408
// Simulate create call from the framework.
346
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
409
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
347
410
platformViewsController .initializePlatformViewIfNeeded (platformViewId );
348
411
349
412
assertNotNull (androidView .getParent ());
@@ -354,7 +417,7 @@ public void disposeAndroidView__hybridComposition() {
354
417
assertNull (androidView .getParent ());
355
418
356
419
// Simulate create call from the framework.
357
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
420
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
358
421
platformViewsController .initializePlatformViewIfNeeded (platformViewId );
359
422
360
423
assertNotNull (androidView .getParent ());
@@ -384,7 +447,7 @@ public void onEndFrame__destroysOverlaySurfaceAfterFrameOnFlutterSurfaceView() {
384
447
jni .onFirstFrame ();
385
448
386
449
// Simulate create call from the framework.
387
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
450
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
388
451
389
452
// Produce a frame that displays a platform view and an overlay surface.
390
453
platformViewsController .onBeginFrame ();
@@ -447,7 +510,7 @@ public void onEndFrame__removesPlatformView() {
447
510
jni .onFirstFrame ();
448
511
449
512
// Simulate create call from the framework.
450
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
513
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
451
514
452
515
// Simulate first frame from the framework.
453
516
jni .onFirstFrame ();
@@ -484,7 +547,7 @@ public void onEndFrame__removesPlatformViewParent() {
484
547
jni .onFirstFrame ();
485
548
486
549
// Simulate create call from the framework.
487
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
550
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
488
551
platformViewsController .initializePlatformViewIfNeeded (platformViewId );
489
552
assertEquals (flutterView .getChildCount (), 2 );
490
553
@@ -520,7 +583,7 @@ public void detach__destroysOverlaySurfaces() {
520
583
jni .onFirstFrame ();
521
584
522
585
// Simulate create call from the framework.
523
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
586
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
524
587
525
588
// Produce a frame that displays a platform view and an overlay surface.
526
589
platformViewsController .onBeginFrame ();
@@ -653,7 +716,7 @@ public void convertPlatformViewRenderSurfaceAsDefault() {
653
716
jni .onFirstFrame ();
654
717
655
718
// Simulate create call from the framework.
656
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
719
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
657
720
658
721
// Produce a frame that displays a platform view and an overlay surface.
659
722
platformViewsController .onBeginFrame ();
@@ -702,7 +765,7 @@ public void dontConverRenderSurfaceWhenFlagIsTrue() {
702
765
synchronizeToNativeViewHierarchy (jni , platformViewsController , false );
703
766
704
767
// Simulate create call from the framework.
705
- createPlatformView (jni , platformViewsController , platformViewId , "testType" );
768
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" , /* hybrid=*/ true );
706
769
707
770
// Produce a frame that displays a platform view and an overlay surface.
708
771
platformViewsController .onBeginFrame ();
@@ -734,12 +797,16 @@ private static void createPlatformView(
734
797
FlutterJNI jni ,
735
798
PlatformViewsController platformViewsController ,
736
799
int platformViewId ,
737
- String viewType ) {
800
+ String viewType ,
801
+ boolean hybrid ) {
738
802
final Map <String , Object > platformViewCreateArguments = new HashMap <>();
739
- platformViewCreateArguments .put ("hybrid" , true );
803
+ platformViewCreateArguments .put ("hybrid" , hybrid );
740
804
platformViewCreateArguments .put ("id" , platformViewId );
741
805
platformViewCreateArguments .put ("viewType" , viewType );
742
806
platformViewCreateArguments .put ("direction" , 0 );
807
+ platformViewCreateArguments .put ("width" , 1.0 );
808
+ platformViewCreateArguments .put ("height" , 1.0 );
809
+
743
810
final MethodCall platformCreateMethodCall =
744
811
new MethodCall ("create" , platformViewCreateArguments );
745
812
@@ -776,7 +843,30 @@ private static FlutterView attach(
776
843
executor .onAttachedToJNI ();
777
844
778
845
final Context context = RuntimeEnvironment .application .getApplicationContext ();
779
- platformViewsController .attach (context , null , executor );
846
+ final TextureRegistry registry =
847
+ new TextureRegistry () {
848
+ public void TextureRegistry () {}
849
+
850
+ @ Override
851
+ public SurfaceTextureEntry createSurfaceTexture () {
852
+ return new SurfaceTextureEntry () {
853
+ @ Override
854
+ public SurfaceTexture surfaceTexture () {
855
+ return mock (SurfaceTexture .class );
856
+ }
857
+
858
+ @ Override
859
+ public long id () {
860
+ return 0 ;
861
+ }
862
+
863
+ @ Override
864
+ public void release () {}
865
+ };
866
+ }
867
+ };
868
+
869
+ platformViewsController .attach (context , registry , executor );
780
870
781
871
final FlutterView view =
782
872
new FlutterView (context , FlutterView .RenderMode .surface ) {
0 commit comments