• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.window;
18 
19 import static android.view.WindowInsets.Type.displayCutout;
20 import static android.view.WindowInsets.Type.navigationBars;
21 
22 import static org.junit.Assert.assertEquals;
23 
24 import android.app.Activity;
25 import android.graphics.Point;
26 import android.graphics.Rect;
27 import android.platform.test.annotations.Presubmit;
28 import android.platform.test.annotations.RequiresFlagsDisabled;
29 import android.platform.test.flag.junit.CheckFlagsRule;
30 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
31 import android.view.WindowInsets;
32 import android.view.WindowMetrics;
33 
34 import androidx.test.ext.junit.runners.AndroidJUnit4;
35 import androidx.test.filters.SmallTest;
36 import androidx.test.rule.ActivityTestRule;
37 
38 import com.android.window.flags.Flags;
39 
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 
44 /**
45  * Tests for {@link WindowMetricsHelper}
46  *
47  * <p>Build/Install/Run:
48  *  atest FrameworksCoreTests:WindowMetricsHelperTest
49  *
50  * <p>This test class is a part of Window Manager Service tests and specified in
51  * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}.
52  */
53 @RunWith(AndroidJUnit4.class)
54 @SmallTest
55 @Presubmit
56 public class WindowMetricsHelperTest {
57 
58     @Rule
59     public ActivityTestRule<TestActivity> mActivityRule =
60             new ActivityTestRule<>(TestActivity.class);
61     @Rule
62     public final CheckFlagsRule mCheckFlagsRule =
63             DeviceFlagsValueProvider.createCheckFlagsRule();
64 
65     @Test
66     @RequiresFlagsDisabled(Flags.FLAG_INSETS_DECOUPLED_CONFIGURATION)
testGetBoundsExcludingNavigationBarAndCutoutMatchesDisplayGetSize()67     public void testGetBoundsExcludingNavigationBarAndCutoutMatchesDisplayGetSize()
68             throws Throwable {
69         mActivityRule.runOnUiThread(() -> {
70             Activity activity = mActivityRule.getActivity();
71             final WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
72             final Rect boundsExcludingNavBarAndCutout = WindowMetricsHelper
73                     .getBoundsExcludingNavigationBarAndCutout(metrics);
74 
75             final Point expectedSize = new Point();
76             activity.getDisplay().getSize(expectedSize);
77 
78             assertEquals(expectedSize.x, boundsExcludingNavBarAndCutout.width());
79             assertEquals(expectedSize.y, boundsExcludingNavBarAndCutout.height());
80         });
81     }
82 
83     @Test
testGetBoundsExcludingNavigationBarAndCutout()84     public void testGetBoundsExcludingNavigationBarAndCutout()
85             throws Throwable {
86         mActivityRule.runOnUiThread(() -> {
87             Activity activity = mActivityRule.getActivity();
88             final WindowMetrics metrics = activity.getWindowManager().getCurrentWindowMetrics();
89             final Rect boundsExcludingNavBarAndCutout = WindowMetricsHelper
90                     .getBoundsExcludingNavigationBarAndCutout(metrics);
91 
92             final WindowInsets windowInsets = metrics.getWindowInsets();
93             final Rect expectedBounds = new Rect(metrics.getBounds());
94             expectedBounds.inset(windowInsets.getInsetsIgnoringVisibility(
95                     navigationBars() | displayCutout()));
96 
97             assertEquals(expectedBounds.width(), boundsExcludingNavBarAndCutout.width());
98             assertEquals(expectedBounds.height(), boundsExcludingNavBarAndCutout.height());
99         });
100     }
101 
102     public static class TestActivity extends Activity { }
103 }
104