Skip to content

Commit 115c859

Browse files
authored
Refactor FlutterViewTest with extracting common test code to methods (flutter#29226)
1 parent 942748a commit 115c859

File tree

1 file changed

+66
-120
lines changed

1 file changed

+66
-120
lines changed

shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java

Lines changed: 66 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.media.ImageReader;
2727
import android.os.Build;
2828
import android.view.DisplayCutout;
29+
import android.view.Surface;
2930
import android.view.View;
3031
import android.view.ViewGroup;
3132
import android.view.WindowInsets;
@@ -270,10 +271,7 @@ public void setPaddingTopToZeroForFullscreenMode() {
270271

271272
// Verify.
272273
verify(flutterRenderer, times(3)).setViewportMetrics(viewportMetricsCaptor.capture());
273-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingTop);
274-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingBottom);
275-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingLeft);
276-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingRight);
274+
validateViewportMetricPadding(viewportMetricsCaptor, 100, 100, 100, 100);
277275
}
278276

279277
// This test uses the pre-API 30 Algorithm for window insets.
@@ -303,18 +301,12 @@ public void setPaddingTopToZeroForFullscreenModeLegacy() {
303301

304302
// Then we simulate the system applying a window inset.
305303
WindowInsets windowInsets = mock(WindowInsets.class);
306-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(100);
307-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(100);
308-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(100);
309-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(100);
304+
mockSystemWindowInsets(windowInsets, 100, 100, 100, 100);
310305
flutterView.onApplyWindowInsets(windowInsets);
311306

312307
// Verify.
313308
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
314-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingTop);
315-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingBottom);
316-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingLeft);
317-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingRight);
309+
validateViewportMetricPadding(viewportMetricsCaptor, 100, 0, 100, 0);
318310
}
319311

320312
// This test uses the API 30+ Algorithm for window insets. The legacy algorithm is
@@ -354,10 +346,7 @@ public void reportSystemInsetWhenNotFullscreen() {
354346
// Verify.
355347
verify(flutterRenderer, times(3)).setViewportMetrics(viewportMetricsCaptor.capture());
356348
// Top padding is reported as-is.
357-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingTop);
358-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingBottom);
359-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingLeft);
360-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingRight);
349+
validateViewportMetricPadding(viewportMetricsCaptor, 100, 100, 100, 100);
361350
}
362351

363352
// This test uses the pre-API 30 Algorithm for window insets.
@@ -385,32 +374,21 @@ public void reportSystemInsetWhenNotFullscreenLegacy() {
385374

386375
// Then we simulate the system applying a window inset.
387376
WindowInsets windowInsets = mock(WindowInsets.class);
388-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(100);
389-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(100);
390-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(100);
391-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(100);
377+
mockSystemWindowInsets(windowInsets, 100, 100, 100, 100);
392378
flutterView.onApplyWindowInsets(windowInsets);
393379

394380
// Verify.
395381
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
396382
// Top padding is reported as-is.
397-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingTop);
398-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingBottom);
399-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingLeft);
400-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingRight);
383+
validateViewportMetricPadding(viewportMetricsCaptor, 100, 100, 100, 0);
401384
}
402385

403386
@Test
404387
@Config(minSdk = 23, maxSdk = 29)
405388
public void systemInsetHandlesFullscreenNavbarRight() {
406389
RuntimeEnvironment.setQualifiers("+land");
407390
FlutterView flutterView = spy(new FlutterView(RuntimeEnvironment.systemContext));
408-
ShadowDisplay display =
409-
Shadows.shadowOf(
410-
((WindowManager)
411-
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
412-
.getDefaultDisplay());
413-
display.setRotation(1);
391+
setExpectedDisplayRotation(Surface.ROTATION_90);
414392
assertEquals(0, flutterView.getSystemUiVisibility());
415393
when(flutterView.getWindowSystemUiVisibility())
416394
.thenReturn(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
@@ -431,37 +409,24 @@ public void systemInsetHandlesFullscreenNavbarRight() {
431409

432410
// Then we simulate the system applying a window inset.
433411
WindowInsets windowInsets = mock(WindowInsets.class);
434-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(100);
435-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(100);
436-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(100);
437-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(100);
438-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
439-
when(windowInsets.getSystemGestureInsets()).thenReturn(Insets.NONE);
440-
}
412+
mockSystemWindowInsets(windowInsets, 100, 100, 100, 100);
413+
mockSystemGestureInsetsIfNeed(windowInsets);
441414

442415
flutterView.onApplyWindowInsets(windowInsets);
443416

444417
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
445418
// Top padding is removed due to full screen.
446-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingTop);
447-
// Bottom padding is removed due to hide navigation.
448-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingBottom);
449-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingLeft);
450419
// Right padding is zero because the rotation is 90deg
451-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingRight);
420+
// Bottom padding is removed due to hide navigation.
421+
validateViewportMetricPadding(viewportMetricsCaptor, 100, 0, 0, 0);
452422
}
453423

