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
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/frame/frame.h"
#include "base/test/metrics/histogram_tester.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/frame/user_activation_notification_type.mojom-blink.h"
#include "third_party/blink/renderer/core/animation/scroll_timeline.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/frame.h"
#include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/remote_frame.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
namespace blink {
class FrameTest : public PageTestBase {
public:
void SetUp() override {
PageTestBase::SetUp();
Navigate("https://example.com/", false);
ASSERT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
ASSERT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
}
void Navigate(const String& destinationUrl, bool user_activated) {
const KURL& url = KURL(NullURL(), destinationUrl);
auto navigation_params =
WebNavigationParams::CreateWithEmptyHTMLForTesting(url);
if (user_activated)
navigation_params->is_user_activated = true;
GetDocument().GetFrame()->Loader().CommitNavigation(
std::move(navigation_params), nullptr /* extra_data */);
blink::test::RunPendingTasks();
ASSERT_EQ(url.GetString(), GetDocument().Url().GetString());
}
void NavigateSameDomain(const String& page) {
NavigateSameDomain(page, true);
}
void NavigateSameDomain(const String& page, bool user_activated) {
Navigate("https://test.example.com/" + page, user_activated);
}
void NavigateDifferentDomain() { Navigate("https://example.org/", false); }
};
TEST_F(FrameTest, NoGesture) {
// A nullptr LocalFrame* will not set user gesture state.
LocalFrame::NotifyUserActivation(
nullptr, mojom::UserActivationNotificationType::kTest);
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
}
TEST_F(FrameTest, PossiblyExisting) {
// A non-null LocalFrame* will set state, but a subsequent nullptr Document*
// token will not override it.
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(), mojom::UserActivationNotificationType::kTest);
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
LocalFrame::NotifyUserActivation(
nullptr, mojom::UserActivationNotificationType::kTest);
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
}
TEST_F(FrameTest, NavigateDifferentDomain) {
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(), mojom::UserActivationNotificationType::kTest);
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to a different Document. In the main frame, user gesture state
// will get reset. State will not persist since the domain has changed.
NavigateDifferentDomain();
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
}
TEST_F(FrameTest, NavigateSameDomainMultipleTimes) {
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(), mojom::UserActivationNotificationType::kTest);
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to a different Document in the same domain. In the main frame,
// user gesture state will get reset, but persisted state will be true.
NavigateSameDomain("page1");
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_TRUE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to a different Document in the same domain, the persisted
// state will be true.
NavigateSameDomain("page2");
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_TRUE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to the same URL in the same domain, the persisted state
// will be true, but the user gesture state will be reset.
NavigateSameDomain("page2");
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_TRUE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to a different Document in the same domain, the persisted
// state will be true.
NavigateSameDomain("page3");
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_TRUE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
}
TEST_F(FrameTest, NavigateSameDomainDifferentDomain) {
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(), mojom::UserActivationNotificationType::kTest);
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to a different Document in the same domain. In the main frame,
// user gesture state will get reset, but persisted state will be true.
NavigateSameDomain("page1");
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_TRUE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
// Navigate to a different Document in a different domain, the persisted
// state will be reset.
NavigateDifferentDomain();
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
}
TEST_F(FrameTest, NavigateSameDomainNoGesture) {
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
NavigateSameDomain("page1", false);
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
GetDocument().GetFrame()->HadStickyUserActivationBeforeNavigation());
}
TEST_F(FrameTest, UserActivationInterfaceTest) {
// Initially both sticky and transient bits are false.
EXPECT_FALSE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
LocalFrame::HasTransientUserActivation(GetDocument().GetFrame()));
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(), mojom::UserActivationNotificationType::kTest);
// Now both sticky and transient bits are true, hence consumable.
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_TRUE(LocalFrame::HasTransientUserActivation(GetDocument().GetFrame()));
EXPECT_TRUE(
LocalFrame::ConsumeTransientUserActivation(GetDocument().GetFrame()));
// After consumption, only the transient bit resets to false.
EXPECT_TRUE(GetDocument().GetFrame()->HasStickyUserActivation());
EXPECT_FALSE(
LocalFrame::HasTransientUserActivation(GetDocument().GetFrame()));
EXPECT_FALSE(
LocalFrame::ConsumeTransientUserActivation(GetDocument().GetFrame()));
}
TEST_F(FrameTest, UserActivationTriggerHistograms) {
base::HistogramTester histograms;
// Without user activation, all counts are zero.
GetDocument().GetFrame()->HasStickyUserActivation();
LocalFrame::HasTransientUserActivation(GetDocument().GetFrame());
LocalFrame::ConsumeTransientUserActivation(GetDocument().GetFrame());
histograms.ExpectTotalCount("Event.UserActivation.TriggerForConsuming", 0);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForSticky", 0);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 0);
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(), mojom::UserActivationNotificationType::kTest);
// With user activation but without any status-check calls, all counts remain
// zero.
histograms.ExpectTotalCount("Event.UserActivation.TriggerForConsuming", 0);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForSticky", 0);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 0);
// A call to check the sticky state is counted.
GetDocument().GetFrame()->HasStickyUserActivation();
histograms.ExpectBucketCount("Event.UserActivation.TriggerForSticky", 9, 1);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForSticky", 1);
// A call to check the transient state is counted.
LocalFrame::HasTransientUserActivation(GetDocument().GetFrame());
histograms.ExpectBucketCount("Event.UserActivation.TriggerForTransient", 9,
1);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 1);
// A call to consume is counted also as a transient state check.
LocalFrame::ConsumeTransientUserActivation(GetDocument().GetFrame());
histograms.ExpectBucketCount("Event.UserActivation.TriggerForTransient", 9,
2);
histograms.ExpectBucketCount("Event.UserActivation.TriggerForConsuming", 9,
1);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 2);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForConsuming", 1);
// Post-consumption status-checks affect only the sticky count.
GetDocument().GetFrame()->HasStickyUserActivation();
LocalFrame::HasTransientUserActivation(GetDocument().GetFrame());
LocalFrame::ConsumeTransientUserActivation(GetDocument().GetFrame());
histograms.ExpectTotalCount("Event.UserActivation.TriggerForConsuming", 1);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForSticky", 2);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 2);
// After a new user activation of a different trigger-type, status-check calls
// are counted in a different bucket for the transient and consuming cases,
// but in the same old bucket for the sticky case.
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(),
mojom::UserActivationNotificationType::kInteraction);
GetDocument().GetFrame()->HasStickyUserActivation();
LocalFrame::HasTransientUserActivation(GetDocument().GetFrame());
LocalFrame::ConsumeTransientUserActivation(GetDocument().GetFrame());
histograms.ExpectBucketCount("Event.UserActivation.TriggerForConsuming", 1,
1);
histograms.ExpectBucketCount("Event.UserActivation.TriggerForSticky", 9, 3);
histograms.ExpectBucketCount("Event.UserActivation.TriggerForTransient", 1,
2);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForConsuming", 2);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForSticky", 3);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 4);
// After a activation-state-reset plus a new user activation of a different
// trigger-type, the sticky case is counted in the new bucket.
GetDocument().GetFrame()->ClearUserActivation();
LocalFrame::NotifyUserActivation(
GetDocument().GetFrame(),
mojom::UserActivationNotificationType::kInteraction);
GetDocument().GetFrame()->HasStickyUserActivation();
histograms.ExpectBucketCount("Event.UserActivation.TriggerForConsuming", 1,
1);
histograms.ExpectBucketCount("Event.UserActivation.TriggerForSticky", 1, 1);
histograms.ExpectBucketCount("Event.UserActivation.TriggerForTransient", 1,
2);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForConsuming", 2);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForSticky", 4);
histograms.ExpectTotalCount("Event.UserActivation.TriggerForTransient", 4);
}
TEST_F(FrameTest, NavigateClearsScrollSnapshotClients) {
ScrollTimeline::Create(&GetDocument(),
GetDocument().ScrollingElementNoLayout(),
ScrollTimeline::ScrollAxis::kBlock);
EXPECT_EQ(
GetDocument().GetFrame()->GetScrollSnapshotClientsForTesting().size(),
1U);
NavigateSameDomain("page1");
EXPECT_EQ(
GetDocument().GetFrame()->GetScrollSnapshotClientsForTesting().size(),
0U);
}
class FrameDetachTest : public ::testing::Test {
public:
Frame* MainFrame() {
return web_view_helper_.GetWebView()->GetPage()->MainFrame();
}
frame_test_helpers::WebViewHelper& WebViewHelper() {
return web_view_helper_;
}
private:
test::TaskEnvironment task_environment_;
frame_test_helpers::WebViewHelper web_view_helper_;
};
// Verify that a local frame cannot be looked up by token after detach.
TEST_F(FrameDetachTest, Local) {
WebViewHelper().Initialize();
// Keep a reference to the main frame on the stack to keep the GC from
// collecting it.
auto* frame = DynamicTo<LocalFrame>(MainFrame());
ASSERT_TRUE(frame);
WebViewHelper().Reset();
EXPECT_EQ(nullptr, Frame::ResolveFrame(frame->GetFrameToken()));
EXPECT_EQ(nullptr, LocalFrame::FromFrameToken(frame->GetLocalFrameToken()));
}
// Verify that a remote frame cannot be looked up by token after detach.
TEST_F(FrameDetachTest, Remote) {
WebViewHelper().InitializeRemote();
// Keep a reference to the main frame on the stack to keep the GC from
// collecting it.
auto* frame = DynamicTo<RemoteFrame>(MainFrame());
ASSERT_TRUE(frame);
WebViewHelper().Reset();
EXPECT_EQ(nullptr, Frame::ResolveFrame(frame->GetFrameToken()));
EXPECT_EQ(nullptr, RemoteFrame::FromFrameToken(frame->GetRemoteFrameToken()));
}
} // namespace blink
|