1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/display_cutout/safe_area_insets_host_impl.h"
#include <optional>
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/features.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_renderer_host.h"
#include "content/test/test_web_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace content {
class TestSafeAreaInsetsHostImpl : public SafeAreaInsetsHostImpl {
public:
explicit TestSafeAreaInsetsHostImpl(WebContentsImpl* web_contents_impl)
: SafeAreaInsetsHostImpl(web_contents_impl) {}
void ResetSafeAreaTracking() {
latest_safe_area_insets_ = std::nullopt;
previous_safe_area_insets_ = std::nullopt;
latest_rfh_ = std::nullopt;
previous_rfh_ = std::nullopt;
}
void ResetSendSafeAreaToFrameCallCount() {
send_safe_area_to_frame_call_count_ = 0;
}
void SetHasSentNonZeroInsets(bool has_sent_non_zero_insets) {
has_sent_non_zero_insets_ = has_sent_non_zero_insets;
}
// Override to allow test access.
void ViewportFitChangedForFrame(RenderFrameHost* rfh,
blink::mojom::ViewportFit value) override {
SafeAreaInsetsHostImpl::ViewportFitChangedForFrame(rfh, value);
}
void ComplexSafeAreaConstraintChangedForFrame(RenderFrameHost* rfh,
bool has_constraint) override {
SafeAreaInsetsHostImpl::ComplexSafeAreaConstraintChangedForFrame(
rfh, has_constraint);
}
using SafeAreaInsetsHostImpl::GetSafeAreaConstraintOrDefault;
using SafeAreaInsetsHostImpl::GetValueOrDefault;
using SafeAreaInsetsHostImpl::SetSafeAreaConstraintValue;
using SafeAreaInsetsHostImpl::SetViewportFitValue;
bool did_send_safe_area() { return latest_safe_area_insets_.has_value(); }
gfx::Insets latest_safe_area_insets() {
return latest_safe_area_insets_.value();
}
gfx::Insets previous_safe_area_insets() {
return previous_safe_area_insets_.value();
}
RenderFrameHost* latest_rfh() { return latest_rfh_.value(); }
RenderFrameHost* previous_rfh() { return previous_rfh_.value(); }
RenderFrameHost* active_rfh() { return active_render_frame_host(); }
int send_safe_area_to_frame_call_count() {
return send_safe_area_to_frame_call_count_;
}
protected:
// Send the safe area insets to a `RenderFrameHost`.
void SendSafeAreaToFrame(RenderFrameHost* rfh, gfx::Insets insets) override {
previous_safe_area_insets_ = latest_safe_area_insets_;
latest_safe_area_insets_ = insets;
previous_rfh_ = latest_rfh_;
latest_rfh_ = rfh;
send_safe_area_to_frame_call_count_++;
SafeAreaInsetsHostImpl::SendSafeAreaToFrame(rfh, insets);
}
private:
std::optional<gfx::Insets> latest_safe_area_insets_;
std::optional<gfx::Insets> previous_safe_area_insets_;
std::optional<RenderFrameHost*> latest_rfh_;
std::optional<RenderFrameHost*> previous_rfh_;
int send_safe_area_to_frame_call_count_ = 0;
};
class SafeAreaInsetsHostImplTest : public RenderViewHostTestHarness {
protected:
void SetUp() override {
RenderViewHostTestHarness::SetUp();
SetContents(CreateTestWebContents());
std::unique_ptr<TestSafeAreaInsetsHostImpl>
test_safe_area_insets_host_impl =
absl::make_unique<TestSafeAreaInsetsHostImpl>(test_web_contents());
test_safe_area_insets_host_ = raw_ptr<TestSafeAreaInsetsHostImpl>(
test_safe_area_insets_host_impl.get());
test_web_contents()->SetSafeAreaInsetsHost(
std::move(test_safe_area_insets_host_impl));
}
TestWebContents* test_web_contents() const {
return static_cast<TestWebContents*>(web_contents());
}
TestSafeAreaInsetsHostImpl* test_safe_area_insets_host() const {
return test_safe_area_insets_host_;
}
void ResetSafeAreaTracking() {
test_safe_area_insets_host()->ResetSafeAreaTracking();
}
void ResetSendSafeAreaToFrameCallCount() {
test_safe_area_insets_host()->ResetSendSafeAreaToFrameCallCount();
}
void SetHasSentNonZeroInsets(bool has_sent_non_zero_insets) {
test_safe_area_insets_host()->SetHasSentNonZeroInsets(
has_sent_non_zero_insets);
}
blink::mojom::ViewportFit GetValueOrDefault() {
return test_safe_area_insets_host()->GetValueOrDefault(main_rfh());
}
bool GetSafeAreaConstraintOrDefault() {
return test_safe_area_insets_host()->GetSafeAreaConstraintOrDefault(
main_rfh());
}
void SetViewportFitValue(blink::mojom::ViewportFit value) {
return test_safe_area_insets_host()->SetViewportFitValue(main_rfh(), value);
}
void SetSafeAreaConstraintValue(bool has_constraint) {
return test_safe_area_insets_host()->SetSafeAreaConstraintValue(
main_rfh(), has_constraint);
}
void NavigateToCover() {
FocusWebContentsOnMainFrame();
NavigateAndCommit(GURL("https://www.viewportFitCover.com"));
test_safe_area_insets_host()->ViewportFitChangedForFrame(
main_rfh(), blink::mojom::ViewportFit::kCover);
// Simulate window insets changing, e.g. java's
// DisplayCutoutController#onSafeAreaChanged notified from InsetObserver.
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets(42));
}
void NavigateToAuto() {
FocusWebContentsOnMainFrame();
NavigateAndCommit(GURL("https://www.viewportFitAuto.com"));
test_safe_area_insets_host()->ViewportFitChangedForFrame(
main_rfh(), blink::mojom::ViewportFit::kAuto);
test_safe_area_insets_host()->ComplexSafeAreaConstraintChangedForFrame(
main_rfh(), false);
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets(0));
}
void NavigateToContain() {
FocusWebContentsOnMainFrame();
NavigateAndCommit(GURL("https://www.viewportFitAuto.com"));
test_safe_area_insets_host()->ViewportFitChangedForFrame(
main_rfh(), blink::mojom::ViewportFit::kContain);
test_safe_area_insets_host()->ComplexSafeAreaConstraintChangedForFrame(
main_rfh(), true);
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets(0));
}
void ExpectAuto() {
EXPECT_EQ(blink::mojom::ViewportFit::kAuto, GetValueOrDefault())
<< "Not in Auto? Expected the main_rfh() to have kAuto in UserData, "
"but it is not!";
}
void ExpectCover() {
EXPECT_EQ(blink::mojom::ViewportFit::kCover, GetValueOrDefault())
<< "Not in Cover? Expected the main_rfh() to have kCover in UserData, "
"but it is not!";
}
private:
raw_ptr<TestSafeAreaInsetsHostImpl> test_safe_area_insets_host_;
};
TEST_F(SafeAreaInsetsHostImplTest, SetDisplayCutoutSafeAreaRedundantInsets) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
/*enabled_features=*/{features::kDrawCutoutEdgeToEdge},
/*disabled_features=*/{});
ResetSafeAreaTracking();
FocusWebContentsOnMainFrame();
NavigateAndCommit(GURL("https://www.viewportFitCover.com"));
test_safe_area_insets_host()->ViewportFitChangedForFrame(
main_rfh(), blink::mojom::ViewportFit::kCover);
ResetSendSafeAreaToFrameCallCount();
// Simulate window insets changing, e.g. java's
// DisplayCutoutController#onSafeAreaChanged notified from InsetObserver.
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets::TLBR(42, 0, 0, 0));
EXPECT_EQ(1,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "New insets should have been sent to the frame.";
ResetSendSafeAreaToFrameCallCount();
test_web_contents()->SetDisplayCutoutSafeArea(
gfx::Insets::TLBR(42, 0, 60, 0));
test_web_contents()->SetDisplayCutoutSafeArea(
gfx::Insets::TLBR(42, 0, 60, 0));
EXPECT_EQ(1,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "Only one new set of insets should have been sent to the frame.";
ResetSendSafeAreaToFrameCallCount();
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets::TLBR(42, 0, 0, 0));
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets::TLBR(42, 0, 0, 0));
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets::TLBR(42, 0, 0, 0));
EXPECT_EQ(1,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "Only one new set of insets should have been sent to the frame.";
}
TEST_F(SafeAreaInsetsHostImplTest, AutoToCover) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
/*enabled_features=*/{features::kDrawCutoutEdgeToEdge},
/*disabled_features=*/{});
// Redundant zero insets are not sent if no non-zero insets have been sent
// previously. Navigate to cover to send non-zero insets such that no future
// inset updates will be skipped.
NavigateToCover();
ResetSafeAreaTracking();
NavigateToAuto();
ExpectAuto();
EXPECT_TRUE(test_safe_area_insets_host()->did_send_safe_area())
<< "The Insets should always be sent when they change.";
EXPECT_EQ(0, test_safe_area_insets_host()->latest_safe_area_insets().top())
<< "No Display Cutout, so the top inset should have been zero";
ResetSafeAreaTracking();
NavigateToCover();
ExpectCover();
EXPECT_TRUE(test_safe_area_insets_host()->did_send_safe_area())
<< "The Insets should always be sent when they change.";
EXPECT_EQ(42, test_safe_area_insets_host()->latest_safe_area_insets().top())
<< "The Display Cutout should have caused a non-zero top inset";
}
TEST_F(SafeAreaInsetsHostImplTest, CoverToAuto) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
/*enabled_features=*/{features::kDrawCutoutEdgeToEdge},
/*disabled_features=*/{});
ResetSafeAreaTracking();
NavigateToCover();
ExpectCover();
EXPECT_TRUE(test_safe_area_insets_host()->did_send_safe_area())
<< "The Insets should always be sent when they change.";
EXPECT_EQ(42, test_safe_area_insets_host()->latest_safe_area_insets().top())
<< "The Display Cutout should have caused a non-zero top inset";
ResetSafeAreaTracking();
NavigateToAuto();
ExpectAuto();
EXPECT_TRUE(test_safe_area_insets_host()->did_send_safe_area())
<< "The Insets should always be sent when they change.";
EXPECT_EQ(0, test_safe_area_insets_host()->latest_safe_area_insets().top())
<< "No Display Cutout, so the top inset should have been zero";
}
TEST_F(SafeAreaInsetsHostImplTest, SetViewportFitValue) {
SetViewportFitValue(blink::mojom::ViewportFit::kAuto);
EXPECT_EQ(blink::mojom::ViewportFit::kAuto, GetValueOrDefault());
SetViewportFitValue(blink::mojom::ViewportFit::kCover);
EXPECT_EQ(blink::mojom::ViewportFit::kCover, GetValueOrDefault());
// Setting to a default value (kAuto) after a non default value may be
// optimized to not store anything, but we do not test that directly.
SetViewportFitValue(blink::mojom::ViewportFit::kAuto);
EXPECT_EQ(blink::mojom::ViewportFit::kAuto, GetValueOrDefault());
}
TEST_F(SafeAreaInsetsHostImplTest, SetHasSafeAreaConstraintValue) {
SetSafeAreaConstraintValue(false);
EXPECT_FALSE(GetSafeAreaConstraintOrDefault());
SetSafeAreaConstraintValue(true);
EXPECT_TRUE(GetSafeAreaConstraintOrDefault());
}
TEST_F(SafeAreaInsetsHostImplTest, GetValueOrDefault_ExpiredRfh) {
base::WeakPtr<RenderFrameHostImpl> null_rfh;
EXPECT_EQ(blink::mojom::ViewportFit::kAuto,
test_safe_area_insets_host()->GetValueOrDefault(null_rfh.get()))
<< "Passing in a null pointer should return kAuto instead of crashing.";
}
TEST_F(SafeAreaInsetsHostImplTest, NavigateNoViewportFitChange) {
// Redundant zero insets are not sent if no non-zero insets have been sent
// previously. Navigate to cover to send non-zero insets such that no future
// inset updates will be skipped.
NavigateToCover();
ResetSafeAreaTracking();
ResetSendSafeAreaToFrameCallCount();
NavigateAndCommit(GURL("https://www.test-site-a.com"));
EXPECT_EQ(1,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "Navigating to a new url without a change in viewport-fit should only "
"trigger one update to safe-area-insets.";
ResetSendSafeAreaToFrameCallCount();
NavigateAndCommit(GURL("https://www.test-site-b.com"));
EXPECT_EQ(1,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "Navigating to a new url without a change in viewport-fit should only "
"trigger one update to safe-area-insets.";
ResetSendSafeAreaToFrameCallCount();
NavigateAndCommit(GURL("https://www.test-site-c.com"));
EXPECT_EQ(1,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "Navigating to a new url without a change in viewport-fit should only "
"trigger one update to safe-area-insets.";
}
TEST_F(SafeAreaInsetsHostImplTest, NavigateOnlyZeroInsets) {
SetHasSentNonZeroInsets(false);
ResetSafeAreaTracking();
ResetSendSafeAreaToFrameCallCount();
NavigateAndCommit(GURL("https://www.test-site-a.com"));
NavigateAndCommit(GURL("https://www.test-site-b.com"));
NavigateAndCommit(GURL("https://www.test-site-c.com"));
test_web_contents()->SetDisplayCutoutSafeArea(gfx::Insets(0));
EXPECT_EQ(0,
test_safe_area_insets_host()->send_safe_area_to_frame_call_count())
<< "If no non-zero insets have been sent, updates sending zero insets "
"should be skipped.";
}
TEST_F(SafeAreaInsetsHostImplTest, ActiveFrameInFullscreen) {
base::test::ScopedFeatureList feature_list;
feature_list.InitWithFeatures(
/*enabled_features=*/{features::kDrawCutoutEdgeToEdge},
/*disabled_features=*/{});
ResetSafeAreaTracking();
NavigateToCover();
ExpectCover();
auto* subframe = static_cast<RenderFrameHostImpl*>(
RenderFrameHostTester::For(main_rfh())->AppendChild("subframe"));
test_safe_area_insets_host()->DidAcquireFullscreen(subframe);
EXPECT_EQ(main_rfh(), test_safe_area_insets_host()->previous_rfh())
<< "The main frame should have been previously updated.";
EXPECT_EQ(gfx::Insets(),
test_safe_area_insets_host()->previous_safe_area_insets())
<< "The main frame should have had its insets cleared.";
EXPECT_EQ(subframe, test_safe_area_insets_host()->active_rfh())
<< "The fullscreen subframe should be the new active frame.";
EXPECT_EQ(subframe, test_safe_area_insets_host()->latest_rfh())
<< "The fullscreen subframe should be the latest frame to have been "
"updated.";
EXPECT_EQ(gfx::Insets(42),
test_safe_area_insets_host()->latest_safe_area_insets())
<< "The Display Cutout should have caused a non-zero top inset";
// Exit fullscreen from sub frame.
ResetSafeAreaTracking();
test_safe_area_insets_host()->DidExitFullscreen();
EXPECT_EQ(subframe, test_safe_area_insets_host()->previous_rfh())
<< "The fullscreen subframe should have been previously updated.";
EXPECT_EQ(gfx::Insets(),
test_safe_area_insets_host()->previous_safe_area_insets())
<< "The fullscreen subframe should have had its insets cleared.";
EXPECT_EQ(main_rfh(), test_safe_area_insets_host()->active_rfh())
<< "The main frame should be the new active frame.";
EXPECT_EQ(main_rfh(), test_safe_area_insets_host()->latest_rfh())
<< "The main frame should be the latest frame to have been updated.";
EXPECT_EQ(gfx::Insets(42),
test_safe_area_insets_host()->latest_safe_area_insets())
<< "The Display Cutout should have caused a non-zero top inset";
}
} // namespace content
|