File: form_interaction_tab_helper_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (189 lines) | stat: -rw-r--r-- 7,289 bytes parent folder | download | duplicates (6)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/tab_contents/form_interaction_tab_helper.h"

#include <memory>
#include <utility>

#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/task/task_traits.h"
#include "base/test/bind.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/performance_manager/test_support/graph_impl.h"
#include "components/performance_manager/test_support/mock_graphs.h"
#include "components/performance_manager/test_support/page_aggregator.h"
#include "components/performance_manager/test_support/test_harness_helper.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"

class FormInteractionTabHelperTest : public ChromeRenderViewHostTestHarness {
 public:
  FormInteractionTabHelperTest() = default;
  ~FormInteractionTabHelperTest() override = default;
  FormInteractionTabHelperTest(const FormInteractionTabHelperTest& other) =
      delete;
  FormInteractionTabHelperTest& operator=(const FormInteractionTabHelperTest&) =
      delete;

  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
    pm_harness_.SetUp();
    performance_manager::testing::CreatePageAggregatorAndPassItToGraph();
    performance_manager::Graph* graph =
        performance_manager::PerformanceManager::GetGraph();
    graph->PassToGraph(FormInteractionTabHelper::CreateGraphObserver());
  }

  std::unique_ptr<content::WebContents> CreateTestWebContents() {
    std::unique_ptr<content::WebContents> contents =
        ChromeRenderViewHostTestHarness::CreateTestWebContents();
    FormInteractionTabHelper::CreateForWebContents(contents.get());
    // Simulate a navigation event to force the initialization of the main
    // frame.
    content::WebContentsTester::For(contents.get())
        ->NavigateAndCommit(GURL("https://foo.com"));
    task_environment()->RunUntilIdle();
    return contents;
  }

  void TearDown() override {
    pm_harness_.TearDown();
    ChromeRenderViewHostTestHarness::TearDown();
  }

  void SetHadFormInteraction(content::RenderFrameHost* rfh) {
    base::WeakPtr<performance_manager::FrameNode> node =
        performance_manager::PerformanceManager::GetFrameNodeForRenderFrameHost(
            rfh);
    auto* frame_node = performance_manager::FrameNodeImpl::FromNode(node.get());
    frame_node->SetHadFormInteraction();
  }

 private:
  performance_manager::PerformanceManagerTestHarnessHelper pm_harness_;
};

TEST_F(FormInteractionTabHelperTest, HadFormInteractionSingleFrame) {
  std::unique_ptr<content::WebContents> contents = CreateTestWebContents();
  auto* helper = FormInteractionTabHelper::FromWebContents(contents.get());

  EXPECT_FALSE(helper->had_form_interaction());
  SetHadFormInteraction(contents->GetPrimaryMainFrame());
  EXPECT_TRUE(helper->had_form_interaction());

  // A navigation event should reset the |had_form_interaction| for this page.
  content::WebContentsTester::For(contents.get())
      ->NavigateAndCommit(GURL("https://bar.com"));
  // Some task are posted to the graph after a navigation event, wait for them
  // to complete.
  task_environment()->RunUntilIdle();

  EXPECT_FALSE(helper->had_form_interaction());
}

enum class ChildFrameType {
  kIFrame,
  kFencedFrame,
};

class FormInteractionTabHelperWithChildTest
    : public FormInteractionTabHelperTest,
      public testing::WithParamInterface<ChildFrameType> {
 public:
  FormInteractionTabHelperWithChildTest() {
    std::vector<base::test::FeatureRefAndParams> enabled;
    enabled.push_back(
        {blink::features::kFencedFrames, {{"implementation_type", "mparch"}}});
    scoped_feature_list_.InitWithFeaturesAndParameters(
        enabled, std::vector<base::test::FeatureRef>());
  }
  ~FormInteractionTabHelperWithChildTest() override = default;

  FormInteractionTabHelperWithChildTest(
      const FormInteractionTabHelperWithChildTest&) = delete;
  FormInteractionTabHelperWithChildTest& operator=(
      const FormInteractionTabHelperWithChildTest&) = delete;

  content::RenderFrameHost* AppendChild(content::WebContents* contents) {
    auto* parent_tester =
        content::RenderFrameHostTester::For(contents->GetPrimaryMainFrame());
    switch (GetParam()) {
      case ChildFrameType::kIFrame:
        return parent_tester->AppendChild("child");

      case ChildFrameType::kFencedFrame:
        return parent_tester->AppendFencedFrame();
    }
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(All,
                         FormInteractionTabHelperWithChildTest,
                         ::testing::Values(ChildFrameType::kIFrame,
                                           ChildFrameType::kFencedFrame));

TEST_P(FormInteractionTabHelperWithChildTest, HadFormInteractionInChildFrame) {
  std::unique_ptr<content::WebContents> contents = CreateTestWebContents();
  auto* helper = FormInteractionTabHelper::FromWebContents(contents.get());

  EXPECT_FALSE(helper->had_form_interaction());

  content::RenderFrameHost* child =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL("https://foochild.com"), AppendChild(contents.get()));

  SetHadFormInteraction(child);
  EXPECT_TRUE(helper->had_form_interaction());

  // A navigation event should reset the |had_form_interaction| for this page.
  content::NavigationSimulator::NavigateAndCommitFromDocument(
      GURL("https://barchild.com"), child);

  // Some task are posted to the graph after a navigation event, wait for them
  // to complete.
  task_environment()->RunUntilIdle();

  EXPECT_FALSE(helper->had_form_interaction());
}

TEST_P(FormInteractionTabHelperWithChildTest,
       HadFormInteractionInBothMainAndChild) {
  std::unique_ptr<content::WebContents> contents = CreateTestWebContents();
  auto* helper = FormInteractionTabHelper::FromWebContents(contents.get());

  EXPECT_FALSE(helper->had_form_interaction());

  SetHadFormInteraction(contents->GetPrimaryMainFrame());
  EXPECT_TRUE(helper->had_form_interaction());

  content::RenderFrameHost* child =
      content::NavigationSimulator::NavigateAndCommitFromDocument(
          GURL("https://foochild.com"), AppendChild(contents.get()));

  SetHadFormInteraction(child);
  EXPECT_TRUE(helper->had_form_interaction());

  // The |had_form_interaction| for this page should be still true even though
  // the navigation happens on the child frame, since the main frame have had an
  // interaction.
  content::NavigationSimulator::NavigateAndCommitFromDocument(
      GURL("https://barchild.com"), child);

  // Some task are posted to the graph after a navigation event, wait for them
  // to complete.
  task_environment()->RunUntilIdle();

  EXPECT_TRUE(helper->had_form_interaction());
}