File: frame_test.cc

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (266 lines) | stat: -rw-r--r-- 12,056 bytes parent folder | download | duplicates (2)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_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/runtime_enabled_features_test_helpers.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::CreateWithHTMLBufferForTesting(
            SharedBuffer::Create(), 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);
}

}  // namespace blink