[go: nahoru, domu]

blob: ad08beb9dc2bee83ec1b1efedb2aad905b8dfcf0 [file] [log] [blame]
Avi Drissman4d56a5d2022-09-07 17:54:531// Copyright 2020 The Chromium Authors
Changwan Ryu0c794112020-12-01 00:43:112// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package org.chromium.android_webview.test;
6
7import android.app.Activity;
8import android.os.Build;
9import android.view.View;
10import android.view.WindowManager;
11
Eden Wangeda6c2ab2022-05-23 18:07:4812import androidx.annotation.RequiresApi;
Changwan Ryu0c794112020-12-01 00:43:1113import androidx.test.filters.SmallTest;
14
15import org.junit.After;
16import org.junit.Assert;
17import org.junit.Before;
18import org.junit.Rule;
19import org.junit.Test;
20import org.junit.runner.RunWith;
Mateusz Zbikowskifbd46b22023-11-02 19:23:2421import org.junit.runners.Parameterized;
22import org.junit.runners.Parameterized.UseParametersRunnerFactory;
Changwan Ryu0c794112020-12-01 00:43:1123
24import org.chromium.android_webview.AwContents;
25import org.chromium.android_webview.AwDisplayCutoutController.Insets;
26import org.chromium.android_webview.common.AwFeatures;
27import org.chromium.base.test.util.CommandLineFlags;
28import org.chromium.base.test.util.MinAndroidSdkLevel;
29import org.chromium.content_public.browser.test.util.TestThreadUtils;
30import org.chromium.net.test.util.TestWebServer;
31
Andrew Grievee02607132023-10-25 21:39:0632/** Tests for DisplayCutout. */
Mateusz Zbikowskifbd46b22023-11-02 19:23:2433@RunWith(Parameterized.class)
34@UseParametersRunnerFactory(AwJUnit4ClassRunnerWithParameters.Factory.class)
Changwan Ryu0c794112020-12-01 00:43:1135@MinAndroidSdkLevel(Build.VERSION_CODES.P)
36@CommandLineFlags.Add({"enable-features=" + AwFeatures.WEBVIEW_DISPLAY_CUTOUT})
Eden Wangeda6c2ab2022-05-23 18:07:4837@RequiresApi(Build.VERSION_CODES.P)
Mateusz Zbikowskifbd46b22023-11-02 19:23:2438public class AwDisplayCutoutTest extends AwParameterizedTest {
Andrew Grievefeff21762023-11-23 20:41:0039 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 Ryu0c794112020-12-01 00:43:1158
Andrew Grievefeff21762023-11-23 20:41:0059 @Rule public AwActivityTestRule mActivityTestRule;
Mateusz Zbikowskifbd46b22023-11-02 19:23:2460
61 public AwDisplayCutoutTest(AwSettingsMutation param) {
Andrew Grievefeff21762023-11-23 20:41:0062 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 Zbikowskifbd46b22023-11-02 19:23:2470 }
Changwan Ryu0c794112020-12-01 00:43:1171
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 Grievee02607132023-10-25 21:39:0687 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 Ryu0c794112020-12-01 00:43:1193 }
94
95 @After
96 public void tearDown() {
97 mWebServer.shutdown();
98 }
99
100 private void setFullscreen(boolean fullscreen) {
Andrew Grievee02607132023-10-25 21:39:06101 TestThreadUtils.runOnUiThreadBlocking(
102 () -> {
103 Activity activity = mActivityTestRule.getActivity();
104 View decor = activity.getWindow().getDecorView();
Changwan Ryu0c794112020-12-01 00:43:11105
Andrew Grievee02607132023-10-25 21:39:06106 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 Ryu0c794112020-12-01 00:43:11125 }
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 Grievee02607132023-10-25 21:39:06135 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 Ryu0c794112020-12-01 00:43:11144 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 Grievee02607132023-10-25 21:39:06155 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 Ryu0c794112020-12-01 00:43:11164 mActivityTestRule.executeJavaScriptAndWaitForResult(
165 mAwContents, mContentsClient, code));
166 }
Eden Wangeda6c2ab2022-05-23 18:07:48167}