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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/feature_list.h"
#include "base/test/scoped_feature_list.h"
#include "components/autofill/content/renderer/autofill_agent.h"
#include "components/autofill/content/renderer/autofill_renderer_test.h"
#include "components/autofill/content/renderer/form_autofill_util.h"
#include "components/autofill/core/common/aliases.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/form_field_data.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_frame_widget.h"
#include "third_party/blink/public/web/web_input_element.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_settings.h"
#include "third_party/blink/public/web/web_view.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/geometry/rect.h"
namespace autofill {
namespace {
using ::testing::_;
using ::testing::AllOf;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Field;
using ::testing::InSequence;
using ::testing::MockFunction;
using ::testing::Not;
using ::testing::Property;
// Returns a matcher that matches a `FormFieldData::form_control_type`.
auto HasType(FormControlType type) {
return Property("FormFieldData::form_control_type",
&FormFieldData::form_control_type, type);
}
auto IsContentEditable() {
return HasType(FormControlType::kContentEditable);
}
template <typename... Args>
auto FieldsAre(Args&&... matchers) {
return Property("FormData::fields", &FormData::fields,
ElementsAre(std::forward<Args>(matchers)...));
}
// TODO(crbug.com/40286775): Clean up these functions once
// `kAutofillAndroidDisableSuggestionsOnJSFocus` is launched and Android and
// Desktop behave identically.
// Returns the expected number of calls to AskForValuesToFill when focusing a
// text field without left clicking or tapping it.
int NumCallsToAskForValuesToFillOnTextfieldFocusWithoutLeftClick() {
if constexpr (BUILDFLAG(IS_ANDROID)) {
return base::FeatureList::IsEnabled(
features::kAutofillAndroidDisableSuggestionsOnJSFocus)
? 0
// Called by `AutofillAgent::FocusElementChanged`.
: 1;
}
return 0;
}
// Returns the expected number of calls to HidePopup when unfocusing a field.
int NumCallsToHidePopupOnFocusLoss() {
if constexpr (BUILDFLAG(IS_ANDROID)) {
return 0; // The accessory allows to fill on focus loss.
}
return 1; // Any dropdown should disappear on focus loss.
}
class AutofillAgentFormInteractionTest : public test::AutofillRendererTest {
public:
void SetUp() override {
// TODO(crbug.com/41268731): parameterize tests over AutofillAgent::Config.
test::AutofillRendererTest::SetUp();
web_view_->SetDefaultPageScaleLimits(1, 4);
LoadHTML(
"<form>"
" <input type='text' id='text'></input><br>"
" <input type='text' id='text_disabled' disabled></input><br>"
" <input type='text' id='text_readonly' readonly></input><br>"
" <textarea id='textarea'></textarea><br>"
" <textarea id='textarea_disabled' disabled></textarea><br>"
" <textarea id='textarea_readonly' readonly></textarea><br>"
" <input type='button' id='button'></input><br>"
"</form>");
GetWebFrameWidget()->Resize(gfx::Size(500, 500));
GetWebFrameWidget()->SetFocus(true);
// Enable show-ime event when element is focused by indicating that a user
// gesture has been processed since load.
EXPECT_TRUE(SimulateElementClick("button"));
}
FieldRendererId GetFieldRendererIdById(std::string_view id) {
return form_util::GetFieldRendererId(
GetMainFrame()->GetDocument().GetElementById(
blink::WebString::FromUTF8(id)));
}
// Makes sure the next AskForValuesToFill() event is not throttled in
// AutofillAgent.
void SkipThrottle() { task_environment_.FastForwardBy(base::Seconds(1)); }
};
// Tests that (repeatedly) clicking a text input field calls AskForValuesToFill
// each time, but clicking a button does not.
TEST_F(AutofillAgentFormInteractionTest, TextInputLeftClick) {
MockFunction<void(int)> check;
{
InSequence s;
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("text"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
EXPECT_CALL(check, Call(1));
// The second click triggers no call because it's throttled.
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
EXPECT_CALL(check, Call(2));
// The third click only triggers a single call, regardless of OS.
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("text"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
EXPECT_CALL(check, Call(3));
}
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("text"));
check.Call(1);
EXPECT_TRUE(SimulateElementClickAndWait("text"));
check.Call(2);
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("text"));
task_environment_.RunUntilIdle(); // nocheck
check.Call(3);
// No notification should be sent on clicking the button.
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("button"));
}
// Tests that a right click does not trigger AskForValuesToFill on Desktop, but
// does on Android.
TEST_F(AutofillAgentFormInteractionTest, TextInputRightClick) {
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("text"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)))
.Times(NumCallsToAskForValuesToFillOnTextfieldFocusWithoutLeftClick());
EXPECT_TRUE(SimulateElementRightClick("text"));
}
// Tests that focusing the text field without a click does not call
// AskForValuesToFill on Desktop, but does on Android. A later click interaction
// triggers it on both.
TEST_F(AutofillAgentFormInteractionTest, TextInputFocusAndLeftClick) {
MockFunction<void(int)> check;
{
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("text"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)))
.Times(NumCallsToAskForValuesToFillOnTextfieldFocusWithoutLeftClick());
InSequence s;
EXPECT_CALL(check, Call(1));
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("text"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
EXPECT_CALL(check, Call(2));
}
SkipThrottle();
SimulateElementFocusAndWait("text");
check.Call(1);
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("text"));
check.Call(2);
}
// Tests that (repeated) left clicks on a textarea trigger AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, TextAreaLeftClick) {
MockFunction<void(int)> check;
{
InSequence s;
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("textarea"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
EXPECT_CALL(check, Call(1));
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("textarea"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
EXPECT_CALL(check, Call(2));
}
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("textarea"));
check.Call(1);
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("textarea"));
check.Call(2);
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("button"));
}
// Tests that focusing a textarea without a click calls AskForValuesToFill with
// trigger source `kTextareaFocusedWithoutClick` on Desktop.
TEST_F(AutofillAgentFormInteractionTest, TextareaFocusAndLeftClick) {
base::test::ScopedFeatureList feature_list{
features::kAutofillAndroidDisableSuggestionsOnJSFocus};
MockFunction<void(int)> check;
{
InSequence s;
using enum AutofillSuggestionTriggerSource;
if constexpr (!BUILDFLAG(IS_ANDROID)) {
EXPECT_CALL(
autofill_driver(),
AskForValuesToFill(_, GetFieldRendererIdById("textarea"), _,
kTextareaFocusedWithoutClick, Eq(std::nullopt)));
} else {
EXPECT_CALL(
autofill_driver(),
AskForValuesToFill(_, GetFieldRendererIdById("textarea"), _,
kFormControlElementClicked, Eq(std::nullopt)))
.Times(
NumCallsToAskForValuesToFillOnTextfieldFocusWithoutLeftClick());
}
EXPECT_CALL(check, Call(1));
EXPECT_CALL(
autofill_driver(),
AskForValuesToFill(_, GetFieldRendererIdById("textarea"), _,
kFormControlElementClicked, Eq(std::nullopt)));
EXPECT_CALL(check, Call(2));
}
SkipThrottle();
SimulateElementFocusAndWait("textarea");
check.Call(1);
SkipThrottle();
EXPECT_TRUE(SimulateElementClickAndWait("textarea"));
check.Call(2);
}
// Tests that left clicking a scaled text area triggers AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, ScaledTextareaLeftClick) {
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("textarea"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
web_view_->SetPageScaleFactor(3);
web_view_->SetVisualViewportOffset(gfx::PointF(50, 50));
SimulatePointClick(GetElementBounds("textarea").CenterPoint());
}
// Tests that tapping a scaled text area triggers AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, ScaledTextareaTapped) {
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("textarea"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
web_view_->SetPageScaleFactor(3);
web_view_->SetVisualViewportOffset(gfx::PointF(50, 50));
gfx::Point center = GetElementBounds("textarea").CenterPoint();
SimulateRectTap(gfx::Rect(center, gfx::Size(30, 30)));
task_environment_.RunUntilIdle(); // nocheck
}
// Tests that left clicking a disabled input field does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, DisabledInputLeftClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
EXPECT_TRUE(SimulateElementClickAndWait("text_disabled"));
}
// Tests that focusing a disabled input field does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, DisabledInputFocusWithoutClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
SimulateElementFocusAndWait("text_disabled");
}
// Tests that left clicking a disabled textarea does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, DisabledTextareaLeftClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
EXPECT_TRUE(SimulateElementClickAndWait("textarea_disabled"));
}
// Tests that focusing a disabled textarea without click does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, DisabledTextareaFocusWithoutClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
SimulateElementFocusAndWait("textarea_disabled");
}
// Tests that left clicking a readonly input field does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, ReadonlyInputLeftClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
EXPECT_TRUE(SimulateElementClickAndWait("text_readonly"));
}
// Tests that focusing a readonly input field does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, ReadonlyInputFocusWithoutClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
SimulateElementFocusAndWait("text_readonly");
}
// Tests that left clicking a readonly textarea does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, ReadonlyTextareaLeftClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
EXPECT_TRUE(SimulateElementClickAndWait("textarea_readonly"));
}
// Tests that focusing a readonly textarea without click does not trigger
// AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, ReadonlyTextareaFocusWithoutClick) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
SimulateElementFocusAndWait("textarea_readonly");
}
// Tests that a tap near the edge of an input field trigger AskForValuesToFill.
TEST_F(AutofillAgentFormInteractionTest, TapNearEdge) {
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(
_, GetFieldRendererIdById("text"), _,
AutofillSuggestionTriggerSource::kFormControlElementClicked,
Eq(std::nullopt)));
gfx::Rect element_bounds = GetElementBounds("text");
SimulateRectTap(element_bounds -
gfx::Vector2d(element_bounds.width() / 2 + 1, 0));
}
using AutofillAgentContentEditableInteractionTest = test::AutofillRendererTest;
// Tests that left clicking on an contenteditable triggers AskForValuesToFill.
TEST_F(AutofillAgentContentEditableInteractionTest, LeftClick) {
EXPECT_CALL(
autofill_driver(),
AskForValuesToFill(
FieldsAre(IsContentEditable()), _, _,
mojom::AutofillSuggestionTriggerSource::kContentEditableClicked,
Eq(std::nullopt)));
LoadHTML("<body><div id=ce contenteditable></body>");
WaitForFormsSeen();
SimulateElementClickAndWait("ce");
}
// Tests that unfocusing a contenteditable triggers a call to
// `AutofillDriver::HidePopup()`.
TEST_F(AutofillAgentContentEditableInteractionTest,
LossOfFocusOfContentEditableTriggersHideAutofillSuggestions) {
MockFunction<void()> check;
{
InSequence s;
EXPECT_CALL(
autofill_driver(),
AskForValuesToFill(
FieldsAre(IsContentEditable()), _, _,
mojom::AutofillSuggestionTriggerSource::kContentEditableClicked,
Eq(std::nullopt)));
EXPECT_CALL(check, Call);
EXPECT_CALL(autofill_driver(), HidePopup)
.Times(NumCallsToHidePopupOnFocusLoss());
}
LoadHTML("<body><div id=ce contenteditable></div>");
WaitForFormsSeen();
SimulateElementClickAndWait("ce");
check.Call();
ChangeFocusToNull(GetMainFrame()->GetDocument());
}
// Scrolling doesn't hide the popup on Android.
#if !BUILDFLAG(IS_ANDROID)
// Tests that scrolling triggers a call to `AutofillDriver::HidePopup()`.
TEST_F(AutofillAgentContentEditableInteractionTest,
ScrollingHidesAutofillPopup) {
EXPECT_CALL(autofill_driver(), HidePopup);
// "height" is needed so that the page is long enough to be able to scroll.
LoadHTML(
"<body><textarea style=\"height:10000px\" id=ce "
"contenteditable></textarea></body>");
SimulateElementClickAndWait("ce");
SimulateElementFocusAndWait("ce");
SimulateScrollingAndWait();
}
#endif // !BUILDFLAG(IS_ANDROID)
// Tests that clicking on a contenteditable form is ignored.
TEST_F(AutofillAgentContentEditableInteractionTest,
ClickOnContentEditableFormIsIgnored) {
EXPECT_CALL(autofill_driver(), AskForValuesToFill).Times(0);
LoadHTML("<body><form id=ce contenteditable></form>");
WaitForFormsSeen();
SimulateElementClickAndWait("ce");
}
// Tests that clicking on a contenteditable form element triggers
// AskForValuesToFill for the form element and not the contenteditable.
TEST_F(AutofillAgentContentEditableInteractionTest,
ClickOnContentEditableFormControlIsTreatedAsFormControl) {
MockFunction<void()> check;
{
InSequence s;
EXPECT_CALL(autofill_driver(), FormsSeen);
EXPECT_CALL(check, Call);
EXPECT_CALL(autofill_driver(),
AskForValuesToFill(FieldsAre(Not(IsContentEditable())), _, _, _,
Eq(std::nullopt)));
}
LoadHTML("<body><textarea id=ce contenteditable></textarea>");
WaitForFormsSeen();
check.Call();
SimulateElementClickAndWait("ce");
}
} // namespace
} // namespace autofill
|