Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/autofill/card_unmask_prompt_controller_impl.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/prefs/pref_service.h" | |
| 9 #include "base/strings/utf_string_conversions.cc" | |
| 10 #include "base/test/histogram_tester.h" | |
| 11 #include "chrome/browser/ui/autofill/card_unmask_prompt_view.h" | |
| 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | |
| 13 #include "components/autofill/core/browser/autofill_client.h" | |
| 14 #include "components/autofill/core/browser/autofill_metrics.h" | |
| 15 #include "components/autofill/core/browser/autofill_test_utils.h" | |
| 16 #include "components/autofill/core/common/autofill_pref_names.h" | |
| 17 #include "components/user_prefs/user_prefs.h" | |
| 18 #include "content/public/browser/web_contents.h" | |
| 19 #include "content/public/test/test_utils.h" | |
| 20 | |
| 21 namespace autofill { | |
| 22 | |
| 23 CreditCard CreateCard() { | |
| 
 
Evan Stade
2015/03/19 01:06:57
we should really put this in autofill_test_utils a
 
Walter Cacau
2015/03/19 01:20:41
Acknowledged.
 
 | |
| 24 CreditCard credit_card(CreditCard::MASKED_SERVER_CARD, "a123"); | |
| 25 test::SetCreditCardInfo(&credit_card, "Bonnie Parker", | |
| 26 "2109" /* Mastercard */, "12", "2012"); | |
| 27 credit_card.SetTypeForMaskedCard(kMasterCard); | |
| 28 return credit_card; | |
| 29 } | |
| 30 | |
| 31 class TestCardUnmaskDelegate : public CardUnmaskDelegate { | |
| 32 public: | |
| 33 TestCardUnmaskDelegate() : weak_factory_(this) {} | |
| 34 | |
| 35 virtual ~TestCardUnmaskDelegate() {} | |
| 36 | |
| 37 // CardUnmaskDelegate implementation. | |
| 38 void OnUnmaskResponse(const UnmaskResponse& response) override {} | |
| 39 void OnUnmaskPromptClosed() override {} | |
| 40 | |
| 41 base::WeakPtr<TestCardUnmaskDelegate> GetWeakPtr() { | |
| 42 return weak_factory_.GetWeakPtr(); | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 base::WeakPtrFactory<TestCardUnmaskDelegate> weak_factory_; | |
| 47 | |
| 48 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskDelegate); | |
| 49 }; | |
| 50 | |
| 51 class TestCardUnmaskPromptView : public CardUnmaskPromptView { | |
| 52 public: | |
| 53 void ControllerGone() override {} | |
| 54 void DisableAndWaitForVerification() override {} | |
| 55 void GotVerificationResult(const base::string16& error_message, | |
| 56 bool allow_retry) override {} | |
| 
 
Evan Stade
2015/03/19 01:06:57
is this the output of git cl format?
 
Walter Cacau
2015/03/19 01:20:41
nope, my editor messing with identation
 
 | |
| 57 }; | |
| 58 | |
| 59 class TestCardUnmaskPromptController : public CardUnmaskPromptControllerImpl { | |
| 60 public: | |
| 61 TestCardUnmaskPromptController( | |
| 62 content::WebContents* contents, | |
| 63 TestCardUnmaskPromptView* test_unmask_prompt_view, | |
| 64 scoped_refptr<content::MessageLoopRunner> runner) | |
| 65 : CardUnmaskPromptControllerImpl(contents), | |
| 66 test_unmask_prompt_view_(test_unmask_prompt_view), | |
| 67 runner_(runner), | |
| 68 weak_factory_(this) {} | |
| 69 | |
| 70 CardUnmaskPromptView* CreateAndShowView() override { | |
| 71 return test_unmask_prompt_view_; | |
| 72 } | |
| 73 void LoadRiskFingerprint() override {} | |
| 74 | |
| 75 base::WeakPtr<TestCardUnmaskPromptController> GetWeakPtr() { | |
| 76 return weak_factory_.GetWeakPtr(); | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 TestCardUnmaskPromptView* test_unmask_prompt_view_; | |
| 81 scoped_refptr<content::MessageLoopRunner> runner_; | |
| 82 base::WeakPtrFactory<TestCardUnmaskPromptController> weak_factory_; | |
| 83 | |
| 84 DISALLOW_COPY_AND_ASSIGN(TestCardUnmaskPromptController); | |
| 85 }; | |
| 86 | |
| 87 class CardUnmaskPromptControllerImplTest | |
| 88 : public ChromeRenderViewHostTestHarness { | |
| 89 public: | |
| 90 CardUnmaskPromptControllerImplTest() {} | |
| 91 ~CardUnmaskPromptControllerImplTest() override {} | |
| 92 | |
| 93 void SetUp() override { | |
| 94 ChromeRenderViewHostTestHarness::SetUp(); | |
| 95 test_unmask_prompt_view_.reset(new TestCardUnmaskPromptView()); | |
| 96 controller_.reset(new TestCardUnmaskPromptController( | |
| 97 web_contents(), test_unmask_prompt_view_.get(), runner_)); | |
| 98 delegate_.reset(new TestCardUnmaskDelegate()); | |
| 99 SetImportCheckboxState(false); | |
| 100 } | |
| 101 | |
| 102 void TearDown() override { | |
| 103 ChromeRenderViewHostTestHarness::TearDown(); | |
| 104 } | |
| 105 | |
| 106 TestCardUnmaskPromptController* controller() { return controller_.get(); } | |
| 107 TestCardUnmaskDelegate* delegate() { return delegate_.get(); } | |
| 108 | |
| 109 protected: | |
| 110 void SetImportCheckboxState(bool value) { | |
| 111 user_prefs::UserPrefs::Get(web_contents()->GetBrowserContext()) | |
| 112 ->SetBoolean(prefs::kAutofillWalletImportStorageCheckboxState, value); | |
| 113 } | |
| 114 | |
| 115 private: | |
| 116 // This member must outlive the controller. | |
| 117 scoped_refptr<content::MessageLoopRunner> runner_; | |
| 118 | |
| 119 scoped_ptr<TestCardUnmaskPromptView> test_unmask_prompt_view_; | |
| 120 scoped_ptr<TestCardUnmaskPromptController> controller_; | |
| 121 scoped_ptr<TestCardUnmaskDelegate> delegate_; | |
| 122 }; | |
| 123 | |
| 124 TEST_F(CardUnmaskPromptControllerImplTest, LogShown) { | |
| 125 base::HistogramTester histogram_tester; | |
| 126 | |
| 127 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 128 | |
| 129 histogram_tester.ExpectUniqueSample( | |
| 130 "Autofill.UnmaskPrompt.Events", | |
| 131 AutofillMetrics::UNMASK_PROMPT_SHOWN, 1); | |
| 132 } | |
| 133 | |
| 134 TEST_F(CardUnmaskPromptControllerImplTest, LogClosedNoAttempts) { | |
| 135 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 136 base::HistogramTester histogram_tester; | |
| 137 | |
| 138 controller()->OnUnmaskDialogClosed(); | |
| 139 | |
| 140 histogram_tester.ExpectBucketCount( | |
| 141 "Autofill.UnmaskPrompt.Events", | |
| 142 AutofillMetrics::UNMASK_PROMPT_CLOSED_NO_ATTEMPTS, 1); | |
| 143 } | |
| 144 | |
| 145 TEST_F(CardUnmaskPromptControllerImplTest, LogClosedFailedToUnmaskRetriable) { | |
| 146 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 147 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 148 base::ASCIIToUTF16("01"), | |
| 149 base::ASCIIToUTF16("2015"), | |
| 150 false /* should_store_pan */); | |
| 151 controller()->OnVerificationResult(AutofillClient::TRY_AGAIN_FAILURE); | |
| 152 base::HistogramTester histogram_tester; | |
| 153 | |
| 154 controller()->OnUnmaskDialogClosed(); | |
| 155 | |
| 156 histogram_tester.ExpectBucketCount( | |
| 157 "Autofill.UnmaskPrompt.Events", | |
| 158 AutofillMetrics | |
| 159 ::UNMASK_PROMPT_CLOSED_FAILED_TO_UNMASK_RETRIABLE_FAILURE, | |
| 160 1); | |
| 161 } | |
| 162 | |
| 163 TEST_F(CardUnmaskPromptControllerImplTest, LogClosedFailedToUnmaskNonRetriable) | |
| 164 { | |
| 165 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 166 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 167 base::ASCIIToUTF16("01"), | |
| 168 base::ASCIIToUTF16("2015"), | |
| 169 false /* should_store_pan */); | |
| 170 controller()->OnVerificationResult(AutofillClient::PERMANENT_FAILURE); | |
| 171 base::HistogramTester histogram_tester; | |
| 172 | |
| 173 controller()->OnUnmaskDialogClosed(); | |
| 174 | |
| 175 histogram_tester.ExpectBucketCount( | |
| 176 "Autofill.UnmaskPrompt.Events", | |
| 177 AutofillMetrics | |
| 178 ::UNMASK_PROMPT_CLOSED_FAILED_TO_UNMASK_NON_RETRIABLE_FAILURE, | |
| 179 1); | |
| 180 } | |
| 181 | |
| 182 TEST_F(CardUnmaskPromptControllerImplTest, LogUnmaskedCardFirstAttempt) { | |
| 183 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 184 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 185 base::ASCIIToUTF16("01"), | |
| 186 base::ASCIIToUTF16("2015"), | |
| 187 false /* should_store_pan */); | |
| 188 base::HistogramTester histogram_tester; | |
| 189 | |
| 190 controller()->OnVerificationResult(AutofillClient::SUCCESS); | |
| 191 controller()->OnUnmaskDialogClosed(); | |
| 192 | |
| 193 histogram_tester.ExpectBucketCount( | |
| 194 "Autofill.UnmaskPrompt.Events", | |
| 195 AutofillMetrics::UNMASK_PROMPT_UNMASKED_CARD_FIRST_ATTEMPT, 1); | |
| 196 } | |
| 197 | |
| 198 TEST_F(CardUnmaskPromptControllerImplTest, LogUnmaskedCardAfterFailure) { | |
| 199 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 200 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 201 base::ASCIIToUTF16("01"), | |
| 202 base::ASCIIToUTF16("2015"), | |
| 203 false /* should_store_pan */); | |
| 204 controller()->OnVerificationResult(AutofillClient::TRY_AGAIN_FAILURE); | |
| 205 controller()->OnUnmaskResponse(base::ASCIIToUTF16("444"), | |
| 206 base::ASCIIToUTF16("01"), | |
| 207 base::ASCIIToUTF16("2015"), | |
| 208 false /* should_store_pan */); | |
| 209 base::HistogramTester histogram_tester; | |
| 210 | |
| 211 controller()->OnVerificationResult(AutofillClient::SUCCESS); | |
| 212 controller()->OnUnmaskDialogClosed(); | |
| 213 | |
| 214 histogram_tester.ExpectBucketCount( | |
| 215 "Autofill.UnmaskPrompt.Events", | |
| 216 AutofillMetrics::UNMASK_PROMPT_UNMASKED_CARD_AFTER_FAILED_ATTEMPTS, 1); | |
| 217 } | |
| 218 | |
| 219 TEST_F(CardUnmaskPromptControllerImplTest, LogSavedCardLocally) { | |
| 220 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 221 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 222 base::ASCIIToUTF16("01"), | |
| 223 base::ASCIIToUTF16("2015"), | |
| 224 true /* should_store_pan */); | |
| 225 base::HistogramTester histogram_tester; | |
| 226 | |
| 227 controller()->OnVerificationResult(AutofillClient::SUCCESS); | |
| 228 controller()->OnUnmaskDialogClosed(); | |
| 229 | |
| 230 histogram_tester.ExpectBucketCount( | |
| 231 "Autofill.UnmaskPrompt.Events", | |
| 232 AutofillMetrics::UNMASK_PROMPT_SAVED_CARD_LOCALLY, 1); | |
| 233 } | |
| 234 | |
| 235 TEST_F(CardUnmaskPromptControllerImplTest, LogDidOptIn) { | |
| 236 SetImportCheckboxState(false); | |
| 237 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 238 base::HistogramTester histogram_tester; | |
| 239 | |
| 240 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 241 base::ASCIIToUTF16("01"), | |
| 242 base::ASCIIToUTF16("2015"), | |
| 243 true /* should_store_pan */); | |
| 244 controller()->OnUnmaskDialogClosed(); | |
| 245 | |
| 246 histogram_tester.ExpectBucketCount( | |
| 247 "Autofill.UnmaskPrompt.Events", | |
| 248 AutofillMetrics::UNMASK_PROMPT_LOCAL_SAVE_DID_OPT_IN, 1); | |
| 249 } | |
| 250 | |
| 251 TEST_F(CardUnmaskPromptControllerImplTest, LogDidNotOptIn) { | |
| 252 SetImportCheckboxState(false); | |
| 253 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 254 base::HistogramTester histogram_tester; | |
| 255 | |
| 256 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 257 base::ASCIIToUTF16("01"), | |
| 258 base::ASCIIToUTF16("2015"), | |
| 259 false /* should_store_pan */); | |
| 260 controller()->OnUnmaskDialogClosed(); | |
| 261 | |
| 262 histogram_tester.ExpectBucketCount( | |
| 263 "Autofill.UnmaskPrompt.Events", | |
| 264 AutofillMetrics::UNMASK_PROMPT_LOCAL_SAVE_DID_NOT_OPT_IN, 1); | |
| 265 } | |
| 266 | |
| 267 TEST_F(CardUnmaskPromptControllerImplTest, LogDidOptOut) { | |
| 268 SetImportCheckboxState(true); | |
| 269 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 270 base::HistogramTester histogram_tester; | |
| 271 | |
| 272 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 273 base::ASCIIToUTF16("01"), | |
| 274 base::ASCIIToUTF16("2015"), | |
| 275 false /* should_store_pan */); | |
| 276 controller()->OnUnmaskDialogClosed(); | |
| 277 | |
| 278 histogram_tester.ExpectBucketCount( | |
| 279 "Autofill.UnmaskPrompt.Events", | |
| 280 AutofillMetrics::UNMASK_PROMPT_LOCAL_SAVE_DID_OPT_OUT, 1); | |
| 281 } | |
| 282 | |
| 283 TEST_F(CardUnmaskPromptControllerImplTest, LogDidNotOptOut) { | |
| 284 SetImportCheckboxState(true); | |
| 285 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 286 base::HistogramTester histogram_tester; | |
| 287 | |
| 288 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 289 base::ASCIIToUTF16("01"), | |
| 290 base::ASCIIToUTF16("2015"), | |
| 291 true /* should_store_pan */); | |
| 292 controller()->OnUnmaskDialogClosed(); | |
| 293 | |
| 294 histogram_tester.ExpectBucketCount( | |
| 295 "Autofill.UnmaskPrompt.Events", | |
| 296 AutofillMetrics::UNMASK_PROMPT_LOCAL_SAVE_DID_NOT_OPT_OUT, 1); | |
| 297 } | |
| 298 | |
| 299 TEST_F(CardUnmaskPromptControllerImplTest, LogRealPanResultSuccess) { | |
| 300 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 301 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 302 base::ASCIIToUTF16("01"), | |
| 303 base::ASCIIToUTF16("2015"), | |
| 304 false /* should_store_pan */); | |
| 305 base::HistogramTester histogram_tester; | |
| 306 | |
| 307 controller()->OnVerificationResult(AutofillClient::SUCCESS); | |
| 308 | |
| 309 histogram_tester.ExpectBucketCount( | |
| 310 "Autofill.UnmaskPrompt.GetRealPanResult", | |
| 311 AutofillMetrics::GET_REAL_PAN_RESULT_SUCCESS, 1); | |
| 312 } | |
| 313 | |
| 314 TEST_F(CardUnmaskPromptControllerImplTest, LogRealPanTryAgainFailure) { | |
| 315 controller()->ShowPrompt(CreateCard(), delegate()->GetWeakPtr()); | |
| 316 controller()->OnUnmaskResponse(base::ASCIIToUTF16("123"), | |
| 317 base::ASCIIToUTF16("01"), | |
| 318 base::ASCIIToUTF16("2015"), | |
| 319 false /* should_store_pan */); | |
| 320 base::HistogramTester histogram_tester; | |
| 321 | |
| 322 controller()->OnVerificationResult(AutofillClient::TRY_AGAIN_FAILURE); | |
| 323 | |
| 324 histogram_tester.ExpectBucketCount( | |
| 325 "Autofill.UnmaskPrompt.GetRealPanResult", | |
| 326 AutofillMetrics::GET_REAL_PAN_RESULT_TRY_AGAIN_FAILURE, 1); | |
| 327 } | |
| 328 | |
| 329 } // namespace autofill | |
| OLD | NEW |