Commit 0d9d3758 authored by Jared Saul's avatar Jared Saul Committed by Commit Bot

Make Upstream unit tests a little more robust

Bug: 
Change-Id: Ib144cc6e0e13329aa346bf9bef4e272c06d2a06e
Reviewed-on: https://chromium-review.googlesource.com/569076
Commit-Queue: Jared Saul <jsaul@google.com>
Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487300}
parent cb4af30e
......@@ -18,6 +18,7 @@
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/autofill/core/browser/autofill_metrics.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/user_prefs/user_prefs.h"
......@@ -91,9 +92,11 @@ class SaveCardBubbleControllerImplTest : public BrowserWithTestWindowTest {
base::Bind(&SaveCardCallback));
}
void ShowLocalBubble() {
controller()->ShowBubbleForLocalSave(CreditCard(),
base::Bind(&SaveCardCallback));
void ShowLocalBubble(CreditCard* card = nullptr) {
controller()->ShowBubbleForLocalSave(
card ? CreditCard(*card)
: autofill::test::GetCreditCard(), // Visa by default
base::Bind(&SaveCardCallback));
}
void ShowUploadBubble(bool should_cvc_be_requested = false) {
......@@ -160,7 +163,7 @@ TEST_F(SaveCardBubbleControllerImplTest,
TEST_F(SaveCardBubbleControllerImplTest,
PropagateShouldRequestCvcFromUserWhenTrue) {
ShowUploadBubble(true /* should_cvc_be_requested */);
ShowUploadBubble(/*should_cvc_be_requested=*/true);
EXPECT_TRUE(controller()->ShouldRequestCvcFromUser());
}
......@@ -203,7 +206,7 @@ TEST_F(SaveCardBubbleControllerImplTest,
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_FirstShow_ShowBubble_RequestCvc) {
base::HistogramTester histogram_tester;
ShowUploadBubble(true /* should_cvc_be_requested */);
ShowUploadBubble(/*should_cvc_be_requested=*/true);
EXPECT_THAT(
histogram_tester.GetAllSamples(
......@@ -212,7 +215,8 @@ TEST_F(SaveCardBubbleControllerImplTest,
Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1)));
}
TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_Reshows_ShowBubble) {
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_Reshows_ShowBubble_NotRequestCvc) {
ShowUploadBubble();
base::HistogramTester histogram_tester;
......@@ -225,6 +229,20 @@ TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_Reshows_ShowBubble) {
Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1)));
}
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_Reshows_ShowBubble_RequestCvc) {
ShowUploadBubble(/*should_cvc_be_requested=*/true);
base::HistogramTester histogram_tester;
CloseAndReshowBubble();
EXPECT_THAT(
histogram_tester.GetAllSamples(
"Autofill.SaveCreditCardPrompt.Upload.Reshows"),
ElementsAre(Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOW_REQUESTED, 1),
Bucket(AutofillMetrics::SAVE_CARD_PROMPT_SHOWN, 1)));
}
TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_SaveButton) {
ShowLocalBubble();
......@@ -337,8 +355,17 @@ TEST_F(SaveCardBubbleControllerImplTest,
ShowLocalBubble();
base::HistogramTester histogram_tester;
// Fake-navigate after bubble has been visible for a long time.
controller()->set_elapsed(base::TimeDelta::FromMinutes(1));
// The bubble should still stick around for up to kSurviveNavigationSeconds
// (5) seconds regardless of navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(3));
controller()->SimulateNavigation();
histogram_tester.ExpectTotalCount(
"Autofill.SaveCreditCardPrompt.Local.FirstShow", 0);
// Wait 3 more seconds (6 total); bubble should go away on next navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(6));
controller()->SimulateNavigation();
......@@ -353,8 +380,18 @@ TEST_F(SaveCardBubbleControllerImplTest,
CloseAndReshowBubble();
base::HistogramTester histogram_tester;
// Fake-navigate after bubble has been visible for a long time.
controller()->set_elapsed(base::TimeDelta::FromMinutes(1));
// The bubble should still stick around for up to kSurviveNavigationSeconds
// (5) seconds regardless of navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(3));
controller()->SimulateNavigation();
histogram_tester.ExpectTotalCount(
"Autofill.SaveCreditCardPrompt.Local.Reshows", 0);
// Wait 3 more seconds (6 total); bubble should go away on next navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(6));
controller()->SimulateNavigation();
histogram_tester.ExpectUniqueSample(
......@@ -362,6 +399,55 @@ TEST_F(SaveCardBubbleControllerImplTest,
AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_SHOWING, 1);
}
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_FirstShow_NavigateWhileShowing) {
ShowUploadBubble();
base::HistogramTester histogram_tester;
// The bubble should still stick around for up to kSurviveNavigationSeconds
// (5) seconds regardless of navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(3));
controller()->SimulateNavigation();
histogram_tester.ExpectTotalCount(
"Autofill.SaveCreditCardPrompt.Upload.FirstShow", 0);
// Wait 3 more seconds (6 total); bubble should go away on next navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(6));
controller()->SimulateNavigation();
histogram_tester.ExpectUniqueSample(
"Autofill.SaveCreditCardPrompt.Upload.FirstShow",
AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_SHOWING, 1);
}
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_Reshows_NavigateWhileShowing) {
ShowUploadBubble();
CloseAndReshowBubble();
base::HistogramTester histogram_tester;
// The bubble should still stick around for up to kSurviveNavigationSeconds
// (5) seconds regardless of navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(3));
controller()->SimulateNavigation();
histogram_tester.ExpectTotalCount(
"Autofill.SaveCreditCardPrompt.Upload.Reshows", 0);
// Wait 3 more seconds (6 total); bubble should go away on next navigation.
controller()->set_elapsed(base::TimeDelta::FromSeconds(6));
controller()->SimulateNavigation();
histogram_tester.ExpectUniqueSample(
"Autofill.SaveCreditCardPrompt.Upload.Reshows",
AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_SHOWING, 1);
}
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Local_FirstShow_NavigateWhileHidden) {
ShowLocalBubble();
......@@ -393,6 +479,60 @@ TEST_F(SaveCardBubbleControllerImplTest,
AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_HIDDEN, 1);
}
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_FirstShow_NavigateWhileHidden) {
ShowUploadBubble();
base::HistogramTester histogram_tester;
controller()->OnBubbleClosed();
// Fake-navigate after bubble has been visible for a long time.
controller()->set_elapsed(base::TimeDelta::FromMinutes(1));
controller()->SimulateNavigation();
histogram_tester.ExpectUniqueSample(
"Autofill.SaveCreditCardPrompt.Upload.FirstShow",
AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_HIDDEN, 1);
}
TEST_F(SaveCardBubbleControllerImplTest,
Metrics_Upload_Reshows_NavigateWhileHidden) {
ShowUploadBubble();
CloseAndReshowBubble();
base::HistogramTester histogram_tester;
controller()->OnBubbleClosed();
// Fake-navigate after bubble has been visible for a long time.
controller()->set_elapsed(base::TimeDelta::FromMinutes(1));
controller()->SimulateNavigation();
histogram_tester.ExpectUniqueSample(
"Autofill.SaveCreditCardPrompt.Upload.Reshows",
AutofillMetrics::SAVE_CARD_PROMPT_END_NAVIGATION_HIDDEN, 1);
}
TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_FirstShow_LearnMore) {
ShowLocalBubble();
base::HistogramTester histogram_tester;
controller()->OnLearnMoreClicked();
histogram_tester.ExpectUniqueSample(
"Autofill.SaveCreditCardPrompt.Local.FirstShow",
AutofillMetrics::SAVE_CARD_PROMPT_DISMISS_CLICK_LEARN_MORE, 1);
}
TEST_F(SaveCardBubbleControllerImplTest, Metrics_Local_Reshows_LearnMore) {
ShowLocalBubble();
CloseAndReshowBubble();
base::HistogramTester histogram_tester;
controller()->OnLearnMoreClicked();
histogram_tester.ExpectUniqueSample(
"Autofill.SaveCreditCardPrompt.Local.Reshows",
AutofillMetrics::SAVE_CARD_PROMPT_DISMISS_CLICK_LEARN_MORE, 1);
}
TEST_F(SaveCardBubbleControllerImplTest, Metrics_Upload_FirstShow_LearnMore) {
ShowUploadBubble();
......@@ -523,4 +663,27 @@ TEST_F(SaveCardBubbleControllerImplTest, OnlyOneActiveBubble_UploadThenLocal) {
.empty());
}
TEST_F(SaveCardBubbleControllerImplTest, TestInputCvcIsValid) {
// Note: InputCvcIsValid(~) handles string trimming.
// 3-digit CVC:
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("text")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("1")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("12")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("12 ")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("1234")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("12345")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("1x3")));
EXPECT_TRUE(controller()->InputCvcIsValid(base::ASCIIToUTF16("123")));
EXPECT_TRUE(controller()->InputCvcIsValid(base::ASCIIToUTF16("123 ")));
EXPECT_TRUE(controller()->InputCvcIsValid(base::ASCIIToUTF16(" 123")));
EXPECT_TRUE(controller()->InputCvcIsValid(base::ASCIIToUTF16("999")));
// 4-digit CVC:
CreditCard amex = autofill::test::GetCreditCard2(); // Amex
ShowLocalBubble(&amex);
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("123")));
EXPECT_TRUE(controller()->InputCvcIsValid(base::ASCIIToUTF16("1234")));
EXPECT_FALSE(controller()->InputCvcIsValid(base::ASCIIToUTF16("12345")));
}
} // namespace autofill
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