7
7
8
8
import android .content .Context ;
9
9
import android .content .res .AssetManager ;
10
+ import android .util .SparseArray ;
10
11
import android .view .MotionEvent ;
11
12
import android .view .Surface ;
12
13
import android .view .SurfaceHolder ;
28
29
import io .flutter .embedding .engine .systemchannels .MouseCursorChannel ;
29
30
import io .flutter .embedding .engine .systemchannels .SettingsChannel ;
30
31
import io .flutter .embedding .engine .systemchannels .TextInputChannel ;
32
+ import io .flutter .plugin .common .FlutterException ;
31
33
import io .flutter .plugin .common .MethodCall ;
34
+ import io .flutter .plugin .common .StandardMessageCodec ;
32
35
import io .flutter .plugin .common .StandardMethodCodec ;
33
36
import io .flutter .plugin .localization .LocalizationPlugin ;
34
37
import java .nio .ByteBuffer ;
@@ -242,7 +245,29 @@ public void getPlatformViewById__hybridComposition() {
242
245
243
246
@ Test
244
247
@ Config (shadows = {ShadowFlutterJNI .class })
245
- public void initializePlatformViewIfNeeded__throwsIfViewIsNull () {
248
+ public void createPlatformViewMessage__initializesAndroidView () {
249
+ PlatformViewsController platformViewsController = new PlatformViewsController ();
250
+
251
+ int platformViewId = 0 ;
252
+ assertNull (platformViewsController .getPlatformViewById (platformViewId ));
253
+
254
+ PlatformViewFactory viewFactory = mock (PlatformViewFactory .class );
255
+ PlatformView platformView = mock (PlatformView .class );
256
+ when (platformView .getView ()).thenReturn (mock (View .class ));
257
+ when (viewFactory .create (any (), eq (platformViewId ), any ())).thenReturn (platformView );
258
+ platformViewsController .getRegistry ().registerViewFactory ("testType" , viewFactory );
259
+
260
+ FlutterJNI jni = new FlutterJNI ();
261
+ attach (jni , platformViewsController );
262
+
263
+ // Simulate create call from the framework.
264
+ createPlatformView (jni , platformViewsController , platformViewId , "testType" );
265
+ verify (platformView , times (1 )).getView ();
266
+ }
267
+
268
+ @ Test
269
+ @ Config (shadows = {ShadowFlutterJNI .class })
270
+ public void createPlatformViewMessage__throwsIfViewIsNull () {
246
271
PlatformViewsController platformViewsController = new PlatformViewsController ();
247
272
248
273
int platformViewId = 0 ;
@@ -259,22 +284,28 @@ public void initializePlatformViewIfNeeded__throwsIfViewIsNull() {
259
284
260
285
// Simulate create call from the framework.
261
286
createPlatformView (jni , platformViewsController , platformViewId , "testType" );
287
+ assertEquals (ShadowFlutterJNI .getResponses ().size (), 1 );
262
288
289
+ final ByteBuffer responseBuffer = ShadowFlutterJNI .getResponses ().get (0 );
290
+ responseBuffer .rewind ();
291
+
292
+ StandardMethodCodec methodCodec = new StandardMethodCodec (new StandardMessageCodec ());
263
293
try {
264
- platformViewsController .initializePlatformViewIfNeeded (platformViewId );
265
- } catch (Exception exception ) {
266
- assertTrue (exception instanceof IllegalStateException );
267
- assertEquals (
268
- exception .getMessage (),
269
- "PlatformView#getView() returned null, but an Android view reference was expected." );
294
+ methodCodec .decodeEnvelope (responseBuffer );
295
+ } catch (FlutterException exception ) {
296
+ assertTrue (
297
+ exception
298
+ .getMessage ()
299
+ .contains (
300
+ "PlatformView#getView() returned null, but an Android view reference was expected." ));
270
301
return ;
271
302
}
272
- assertTrue ( false );
303
+ assertFalse ( true );
273
304
}
274
305
275
306
@ Test
276
307
@ Config (shadows = {ShadowFlutterJNI .class })
277
- public void initializePlatformViewIfNeeded__throwsIfViewHasParent () {
308
+ public void createPlatformViewMessage__throwsIfViewHasParent () {
278
309
PlatformViewsController platformViewsController = new PlatformViewsController ();
279
310
280
311
int platformViewId = 0 ;
@@ -293,16 +324,23 @@ public void initializePlatformViewIfNeeded__throwsIfViewHasParent() {
293
324
294
325
// Simulate create call from the framework.
295
326
createPlatformView (jni , platformViewsController , platformViewId , "testType" );
327
+ assertEquals (ShadowFlutterJNI .getResponses ().size (), 1 );
328
+
329
+ final ByteBuffer responseBuffer = ShadowFlutterJNI .getResponses ().get (0 );
330
+ responseBuffer .rewind ();
331
+
332
+ StandardMethodCodec methodCodec = new StandardMethodCodec (new StandardMessageCodec ());
296
333
try {
297
- platformViewsController .initializePlatformViewIfNeeded (platformViewId );
298
- } catch (Exception exception ) {
299
- assertTrue (exception instanceof IllegalStateException );
300
- assertEquals (
301
- exception .getMessage (),
302
- "The Android view returned from PlatformView#getView() was already added to a parent view." );
334
+ methodCodec .decodeEnvelope (responseBuffer );
335
+ } catch (FlutterException exception ) {
336
+ assertTrue (
337
+ exception
338
+ .getMessage ()
339
+ .contains (
340
+ "The Android view returned from PlatformView#getView() was already added to a parent view." ));
303
341
return ;
304
342
}
305
- assertTrue ( false );
343
+ assertFalse ( true );
306
344
}
307
345
308
346
@ Test
@@ -481,6 +519,7 @@ public FlutterImageView createImageView() {
481
519
482
520
@ Implements (FlutterJNI .class )
483
521
public static class ShadowFlutterJNI {
522
+ private static SparseArray <ByteBuffer > replies = new SparseArray <>();
484
523
485
524
public ShadowFlutterJNI () {}
486
525
@@ -527,7 +566,13 @@ public void setViewportMetrics(
527
566
528
567
@ Implementation
529
568
public void invokePlatformMessageResponseCallback (
530
- int responseId , ByteBuffer message , int position ) {}
569
+ int responseId , ByteBuffer message , int position ) {
570
+ replies .put (responseId , message );
571
+ }
572
+
573
+ public static SparseArray <ByteBuffer > getResponses () {
574
+ return replies ;
575
+ }
531
576
}
532
577
533
578
@ Implements (SurfaceView .class )
0 commit comments