File: autofill_renderer_test.h

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 (172 lines) | stat: -rw-r--r-- 6,511 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
172
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_RENDERER_TEST_H_
#define COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_RENDERER_TEST_H_

#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "components/autofill/content/common/mojom/autofill_driver.mojom.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/password_autofill_agent.h"
#include "components/autofill/content/renderer/password_generation_agent.h"
#include "content/public/test/render_view_test.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/self_owned_associated_receiver.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/metrics/document_update_reason.h"
#include "third_party/blink/public/web/web_frame_widget.h"

namespace autofill::test {

class MockAutofillDriver : public mojom::AutofillDriver {
 public:
  MockAutofillDriver();
  ~MockAutofillDriver() override;

  void BindPendingReceiver(mojo::ScopedInterfaceEndpointHandle handle) {
    receivers_.Add(this, mojo::PendingAssociatedReceiver<mojom::AutofillDriver>(
                             std::move(handle)));
  }

  MOCK_METHOD(void,
              FormsSeen,
              (const std::vector<FormData>& updated_forms,
               const std::vector<FormRendererId>& removed_forms),
              (override));
  MOCK_METHOD(void,
              FormSubmitted,
              (const FormData& form,
               mojom::SubmissionSource source),
              (override));
  MOCK_METHOD(void,
              CaretMovedInFormField,
              (const FormData& form,
               FieldRendererId field_id,
               const gfx::Rect& caret_bounds),
              (override));
  MOCK_METHOD(void,
              TextFieldValueChanged,
              (const FormData& form,
               FieldRendererId field_id,
               base::TimeTicks timestamp),
              (override));
  MOCK_METHOD(void,
              TextFieldDidScroll,
              (const FormData& form, FieldRendererId field_id),
              (override));
  MOCK_METHOD(void,
              SelectControlSelectionChanged,
              (const FormData& form, FieldRendererId field_id),
              (override));
  MOCK_METHOD(void,
              SelectFieldOptionsDidChange,
              (const FormData& form),
              (override));
  MOCK_METHOD(void,
              JavaScriptChangedAutofilledValue,
              (const FormData& form,
               FieldRendererId field_id,
               const std::u16string& old_value),
              (override));
  MOCK_METHOD(
      void,
      AskForValuesToFill,
      (const FormData& form,
       FieldRendererId field_id,
       const gfx::Rect& caret_bounds,
       AutofillSuggestionTriggerSource trigger_source,
       const std::optional<PasswordSuggestionRequest>& password_request),
      (override));
  MOCK_METHOD(void, HidePopup, (), (override));
  MOCK_METHOD(void, FocusOnNonFormField, (), (override));
  MOCK_METHOD(void,
              FocusOnFormField,
              (const FormData& form, FieldRendererId field_id),
              (override));
  MOCK_METHOD(void,
              DidFillAutofillFormData,
              (const FormData& form, base::TimeTicks timestamp),
              (override));
  MOCK_METHOD(void, DidEndTextFieldEditing, (), (override));

 private:
  mojo::AssociatedReceiverSet<mojom::AutofillDriver> receivers_;
};

class AutofillRendererTest : public content::RenderViewTest {
 public:
  AutofillRendererTest();
  ~AutofillRendererTest() override;

  // content::RenderViewTest:
  void SetUp() override;
  void TearDown() override;

  virtual std::unique_ptr<AutofillAgent> CreateAutofillAgent(
      content::RenderFrame* render_frame,
      std::unique_ptr<PasswordAutofillAgent> password_autofill_agent,
      std::unique_ptr<PasswordGenerationAgent> password_generation_agent,
      blink::AssociatedInterfaceRegistry* associated_interfaces);

  blink::WebDocument GetDocument() { return GetMainFrame()->GetDocument(); }

  blink::WebElement GetWebElementById(std::string_view id) {
    return GetDocument().GetElementById(blink::WebString::FromUTF8(id));
  }

  blink::WebFormControlElement GetFormControlElementById(std::string_view id) {
    return GetWebElementById(id).DynamicTo<blink::WebFormControlElement>();
  }

  blink::WebInputElement GetInputElementById(std::string_view id) {
    return GetWebElementById(id).DynamicTo<blink::WebInputElement>();
  }

  // Simulates a click on the element with id `element_id` and, if, successful,
  // runs until the task environment is idle. Waits until the `TaskEnvironment`
  // is idle to ensure that the `AutofillDriver` is notified via mojo.
  bool SimulateElementClickAndWait(const std::string& element_id);

  // Simulates focusing an element without clicking it. Waits until the
  // `TaskEnvironment` is idle to ensure that the `AutofillDriver` is notified
  // via mojo.
  void SimulateElementFocusAndWait(std::string_view element_id);

  // Simulates scrolling. Waits until the `TaskEnvironment` is idle to ensure
  // that the `AutofillDriver` is notified via mojo.
  void SimulateScrollingAndWait();

  // AutofillDriver::FormsSeen() is throttled indirectly because some callsites
  // of AutofillAgent::ProcessForms() are throttled. This function blocks until
  // FormsSeen() has happened.
  void WaitForFormsSeen() {
    task_environment_.FastForwardBy(AutofillAgent::kFormsSeenThrottle * 3 / 2);
  }

  // This triggers a layout update to apply JS changes like display = 'none'.
  void ForceLayoutUpdate() {
    GetWebFrameWidget()->UpdateAllLifecyclePhases(
        blink::DocumentUpdateReason::kTest);
  }

 protected:
  AutofillAgent& autofill_agent() { return *autofill_agent_; }
  MockAutofillDriver& autofill_driver() { return autofill_driver_; }

 private:
  ::testing::NiceMock<MockAutofillDriver> autofill_driver_;
  blink::AssociatedInterfaceRegistry associated_interfaces_;
  std::unique_ptr<AutofillAgent> autofill_agent_;
};

}  // namespace autofill::test

#endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_RENDERER_TEST_H_