| OLD | NEW |
| 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 <vector> | 6 #include <vector> |
| 6 | 7 |
| 7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 10 #include "base/string16.h" | 11 #include "base/string16.h" |
| 11 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 12 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 13 #include "base/time.h" | 14 #include "base/time.h" |
| 14 #include "base/tuple.h" | 15 #include "base/tuple.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 32 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 33 #include "chrome/test/base/testing_profile.h" | 34 #include "chrome/test/base/testing_profile.h" |
| 34 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 35 #include "content/test/mock_render_process_host.h" | 36 #include "content/test/mock_render_process_host.h" |
| 36 #include "content/test/test_browser_thread.h" | 37 #include "content/test/test_browser_thread.h" |
| 37 #include "googleurl/src/gurl.h" | 38 #include "googleurl/src/gurl.h" |
| 38 #include "grit/generated_resources.h" | 39 #include "grit/generated_resources.h" |
| 39 #include "ipc/ipc_test_sink.h" | 40 #include "ipc/ipc_test_sink.h" |
| 40 #include "testing/gmock/include/gmock/gmock.h" | 41 #include "testing/gmock/include/gmock/gmock.h" |
| 41 #include "testing/gtest/include/gtest/gtest.h" | 42 #include "testing/gtest/include/gtest/gtest.h" |
| 43 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| 42 #include "ui/base/l10n/l10n_util.h" | 44 #include "ui/base/l10n/l10n_util.h" |
| 43 #include "ui/gfx/rect.h" | 45 #include "ui/gfx/rect.h" |
| 44 #include "webkit/forms/form_data.h" | 46 #include "webkit/forms/form_data.h" |
| 45 #include "webkit/forms/form_field.h" | 47 #include "webkit/forms/form_field.h" |
| 46 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | |
| 47 | 48 |
| 48 using content::BrowserThread; | 49 using content::BrowserThread; |
| 49 using content::WebContents; | 50 using content::WebContents; |
| 50 using testing::_; | 51 using testing::_; |
| 51 using webkit::forms::FormData; | 52 using webkit::forms::FormData; |
| 52 using webkit::forms::FormField; | 53 using webkit::forms::FormField; |
| 53 | 54 |
| 54 namespace { | 55 namespace { |
| 55 | 56 |
| 56 // The page ID sent to the AutofillManager from the RenderView, used to send | 57 // The page ID sent to the AutofillManager from the RenderView, used to send |
| (...skipping 22 matching lines...) Expand all Loading... |
| 79 | 80 |
| 80 AutofillProfile* GetProfileWithGUID(const char* guid) { | 81 AutofillProfile* GetProfileWithGUID(const char* guid) { |
| 81 for (std::vector<AutofillProfile *>::iterator it = web_profiles_.begin(); | 82 for (std::vector<AutofillProfile *>::iterator it = web_profiles_.begin(); |
| 82 it != web_profiles_.end(); ++it) { | 83 it != web_profiles_.end(); ++it) { |
| 83 if (!(*it)->guid().compare(guid)) | 84 if (!(*it)->guid().compare(guid)) |
| 84 return *it; | 85 return *it; |
| 85 } | 86 } |
| 86 return NULL; | 87 return NULL; |
| 87 } | 88 } |
| 88 | 89 |
| 90 CreditCard* GetCreditCardWithGUID(const char* guid) { |
| 91 for (std::vector<CreditCard *>::iterator it = credit_cards_.begin(); |
| 92 it != credit_cards_.end(); ++it){ |
| 93 if (!(*it)->guid().compare(guid)) |
| 94 return *it; |
| 95 } |
| 96 return NULL; |
| 97 } |
| 98 |
| 89 void AddProfile(AutofillProfile* profile) { | 99 void AddProfile(AutofillProfile* profile) { |
| 90 web_profiles_->push_back(profile); | 100 web_profiles_->push_back(profile); |
| 91 } | 101 } |
| 92 | 102 |
| 93 void AddCreditCard(CreditCard* credit_card) { | 103 void AddCreditCard(CreditCard* credit_card) { |
| 94 credit_cards_->push_back(credit_card); | 104 credit_cards_->push_back(credit_card); |
| 95 } | 105 } |
| 96 | 106 |
| 107 virtual void RemoveProfile(const std::string& guid) OVERRIDE { |
| 108 AutofillProfile* profile = GetProfileWithGUID(guid.c_str()); |
| 109 |
| 110 web_profiles_.erase( |
| 111 std::remove(web_profiles_.begin(), web_profiles_.end(), profile), |
| 112 web_profiles_.end()); |
| 113 } |
| 114 |
| 115 virtual void RemoveCreditCard(const std::string& guid) OVERRIDE { |
| 116 CreditCard* credit_card = GetCreditCardWithGUID(guid.c_str()); |
| 117 |
| 118 credit_cards_.erase( |
| 119 std::remove(credit_cards_.begin(), credit_cards_.end(), credit_card), |
| 120 credit_cards_.end()); |
| 121 } |
| 122 |
| 97 void ClearAutofillProfiles() { | 123 void ClearAutofillProfiles() { |
| 98 web_profiles_.reset(); | 124 web_profiles_.reset(); |
| 99 } | 125 } |
| 100 | 126 |
| 101 void ClearCreditCards() { | 127 void ClearCreditCards() { |
| 102 credit_cards_.reset(); | 128 credit_cards_.reset(); |
| 103 } | 129 } |
| 104 | 130 |
| 105 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) { | 131 void CreateTestCreditCardsYearAndMonth(const char* year, const char* month) { |
| 106 ClearCreditCards(); | 132 ClearCreditCards(); |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 } | 528 } |
| 503 | 529 |
| 504 const std::string GetSubmittedFormSignature() { | 530 const std::string GetSubmittedFormSignature() { |
| 505 return submitted_form_signature_; | 531 return submitted_form_signature_; |
| 506 } | 532 } |
| 507 | 533 |
| 508 AutofillProfile* GetProfileWithGUID(const char* guid) { | 534 AutofillProfile* GetProfileWithGUID(const char* guid) { |
| 509 return personal_data_->GetProfileWithGUID(guid); | 535 return personal_data_->GetProfileWithGUID(guid); |
| 510 } | 536 } |
| 511 | 537 |
| 538 CreditCard* GetCreditCardWithGUID(const char* guid) { |
| 539 return personal_data_->GetCreditCardWithGUID(guid); |
| 540 } |
| 541 |
| 512 void AddProfile(AutofillProfile* profile) { | 542 void AddProfile(AutofillProfile* profile) { |
| 513 personal_data_->AddProfile(profile); | 543 personal_data_->AddProfile(profile); |
| 514 } | 544 } |
| 515 | 545 |
| 516 void AddCreditCard(CreditCard* credit_card) { | 546 void AddCreditCard(CreditCard* credit_card) { |
| 517 personal_data_->AddCreditCard(credit_card); | 547 personal_data_->AddCreditCard(credit_card); |
| 518 } | 548 } |
| 519 | 549 |
| 520 int GetPackedCreditCardID(int credit_card_id) { | 550 int GetPackedCreditCardID(int credit_card_id) { |
| 521 std::string credit_card_guid = | 551 std::string credit_card_guid = |
| (...skipping 2414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2936 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size()); | 2966 EXPECT_EQ(0u, autofill_manager_->GetSentStates().size()); |
| 2937 | 2967 |
| 2938 // When a new render_view is created, we send the state even if it's the | 2968 // When a new render_view is created, we send the state even if it's the |
| 2939 // same. | 2969 // same. |
| 2940 UpdatePasswordGenerationState(true); | 2970 UpdatePasswordGenerationState(true); |
| 2941 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size()); | 2971 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size()); |
| 2942 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]); | 2972 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]); |
| 2943 autofill_manager_->ClearSentStates(); | 2973 autofill_manager_->ClearSentStates(); |
| 2944 } | 2974 } |
| 2945 | 2975 |
| 2976 TEST_F(AutofillManagerTest, RemoveProfile) { |
| 2977 // Add and remove an Autofill profile. |
| 2978 AutofillProfile* profile = new AutofillProfile; |
| 2979 std::string guid = "00000000-0000-0000-0000-000000000102"; |
| 2980 profile->set_guid(guid.c_str()); |
| 2981 autofill_manager_->AddProfile(profile); |
| 2982 |
| 2983 GUIDPair guid_pair(guid, 0); |
| 2984 GUIDPair empty(std::string(), 0); |
| 2985 int id = PackGUIDs(empty, guid_pair); |
| 2986 |
| 2987 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); |
| 2988 |
| 2989 EXPECT_FALSE(autofill_manager_->GetProfileWithGUID(guid.c_str())); |
| 2990 } |
| 2991 |
| 2992 TEST_F(AutofillManagerTest, RemoveCreditCard){ |
| 2993 // Add and remove an Autofill credit card. |
| 2994 CreditCard* credit_card = new CreditCard; |
| 2995 std::string guid = "00000000-0000-0000-0000-000000100007"; |
| 2996 credit_card->set_guid(guid.c_str()); |
| 2997 autofill_manager_->AddCreditCard(credit_card); |
| 2998 |
| 2999 GUIDPair guid_pair(guid, 0); |
| 3000 GUIDPair empty(std::string(), 0); |
| 3001 int id = PackGUIDs(guid_pair, empty); |
| 3002 |
| 3003 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); |
| 3004 |
| 3005 EXPECT_FALSE(autofill_manager_->GetCreditCardWithGUID(guid.c_str())); |
| 3006 } |
| 3007 |
| 3008 TEST_F(AutofillManagerTest, RemoveProfileVariant) { |
| 3009 // Add and remove an Autofill profile. |
| 3010 AutofillProfile* profile = new AutofillProfile; |
| 3011 std::string guid = "00000000-0000-0000-0000-000000000102"; |
| 3012 profile->set_guid(guid.c_str()); |
| 3013 autofill_manager_->AddProfile(profile); |
| 3014 |
| 3015 GUIDPair guid_pair(guid, 1); |
| 3016 GUIDPair empty(std::string(), 0); |
| 3017 int id = PackGUIDs(empty, guid_pair); |
| 3018 |
| 3019 autofill_manager_->RemoveAutofillProfileOrCreditCard(id); |
| 3020 |
| 3021 // TODO(csharp): Currently variants should not be deleted, but once they are |
| 3022 // update these expectations. |
| 3023 // http://crbug.com/124211 |
| 3024 EXPECT_TRUE(autofill_manager_->GetProfileWithGUID(guid.c_str())); |
| 3025 } |
| 3026 |
| 2946 namespace { | 3027 namespace { |
| 2947 | 3028 |
| 2948 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { | 3029 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { |
| 2949 public: | 3030 public: |
| 2950 explicit MockAutofillExternalDelegate(TabContentsWrapper* wrapper, | 3031 explicit MockAutofillExternalDelegate(TabContentsWrapper* wrapper, |
| 2951 AutofillManager* autofill_manager) | 3032 AutofillManager* autofill_manager) |
| 2952 : TestAutofillExternalDelegate(wrapper, autofill_manager) {} | 3033 : TestAutofillExternalDelegate(wrapper, autofill_manager) {} |
| 2953 virtual ~MockAutofillExternalDelegate() {} | 3034 virtual ~MockAutofillExternalDelegate() {} |
| 2954 | 3035 |
| 2955 MOCK_METHOD5(OnQuery, void(int query_id, | 3036 MOCK_METHOD5(OnQuery, void(int query_id, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3003 | 3084 |
| 3004 AutofillManager* autofill_manager = contents_wrapper()->autofill_manager(); | 3085 AutofillManager* autofill_manager = contents_wrapper()->autofill_manager(); |
| 3005 EXPECT_TRUE(autofill_manager->external_delegate()); | 3086 EXPECT_TRUE(autofill_manager->external_delegate()); |
| 3006 | 3087 |
| 3007 AutocompleteHistoryManager* autocomplete_history_manager = | 3088 AutocompleteHistoryManager* autocomplete_history_manager = |
| 3008 contents_wrapper()->autocomplete_history_manager(); | 3089 contents_wrapper()->autocomplete_history_manager(); |
| 3009 EXPECT_TRUE(autocomplete_history_manager->external_delegate()); | 3090 EXPECT_TRUE(autocomplete_history_manager->external_delegate()); |
| 3010 } | 3091 } |
| 3011 | 3092 |
| 3012 #endif // OS_ANDROID | 3093 #endif // OS_ANDROID |
| OLD | NEW |