Avi Drissman | 4d56a5d | 2022-09-07 17:54:53 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.app.Activity; |
| 8 | import android.os.Build; |
| 9 | import android.view.View; |
| 10 | import android.view.WindowManager; |
| 11 | |
Eden Wang | eda6c2ab | 2022-05-23 18:07:48 | [diff] [blame] | 12 | import androidx.annotation.RequiresApi; |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 13 | import androidx.test.filters.SmallTest; |
| 14 | |
| 15 | import org.junit.After; |
| 16 | import org.junit.Assert; |
| 17 | import org.junit.Before; |
| 18 | import org.junit.Rule; |
| 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
Mateusz Zbikowski | fbd46b2 | 2023-11-02 19:23:24 | [diff] [blame] | 21 | import org.junit.runners.Parameterized; |
| 22 | import org.junit.runners.Parameterized.UseParametersRunnerFactory; |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 23 | |
| 24 | import org.chromium.android_webview.AwContents; |
| 25 | import org.chromium.android_webview.AwDisplayCutoutController.Insets; |
| 26 | import org.chromium.android_webview.common.AwFeatures; |
| 27 | import org.chromium.base.test.util.CommandLineFlags; |
| 28 | import org.chromium.base.test.util.MinAndroidSdkLevel; |
| 29 | import org.chromium.content_public.browser.test.util.TestThreadUtils; |
| 30 | import org.chromium.net.test.util.TestWebServer; |
| 31 | |
Andrew Grieve | e0260713 | 2023-10-25 21:39:06 | [diff] [blame] | 32 | /** Tests for DisplayCutout. */ |
Mateusz Zbikowski | fbd46b2 | 2023-11-02 19:23:24 | [diff] [blame] | 33 | @RunWith(Parameterized.class) |
| 34 | @UseParametersRunnerFactory(AwJUnit4ClassRunnerWithParameters.Factory.class) |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 35 | @MinAndroidSdkLevel(Build.VERSION_CODES.P) |
| 36 | @CommandLineFlags.Add({"enable-features=" + AwFeatures.WEBVIEW_DISPLAY_CUTOUT}) |
Eden Wang | eda6c2ab | 2022-05-23 18:07:48 | [diff] [blame] | 37 | @RequiresApi(Build.VERSION_CODES.P) |
Mateusz Zbikowski | fbd46b2 | 2023-11-02 19:23:24 | [diff] [blame] | 38 | public class AwDisplayCutoutTest extends AwParameterizedTest { |
Andrew Grieve | feff2176 | 2023-11-23 20:41:00 | [diff] [blame^] | 39 | private static final String TEST_HTML = |
| 40 | "<html><head><style>\n" |
| 41 | + "body {\n" |
| 42 | + " margin: 0;\n" |
| 43 | + " padding: 0pt 0pt 0pt 0pt;\n" |
| 44 | + "}\n" |
| 45 | + "div {\n" |
| 46 | + " margin: 0;\n" |
| 47 | + " padding: env(safe-area-inset-top) " |
| 48 | + " env(safe-area-inset-right)" |
| 49 | + " env(safe-area-inset-bottom)" |
| 50 | + " env(safe-area-inset-left);\n" |
| 51 | + "}\n" |
| 52 | + "</style></head><body>\n" |
| 53 | + "<div id='text'>" |
| 54 | + "On notched phones, there should be enough padding on the top" |
| 55 | + " to not have this text appear under the statusbar/notch.\n" |
| 56 | + "</div>\n" |
| 57 | + "</body></html>"; |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 58 | |
Andrew Grieve | feff2176 | 2023-11-23 20:41:00 | [diff] [blame^] | 59 | @Rule public AwActivityTestRule mActivityTestRule; |
Mateusz Zbikowski | fbd46b2 | 2023-11-02 19:23:24 | [diff] [blame] | 60 | |
| 61 | public AwDisplayCutoutTest(AwSettingsMutation param) { |
Andrew Grieve | feff2176 | 2023-11-23 20:41:00 | [diff] [blame^] | 62 | mActivityTestRule = |
| 63 | new AwActivityTestRule(param.getMutation()) { |
| 64 | @Override |
| 65 | public boolean needsHideActionBar() { |
| 66 | // If action bar is showing, WebView cannot be fully occupying the screen. |
| 67 | return true; |
| 68 | } |
| 69 | }; |
Mateusz Zbikowski | fbd46b2 | 2023-11-02 19:23:24 | [diff] [blame] | 70 | } |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 71 | |
| 72 | private TestWebServer mWebServer; |
| 73 | private TestAwContentsClient mContentsClient; |
| 74 | private AwTestContainerView mContainerView; |
| 75 | private AwContents mAwContents; |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() throws Exception { |
| 79 | mWebServer = TestWebServer.start(); |
| 80 | |
| 81 | mContentsClient = new TestAwContentsClient(); |
| 82 | mContainerView = mActivityTestRule.createAwTestContainerViewOnMainSync(mContentsClient); |
| 83 | mAwContents = mContainerView.getAwContents(); |
| 84 | AwActivityTestRule.enableJavaScriptOnUiThread(mAwContents); |
| 85 | |
| 86 | // In pre-R, we need to explicitly set this to draw under notch. |
Andrew Grieve | e0260713 | 2023-10-25 21:39:06 | [diff] [blame] | 87 | TestThreadUtils.runOnUiThreadBlocking( |
| 88 | () -> { |
| 89 | Activity activity = mActivityTestRule.getActivity(); |
| 90 | activity.getWindow().getAttributes().layoutInDisplayCutoutMode = |
| 91 | WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; |
| 92 | }); |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @After |
| 96 | public void tearDown() { |
| 97 | mWebServer.shutdown(); |
| 98 | } |
| 99 | |
| 100 | private void setFullscreen(boolean fullscreen) { |
Andrew Grieve | e0260713 | 2023-10-25 21:39:06 | [diff] [blame] | 101 | TestThreadUtils.runOnUiThreadBlocking( |
| 102 | () -> { |
| 103 | Activity activity = mActivityTestRule.getActivity(); |
| 104 | View decor = activity.getWindow().getDecorView(); |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 105 | |
Andrew Grieve | e0260713 | 2023-10-25 21:39:06 | [diff] [blame] | 106 | int systemUiVisibility = decor.getSystemUiVisibility(); |
| 107 | int flags = |
| 108 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
| 109 | | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 110 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
| 111 | | View.SYSTEM_UI_FLAG_FULLSCREEN |
| 112 | | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; |
| 113 | if (fullscreen) { |
| 114 | activity.getWindow() |
| 115 | .setFlags( |
| 116 | WindowManager.LayoutParams.FLAG_FULLSCREEN, |
| 117 | WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 118 | systemUiVisibility |= flags; |
| 119 | } else { |
| 120 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 121 | systemUiVisibility &= flags; |
| 122 | } |
| 123 | decor.setSystemUiVisibility(systemUiVisibility); |
| 124 | }); |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | @SmallTest |
| 129 | public void testNoSafeAreaSet() throws Throwable { |
| 130 | setFullscreen(true); |
| 131 | mActivityTestRule.loadHtmlSync( |
| 132 | mAwContents, mContentsClient.getOnPageFinishedHelper(), TEST_HTML); |
| 133 | // Reset safe area just in case we have a notch. |
| 134 | Insets insets = new Insets(0, 0, 0, 0); |
Andrew Grieve | e0260713 | 2023-10-25 21:39:06 | [diff] [blame] | 135 | TestThreadUtils.runOnUiThreadBlocking( |
| 136 | () -> { |
| 137 | mAwContents.getDisplayCutoutController().onApplyWindowInsetsInternal(insets); |
| 138 | }); |
| 139 | final String code = |
| 140 | "window.getComputedStyle(document.getElementById('text'))" |
| 141 | + ".getPropertyValue('padding-top')"; |
| 142 | Assert.assertEquals( |
| 143 | "\"0px\"", |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 144 | mActivityTestRule.executeJavaScriptAndWaitForResult( |
| 145 | mAwContents, mContentsClient, code)); |
| 146 | } |
| 147 | |
| 148 | @Test |
| 149 | @SmallTest |
| 150 | public void testSafeAreaSet() throws Throwable { |
| 151 | setFullscreen(true); |
| 152 | mActivityTestRule.loadHtmlSync( |
| 153 | mAwContents, mContentsClient.getOnPageFinishedHelper(), TEST_HTML); |
| 154 | Insets insets = new Insets(0, 130, 0, 0); |
Andrew Grieve | e0260713 | 2023-10-25 21:39:06 | [diff] [blame] | 155 | TestThreadUtils.runOnUiThreadBlocking( |
| 156 | () -> { |
| 157 | mAwContents.getDisplayCutoutController().onApplyWindowInsetsInternal(insets); |
| 158 | }); |
| 159 | final String code = |
| 160 | "window.getComputedStyle(document.getElementById('text'))" |
| 161 | + ".getPropertyValue('padding-top')"; |
| 162 | Assert.assertNotEquals( |
| 163 | "\"0px\"", |
Changwan Ryu | 0c79411 | 2020-12-01 00:43:11 | [diff] [blame] | 164 | mActivityTestRule.executeJavaScriptAndWaitForResult( |
| 165 | mAwContents, mContentsClient, code)); |
| 166 | } |
Eden Wang | eda6c2ab | 2022-05-23 18:07:48 | [diff] [blame] | 167 | } |