454424
@Test
455425
@Config(minSdk = 20, maxSdk = 22)
456426
public void systemInsetHandlesFullscreenNavbarRightBelowSDK23() {
457427
RuntimeEnvironment.setQualifiers("+land");
458428
FlutterView flutterView = spy(new FlutterView(RuntimeEnvironment.systemContext));
459-
ShadowDisplay display =
460-
Shadows.shadowOf(
461-
((WindowManager)
462-
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
463-
.getDefaultDisplay());
464-
display.setRotation(3);
429+
setExpectedDisplayRotation(Surface.ROTATION_270);
465430
assertEquals(0, flutterView.getSystemUiVisibility());
466431
when(flutterView.getWindowSystemUiVisibility())
467432
.thenReturn(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
@@ -482,37 +447,24 @@ public void systemInsetHandlesFullscreenNavbarRightBelowSDK23() {
482447

483448
// Then we simulate the system applying a window inset.
484449
WindowInsets windowInsets = mock(WindowInsets.class);
485-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(100);
486-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(100);
487-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(100);
488-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(100);
489-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
490-
when(windowInsets.getSystemGestureInsets()).thenReturn(Insets.NONE);
491-
}
450+
mockSystemWindowInsets(windowInsets, 100, 100, 100, 100);
451+
mockSystemGestureInsetsIfNeed(windowInsets);
492452

493453
flutterView.onApplyWindowInsets(windowInsets);
494454

495455
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
496456
// Top padding is removed due to full screen.
497-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingTop);
498-
// Bottom padding is removed due to hide navigation.
499-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingBottom);
500457
// Right padding is zero because the rotation is 270deg under SDK 23
501-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingRight);
502-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingLeft);
458+
// Bottom padding is removed due to hide navigation.
459+
validateViewportMetricPadding(viewportMetricsCaptor, 100, 0, 0, 0);
503460
}
504461

505462
@Test
506463
@Config(minSdk = 23, maxSdk = 29)
507464
public void systemInsetHandlesFullscreenNavbarLeft() {
508465
RuntimeEnvironment.setQualifiers("+land");
509466
FlutterView flutterView = spy(new FlutterView(RuntimeEnvironment.systemContext));
510-
ShadowDisplay display =
511-
Shadows.shadowOf(
512-
((WindowManager)
513-
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
514-
.getDefaultDisplay());
515-
display.setRotation(3);
467+
setExpectedDisplayRotation(Surface.ROTATION_270);
516468
assertEquals(0, flutterView.getSystemUiVisibility());
517469
when(flutterView.getWindowSystemUiVisibility())
518470
.thenReturn(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
@@ -533,24 +485,16 @@ public void systemInsetHandlesFullscreenNavbarLeft() {
533485

534486
// Then we simulate the system applying a window inset.
535487
WindowInsets windowInsets = mock(WindowInsets.class);
536-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(100);
537-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(100);
538-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(100);
539-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(100);
540-
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
541-
when(windowInsets.getSystemGestureInsets()).thenReturn(Insets.NONE);
542-
}
488+
mockSystemWindowInsets(windowInsets, 100, 100, 100, 100);
489+
mockSystemGestureInsetsIfNeed(windowInsets);
543490

544491
flutterView.onApplyWindowInsets(windowInsets);
545492

546493
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
494+
// Left padding is zero because the rotation is 270deg
547495
// Top padding is removed due to full screen.
548-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingTop);
549496
// Bottom padding is removed due to hide navigation.
550-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingBottom);
551-
// Left padding is zero because the rotation is 270deg
552-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingLeft);
553-
assertEquals(100, viewportMetricsCaptor.getValue().viewPaddingRight);
497+
validateViewportMetricPadding(viewportMetricsCaptor, 0, 0, 100, 0);
554498
}
555499

