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 "base/guid.h" | 5 #include "base/guid.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" | 10 #include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h" |
11 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" | 11 #include "chrome/browser/ui/autofill/autofill_dialog_view.h" |
12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
13 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
14 #include "components/autofill/browser/autofill_common_test.h" | 14 #include "components/autofill/browser/autofill_common_test.h" |
15 #include "components/autofill/browser/autofill_metrics.h" | 15 #include "components/autofill/browser/autofill_metrics.h" |
| 16 #include "components/autofill/browser/risk/proto/fingerprint.pb.h" |
16 #include "components/autofill/browser/test_personal_data_manager.h" | 17 #include "components/autofill/browser/test_personal_data_manager.h" |
17 #include "components/autofill/browser/wallet/full_wallet.h" | 18 #include "components/autofill/browser/wallet/full_wallet.h" |
18 #include "components/autofill/browser/wallet/instrument.h" | 19 #include "components/autofill/browser/wallet/instrument.h" |
19 #include "components/autofill/browser/wallet/wallet_address.h" | 20 #include "components/autofill/browser/wallet/wallet_address.h" |
20 #include "components/autofill/browser/wallet/wallet_client.h" | 21 #include "components/autofill/browser/wallet/wallet_client.h" |
21 #include "components/autofill/browser/wallet/wallet_test_util.h" | 22 #include "components/autofill/browser/wallet/wallet_test_util.h" |
22 #include "components/autofill/common/form_data.h" | 23 #include "components/autofill/common/form_data.h" |
23 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
24 #include "content/public/test/test_browser_thread.h" | 25 #include "content/public/test/test_browser_thread.h" |
25 #include "content/public/test/web_contents_tester.h" | 26 #include "content/public/test/web_contents_tester.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 173 } |
173 | 174 |
174 TestPersonalDataManager* GetTestingManager() { | 175 TestPersonalDataManager* GetTestingManager() { |
175 return &test_manager_; | 176 return &test_manager_; |
176 } | 177 } |
177 | 178 |
178 TestWalletClient* GetTestingWalletClient() { | 179 TestWalletClient* GetTestingWalletClient() { |
179 return &test_wallet_client_; | 180 return &test_wallet_client_; |
180 } | 181 } |
181 | 182 |
| 183 MOCK_METHOD0(LoadRiskFingerprintData, void()); |
| 184 |
182 void set_is_first_run(bool is_first_run) { is_first_run_ = is_first_run; } | 185 void set_is_first_run(bool is_first_run) { is_first_run_ = is_first_run; } |
| 186 void set_fingerprint_data(const std::string& fingerprint_data) { |
| 187 fingerprint_data_ = fingerprint_data; |
| 188 } |
183 | 189 |
184 protected: | 190 protected: |
185 virtual PersonalDataManager* GetManager() OVERRIDE { | 191 virtual PersonalDataManager* GetManager() OVERRIDE { |
186 return &test_manager_; | 192 return &test_manager_; |
187 } | 193 } |
188 | 194 |
189 virtual wallet::WalletClient* GetWalletClient() OVERRIDE { | 195 virtual wallet::WalletClient* GetWalletClient() OVERRIDE { |
190 return &test_wallet_client_; | 196 return &test_wallet_client_; |
191 } | 197 } |
192 | 198 |
193 virtual bool IsFirstRun() const OVERRIDE { | 199 virtual bool IsFirstRun() const OVERRIDE { |
194 return is_first_run_; | 200 return is_first_run_; |
195 } | 201 } |
196 | 202 |
| 203 virtual void SerializeFingerprint(risk::Fingerprint* fingerprint, |
| 204 std::string* data) { |
| 205 // Ignore |fingerprint|, it's NULL. |
| 206 data->assign(fingerprint_data_); |
| 207 } |
| 208 |
197 private: | 209 private: |
198 // To specify our own metric logger. | 210 // To specify our own metric logger. |
199 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { | 211 virtual const AutofillMetrics& GetMetricLogger() const OVERRIDE { |
200 return metric_logger_; | 212 return metric_logger_; |
201 } | 213 } |
202 | 214 |
203 const AutofillMetrics& metric_logger_; | 215 const AutofillMetrics& metric_logger_; |
204 TestPersonalDataManager test_manager_; | 216 TestPersonalDataManager test_manager_; |
205 testing::NiceMock<TestWalletClient> test_wallet_client_; | 217 testing::NiceMock<TestWalletClient> test_wallet_client_; |
206 bool is_first_run_; | 218 bool is_first_run_; |
| 219 std::string fingerprint_data_; |
207 | 220 |
208 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); | 221 DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController); |
209 }; | 222 }; |
210 | 223 |
211 class AutofillDialogControllerTest : public testing::Test { | 224 class AutofillDialogControllerTest : public testing::Test { |
212 public: | 225 public: |
213 AutofillDialogControllerTest() | 226 AutofillDialogControllerTest() |
214 : ui_thread_(BrowserThread::UI, &loop_), | 227 : ui_thread_(BrowserThread::UI, &loop_), |
215 file_thread_(BrowserThread::FILE), | 228 file_thread_(BrowserThread::FILE), |
216 file_blocking_thread_(BrowserThread::FILE_USER_BLOCKING), | 229 file_blocking_thread_(BrowserThread::FILE_USER_BLOCKING), |
(...skipping 14 matching lines...) Expand all Loading... |
231 form_data.fields.push_back(field); | 244 form_data.fields.push_back(field); |
232 } | 245 } |
233 | 246 |
234 profile()->CreateRequestContext(); | 247 profile()->CreateRequestContext(); |
235 test_web_contents_.reset( | 248 test_web_contents_.reset( |
236 content::WebContentsTester::CreateTestWebContents(profile(), NULL)); | 249 content::WebContentsTester::CreateTestWebContents(profile(), NULL)); |
237 | 250 |
238 base::Callback<void(const FormStructure*, const std::string&)> callback = | 251 base::Callback<void(const FormStructure*, const std::string&)> callback = |
239 base::Bind(&AutofillDialogControllerTest::FinishedCallback, | 252 base::Bind(&AutofillDialogControllerTest::FinishedCallback, |
240 base::Unretained(this)); | 253 base::Unretained(this)); |
241 controller_ = (new TestAutofillDialogController( | 254 controller_ = (new testing::NiceMock<TestAutofillDialogController>( |
242 test_web_contents_.get(), | 255 test_web_contents_.get(), |
243 form_data, | 256 form_data, |
244 GURL(), | 257 GURL(), |
245 metric_logger_, | 258 metric_logger_, |
246 DIALOG_TYPE_REQUEST_AUTOCOMPLETE, | 259 DIALOG_TYPE_REQUEST_AUTOCOMPLETE, |
247 callback))->AsWeakPtr(); | 260 callback))->AsWeakPtr(); |
248 controller_->Init(profile()); | 261 controller_->Init(profile()); |
249 controller_->Show(); | 262 controller_->Show(); |
250 controller_->OnUserNameFetchSuccess(kFakeEmail); | 263 controller_->OnUserNameFetchSuccess(kFakeEmail); |
251 } | 264 } |
(...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
935 controller()->EditCancelledForSection(SECTION_EMAIL); | 948 controller()->EditCancelledForSection(SECTION_EMAIL); |
936 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); | 949 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); |
937 | 950 |
938 controller()->MenuModelForSection(SECTION_EMAIL)->ActivatedAt(1); | 951 controller()->MenuModelForSection(SECTION_EMAIL)->ActivatedAt(1); |
939 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome()); | 952 EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome()); |
940 | 953 |
941 profile()->set_incognito(true); | 954 profile()->set_incognito(true); |
942 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); | 955 EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome()); |
943 } | 956 } |
944 | 957 |
| 958 TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) { |
| 959 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0); |
| 960 |
| 961 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
| 962 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); |
| 963 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
| 964 |
| 965 EXPECT_EQ("risky business", controller()->GetRiskData()); |
| 966 } |
| 967 |
| 968 TEST_F(AutofillDialogControllerTest, RiskLoadsWithoutPendingLegalDocuments) { |
| 969 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(1); |
| 970 controller()->set_fingerprint_data("goodbye"); |
| 971 |
| 972 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
| 973 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
| 974 // No legal Documents are present, |legal_documents_are_current_| should be |
| 975 // true and |LoadRiskFingerprintData()| should have been called. |
| 976 |
| 977 // |OnRiskFingerprintDataLoaded()| hasn't happened yet, |risk_data_| should be |
| 978 // empty still (and the default, "risky business", should be returned). |
| 979 EXPECT_EQ("risky business", controller()->GetRiskData()); |
| 980 |
| 981 // Now simulate a risk load success and verify that it's base64 encoded. |
| 982 controller()->OnDidLoadRiskFingerprintData(scoped_ptr<risk::Fingerprint>()); |
| 983 EXPECT_EQ("Z29vZGJ5ZQ==", controller()->GetRiskData()); |
| 984 } |
| 985 |
| 986 TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) { |
| 987 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0); |
| 988 controller()->set_fingerprint_data("hello"); |
| 989 |
| 990 scoped_ptr<wallet::WalletItems> wallet_items = wallet::GetTestWalletItems(); |
| 991 wallet_items->AddLegalDocument(wallet::GetTestLegalDocument()); |
| 992 controller()->OnDidGetWalletItems(wallet_items.Pass()); |
| 993 // |legal_documents_are_current_| should be false as there's a legal document. |
| 994 |
| 995 // If, for some reason, risk is loaded before |legal_documents_are_current_| |
| 996 // is true, it should not take effect (and the default should be returned). |
| 997 controller()->OnDidLoadRiskFingerprintData(scoped_ptr<risk::Fingerprint>()); |
| 998 EXPECT_EQ("risky business", controller()->GetRiskData()); |
| 999 |
| 1000 testing::Mock::VerifyAndClear(controller()); |
| 1001 EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(1); |
| 1002 |
| 1003 controller()->OnDidAcceptLegalDocuments(); |
| 1004 // Now |legal_documents_are_current_| should be true. |
| 1005 |
| 1006 // No |OnRiskFingerprintDataLoaded()| yet, |risk_data_| is still empty. |
| 1007 EXPECT_EQ("risky business", controller()->GetRiskData()); |
| 1008 |
| 1009 // Now simulate a successful risk load and verify that it's base64 encoded. |
| 1010 controller()->OnDidLoadRiskFingerprintData(scoped_ptr<risk::Fingerprint>()); |
| 1011 EXPECT_EQ("aGVsbG8=", controller()->GetRiskData()); |
| 1012 } |
| 1013 |
945 } // namespace autofill | 1014 } // namespace autofill |
OLD | NEW |