Commit 9839a647 authored by Olivier Robin's avatar Olivier Robin Committed by Commit Bot

FixAutofillControllerTests

As a consequence of WebFrameMessaging, the initial processing of
frames is asynchronous.
Wait for it to happen in tests.

Bug: 938837
Change-Id: I05a759ea17944f316a9a19999afd1cab4fe5a59b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1505532
Commit-Queue: Olivier Robin <olivierrobin@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638071}
parent 048aed00
...@@ -195,7 +195,7 @@ class AutofillControllerTest : public ChromeWebTest { ...@@ -195,7 +195,7 @@ class AutofillControllerTest : public ChromeWebTest {
void SetUp() override; void SetUp() override;
void TearDown() override; void TearDown() override;
void SetUpForSuggestions(NSString* data); void SetUpForSuggestions(NSString* data, size_t expected_number_of_forms);
// Adds key value data to the Personal Data Manager and loads test page. // Adds key value data to the Personal Data Manager and loads test page.
void SetUpKeyValueData(); void SetUpKeyValueData();
...@@ -207,7 +207,13 @@ class AutofillControllerTest : public ChromeWebTest { ...@@ -207,7 +207,13 @@ class AutofillControllerTest : public ChromeWebTest {
// Blocks until |expected_size| forms have been fecthed. // Blocks until |expected_size| forms have been fecthed.
bool WaitForFormFetched(AutofillManager* manager, bool WaitForFormFetched(AutofillManager* manager,
size_t expected_size) WARN_UNUSED_RESULT; size_t expected_number_of_forms) WARN_UNUSED_RESULT;
// Loads the page and wait until the initial form processing has been done.
// This processing must find |expected_size| forms.
bool LoadHtmlAndWaitForFormFetched(NSString* html,
size_t expected_number_of_forms)
WARN_UNUSED_RESULT;
// Fails if the specified metric was not registered the given number of times. // Fails if the specified metric was not registered the given number of times.
void ExpectMetric(const std::string& histogram_name, int sum); void ExpectMetric(const std::string& histogram_name, int sum);
...@@ -303,14 +309,26 @@ void AutofillControllerTest::WaitForSuggestionRetrieval(BOOL wait_for_trigger) { ...@@ -303,14 +309,26 @@ void AutofillControllerTest::WaitForSuggestionRetrieval(BOOL wait_for_trigger) {
}); });
} }
bool AutofillControllerTest::WaitForFormFetched(AutofillManager* manager, bool AutofillControllerTest::WaitForFormFetched(
size_t expected_size) { AutofillManager* manager,
size_t expected_number_of_forms) {
return base::test::ios::WaitUntilConditionOrTimeout( return base::test::ios::WaitUntilConditionOrTimeout(
base::test::ios::kWaitForPageLoadTimeout, ^bool { base::test::ios::kWaitForPageLoadTimeout, ^bool {
return manager->form_structures().size() == expected_size; return manager->form_structures().size() == expected_number_of_forms;
}); });
} }
bool AutofillControllerTest::LoadHtmlAndWaitForFormFetched(
NSString* html,
size_t expected_number_of_forms) {
LoadHtml(html);
web::WebFrame* main_frame = web::GetMainWebFrame(web_state());
AutofillManager* autofill_manager =
AutofillDriverIOS::FromWebStateAndWebFrame(web_state(), main_frame)
->autofill_manager();
return WaitForFormFetched(autofill_manager, expected_number_of_forms);
}
void AutofillControllerTest::ExpectMetric(const std::string& histogram_name, void AutofillControllerTest::ExpectMetric(const std::string& histogram_name,
int sum) { int sum) {
histogram_tester_->ExpectBucketCount(histogram_name, sum, 1); histogram_tester_->ExpectBucketCount(histogram_name, sum, 1);
...@@ -324,12 +342,11 @@ void AutofillControllerTest::ExpectHappinessMetric( ...@@ -324,12 +342,11 @@ void AutofillControllerTest::ExpectHappinessMetric(
// Checks that viewing an HTML page containing a form results in the form being // Checks that viewing an HTML page containing a form results in the form being
// registered as a FormStructure by the AutofillManager. // registered as a FormStructure by the AutofillManager.
TEST_F(AutofillControllerTest, ReadForm) { TEST_F(AutofillControllerTest, ReadForm) {
LoadHtml(kProfileFormHtml); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(kProfileFormHtml, 1));
web::WebFrame* main_frame = web::GetMainWebFrame(web_state()); web::WebFrame* main_frame = web::GetMainWebFrame(web_state());
AutofillManager* autofill_manager = AutofillManager* autofill_manager =
AutofillDriverIOS::FromWebStateAndWebFrame(web_state(), main_frame) AutofillDriverIOS::FromWebStateAndWebFrame(web_state(), main_frame)
->autofill_manager(); ->autofill_manager();
EXPECT_TRUE(WaitForFormFetched(autofill_manager, 1));
const auto& forms = autofill_manager->form_structures(); const auto& forms = autofill_manager->form_structures();
const auto& form = *(forms.begin()->second); const auto& form = *(forms.begin()->second);
CheckField(form, NAME_FULL, "name_1"); CheckField(form, NAME_FULL, "name_1");
...@@ -345,12 +362,11 @@ TEST_F(AutofillControllerTest, ReadForm) { ...@@ -345,12 +362,11 @@ TEST_F(AutofillControllerTest, ReadForm) {
// the form being registered as a FormStructure by the AutofillManager, and the // the form being registered as a FormStructure by the AutofillManager, and the
// name is correctly set. // name is correctly set.
TEST_F(AutofillControllerTest, ReadFormName) { TEST_F(AutofillControllerTest, ReadFormName) {
LoadHtml(kMinimalFormWithNameHtml); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(kMinimalFormWithNameHtml, 1));
web::WebFrame* main_frame = web::GetMainWebFrame(web_state()); web::WebFrame* main_frame = web::GetMainWebFrame(web_state());
AutofillManager* autofill_manager = AutofillManager* autofill_manager =
AutofillDriverIOS::FromWebStateAndWebFrame(web_state(), main_frame) AutofillDriverIOS::FromWebStateAndWebFrame(web_state(), main_frame)
->autofill_manager(); ->autofill_manager();
EXPECT_TRUE(WaitForFormFetched(autofill_manager, 1));
const auto& forms = autofill_manager->form_structures(); const auto& forms = autofill_manager->form_structures();
const auto& form = *(forms.begin()->second); const auto& form = *(forms.begin()->second);
EXPECT_EQ(base::UTF8ToUTF16("form1"), form.ToFormData().name); EXPECT_EQ(base::UTF8ToUTF16("form1"), form.ToFormData().name);
...@@ -365,7 +381,7 @@ TEST_F(AutofillControllerTest, ProfileImport) { ...@@ -365,7 +381,7 @@ TEST_F(AutofillControllerTest, ProfileImport) {
ios::ChromeBrowserState::FromBrowserState(GetBrowserState())); ios::ChromeBrowserState::FromBrowserState(GetBrowserState()));
// Check there are no registered profiles already. // Check there are no registered profiles already.
EXPECT_EQ(0U, personal_data_manager->GetProfiles().size()); EXPECT_EQ(0U, personal_data_manager->GetProfiles().size());
LoadHtml(kProfileFormHtml); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(kProfileFormHtml, 1));
ExecuteJavaScript(@"document.forms[0].name.value = 'Homer Simpson'"); ExecuteJavaScript(@"document.forms[0].name.value = 'Homer Simpson'");
ExecuteJavaScript(@"document.forms[0].address.value = '123 Main Street'"); ExecuteJavaScript(@"document.forms[0].address.value = '123 Main Street'");
ExecuteJavaScript(@"document.forms[0].city.value = 'Springfield'"); ExecuteJavaScript(@"document.forms[0].city.value = 'Springfield'");
...@@ -392,7 +408,9 @@ TEST_F(AutofillControllerTest, ProfileImport) { ...@@ -392,7 +408,9 @@ TEST_F(AutofillControllerTest, ProfileImport) {
profile.GetInfo(AutofillType(ADDRESS_HOME_ZIP), "en-US")); profile.GetInfo(AutofillType(ADDRESS_HOME_ZIP), "en-US"));
} }
void AutofillControllerTest::SetUpForSuggestions(NSString* data) { void AutofillControllerTest::SetUpForSuggestions(
NSString* data,
size_t expected_number_of_forms) {
PersonalDataManager* personal_data_manager = PersonalDataManager* personal_data_manager =
PersonalDataManagerFactory::GetForBrowserState( PersonalDataManagerFactory::GetForBrowserState(
ios::ChromeBrowserState::FromBrowserState(GetBrowserState())); ios::ChromeBrowserState::FromBrowserState(GetBrowserState()));
...@@ -406,7 +424,7 @@ void AutofillControllerTest::SetUpForSuggestions(NSString* data) { ...@@ -406,7 +424,7 @@ void AutofillControllerTest::SetUpForSuggestions(NSString* data) {
personal_data_manager->SaveImportedProfile(profile); personal_data_manager->SaveImportedProfile(profile);
EXPECT_EQ(1U, personal_data_manager->GetProfiles().size()); EXPECT_EQ(1U, personal_data_manager->GetProfiles().size());
LoadHtml(data); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(data, expected_number_of_forms));
WaitForBackgroundTasks(); WaitForBackgroundTasks();
} }
...@@ -414,7 +432,7 @@ void AutofillControllerTest::SetUpForSuggestions(NSString* data) { ...@@ -414,7 +432,7 @@ void AutofillControllerTest::SetUpForSuggestions(NSString* data) {
// suggestions being sent to the AutofillAgent, once data has been loaded into a // suggestions being sent to the AutofillAgent, once data has been loaded into a
// test data manager. // test data manager.
TEST_F(AutofillControllerTest, ProfileSuggestions) { TEST_F(AutofillControllerTest, ProfileSuggestions) {
SetUpForSuggestions(kProfileFormHtml); SetUpForSuggestions(kProfileFormHtml, 1);
ForceViewRendering(web_state()->GetView()); ForceViewRendering(web_state()->GetView());
ExecuteJavaScript(@"document.forms[0].name.focus()"); ExecuteJavaScript(@"document.forms[0].name.focus()");
WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES); WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES);
...@@ -429,7 +447,8 @@ TEST_F(AutofillControllerTest, ProfileSuggestions) { ...@@ -429,7 +447,8 @@ TEST_F(AutofillControllerTest, ProfileSuggestions) {
// there is another anonymous form on the page. // there is another anonymous form on the page.
TEST_F(AutofillControllerTest, ProfileSuggestionsTwoAnonymousForms) { TEST_F(AutofillControllerTest, ProfileSuggestionsTwoAnonymousForms) {
SetUpForSuggestions( SetUpForSuggestions(
[NSString stringWithFormat:@"%@%@", kProfileFormHtml, kProfileFormHtml]); [NSString stringWithFormat:@"%@%@", kProfileFormHtml, kProfileFormHtml],
2);
ForceViewRendering(web_state()->GetView()); ForceViewRendering(web_state()->GetView());
ExecuteJavaScript(@"document.forms[0].name.focus()"); ExecuteJavaScript(@"document.forms[0].name.focus()");
WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES); WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES);
...@@ -444,7 +463,7 @@ TEST_F(AutofillControllerTest, ProfileSuggestionsTwoAnonymousForms) { ...@@ -444,7 +463,7 @@ TEST_F(AutofillControllerTest, ProfileSuggestionsTwoAnonymousForms) {
// in suggestions being sent to the AutofillAgent, once data has been loaded // in suggestions being sent to the AutofillAgent, once data has been loaded
// into a test data manager. // into a test data manager.
TEST_F(AutofillControllerTest, ProfileSuggestionsFromSelectField) { TEST_F(AutofillControllerTest, ProfileSuggestionsFromSelectField) {
SetUpForSuggestions(kProfileFormHtml); SetUpForSuggestions(kProfileFormHtml, 1);
ForceViewRendering(web_state()->GetView()); ForceViewRendering(web_state()->GetView());
ExecuteJavaScript(@"document.forms[0].state.focus()"); ExecuteJavaScript(@"document.forms[0].state.focus()");
WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES); WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES);
...@@ -483,9 +502,7 @@ TEST_F(AutofillControllerTest, MultipleProfileSuggestions) { ...@@ -483,9 +502,7 @@ TEST_F(AutofillControllerTest, MultipleProfileSuggestions) {
profile2.SetRawInfo(ADDRESS_HOME_ZIP, base::UTF8ToUTF16("94043")); profile2.SetRawInfo(ADDRESS_HOME_ZIP, base::UTF8ToUTF16("94043"));
personal_data_manager->SaveImportedProfile(profile2); personal_data_manager->SaveImportedProfile(profile2);
EXPECT_EQ(2U, personal_data_manager->GetProfiles().size()); EXPECT_EQ(2U, personal_data_manager->GetProfiles().size());
LoadHtml(kProfileFormHtml); EXPECT_TRUE(LoadHtmlAndWaitForFormFetched(kProfileFormHtml, 1));
base::TaskScheduler::GetInstance()->FlushForTesting();
WaitForBackgroundTasks();
ForceViewRendering(web_state()->GetView()); ForceViewRendering(web_state()->GetView());
ExecuteJavaScript(@"document.forms[0].name.focus()"); ExecuteJavaScript(@"document.forms[0].name.focus()");
WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES); WaitForSuggestionRetrieval(/*wait_for_trigger=*/YES);
...@@ -498,7 +515,7 @@ TEST_F(AutofillControllerTest, MultipleProfileSuggestions) { ...@@ -498,7 +515,7 @@ TEST_F(AutofillControllerTest, MultipleProfileSuggestions) {
// with scripts (simulating user form submission) results in data being // with scripts (simulating user form submission) results in data being
// successfully registered. // successfully registered.
TEST_F(AutofillControllerTest, KeyValueImport) { TEST_F(AutofillControllerTest, KeyValueImport) {
LoadHtml(kKeyValueFormHtml); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(kKeyValueFormHtml, 1));
ExecuteJavaScript(@"document.forms[0].greeting.value = 'Hello'"); ExecuteJavaScript(@"document.forms[0].greeting.value = 'Hello'");
scoped_refptr<AutofillWebDataService> web_data_service = scoped_refptr<AutofillWebDataService> web_data_service =
ios::WebDataServiceFactory::GetAutofillWebDataForBrowserState( ios::WebDataServiceFactory::GetAutofillWebDataForBrowserState(
...@@ -540,7 +557,7 @@ void AutofillControllerTest::SetUpKeyValueData() { ...@@ -540,7 +557,7 @@ void AutofillControllerTest::SetUpKeyValueData() {
web_data_service->AddFormFields(values); web_data_service->AddFormFields(values);
// Load test page. // Load test page.
LoadHtml(kKeyValueFormHtml); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(kKeyValueFormHtml, 1));
WaitForBackgroundTasks(); WaitForBackgroundTasks();
} }
...@@ -631,7 +648,7 @@ TEST_F(AutofillControllerTest, CreditCardImport) { ...@@ -631,7 +648,7 @@ TEST_F(AutofillControllerTest, CreditCardImport) {
// Check there are no registered profiles already. // Check there are no registered profiles already.
EXPECT_EQ(0U, personal_data_manager->GetCreditCards().size()); EXPECT_EQ(0U, personal_data_manager->GetCreditCards().size());
LoadHtml(kCreditCardFormHtml); ASSERT_TRUE(LoadHtmlAndWaitForFormFetched(kCreditCardFormHtml, 1));
ExecuteJavaScript(@"document.forms[0].name.value = 'Superman'"); ExecuteJavaScript(@"document.forms[0].name.value = 'Superman'");
ExecuteJavaScript(@"document.forms[0].CCNo.value = '4000-4444-4444-4444'"); ExecuteJavaScript(@"document.forms[0].CCNo.value = '4000-4444-4444-4444'");
ExecuteJavaScript(@"document.forms[0].CCExpiresMonth.value = '11'"); ExecuteJavaScript(@"document.forms[0].CCExpiresMonth.value = '11'");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment