File: form_tracker_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (171 lines) | stat: -rw-r--r-- 6,491 bytes parent folder | download | duplicates (4)
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
// 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 "components/autofill/content/renderer/form_tracker.h"

#include <optional>

#include "base/test/scoped_feature_list.h"
#include "components/autofill/content/renderer/autofill_agent_test_api.h"
#include "components/autofill/content/renderer/autofill_renderer_test.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/mojom/autofill_types.mojom-shared.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/web/web_form_control_element.h"
#include "third_party/blink/public/web/web_local_frame.h"

namespace autofill {
namespace {

using ::testing::_;

class MockFormTracker : public FormTracker {
 public:
  using FormTracker::FormTracker;
  MOCK_METHOD((void),
              FireFormSubmission,
              (mojom::SubmissionSource, std::optional<blink::WebFormElement>),
              (override));
};

class FormTrackerTest : public test::AutofillRendererTest,
                        public testing::WithParamInterface<int> {
 public:
  FormTrackerTest() {
    EXPECT_LE(GetParam(), 5);
    std::vector<base::test::FeatureRef> features = {
        features::kAutofillUseSubmittedFormInHtmlSubmission,
        features::kAutofillPreferSavedFormAsSubmittedForm,
        features::kAutofillFixFormTracking,
        features::kAutofillReplaceCachedWebElementsByRendererIds,
        features::kAutofillReplaceFormElementObserver};

    std::vector<base::test::FeatureRef> enabled_features(
        features.begin(), features.begin() + GetParam());
    std::vector<base::test::FeatureRef> disabled_features(
        features.begin() + GetParam(), features.end());
    scoped_feature_list_.InitWithFeatures(enabled_features, disabled_features);
  }

  void SetUp() override {
    test::AutofillRendererTest::SetUp();
    auto tracker = std::make_unique<MockFormTracker>(GetMainRenderFrame(),
                                                     autofill_agent());
    tracker->SetUserGestureRequired(FormTracker::UserGestureRequired(true));
    test_api(autofill_agent()).set_form_tracker(std::move(tracker));
  }

  MockFormTracker& form_tracker() {
    return static_cast<MockFormTracker&>(
        test_api(autofill_agent()).form_tracker());
  }

  blink::WebFormControlElement GetFormControlById(const std::string& id) {
    return GetMainFrame()
        ->GetDocument()
        .GetElementById(blink::WebString::FromUTF8(id))
        .DynamicTo<blink::WebFormControlElement>();
  }

 protected:
  base::test::ScopedFeatureList scoped_feature_list_;
};

INSTANTIATE_TEST_SUITE_P(AutofillSubmissionTest,
                         FormTrackerTest,
                         ::testing::Values(0, 1, 2, 3, 4, 5));

// Check that submission is detected on a page with no <form> when in sequence:
// 1) User types into a field.
// 2) Page does an XHR.
// 3) Page hides all of the inputs.
TEST_P(FormTrackerTest, FormlessXHRThenHide) {
  LoadHTML("<!DOCTYPE HTML><input id='input1'><input id='input2'/>");

  blink::WebFormControlElement input1 = GetFormControlById("input1");

  GetMainFrame()->NotifyUserActivation(
      blink::mojom::UserActivationNotificationType::kTest);
  ExecuteJavaScriptForTests("document.getElementById('input1').focus();");
  form_tracker().TextFieldValueChanged(input1);

  task_environment_.RunUntilIdle();

  form_tracker().AjaxSucceeded();
  task_environment_.RunUntilIdle();
  // FormTracker should not think there is a submission because the <input>s are
  // still visible.

  // FormTracker should detect a submission after the <input>s are hidden.
  EXPECT_CALL(form_tracker(),
              FireFormSubmission(mojom::SubmissionSource::XHR_SUCCEEDED, _))
      .Times(1);
  ExecuteJavaScriptForTests(
      R"(document.getElementById('input1').style.display = 'none';
         document.getElementById('input2').style.display = 'none';)");
  ForceLayoutUpdate();
}

// Check that submission is detected on a page with no <form> when in sequence:
// 1) User types into a field.
// 2) Page hides all of the inputs.
// 3) Page does an XHR.
TEST_P(FormTrackerTest, FormlessHideThenXhr) {
  LoadHTML("<!DOCTYPE HTML><input id='input1'><input id='input2'/>");

  blink::WebFormControlElement input1 = GetFormControlById("input1");

  GetMainFrame()->NotifyUserActivation(
      blink::mojom::UserActivationNotificationType::kTest);
  ExecuteJavaScriptForTests("document.getElementById('input1').focus();");
  form_tracker().TextFieldValueChanged(input1);
  task_environment_.RunUntilIdle();

  ExecuteJavaScriptForTests(
      "document.getElementById('input1').style.display = 'none';"
      "document.getElementById('input2').style.display = 'none';");
  ForceLayoutUpdate();
  task_environment_.RunUntilIdle();
  // FormTracker should not think there is a submission because the page has not
  // done any XHRs.

  // FormTracker should detect a submission when the XHR succeeds.
  EXPECT_CALL(form_tracker(),
              FireFormSubmission(mojom::SubmissionSource::XHR_SUCCEEDED, _))
      .Times(1);
  form_tracker().AjaxSucceeded();
  task_environment_.RunUntilIdle();
}

// Check that if a SelectControlSelectionChanged() is called asynchronously
// after a navigation, the event is a dead end.
TEST_P(FormTrackerTest, IgnoreSelectChangeInOldDocument) {
  LoadHTML(R"(<!DOCTYPE HTML>
    <select id=select>
    <option value=0>0</option>
    <option value=1>1</option>
    <option value=2>2</option>
    <option value=3>3</option>
    </select>)");
  GetMainFrame()->NotifyUserActivation(
      blink::mojom::UserActivationNotificationType::kTest);

  EXPECT_CALL(autofill_driver(), SelectControlSelectionChanged);
  ExecuteJavaScriptForTests("document.getElementById('select').value = '1';");
  task_environment_.RunUntilIdle();

  EXPECT_CALL(autofill_driver(), SelectControlSelectionChanged);
  ExecuteJavaScriptForTests("document.getElementById('select').value = '2';");
  task_environment_.RunUntilIdle();

  EXPECT_CALL(autofill_driver(), SelectControlSelectionChanged).Times(0);
  ExecuteJavaScriptForTests("document.getElementById('select').value = '3';");
  LoadHTML(R"(<!DOCTYPE HTML><input>)");  // Turns the event into a no-op.
  task_environment_.RunUntilIdle();
}

}  // namespace
}  // namespace autofill