File: autofill_uitest.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 (332 lines) | stat: -rw-r--r-- 13,012 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
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
328
329
330
331
332
// Copyright 2018 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/autofill/autofill_uitest.h"

#include <string>

#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/autofill/autofill_uitest_util.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager.h"
#include "components/autofill/core/browser/data_manager/personal_data_manager_observer.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager_test_api.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "ui/events/base_event_utils.h"
#include "ui/views/widget/widget.h"

namespace autofill {

std::ostream& operator<<(std::ostream& os, ObservedUiEvents event) {
  switch (event) {
    case ObservedUiEvents::kPreviewFormData:
      return os << "kPreviewFormData";
    case ObservedUiEvents::kFormDataFilled:
      return os << "kFormDataFilled";
    case ObservedUiEvents::kSuggestionsShown:
      return os << "kSuggestionsShown";
    case ObservedUiEvents::kSuggestionsHidden:
      return os << "kSuggestionsHidden";
    case ObservedUiEvents::kNoEvent:
      return os << "kNoEvent";
    default:
      return os << "<OutOfRange>";
  }
}

// Keep in sync with BoundsOverlapWithAnyOpenPrompt() from
// autofill_popup_view_utils.cc.
void TryToCloseAllPrompts(content::WebContents* web_contents) {
  gfx::NativeView top_level_view =
      platform_util::GetViewForWindow(web_contents->GetTopLevelNativeWindow());
  DCHECK(top_level_view);

  // On Aura-based systems, prompts are siblings to the top level native window,
  // and hence we need to go one level up to start searching from the root
  // window.
  top_level_view = platform_util::GetParent(top_level_view)
                       ? platform_util::GetParent(top_level_view)
                       : top_level_view;
  views::Widget::Widgets all_widgets =
      views::Widget::GetAllChildWidgets(top_level_view);
  for (views::Widget* w : all_widgets) {
    if (w->IsDialogBox())
      w->CloseWithReason(views::Widget::ClosedReason::kUnspecified);
  }
}

// BrowserAutofillManagerTestDelegateImpl
// --------------------------------------------
BrowserAutofillManagerTestDelegateImpl::
    BrowserAutofillManagerTestDelegateImpl() = default;

BrowserAutofillManagerTestDelegateImpl::
    ~BrowserAutofillManagerTestDelegateImpl() = default;

void BrowserAutofillManagerTestDelegateImpl::SetIgnoreBackToBackMessages(
    ObservedUiEvents type,
    bool ignore) {
  if (ignore) {
    ignore_back_to_back_event_types_.insert(type);
  } else {
    ignore_back_to_back_event_types_.erase(type);
    if (last_event_ == type)
      last_event_ = ObservedUiEvents::kNoEvent;
  }
}

void BrowserAutofillManagerTestDelegateImpl::FireEvent(ObservedUiEvents event) {
  if (event_waiter_ && (!ignore_back_to_back_event_types_.contains(event) ||
                        last_event_ != event)) {
    event_waiter_->OnEvent(event);
  }
  last_event_ = event;
}

void BrowserAutofillManagerTestDelegateImpl::DidPreviewFormData() {
  FireEvent(ObservedUiEvents::kPreviewFormData);
}

void BrowserAutofillManagerTestDelegateImpl::DidFillFormData() {
  FireEvent(ObservedUiEvents::kFormDataFilled);
}

void BrowserAutofillManagerTestDelegateImpl::DidShowSuggestions() {
  FireEvent(ObservedUiEvents::kSuggestionsShown);
}

void BrowserAutofillManagerTestDelegateImpl::DidHideSuggestions() {
  FireEvent(ObservedUiEvents::kSuggestionsHidden);
}

void BrowserAutofillManagerTestDelegateImpl::SetExpectations(
    std::list<ObservedUiEvents> expected_events,
    base::TimeDelta timeout,
    base::Location location) {
  event_waiter_ = std::make_unique<EventWaiter<ObservedUiEvents>>(
      expected_events, timeout, location);
}

testing::AssertionResult BrowserAutofillManagerTestDelegateImpl::Wait() {
  return event_waiter_->Wait();
}

// AutofillUiTest ----------------------------------------------------
AutofillUiTest::AutofillUiTest(
    const test::AutofillTestEnvironment::Options& options)
    : autofill_test_environment_(options) {}

AutofillUiTest::~AutofillUiTest() = default;

void AutofillUiTest::SetUpOnMainThread() {
  auto* client =
      ChromeAutofillClient::FromWebContentsForTesting(GetWebContents());

  // Make autofill popup stay open by ignoring external changes when possible.
  client->SetKeepPopupOpenForTesting(true);

  // Inject the test delegate into the BrowserAutofillManager of the main frame.
  RenderFrameHostChanged(
      /*old_host=*/nullptr,
      /*new_host=*/GetWebContents()->GetPrimaryMainFrame());
  Observe(GetWebContents());

  // Refills normally only happen if the form changes within 1 second of the
  // initial fill. On a slow bot, this may lead to flakiness. We hence set a
  // very high limit.
  test_api(test_api(*GetBrowserAutofillManager()).form_filler())
      .set_limit_before_refill(base::Hours(1));
  autofill_driver_factory_observation_.Observe(
      &client->GetAutofillDriverFactory());

  // Wait for Personal Data Manager to be fully loaded to prevent that
  // spurious notifications deceive the tests.
  WaitForPersonalDataManagerToBeLoaded(browser()->profile());

  disable_animation_ = std::make_unique<ui::ScopedAnimationDurationScaleMode>(
      ui::ScopedAnimationDurationScaleMode::ZERO_DURATION);

  // If the mouse happened to be over where the suggestions are shown, then
  // the preview will show up and will fail the tests. We need to give it a
  // point that's within the browser frame, or else the method hangs.
  gfx::Point reset_mouse(GetWebContents()->GetContainerBounds().origin());
  reset_mouse = gfx::Point(reset_mouse.x() + 5, reset_mouse.y() + 5);
  ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(reset_mouse));
}