556500
// This test uses the API 30+ Algorithm for window insets. The legacy algorithm is
@@ -561,12 +505,7 @@ public void systemInsetHandlesFullscreenNavbarLeft() {
561505
public void systemInsetGetInsetsFullscreen() {
562506
RuntimeEnvironment.setQualifiers("+land");
563507
FlutterView flutterView = spy(new FlutterView(RuntimeEnvironment.systemContext));
564-
ShadowDisplay display =
565-
Shadows.shadowOf(
566-
((WindowManager)
567-
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
568-
.getDefaultDisplay());
569-
display.setRotation(3);
508+
setExpectedDisplayRotation(Surface.ROTATION_270);
570509
assertEquals(0, flutterView.getSystemUiVisibility());
571510
when(flutterView.getWindowSystemUiVisibility())
572511
.thenReturn(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
@@ -588,19 +527,13 @@ public void systemInsetGetInsetsFullscreen() {
588527
Insets insets = Insets.of(10, 20, 30, 40);
589528
// Then we simulate the system applying a window inset.
590529
WindowInsets windowInsets = mock(WindowInsets.class);
591-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(-1);
592-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(-1);
593-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(-1);
594-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(-1);
530+
mockSystemWindowInsets(windowInsets, -1, -1, -1, -1);
595531
when(windowInsets.getInsets(anyInt())).thenReturn(insets);
596532

597533
flutterView.onApplyWindowInsets(windowInsets);
598534

599535
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
600-
assertEquals(10, viewportMetricsCaptor.getValue().viewPaddingLeft);
601-
assertEquals(20, viewportMetricsCaptor.getValue().viewPaddingTop);
602-
assertEquals(30, viewportMetricsCaptor.getValue().viewPaddingRight);
603-
assertEquals(40, viewportMetricsCaptor.getValue().viewPaddingBottom);
536+
validateViewportMetricPadding(viewportMetricsCaptor, 10, 20, 30, 40);
604537
}
605538

606539
// This test uses the pre-API 30 Algorithm for window insets.
@@ -610,12 +543,7 @@ public void systemInsetGetInsetsFullscreen() {
610543
public void systemInsetGetInsetsFullscreenLegacy() {
611544
RuntimeEnvironment.setQualifiers("+land");
612545
FlutterView flutterView = spy(new FlutterView(RuntimeEnvironment.systemContext));
613-
ShadowDisplay display =
614-
Shadows.shadowOf(
615-
((WindowManager)
616-
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
617-
.getDefaultDisplay());
618-
display.setRotation(3);
546+
setExpectedDisplayRotation(Surface.ROTATION_270);
619547
assertEquals(0, flutterView.getSystemUiVisibility());
620548
when(flutterView.getWindowSystemUiVisibility())
621549
.thenReturn(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
@@ -636,21 +564,15 @@ public void systemInsetGetInsetsFullscreenLegacy() {
636564

637565
// Then we simulate the system applying a window inset.
638566
WindowInsets windowInsets = mock(WindowInsets.class);
639-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(100);
640-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(101);
641-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(102);
642-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(103);
567+
mockSystemWindowInsets(windowInsets, 102, 100, 103, 101);
643568

644569
flutterView.onApplyWindowInsets(windowInsets);
645570

646571
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
572+
// Left padding is zero because the rotation is 270deg
647573
// Top padding is removed due to full screen.
648-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingTop);
649574
// Bottom padding is removed due to hide navigation.
650-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingBottom);
651-
// Left padding is zero because the rotation is 270deg
652-
assertEquals(0, viewportMetricsCaptor.getValue().viewPaddingLeft);
653-
assertEquals(103, viewportMetricsCaptor.getValue().viewPaddingRight);
575+
validateViewportMetricPadding(viewportMetricsCaptor, 0, 0, 103, 0);
654576
}
655577

656578
// This test uses the API 30+ Algorithm for window insets. The legacy algorithm is
@@ -661,11 +583,6 @@ public void systemInsetGetInsetsFullscreenLegacy() {
661583
public void systemInsetDisplayCutoutSimple() {
662584
RuntimeEnvironment.setQualifiers("+land");
663585
FlutterView flutterView = spy(new FlutterView(RuntimeEnvironment.systemContext));
664-
ShadowDisplay display =
665-
Shadows.shadowOf(
666-
((WindowManager)
667-
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
668-
.getDefaultDisplay());
669586
assertEquals(0, flutterView.getSystemUiVisibility());
670587
when(flutterView.getWindowSystemUiVisibility()).thenReturn(0);
671588
when(flutterView.getContext()).thenReturn(RuntimeEnvironment.systemContext);
@@ -688,10 +605,7 @@ public void systemInsetDisplayCutoutSimple() {
688605
// Then we simulate the system applying a window inset.
689606
WindowInsets windowInsets = mock(WindowInsets.class);
690607
DisplayCutout displayCutout = mock(DisplayCutout.class);
691-
when(windowInsets.getSystemWindowInsetTop()).thenReturn(-1);
692-
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(-1);
693-
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(-1);
694-
when(windowInsets.getSystemWindowInsetRight()).thenReturn(-1);
608+
mockSystemWindowInsets(windowInsets, -1, -1, -1, -1);
695609
when(windowInsets.getInsets(anyInt())).thenReturn(insets);
696610
when(windowInsets.getSystemGestureInsets()).thenReturn(systemGestureInsets);
697611
when(windowInsets.getDisplayCutout()).thenReturn(displayCutout);
@@ -706,10 +620,7 @@ public void systemInsetDisplayCutoutSimple() {
706620
flutterView.onApplyWindowInsets(windowInsets);
707621

708622
verify(flutterRenderer, times(2)).setViewportMetrics(viewportMetricsCaptor.capture());
709-
assertEquals(150, viewportMetricsCaptor.getValue().viewPaddingTop);
710-
assertEquals(150, viewportMetricsCaptor.getValue().viewPaddingBottom);
711-
assertEquals(200, viewportMetricsCaptor.getValue().viewPaddingLeft);
712-
assertEquals(200, viewportMetricsCaptor.getValue().viewPaddingRight);
623+
validateViewportMetricPadding(viewportMetricsCaptor, 200, 150, 200, 150);
713624

714625
assertEquals(100, viewportMetricsCaptor.getValue().viewInsetTop);
715626
}
@@ -913,6 +824,41 @@ public void ViewportMetrics_initializedPhysicalTouchSlop() {
913824
assertFalse(-1 == viewportMetricsCaptor.getValue().physicalTouchSlop);
914825
}
915826

827+
private void setExpectedDisplayRotation(int rotation) {
828+
ShadowDisplay display =
829+
Shadows.shadowOf(
830+
((WindowManager)
831+
RuntimeEnvironment.systemContext.getSystemService(Context.WINDOW_SERVICE))
832+
.getDefaultDisplay());
833+
display.setRotation(rotation);
834+
}
835+
836+
private void validateViewportMetricPadding(
837+
ArgumentCaptor<FlutterRenderer.ViewportMetrics> viewportMetricsCaptor,
838+
int left,
839+
int top,
840+
int right,
841+
int bottom) {
842+
assertEquals(left, viewportMetricsCaptor.getValue().viewPaddingLeft);
843+
assertEquals(top, viewportMetricsCaptor.getValue().viewPaddingTop);
844+
assertEquals(right, viewportMetricsCaptor.getValue().viewPaddingRight);
845+
assertEquals(bottom, viewportMetricsCaptor.getValue().viewPaddingBottom);
846+
}
847+
848+
private void mockSystemWindowInsets(
849+
WindowInsets windowInsets, int left, int top, int right, int bottom) {
850+
when(windowInsets.getSystemWindowInsetLeft()).thenReturn(left);
851+
when(windowInsets.getSystemWindowInsetTop()).thenReturn(top);
852+
when(windowInsets.getSystemWindowInsetRight()).thenReturn(right);
853+
when(windowInsets.getSystemWindowInsetBottom()).thenReturn(bottom);
854+
}
855+
856+
private void mockSystemGestureInsetsIfNeed(WindowInsets windowInsets) {
857+
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
858+
when(windowInsets.getSystemGestureInsets()).thenReturn(Insets.NONE);
859+
}
860+
}
861+
916862
/*
917863
* A custom shadow that reports fullscreen flag for system UI visibility
918864
*/

0 commit comments

Comments
 (0)