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
content / browser / display_cutout / safe_area_insets_host_impl_unittest.cc [blame]
// 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() { safe_area_insets_ = std::nullopt; }
void ResetSendSafeAreaToFrameCallCount() {
send_safe_area_to_frame_call_count_ = 0;
}
// Override to allow test access.
void ViewportFitChangedForFrame(RenderFrameHost* rfh,
blink::mojom::ViewportFit value) override {
SafeAreaInsetsHostImpl::ViewportFitChangedForFrame(rfh, value);
}
using SafeAreaInsetsHostImpl::GetValueOrDefault;
using SafeAreaInsetsHostImpl::SetViewportFitValue;
bool did_send_safe_area() { return safe_area_insets_.has_value(); }
gfx::Insets safe_area_insets() { return safe_area_insets_.value(); }
int send_safe_area_to_frame_call_count() {
return send_safe_area_to_frame_call_count_;
}
RenderFrameHost* active_rfh() { return ActiveRenderFrameHost(); }
protected:
// Send the safe area insets to a `RenderFrameHost`.
void SendSafeAreaToFrame(RenderFrameHost* rfh, gfx::Insets insets) override {
safe_area_insets_ = insets;
send_safe_area_to_frame_call_count_++;
SafeAreaInsetsHostImpl::SendSafeAreaToFrame(rfh, insets);
}
private:
std::optional<gfx::Insets> safe_area_insets_;
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();
}
blink::mojom::ViewportFit GetValueOrDefault() {
return test_safe_area_insets_host()->GetValueOrDefault(main_rfh());
}
void SetViewportFitValue(blink::mojom::ViewportFit value) {
return test_safe_area_insets_host()->SetViewportFitValue(main_rfh(), value);
}
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_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=*/{});
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()->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()->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()->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()->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, 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, 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(subframe, test_safe_area_insets_host()->active_rfh());
EXPECT_EQ(42, test_safe_area_insets_host()->safe_area_insets().top())
<< "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(main_rfh(), test_safe_area_insets_host()->active_rfh());
EXPECT_EQ(42, test_safe_area_insets_host()->safe_area_insets().top())
<< "The Display Cutout should have caused a non-zero top inset";
}
} // namespace content