Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1421)

Side by Side Diff: components/autofill/browser/autofill_manager_unittest.cc

Issue 16611003: Ignore ajax on specified pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unit tests Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 new AutocheckoutPageMetaData()); 621 new AutocheckoutPageMetaData());
622 start_of_flow->current_page_number = 0; 622 start_of_flow->current_page_number = 0;
623 start_of_flow->total_pages = 3; 623 start_of_flow->total_pages = 3;
624 WebElementDescriptor* proceed_element = 624 WebElementDescriptor* proceed_element =
625 &start_of_flow->proceed_element_descriptor; 625 &start_of_flow->proceed_element_descriptor;
626 proceed_element->descriptor = "#foo"; 626 proceed_element->descriptor = "#foo";
627 proceed_element->retrieval_method = WebElementDescriptor::ID; 627 proceed_element->retrieval_method = WebElementDescriptor::ID;
628 autocheckout_manager()->OnLoadedPageMetaData(start_of_flow.Pass()); 628 autocheckout_manager()->OnLoadedPageMetaData(start_of_flow.Pass());
629 } 629 }
630 630
631 // Set autocheckout manager's page meta data to first page on Autocheckout
632 // flow.
633 void MarkAsFirstPageInAutocheckoutFlowIgnoringAjax() {
634 scoped_ptr<AutocheckoutPageMetaData> start_of_flow(
635 new AutocheckoutPageMetaData());
636 start_of_flow->current_page_number = 0;
637 start_of_flow->total_pages = 3;
638 start_of_flow->ignore_ajax = true;
639 WebElementDescriptor* proceed_element =
640 &start_of_flow->proceed_element_descriptor;
641 proceed_element->descriptor = "#foo";
642 proceed_element->retrieval_method = WebElementDescriptor::ID;
643 autocheckout_manager()->OnLoadedPageMetaData(start_of_flow.Pass());
644 }
645
631 private: 646 private:
632 // Weak reference. 647 // Weak reference.
633 TestPersonalDataManager* personal_data_; 648 TestPersonalDataManager* personal_data_;
634 649
635 bool autofill_enabled_; 650 bool autofill_enabled_;
636 std::vector<std::pair<WebFormElement::AutocompleteResult, FormData> > 651 std::vector<std::pair<WebFormElement::AutocompleteResult, FormData> >
637 request_autocomplete_results_; 652 request_autocomplete_results_;
638 653
639 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 654 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
640 655
(...skipping 2535 matching lines...) Expand 10 before | Expand all | Expand 10 after
3176 autofill_manager_->OnRequestAutocomplete(FormData(), 3191 autofill_manager_->OnRequestAutocomplete(FormData(),
3177 GURL()); 3192 GURL());
3178 3193
3179 EXPECT_EQ(1U, autofill_manager_->request_autocomplete_results().size()); 3194 EXPECT_EQ(1U, autofill_manager_->request_autocomplete_results().size());
3180 EXPECT_EQ(WebFormElement::AutocompleteResultErrorDisabled, 3195 EXPECT_EQ(WebFormElement::AutocompleteResultErrorDisabled,
3181 autofill_manager_->request_autocomplete_results()[0].first); 3196 autofill_manager_->request_autocomplete_results()[0].first);
3182 } 3197 }
3183 3198
3184 namespace { 3199 namespace {
3185 3200
3186 class MockAutofillManagerDelegate 3201 class MockAutofillManagerDelegate : public TestAutofillManagerDelegate {
3187 : public autofill::TestAutofillManagerDelegate {
3188 public: 3202 public:
3189 MockAutofillManagerDelegate() {} 3203 MockAutofillManagerDelegate()
3204 : autocheckout_bubble_shown_(false) {}
3205
3190 virtual ~MockAutofillManagerDelegate() {} 3206 virtual ~MockAutofillManagerDelegate() {}
3191 3207
3192 MOCK_METHOD3(ShowAutocheckoutBubble, 3208 virtual void ShowAutocheckoutBubble(
3193 void(const gfx::RectF& bounds, 3209 const gfx::RectF& bounds,
3194 bool is_google_user, 3210 bool is_google_user,
3195 const base::Callback<void(bool)>& callback)); 3211 const base::Callback<void(bool)>& callback) OVERRIDE {
3212 autocheckout_bubble_shown_ = true;
3213 callback.Run(true);
3214 }
3215
3216 virtual void ShowRequestAutocompleteDialog(
3217 const FormData& form,
3218 const GURL& source_url,
3219 DialogType dialog_type,
3220 const base::Callback<void(const FormStructure*,
3221 const std::string&)>& callback) OVERRIDE {
3222 callback.Run(user_supplied_data_.get(), "google_transaction_id");
3223 }
3224
3225 void SetUserSuppliedData(scoped_ptr<FormStructure> user_supplied_data) {
3226 user_supplied_data_.reset(user_supplied_data.release());
3227 }
3228
3229 bool autocheckout_bubble_shown() const {
3230 return autocheckout_bubble_shown_;
3231 }
3232
3196 private: 3233 private:
3197 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate); 3234 bool autocheckout_bubble_shown_;
3235 scoped_ptr<FormStructure> user_supplied_data_;
3236
3237 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate);
3198 }; 3238 };
3199 3239
3200 class MockAutofillExternalDelegate : public AutofillExternalDelegate { 3240 class MockAutofillExternalDelegate : public AutofillExternalDelegate {
3201 public: 3241 public:
3202 explicit MockAutofillExternalDelegate(content::WebContents* web_contents, 3242 explicit MockAutofillExternalDelegate(content::WebContents* web_contents,
3203 AutofillManager* autofill_manager) 3243 AutofillManager* autofill_manager)
3204 : AutofillExternalDelegate(web_contents, autofill_manager) {} 3244 : AutofillExternalDelegate(web_contents, autofill_manager) {}
3205 virtual ~MockAutofillExternalDelegate() {} 3245 virtual ~MockAutofillExternalDelegate() {}
3206 3246
3207 MOCK_METHOD5(OnQuery, void(int query_id, 3247 MOCK_METHOD5(OnQuery, void(int query_id,
3208 const FormData& form, 3248 const FormData& form,
3209 const FormFieldData& field, 3249 const FormFieldData& field,
3210 const gfx::RectF& bounds, 3250 const gfx::RectF& bounds,
3211 bool display_warning)); 3251 bool display_warning));
3212 3252
3213 private: 3253 private:
3214 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate); 3254 DISALLOW_COPY_AND_ASSIGN(MockAutofillExternalDelegate);
3215 }; 3255 };
3216 3256
3217 } // namespace 3257 } // namespace
3218 3258
3219 // Test that Autocheckout bubble is offered when server specifies field types. 3259 // Test that Autocheckout bubble is offered when server specifies field types.
3220 TEST_F(AutofillManagerTest, TestBubbleShown) { 3260 TEST_F(AutofillManagerTest, TestBubbleShown) {
3221 MockAutofillManagerDelegate delegate; 3261 MockAutofillManagerDelegate delegate;
3222 autofill_manager_.reset(new TestAutofillManager( 3262 autofill_manager_.reset(new TestAutofillManager(
3223 autofill_driver_.get(), &delegate, &personal_data_)); 3263 autofill_driver_.get(), &delegate, &personal_data_));
3224 autofill_manager_->set_autofill_enabled(true); 3264 autofill_manager_->set_autofill_enabled(true);
3225 autofill_manager_->MarkAsFirstPageInAutocheckoutFlow(); 3265 autofill_manager_->MarkAsFirstPageInAutocheckoutFlow();
3226 3266
3227 EXPECT_CALL(delegate, ShowAutocheckoutBubble(_, _, _));
3228
3229 FormData form; 3267 FormData form;
3230 CreateTestAddressFormData(&form); 3268 CreateTestAddressFormData(&form);
3231 3269
3232 TestFormStructure* form_structure = new TestFormStructure(form); 3270 TestFormStructure* form_structure = new TestFormStructure(form);
3233 AutofillMetrics metrics_logger; // ignored 3271 AutofillMetrics metrics_logger; // ignored
3234 form_structure->DetermineHeuristicTypes(metrics_logger); 3272 form_structure->DetermineHeuristicTypes(metrics_logger);
3235 3273
3236 // Build and add form structure with server data. 3274 // Build and add form structure with server data.
3237 std::vector<AutofillFieldType> heuristic_types, server_types; 3275 std::vector<AutofillFieldType> heuristic_types, server_types;
3238 for (size_t i = 0; i < form.fields.size(); ++i) { 3276 for (size_t i = 0; i < form.fields.size(); ++i) {
3239 heuristic_types.push_back(UNKNOWN_TYPE); 3277 heuristic_types.push_back(UNKNOWN_TYPE);
3240 server_types.push_back(form_structure->field(i)->type()); 3278 server_types.push_back(form_structure->field(i)->type());
3241 } 3279 }
3242 form_structure->SetFieldTypes(heuristic_types, server_types); 3280 form_structure->SetFieldTypes(heuristic_types, server_types);
3243 autofill_manager_->AddSeenForm(form_structure); 3281 autofill_manager_->AddSeenForm(form_structure);
3244 3282
3245 autofill_manager_->OnMaybeShowAutocheckoutBubble(form, gfx::RectF()); 3283 autofill_manager_->OnMaybeShowAutocheckoutBubble(form, gfx::RectF());
3284
3285 ASSERT_TRUE(delegate.autocheckout_bubble_shown());
Ilya Sherman 2013/06/19 23:25:13 nit: EXPECT_TRUE
Dane Wallinga 2013/06/20 19:46:41 Done.
3246 } 3286 }
3247 3287
3248 // Test that Autocheckout bubble is not offered when server doesn't have data 3288 // Test that Autocheckout bubble is not offered when server doesn't have data
3249 // for the form. 3289 // for the form.
3250 TEST_F(AutofillManagerTest, TestAutocheckoutBubbleNotShown) { 3290 TEST_F(AutofillManagerTest, TestAutocheckoutBubbleNotShown) {
3251 MockAutofillManagerDelegate delegate; 3291 MockAutofillManagerDelegate delegate;
3252 autofill_manager_.reset(new TestAutofillManager( 3292 autofill_manager_.reset(new TestAutofillManager(
3253 autofill_driver_.get(), &delegate, &personal_data_)); 3293 autofill_driver_.get(), &delegate, &personal_data_));
3254 autofill_manager_->set_autofill_enabled(true); 3294 autofill_manager_->set_autofill_enabled(true);
3255 autofill_manager_->MarkAsFirstPageInAutocheckoutFlow(); 3295 autofill_manager_->MarkAsFirstPageInAutocheckoutFlow();
3256 3296
3257 FormData form; 3297 FormData form;
3258 CreateTestAddressFormData(&form); 3298 CreateTestAddressFormData(&form);
3259 3299
3260 TestFormStructure* form_structure = new TestFormStructure(form); 3300 TestFormStructure* form_structure = new TestFormStructure(form);
3261 AutofillMetrics metrics_logger; // ignored 3301 AutofillMetrics metrics_logger; // ignored
3262 form_structure->DetermineHeuristicTypes(metrics_logger); 3302 form_structure->DetermineHeuristicTypes(metrics_logger);
3263 3303
3264 // Build form structure without server data. 3304 // Build form structure without server data.
3265 std::vector<AutofillFieldType> heuristic_types, server_types; 3305 std::vector<AutofillFieldType> heuristic_types, server_types;
3266 for (size_t i = 0; i < form.fields.size(); ++i) { 3306 for (size_t i = 0; i < form.fields.size(); ++i) {
3267 heuristic_types.push_back(form_structure->field(i)->type()); 3307 heuristic_types.push_back(form_structure->field(i)->type());
3268 server_types.push_back(NO_SERVER_DATA); 3308 server_types.push_back(NO_SERVER_DATA);
3269 } 3309 }
3270 form_structure->SetFieldTypes(heuristic_types, server_types); 3310 form_structure->SetFieldTypes(heuristic_types, server_types);
3271 autofill_manager_->AddSeenForm(form_structure); 3311 autofill_manager_->AddSeenForm(form_structure);
3272 3312
3273 autofill_manager_->OnMaybeShowAutocheckoutBubble(form, gfx::RectF()); 3313 autofill_manager_->OnMaybeShowAutocheckoutBubble(form, gfx::RectF());
3314
3315 ASSERT_FALSE(delegate.autocheckout_bubble_shown());
Ilya Sherman 2013/06/19 23:25:13 nit: EXPECT_FALSE
Dane Wallinga 2013/06/20 19:46:41 Done.
3274 } 3316 }
3275 3317
3276 // Test our external delegate is called at the right time. 3318 // Test our external delegate is called at the right time.
3277 TEST_F(AutofillManagerTest, TestExternalDelegate) { 3319 TEST_F(AutofillManagerTest, TestExternalDelegate) {
3278 MockAutofillExternalDelegate external_delegate(web_contents(), 3320 MockAutofillExternalDelegate external_delegate(web_contents(),
3279 autofill_manager_.get()); 3321 autofill_manager_.get());
3280 EXPECT_CALL(external_delegate, OnQuery(_, _, _, _, _)); 3322 EXPECT_CALL(external_delegate, OnQuery(_, _, _, _, _));
3281 autofill_manager_->SetExternalDelegate(&external_delegate); 3323 autofill_manager_->SetExternalDelegate(&external_delegate);
3282 3324
3283 FormData form; 3325 FormData form;
3284 CreateTestAddressFormData(&form); 3326 CreateTestAddressFormData(&form);
3285 std::vector<FormData> forms(1, form); 3327 std::vector<FormData> forms(1, form);
3286 FormsSeen(forms); 3328 FormsSeen(forms);
3287 const FormFieldData& field = form.fields[0]; 3329 const FormFieldData& field = form.fields[0];
3288 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() 3330 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
3289 3331
3290 autofill_manager_->SetExternalDelegate(NULL); 3332 autofill_manager_->SetExternalDelegate(NULL);
3291 } 3333 }
3292 3334
3335 // Test that in the case of Autocheckout, forms seen are in order supplied.
3336 TEST_F(AutofillManagerTest, DynamicFormsSeenAndIgnored) {
3337 MockAutofillManagerDelegate delegate;
3338 autofill_manager_.reset(new TestAutofillManager(
3339 autofill_driver_.get(), &delegate, &personal_data_));
3340 FormData shipping_options;
3341 CreateTestShippingOptionsFormData(&shipping_options);
3342 FormData user_supplied;
3343 CreateTestFormWithAutocompleteAttribute(&user_supplied);
3344 FormData address;
3345 CreateTestAddressFormData(&address);
3346
3347 autofill_manager_->set_autocheckout_url_prefix("test-prefix");
3348 // Push address only
3349 std::vector<FormData> forms;
3350 forms.push_back(address);
3351
3352 // Build and add form structure with server data.
3353 TestFormStructure* form_structure = new TestFormStructure(address);
Ilya Sherman 2013/06/19 23:25:13 nit: Wrap this in a scoped_ptr.
Dane Wallinga 2013/06/20 19:46:41 You sure? A raw pointer is used elsewhere in this
Ilya Sherman 2013/06/21 05:10:14 The preferred style is to wrap all new calls in sc
3354 std::vector<AutofillFieldType> heuristic_types, server_types;
3355 for (size_t i = 0; i < address.fields.size(); ++i) {
3356 heuristic_types.push_back(UNKNOWN_TYPE);
3357 server_types.push_back(form_structure->field(i)->type());
3358 }
3359 form_structure->SetFieldTypes(heuristic_types, server_types);
3360 autofill_manager_->AddSeenForm(form_structure);
3361
3362 // Make sure normal form is handled correctly.
3363 // FormsSeen(forms);
Ilya Sherman 2013/06/19 23:25:13 Why doesn't this line get to join the party?
Dane Wallinga 2013/06/20 19:46:41 heh, oops.
3364 autofill_manager_->MarkAsFirstPageInAutocheckoutFlowIgnoringAjax();
3365 std::vector<FormStructure*> form_structures;
3366 form_structures = autofill_manager_->GetFormStructures();
3367 ASSERT_EQ(1U, form_structures.size());
3368 EXPECT_EQ("/form.html", form_structures[0]->source_url().path());
3369
3370 scoped_ptr<FormStructure> filled_form(new TestFormStructure(address));
3371 delegate.SetUserSuppliedData(filled_form.Pass());
3372 autofill_manager_->OnMaybeShowAutocheckoutBubble(address, gfx::RectF());
3373
3374 // Push other forms
3375 forms.push_back(shipping_options);
3376 forms.push_back(user_supplied);
3377
3378 // FormStructure should contain the same forms as before.
3379 DynamicFormsSeen(forms);
3380 form_structures = autofill_manager_->GetFormStructures();
3381 ASSERT_EQ(1U, form_structures.size());
3382 EXPECT_EQ("/form.html", form_structures[0]->source_url().path());
3383 }
3384
3293 } // namespace autofill 3385 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698