void AutofillUiTest::TearDownOnMainThread() {
  // Make sure to close any showing popups prior to tearing down the UI.
  BrowserAutofillManager* autofill_manager = GetBrowserAutofillManager();
  if (autofill_manager)
    autofill_manager->client().HideAutofillSuggestions(
        SuggestionHidingReason::kTabGone);
  current_main_rfh_ = nullptr;
  InProcessBrowserTest::TearDownOnMainThread();
}

testing::AssertionResult AutofillUiTest::SendKeyToPageAndWait(
    ui::DomKey key,
    std::list<ObservedUiEvents> expected_events,
    base::TimeDelta timeout,
    base::Location location) {
  ui::KeyboardCode key_code = ui::NonPrintableDomKeyToKeyboardCode(key);
  ui::DomCode code = ui::UsLayoutKeyboardCodeToDomCode(key_code);
  return SendKeyToPageAndWait(key, code, key_code, std::move(expected_events),
                              timeout, location);
}

testing::AssertionResult AutofillUiTest::SendKeyToPageAndWait(
    ui::DomKey key,
    ui::DomCode code,
    ui::KeyboardCode key_code,
    std::list<ObservedUiEvents> expected_events,
    base::TimeDelta timeout,
    base::Location location) {
  test_delegate()->SetExpectations(std::move(expected_events), timeout,
                                   location);
  content::SimulateKeyPress(GetWebContents(), key, code, key_code, false, false,
                            false, false);
  return test_delegate()->Wait();
}

void AutofillUiTest::SendKeyToPopup(content::RenderFrameHost* render_frame_host,
                                    const ui::DomKey key) {
  ui::KeyboardCode key_code = ui::NonPrintableDomKeyToKeyboardCode(key);
  ui::DomCode code = ui::UsLayoutKeyboardCodeToDomCode(key_code);
  content::RenderWidgetHost* widget =
      render_frame_host->GetView()->GetRenderWidgetHost();

  // Route popup-targeted key presses via the render view host.
  input::NativeWebKeyboardEvent event(
      blink::WebKeyboardEvent::Type::kRawKeyDown,
      blink::WebInputEvent::kNoModifiers, ui::EventTimeForNow());
  event.windows_key_code = key_code;
  event.dom_code = static_cast<int>(code);
  event.dom_key = key;
  // Install the key press event sink to ensure that any events that are not
  // handled by the installed callbacks do not end up crashing the test.
  widget->AddKeyPressEventCallback(key_press_event_sink_);
  widget->ForwardKeyboardEvent(event);
  widget->RemoveKeyPressEventCallback(key_press_event_sink_);
}

