Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 1144638

Browse files
committed
added rotation calculation tests
# Conflicts: # shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java
1 parent 12ccf50 commit 1144638

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

shell/platform/android/io/flutter/Build.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class Build {
99
/** For use in place of the Android Build.VERSION_CODES class. */
1010
public static class API_LEVELS {
11+
public static final int FLUTTER_MIN = 21;
1112
public static final int API_21 = 21;
1213
public static final int API_22 = 22;
1314
public static final int API_23 = 23;

shell/platform/android/io/flutter/embedding/android/FlutterView.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,26 +597,35 @@ protected void setWindowInfoListenerDisplayFeatures(WindowLayoutInfo layoutInfo)
597597
// android may decide to place the software navigation bars on the side. When the nav
598598
// bar is hidden, the reported insets should be removed to prevent extra useless space
599599
// on the sides.
600-
private enum ZeroSides {
600+
@VisibleForTesting
601+
public enum ZeroSides {
601602
NONE,
602603
LEFT,
603604
RIGHT,
604605
BOTH
605606
}
606607

607-
private ZeroSides calculateShouldZeroSides() {
608+
/**
609+
* This method can be run on APIs 30 and above but its intended use is for 30 and below.
610+
*
611+
* @return some ZeroSides enum
612+
*/
613+
@androidx.annotation.DeprecatedSinceApi(api = API_LEVELS.API_30)
614+
@VisibleForTesting
615+
public ZeroSides calculateShouldZeroSides() {
608616
// We get both orientation and rotation because rotation is all 4
609617
// rotations relative to default rotation while orientation is portrait
610618
// or landscape. By combining both, we can obtain a more precise measure
611619
// of the rotation.
612620
Context context = getContext();
613621
int orientation = context.getResources().getConfiguration().orientation;
614-
int rotation =
615-
((DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE))
616-
.getDisplay(0)
617-
.getRotation();
618622

619623
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
624+
int rotation =
625+
((DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE))
626+
.getDisplay(0)
627+
.getRotation();
628+
620629
if (rotation == Surface.ROTATION_90) {
621630
return ZeroSides.RIGHT;
622631
} else if (rotation == Surface.ROTATION_270) {

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.graphics.Rect;
3030
import android.graphics.Region;
3131
import android.hardware.HardwareBuffer;
32+
import android.hardware.display.DisplayManager;
3233
import android.media.Image;
3334
import android.media.Image.Plane;
3435
import android.media.ImageReader;
@@ -39,13 +40,13 @@
3940
import android.view.Surface;
4041
import android.view.View;
4142
import android.view.WindowInsets;
42-
import android.view.WindowManager;
4343
import android.widget.FrameLayout;
4444
import androidx.core.util.Consumer;
4545
import androidx.test.core.app.ApplicationProvider;
4646
import androidx.test.ext.junit.runners.AndroidJUnit4;
4747
import androidx.window.layout.FoldingFeature;
4848
import androidx.window.layout.WindowLayoutInfo;
49+
import io.flutter.Build.API_LEVELS;
4950
import io.flutter.embedding.engine.FlutterEngine;
5051
import io.flutter.embedding.engine.FlutterJNI;
5152
import io.flutter.embedding.engine.loader.FlutterLoader;
@@ -438,6 +439,48 @@ public void systemInsetHandlesFullscreenNavbarRightBelowSDK23() {
438439
validateViewportMetricPadding(viewportMetricsCaptor, 100, 0, 0, 0);
439440
}
440441

442+
@Test
443+
@Config(minSdk = API_LEVELS.FLUTTER_MIN, maxSdk = API_LEVELS.API_29, qualifiers = "port")
444+
public void calculateShouldZeroSidesInPortrait() {
445+
FlutterView flutterView = spy(new FlutterView(ctx));
446+
assertEquals(FlutterView.ZeroSides.NONE, flutterView.calculateShouldZeroSides());
447+
}
448+
449+
@Test
450+
@Config(minSdk = API_LEVELS.FLUTTER_MIN, maxSdk = API_LEVELS.API_29, qualifiers = "land")
451+
public void calculateShouldZeroSidesInLandscapeNeutralRotation() {
452+
FlutterView flutterView = spy(new FlutterView(ctx));
453+
setExpectedDisplayRotation(Surface.ROTATION_0);
454+
assertEquals(FlutterView.ZeroSides.BOTH, flutterView.calculateShouldZeroSides());
455+
456+
setExpectedDisplayRotation(Surface.ROTATION_180);
457+
assertEquals(FlutterView.ZeroSides.BOTH, flutterView.calculateShouldZeroSides());
458+
}
459+
460+
@Test
461+
@Config(minSdk = API_LEVELS.FLUTTER_MIN, maxSdk = API_LEVELS.API_29, qualifiers = "land")
462+
public void calculateShouldZeroSidesInLandscapeRotation90() {
463+
FlutterView flutterView = spy(new FlutterView(ctx));
464+
setExpectedDisplayRotation(Surface.ROTATION_90);
465+
assertEquals(FlutterView.ZeroSides.RIGHT, flutterView.calculateShouldZeroSides());
466+
}
467+
468+
@Test
469+
@Config(minSdk = API_LEVELS.API_21, maxSdk = API_LEVELS.API_22, qualifiers = "land")
470+
public void calculateShouldZeroSidesInLandscapeRotation270API22() {
471+
FlutterView flutterView = spy(new FlutterView(ctx));
472+
setExpectedDisplayRotation(Surface.ROTATION_270);
473+
assertEquals(FlutterView.ZeroSides.RIGHT, flutterView.calculateShouldZeroSides());
474+
}
475+
476+
@Test
477+
@Config(minSdk = API_LEVELS.API_23, maxSdk = API_LEVELS.API_29, qualifiers = "land")
478+
public void calculateShouldZeroSidesInLandscapeRotation270API23Plus() {
479+
FlutterView flutterView = spy(new FlutterView(ctx));
480+
setExpectedDisplayRotation(Surface.ROTATION_270);
481+
assertEquals(FlutterView.ZeroSides.LEFT, flutterView.calculateShouldZeroSides());
482+
}
483+
441484
@SuppressWarnings("deprecation")
442485
// getSystemUiVisibility, getWindowSystemUiVisibility required to test pre api 30 behavior.
443486
@Test
@@ -615,9 +658,6 @@ public void systemInsetDisplayCutoutSimple() {
615658
public void itRegistersAndUnregistersToWindowManager() {
616659
Context context = Robolectric.setupActivity(Activity.class);
617660
FlutterView flutterView = spy(new FlutterView(context));
618-
ShadowDisplay display =
619-
Shadows.shadowOf(
620-
((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay());
621661
WindowInfoRepositoryCallbackAdapterWrapper windowInfoRepo =
622662
mock(WindowInfoRepositoryCallbackAdapterWrapper.class);
623663
// For reasoning behing using doReturn instead of when, read "Important gotcha" at
@@ -646,9 +686,6 @@ public void itRegistersAndUnregistersToWindowManager() {
646686
public void itSendsHingeDisplayFeatureToFlutter() {
647687
Context context = Robolectric.setupActivity(Activity.class);
648688
FlutterView flutterView = spy(new FlutterView(context));
649-
ShadowDisplay display =
650-
Shadows.shadowOf(
651-
((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay());
652689
when(flutterView.getContext()).thenReturn(context);
653690
WindowInfoRepositoryCallbackAdapterWrapper windowInfoRepo =
654691
mock(WindowInfoRepositoryCallbackAdapterWrapper.class);
@@ -1102,10 +1139,10 @@ public SettingsChannel.MessageBuilder answer(InvocationOnMock invocation)
11021139

11031140
@SuppressWarnings("deprecation")
11041141
private void setExpectedDisplayRotation(int rotation) {
1105-
ShadowDisplay display =
1142+
ShadowDisplay myDisplay =
11061143
Shadows.shadowOf(
1107-
((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay());
1108-
display.setRotation(rotation);
1144+
((DisplayManager) ctx.getSystemService(Context.DISPLAY_SERVICE)).getDisplay(0));
1145+
myDisplay.setRotation(rotation);
11091146
}
11101147

11111148
private void validateViewportMetricPadding(

0 commit comments

Comments
 (0)