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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "components/autofill/ios/browser/autofill_agent.h"
#include "base/strings/utf_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "base/test/scoped_feature_list.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/popup_item_ids.h"
#include "components/autofill/core/browser/suggestion.h"
#include "components/autofill/core/browser/test_autofill_client.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/autofill/core/common/form_data.h"
#include "components/autofill/ios/browser/autofill_driver_ios.h"
#import "components/autofill/ios/browser/js_autofill_manager.h"
#include "components/prefs/pref_service.h"
#include "ios/web/public/features.h"
#include "ios/web/public/test/fakes/fake_web_frame.h"
#include "ios/web/public/test/fakes/test_browser_state.h"
#import "ios/web/public/test/fakes/test_web_state.h"
#include "ios/web/public/test/test_web_thread_bundle.h"
#import "ios/web/public/web_state/js/crw_js_injection_receiver.h"
#include "ios/web/public/web_state/web_frame_util.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#include "url/gurl.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
using autofill::POPUP_ITEM_ID_CLEAR_FORM;
using autofill::POPUP_ITEM_ID_GOOGLE_PAY_BRANDING;
using base::test::ios::WaitUntilCondition;
// Subclass of web::FakeWebFrame that allow to set a callback before any
// JavaScript call. This callback can be used to check the state of the page.
class FakeWebFrameCallback : public web::FakeWebFrame {
public:
FakeWebFrameCallback(const std::string& frame_id,
bool is_main_frame,
GURL security_origin,
std::function<void()> callback)
: web::FakeWebFrame(frame_id, is_main_frame, security_origin),
callback_(callback) {}
bool CallJavaScriptFunction(
const std::string& name,
const std::vector<base::Value>& parameters) override {
callback_();
return web::FakeWebFrame::CallJavaScriptFunction(name, parameters);
}
private:
std::function<void()> callback_;
};
// Test fixture for AutofillAgent testing.
class AutofillAgentTests : public PlatformTest {
public:
AutofillAgentTests() {}
void SetUp() override {
PlatformTest::SetUp();
// Mock CRWJSInjectionReceiver for verifying interactions.
mock_js_injection_receiver_ =
[OCMockObject mockForClass:[CRWJSInjectionReceiver class]];
test_web_state_.SetBrowserState(&test_browser_state_);
test_web_state_.SetJSInjectionReceiver(mock_js_injection_receiver_);
test_web_state_.SetContentIsHTML(true);
GURL url("https://example.com");
test_web_state_.SetCurrentURL(url);
test_web_state_.CreateWebFramesManager();
auto main_frame = std::make_unique<web::FakeWebFrame>("frameID", true, url);
fake_main_frame_ = main_frame.get();
test_web_state_.AddWebFrame(std::move(main_frame));
prefs_ = autofill::test::PrefServiceForTesting();
autofill::prefs::SetAutofillEnabled(prefs_.get(), true);
autofill_agent_ =
[[AutofillAgent alloc] initWithPrefService:prefs_.get()
webState:&test_web_state_];
}
web::TestWebThreadBundle thread_bundle_;
web::TestBrowserState test_browser_state_;
web::TestWebState test_web_state_;
web::FakeWebFrame* fake_main_frame_ = nullptr;
autofill::TestAutofillClient client_;
std::unique_ptr<PrefService> prefs_;
AutofillAgent* autofill_agent_;
id mock_js_injection_receiver_;
DISALLOW_COPY_AND_ASSIGN(AutofillAgentTests);
};
// Tests that form's name and fields' identifiers, values, and whether they are
// autofilled are sent to the JS. Fields with empty values and those that are
// not autofilled are skipped.
// Test with "IsAutofillIFrameMessagingEnabled" disabled.
TEST_F(AutofillAgentTests, OnFormDataFilledTest) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
disabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
disabled_features.push_back(web::features::kWebFrameMessaging);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
std::string locale("en");
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
&test_web_state_, &client_, nil, locale,
autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
autofill::FormData form;
form.origin = GURL("https://myform.com");
form.action = GURL("https://myform.com/submit");
form.name = base::ASCIIToUTF16("CC form");
autofill::FormFieldData field;
field.form_control_type = "text";
field.label = base::ASCIIToUTF16("Card number");
field.name = base::ASCIIToUTF16("number");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("number");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("number_value");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Name on Card");
field.name = base::ASCIIToUTF16("name");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("name");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("name_value");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Expiry Month");
field.name = base::ASCIIToUTF16("expiry_month");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("expiry_month");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("01");
field.is_autofilled = false;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Unknown field");
field.name = base::ASCIIToUTF16("unknown");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("unknown");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("");
field.is_autofilled = true;
form.fields.push_back(field);
// Fields are in alphabetical order.
[[mock_js_injection_receiver_ expect]
executeJavaScript:
@"__gCrWeb.autofill.fillForm({\"fields\":{\"name\":{\"section\":\"\","
@"\"value\":\"name_value\"},"
@"\"number\":{\"section\":\"\",\"value\":\"number_value\"}},"
@"\"formName\":\"CC form\"}, \"\");"
completionHandler:[OCMArg any]];
[autofill_agent_ fillFormData:form
inFrame:web::GetMainWebFrame(&test_web_state_)];
test_web_state_.WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_injection_receiver_);
}
// Tests that form's name and fields' identifiers, values, and whether they are
// autofilled are sent to the JS. Fields with empty values and those that are
// not autofilled are skipped.
// Test with "IsAutofillIFrameMessagingEnabled" enabled.
TEST_F(AutofillAgentTests, OnFormDataFilledTestWithFrameMessaging) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
enabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
enabled_features.push_back(web::features::kWebFrameMessaging);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
std::string locale("en");
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
&test_web_state_, &client_, nil, locale,
autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
autofill::FormData form;
form.origin = GURL("https://myform.com");
form.action = GURL("https://myform.com/submit");
form.name = base::ASCIIToUTF16("CC form");
autofill::FormFieldData field;
field.form_control_type = "text";
field.label = base::ASCIIToUTF16("Card number");
field.name = base::ASCIIToUTF16("number");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("number");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("number_value");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Name on Card");
field.name = base::ASCIIToUTF16("name");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("name");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("name_value");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Expiry Month");
field.name = base::ASCIIToUTF16("expiry_month");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("expiry_month");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("01");
field.is_autofilled = false;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Unknown field");
field.name = base::ASCIIToUTF16("unknown");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("unknown");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("");
field.is_autofilled = true;
form.fields.push_back(field);
[autofill_agent_ fillFormData:form
inFrame:web::GetMainWebFrame(&test_web_state_)];
test_web_state_.WasShown();
EXPECT_EQ(
"__gCrWeb.autofill.fillForm({\"fields\":{\"name\":{\"section\":\"\","
"\"value\":\"name_value\"},"
"\"number\":{\"section\":\"\",\"value\":\"number_value\"}},"
"\"formName\":\"CC form\"}, \"\");",
fake_main_frame_->last_javascript_call());
}
// Tests that in the case of conflict in fields' identifiers, the last seen
// value of a given field is used.
// Test with "IsAutofillIFrameMessagingEnabled" disabled.
TEST_F(AutofillAgentTests, OnFormDataFilledWithNameCollisionTest) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
disabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
disabled_features.push_back(web::features::kWebFrameMessaging);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
std::string locale("en");
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
&test_web_state_, &client_, nil, locale,
autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
autofill::FormData form;
form.origin = GURL("https://myform.com");
form.action = GURL("https://myform.com/submit");
autofill::FormFieldData field;
field.form_control_type = "text";
field.label = base::ASCIIToUTF16("State");
field.name = base::ASCIIToUTF16("region");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("region");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("California");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Other field");
field.name = base::ASCIIToUTF16("field1");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("field1");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("value 1");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Other field");
field.name = base::ASCIIToUTF16("field1");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("field1");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("value 2");
field.is_autofilled = true;
form.fields.push_back(field);
// Fields are in alphabetical order.
[[mock_js_injection_receiver_ expect]
executeJavaScript:
@"__gCrWeb.autofill.fillForm({\"fields\":{\"field1\":{\"section\":"
@"\"\",\"value\":\"value "
@"2\"},\"region\":{\"section\":\"\",\"value\":\"California\"}},"
@"\"formName\":\"\"}, \"\");"
completionHandler:[OCMArg any]];
[autofill_agent_ fillFormData:form
inFrame:web::GetMainWebFrame(&test_web_state_)];
test_web_state_.WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_injection_receiver_);
}
// Tests that in the case of conflict in fields' identifiers, the last seen
// value of a given field is used.
// Test with "IsAutofillIFrameMessagingEnabled" enabled.
TEST_F(AutofillAgentTests,
OnFormDataFilledWithNameCollisionTestFrameMessaging) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
enabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
enabled_features.push_back(web::features::kWebFrameMessaging);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
std::string locale("en");
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
&test_web_state_, &client_, nil, locale,
autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
autofill::FormData form;
form.origin = GURL("https://myform.com");
form.action = GURL("https://myform.com/submit");
autofill::FormFieldData field;
field.form_control_type = "text";
field.label = base::ASCIIToUTF16("State");
field.name = base::ASCIIToUTF16("region");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("region");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("California");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Other field");
field.name = base::ASCIIToUTF16("field1");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("field1");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("value 1");
field.is_autofilled = true;
form.fields.push_back(field);
field.label = base::ASCIIToUTF16("Other field");
field.name = base::ASCIIToUTF16("field1");
field.name_attribute = field.name;
field.id_attribute = base::ASCIIToUTF16("field1");
field.unique_id = field.id_attribute;
field.value = base::ASCIIToUTF16("value 2");
field.is_autofilled = true;
form.fields.push_back(field);
// Fields are in alphabetical order.
[autofill_agent_ fillFormData:form
inFrame:web::GetMainWebFrame(&test_web_state_)];
test_web_state_.WasShown();
EXPECT_EQ(
"__gCrWeb.autofill.fillForm({\"fields\":{\"field1\":{\"section\":"
"\"\",\"value\":\"value "
"2\"},\"region\":{\"section\":\"\",\"value\":\"California\"}},"
"\"formName\":\"\"}, \"\");",
fake_main_frame_->last_javascript_call());
}
// Tests that when a user initiated form activity is registered the script to
// extract forms is executed.
// Test with "IsAutofillIFrameMessagingEnabled" disabled.
TEST_F(AutofillAgentTests, CheckIfSuggestionsAvailable_UserInitiatedActivity1) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
disabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
disabled_features.push_back(web::features::kWebFrameMessaging);
enabled_features.push_back(
autofill::features::kAutofillRestrictUnownedFieldsToFormlessCheckout);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
// TODO(crbug.com/881364): Fix for WebFrameMessaging.
[[mock_js_injection_receiver_ expect]
executeJavaScript:@"__gCrWeb.autofill.extractForms(1, true);"
completionHandler:[OCMArg any]];
[autofill_agent_ checkIfSuggestionsAvailableForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
isMainFrame:YES
hasUserGesture:YES
webState:&test_web_state_
completionHandler:nil];
test_web_state_.WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_injection_receiver_);
}
// Tests that when a user initiated form activity is registered the script to
// extract forms is executed.
// Test with "IsAutofillIFrameMessagingEnabled" enabled.
TEST_F(AutofillAgentTests,
CheckIfSuggestionsAvailable_UserInitiatedActivity1FrameMessaging) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
enabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
enabled_features.push_back(web::features::kWebFrameMessaging);
enabled_features.push_back(
autofill::features::kAutofillRestrictUnownedFieldsToFormlessCheckout);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
[autofill_agent_ checkIfSuggestionsAvailableForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
isMainFrame:YES
hasUserGesture:YES
webState:&test_web_state_
completionHandler:nil];
test_web_state_.WasShown();
EXPECT_EQ("__gCrWeb.autofill.extractForms(1, true);",
fake_main_frame_->last_javascript_call());
}
// Tests that when a user initiated form activity is registered the script to
// extract forms is executed.
// Test with "IsAutofillIFrameMessagingEnabled" disabled.
TEST_F(AutofillAgentTests, CheckIfSuggestionsAvailable_UserInitiatedActivity2) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
disabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
disabled_features.push_back(web::features::kWebFrameMessaging);
disabled_features.push_back(
autofill::features::kAutofillRestrictUnownedFieldsToFormlessCheckout);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
// TODO(crbug.com/881364): Fix for WebFrameMessaging.
[[mock_js_injection_receiver_ expect]
executeJavaScript:@"__gCrWeb.autofill.extractForms(1, false);"
completionHandler:[OCMArg any]];
[autofill_agent_ checkIfSuggestionsAvailableForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
isMainFrame:YES
hasUserGesture:YES
webState:&test_web_state_
completionHandler:nil];
test_web_state_.WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_injection_receiver_);
}
// Tests that when a user initiated form activity is registered the script to
// extract forms is executed.
// Test with "IsAutofillIFrameMessagingEnabled" disabled.
TEST_F(AutofillAgentTests,
CheckIfSuggestionsAvailable_UserInitiatedActivity2FrameMessaging) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
disabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
disabled_features.push_back(web::features::kWebFrameMessaging);
disabled_features.push_back(
autofill::features::kAutofillRestrictUnownedFieldsToFormlessCheckout);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
// TODO(crbug.com/881364): Fix for WebFrameMessaging.
[[mock_js_injection_receiver_ expect]
executeJavaScript:@"__gCrWeb.autofill.extractForms(1, false);"
completionHandler:[OCMArg any]];
[autofill_agent_ checkIfSuggestionsAvailableForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
isMainFrame:YES
hasUserGesture:YES
webState:&test_web_state_
completionHandler:nil];
test_web_state_.WasShown();
EXPECT_OCMOCK_VERIFY(mock_js_injection_receiver_);
}
// Tests that when a non user initiated form activity is registered the
// completion callback passed to the call to check if suggestions are available
// is invoked with no suggestions.
TEST_F(AutofillAgentTests,
CheckIfSuggestionsAvailable_NonUserInitiatedActivity) {
__block BOOL completion_handler_success = NO;
__block BOOL completion_handler_called = NO;
[autofill_agent_ checkIfSuggestionsAvailableForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
isMainFrame:YES
hasUserGesture:NO
webState:&test_web_state_
completionHandler:^(BOOL success) {
completion_handler_success = success;
completion_handler_called = YES;
}];
test_web_state_.WasShown();
// Wait until the expected handler is called.
WaitUntilCondition(^bool() {
return completion_handler_called;
});
EXPECT_FALSE(completion_handler_success);
}
// Tests that when Autofill suggestions are made available to AutofillAgent
// "Clear Form" is moved to the start of the list and the order of other
// suggestions remains unchanged.
TEST_F(AutofillAgentTests, onSuggestionsReady_ClearForm) {
__block NSArray<FormSuggestion*>* completion_handler_suggestions = nil;
__block BOOL completion_handler_called = NO;
// Make the suggestions available to AutofillAgent.
std::vector<autofill::Suggestion> suggestions;
suggestions.push_back(autofill::Suggestion("", "", "", 123));
suggestions.push_back(autofill::Suggestion("", "", "", 321));
suggestions.push_back(
autofill::Suggestion("", "", "", POPUP_ITEM_ID_CLEAR_FORM));
[autofill_agent_
showAutofillPopup:suggestions
popupDelegate:base::WeakPtr<autofill::AutofillPopupDelegate>()];
// Retrieves the suggestions.
auto completionHandler = ^(NSArray<FormSuggestion*>* suggestions,
id<FormSuggestionProvider> delegate) {
completion_handler_suggestions = [suggestions copy];
completion_handler_called = YES;
};
[autofill_agent_ retrieveSuggestionsForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
webState:&test_web_state_
completionHandler:completionHandler];
test_web_state_.WasShown();
// Wait until the expected handler is called.
WaitUntilCondition(^bool() {
return completion_handler_called;
});
// "Clear Form" should appear as the first suggestion. Otherwise, the order of
// suggestions should not change.
EXPECT_EQ(3U, completion_handler_suggestions.count);
EXPECT_EQ(POPUP_ITEM_ID_CLEAR_FORM,
completion_handler_suggestions[0].identifier);
EXPECT_EQ(123, completion_handler_suggestions[1].identifier);
EXPECT_EQ(321, completion_handler_suggestions[2].identifier);
}
// Tests that when Autofill suggestions are made available to AutofillAgent
// GPay icon remains as the first suggestion.
TEST_F(AutofillAgentTests, onSuggestionsReady_ClearFormWithGPay) {
__block NSArray<FormSuggestion*>* completion_handler_suggestions = nil;
__block BOOL completion_handler_called = NO;
// Make the suggestions available to AutofillAgent.
std::vector<autofill::Suggestion> suggestions;
suggestions.push_back(
autofill::Suggestion("", "", "", POPUP_ITEM_ID_GOOGLE_PAY_BRANDING));
suggestions.push_back(autofill::Suggestion("", "", "", 123));
suggestions.push_back(autofill::Suggestion("", "", "", 321));
suggestions.push_back(
autofill::Suggestion("", "", "", POPUP_ITEM_ID_CLEAR_FORM));
[autofill_agent_
showAutofillPopup:suggestions
popupDelegate:base::WeakPtr<autofill::AutofillPopupDelegate>()];
// Retrieves the suggestions.
auto completionHandler = ^(NSArray<FormSuggestion*>* suggestions,
id<FormSuggestionProvider> delegate) {
completion_handler_suggestions = [suggestions copy];
completion_handler_called = YES;
};
[autofill_agent_ retrieveSuggestionsForForm:@"form"
fieldIdentifier:@"address"
fieldType:@"text"
type:@"focus"
typedValue:@""
frameID:@"frameID"
webState:&test_web_state_
completionHandler:completionHandler];
test_web_state_.WasShown();
// Wait until the expected handler is called.
WaitUntilCondition(^bool() {
return completion_handler_called;
});
// GPay icon should appear as the first suggestion followed by "Clear Form".
// Otherwise, the order of suggestions should not change.
EXPECT_EQ(4U, completion_handler_suggestions.count);
EXPECT_EQ(POPUP_ITEM_ID_GOOGLE_PAY_BRANDING,
completion_handler_suggestions[0].identifier);
EXPECT_EQ(POPUP_ITEM_ID_CLEAR_FORM,
completion_handler_suggestions[1].identifier);
EXPECT_EQ(123, completion_handler_suggestions[2].identifier);
EXPECT_EQ(321, completion_handler_suggestions[3].identifier);
}
// Test that main frame is processed whatever is the order of pageloading
// callbacks.
TEST_F(AutofillAgentTests, FrameInitializationOrder) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
disabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
disabled_features.push_back(web::features::kWebFrameMessaging);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
std::string locale("en");
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
&test_web_state_, &client_, nil, locale,
autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
// Remove the current main frame.
test_web_state_.RemoveWebFrame(fake_main_frame_->GetFrameId());
// Add frame when page is loading.
auto main_frame_unique =
std::make_unique<web::FakeWebFrame>("main", true, GURL());
web::FakeWebFrame* main_frame = main_frame_unique.get();
autofill::AutofillDriverIOS* main_frame_driver =
autofill::AutofillDriverIOS::FromWebStateAndWebFrame(&test_web_state_,
main_frame);
// autofill::AutofillDriver is created with a null web::WebFrame instance when
// frame messaging is disabled. IsInMainFrame() is expected to return true.
EXPECT_TRUE(main_frame_driver->IsInMainFrame());
test_web_state_.SetLoading(true);
test_web_state_.AddWebFrame(std::move(main_frame_unique));
EXPECT_FALSE(main_frame_driver->is_processed());
test_web_state_.SetLoading(false);
// Toggle mutations.
[[mock_js_injection_receiver_ expect] executeJavaScript:[OCMArg any]
completionHandler:[OCMArg any]];
// Toggle user edited field.
[[mock_js_injection_receiver_ expect] executeJavaScript:[OCMArg any]
completionHandler:[OCMArg any]];
// Extract Forms
[[mock_js_injection_receiver_ expect] executeJavaScript:[OCMArg any]
completionHandler:[OCMArg any]];
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS);
EXPECT_TRUE(main_frame_driver->is_processed());
test_web_state_.RemoveWebFrame(main_frame->GetFrameId());
// Driver is in the WebState, so reset it manually.
main_frame_driver->set_processed(false);
// Add frame when page is loaded.
main_frame_unique = std::make_unique<web::FakeWebFrame>("main", true, GURL());
main_frame = main_frame_unique.get();
main_frame_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, main_frame);
// autofill::AutofillDriver is created with a null web::WebFrame instance when
// frame messaging is disabled. IsInMainFrame() is expected to return true.
EXPECT_TRUE(main_frame_driver->IsInMainFrame());
test_web_state_.SetLoading(false);
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS);
EXPECT_FALSE(main_frame_driver->is_processed());
// Toggle mutations.
[[mock_js_injection_receiver_ expect] executeJavaScript:[OCMArg any]
completionHandler:[OCMArg any]];
// Toggle user edited field.
[[mock_js_injection_receiver_ expect] executeJavaScript:[OCMArg any]
completionHandler:[OCMArg any]];
// Extract Forms
[[mock_js_injection_receiver_ expect] executeJavaScript:[OCMArg any]
completionHandler:[OCMArg any]];
test_web_state_.AddWebFrame(std::move(main_frame_unique));
EXPECT_TRUE(main_frame_driver->is_processed());
test_web_state_.RemoveWebFrame(main_frame->GetFrameId());
}
// Test that every frames are processed whatever is the order of pageloading
// callbacks. The main frame should always be processed first.
TEST_F(AutofillAgentTests, FrameInitializationOrderFrames) {
base::test::ScopedFeatureList scoped_feature_list;
std::vector<base::Feature> enabled_features;
std::vector<base::Feature> disabled_features;
enabled_features.push_back(
autofill::features::kAutofillEnableIFrameSupportOniOS);
enabled_features.push_back(web::features::kWebFrameMessaging);
scoped_feature_list.InitWithFeatures(enabled_features, disabled_features);
std::string locale("en");
autofill::AutofillDriverIOS::PrepareForWebStateWebFrameAndDelegate(
&test_web_state_, &client_, nil, locale,
autofill::AutofillManager::DISABLE_AUTOFILL_DOWNLOAD_MANAGER);
// Remove the current main frame.
test_web_state_.RemoveWebFrame(fake_main_frame_->GetFrameId());
// Both frames available, then page loaded.
test_web_state_.SetLoading(true);
auto main_frame_unique =
std::make_unique<web::FakeWebFrame>("main", true, GURL());
web::FakeWebFrame* main_frame = main_frame_unique.get();
test_web_state_.AddWebFrame(std::move(main_frame_unique));
autofill::AutofillDriverIOS* main_frame_driver =
autofill::AutofillDriverIOS::FromWebStateAndWebFrame(&test_web_state_,
main_frame);
EXPECT_TRUE(main_frame_driver->IsInMainFrame());
auto iframe_unique = std::make_unique<FakeWebFrameCallback>(
"iframe", false, GURL(), [main_frame_driver]() {
EXPECT_TRUE(main_frame_driver->is_processed());
});
FakeWebFrameCallback* iframe = iframe_unique.get();
test_web_state_.AddWebFrame(std::move(iframe_unique));
autofill::AutofillDriverIOS* iframe_driver =
autofill::AutofillDriverIOS::FromWebStateAndWebFrame(&test_web_state_,
iframe);
EXPECT_FALSE(iframe_driver->IsInMainFrame());
EXPECT_FALSE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.SetLoading(false);
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS);
EXPECT_TRUE(main_frame_driver->is_processed());
EXPECT_TRUE(iframe_driver->is_processed());
test_web_state_.RemoveWebFrame(main_frame->GetFrameId());
test_web_state_.RemoveWebFrame(iframe->GetFrameId());
// Main frame available, then page loaded, then iframe available
main_frame_unique = std::make_unique<web::FakeWebFrame>("main", true, GURL());
main_frame = main_frame_unique.get();
main_frame_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, main_frame);
iframe_unique = std::make_unique<FakeWebFrameCallback>(
"iframe", false, GURL(), [main_frame_driver]() {
EXPECT_TRUE(main_frame_driver->is_processed());
});
iframe = iframe_unique.get();
iframe_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, iframe);
test_web_state_.SetLoading(true);
test_web_state_.AddWebFrame(std::move(main_frame_unique));
EXPECT_FALSE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.SetLoading(false);
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS);
EXPECT_TRUE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.AddWebFrame(std::move(iframe_unique));
EXPECT_TRUE(main_frame_driver->is_processed());
EXPECT_TRUE(iframe_driver->is_processed());
test_web_state_.RemoveWebFrame(main_frame->GetFrameId());
test_web_state_.RemoveWebFrame(iframe->GetFrameId());
// Page loaded, then main frame, then iframe
main_frame_unique = std::make_unique<web::FakeWebFrame>("main", true, GURL());
main_frame = main_frame_unique.get();
main_frame_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, main_frame);
iframe_unique = std::make_unique<FakeWebFrameCallback>(
"iframe", false, GURL(), [main_frame_driver]() {
EXPECT_TRUE(main_frame_driver->is_processed());
});
iframe = iframe_unique.get();
iframe_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, iframe);
test_web_state_.SetLoading(true);
test_web_state_.SetLoading(false);
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS);
EXPECT_FALSE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.AddWebFrame(std::move(main_frame_unique));
EXPECT_TRUE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.AddWebFrame(std::move(iframe_unique));
EXPECT_TRUE(main_frame_driver->is_processed());
EXPECT_TRUE(iframe_driver->is_processed());
test_web_state_.RemoveWebFrame(main_frame->GetFrameId());
test_web_state_.RemoveWebFrame(iframe->GetFrameId());
// Page loaded, then iframe, then main frame
main_frame_unique = std::make_unique<web::FakeWebFrame>("main", true, GURL());
main_frame = main_frame_unique.get();
main_frame_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, main_frame);
iframe_unique = std::make_unique<FakeWebFrameCallback>(
"iframe", false, GURL(), [main_frame_driver]() {
EXPECT_TRUE(main_frame_driver->is_processed());
});
iframe = iframe_unique.get();
iframe_driver = autofill::AutofillDriverIOS::FromWebStateAndWebFrame(
&test_web_state_, iframe);
test_web_state_.SetLoading(true);
test_web_state_.SetLoading(false);
test_web_state_.OnPageLoaded(web::PageLoadCompletionStatus::SUCCESS);
EXPECT_FALSE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.AddWebFrame(std::move(iframe_unique));
EXPECT_FALSE(main_frame_driver->is_processed());
EXPECT_FALSE(iframe_driver->is_processed());
test_web_state_.AddWebFrame(std::move(main_frame_unique));
EXPECT_TRUE(main_frame_driver->is_processed());
EXPECT_TRUE(iframe_driver->is_processed());
test_web_state_.RemoveWebFrame(main_frame->GetFrameId());
test_web_state_.RemoveWebFrame(iframe->GetFrameId());
}
|