testing::AssertionResult AutofillUiTest::SendKeyToPopupAndWait(
    ui::DomKey key,
    std::list<ObservedUiEvents> expected_events,
    content::RenderWidgetHost* widget,
    base::TimeDelta timeout,
    base::Location location) {
  ui::KeyboardCode key_code = ui::NonPrintableDomKeyToKeyboardCode(key);
  ui::DomCode code = ui::UsLayoutKeyboardCodeToDomCode(key_code);
  return SendKeyToPopupAndWait(
      key, code, key_code, std::move(expected_events),
      widget ? widget : GetRenderViewHost()->GetWidget(), timeout, location);
}

testing::AssertionResult AutofillUiTest::SendKeyToPopupAndWait(
    ui::DomKey key,
    ui::DomCode code,
    ui::KeyboardCode key_code,
    std::list<ObservedUiEvents> expected_events,
    content::RenderWidgetHost* widget,
    base::TimeDelta timeout,
    base::Location location) {
  // Route popup-targeted key presses via the render view host.
  input::NativeWebKeyboardEvent event(
      blink::WebKeyboardEvent::Type::kRawKeyDown,
      blink::WebInputEvent::kNoModifiers, ui::EventTimeForNow());
  event.windows_key_code = key_code;
  event.dom_code = static_cast<int>(code);
  event.dom_key = key;
  test_delegate()->SetExpectations(std::move(expected_events), timeout,
                                   location);
  // Install the key press event sink to ensure that any events that are not
  // handled by the installed callbacks do not end up crashing the test.
  widget->AddKeyPressEventCallback(key_press_event_sink_);
  widget->ForwardKeyboardEvent(event);
  testing::AssertionResult result = test_delegate()->Wait();
  widget->RemoveKeyPressEventCallback(key_press_event_sink_);
  return result;
}

void AutofillUiTest::DoNothingAndWait(base::TimeDelta timeout,
                                      base::Location location) {
  test_delegate()->SetExpectations({ObservedUiEvents::kNoEvent}, timeout,
                                   location);
  ASSERT_FALSE(test_delegate()->Wait());
}

void AutofillUiTest::DoNothingAndWaitAndIgnoreEvents(base::TimeDelta timeout) {
  base::RunLoop run_loop;
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, run_loop.QuitClosure(), timeout);
  run_loop.Run();
}

bool AutofillUiTest::HandleKeyPressEvent(
    const input::NativeWebKeyboardEvent& event) {
  return true;
}

content::WebContents* AutofillUiTest::GetWebContents() {
  return browser()->tab_strip_model()->GetActiveWebContents();
}

content::RenderViewHost* AutofillUiTest::GetRenderViewHost() {
  return GetWebContents()->GetPrimaryMainFrame()->GetRenderViewHost();
}

BrowserAutofillManager* AutofillUiTest::GetBrowserAutofillManager() {
  ContentAutofillDriver* driver =
      ContentAutofillDriver::GetForRenderFrameHost(current_main_rfh_);
  // ContentAutofillDriver will be null if the current RenderFrameHost
  // is not owned by the current WebContents. This state appears to occur
  // when there is a web page popup during teardown
  if (!driver)
    return nullptr;
  return static_cast<BrowserAutofillManager*>(&driver->GetAutofillManager());
}

void AutofillUiTest::RenderFrameHostChanged(
    content::RenderFrameHost* old_frame,
    content::RenderFrameHost* new_frame) {
  if (current_main_rfh_ != old_frame)
    return;
  current_main_rfh_ = new_frame;
  if (BrowserAutofillManager* autofill_manager = GetBrowserAutofillManager()) {
    test_delegate()->Observe(*autofill_manager);
  }
}

void AutofillUiTest::OnContentAutofillDriverFactoryDestroyed(
    ContentAutofillDriverFactory& factory) {
  autofill_driver_factory_observation_.Reset();
}

void AutofillUiTest::OnContentAutofillDriverCreated(
    ContentAutofillDriverFactory& factory,
    ContentAutofillDriver& driver) {
  // Refills normally only happen if the form changes within 1 second of the
  // initial fill. On a slow bot, this may lead to flakiness. We hence set a
  // very high limit.
  test_api(test_api(static_cast<BrowserAutofillManager&>(
                        driver.GetAutofillManager()))
               .form_filler())
      .set_limit_before_refill(base::Hours(1));
}

}  // namespace autofill