Commit 7d221784 authored by Sujie Zhu's avatar Sujie Zhu Committed by Commit Bot

[AF] Fix next year in autofill_test_utils

Change hardcoded year to NextYear. Also update NextYear implementation
with suggested AutofillClock.

Bug: 1059087
Change-Id: Iaf5bc8c74775c2e1bcab06b0c0b7e87a74281aeb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2103654
Commit-Queue: Sujie Zhu <sujiezhu@google.com>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarSylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarJared Saul <jsaul@google.com>
Cr-Commit-Position: refs/heads/master@{#753780}
parent 9349ee1a
......@@ -39,6 +39,8 @@ using testing::ElementsAre;
namespace autofill {
const base::Time kArbitraryTime = base::Time::FromTimeT(1234567890);
class TestSaveCardBubbleControllerImpl : public SaveCardBubbleControllerImpl {
public:
static void CreateForTesting(content::WebContents* web_contents) {
......@@ -92,6 +94,7 @@ class SaveCardBubbleControllerImplTest : public BrowserWithTestWindowTest {
->SetInteger(
prefs::kAutofillAcceptSaveCreditCardPromptState,
prefs::PREVIOUS_SAVE_CREDIT_CARD_PROMPT_USER_DECISION_NONE);
test_clock_.SetNow(kArbitraryTime);
}
void SetLegalMessage(
......
......@@ -277,7 +277,7 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestCreditCardEditorTestWithGooglePayEnabled,
GetLabelText(static_cast<payments::DialogViewID>(
EditorViewController::GetInputFieldViewId(
autofill::CREDIT_CARD_NAME_FULL))));
EXPECT_EQ(base::ASCIIToUTF16("12/2020"),
EXPECT_EQ(card.ExpirationDateForDisplay(),
GetLabelText(static_cast<payments::DialogViewID>(
EditorViewController::GetInputFieldViewId(
autofill::CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR))));
......
......@@ -147,7 +147,10 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestNoShippingTest, PayWithVisa) {
WaitForObservedEvent();
// The actual structure of the card response is unit-tested.
ExpectBodyContains({"4111111111111111", "Test User", "11", "2022"});
ExpectBodyContains(
{"4111111111111111", "Test User",
base::UTF16ToUTF8(card.Expiration2DigitMonthAsString()).c_str(),
base::UTF16ToUTF8(card.Expiration4DigitYearAsString()).c_str()});
ExpectBodyContains({"John", "H.", "Doe", "Underworld", "666 Erebus St.",
"Apt 8", "Elysium", "CA", "91111", "US", "16502111111"});
}
......
......@@ -5,6 +5,7 @@
#include <vector>
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/views/payments/payment_request_browsertest_base.h"
......@@ -45,10 +46,15 @@ IN_PROC_BROWSER_TEST_F(PaymentRequestPaymentResponseAutofillPaymentAppTest,
PayWithCreditCardAndWait(base::ASCIIToUTF16("123"));
// Test that the card details were sent to the merchant.
ExpectBodyContains({"\"cardNumber\": \"4111111111111111\"",
"\"cardSecurityCode\": \"123\"",
"\"cardholderName\": \"Test User\"",
"\"expiryMonth\": \"11\"", "\"expiryYear\": \"2022\""});
ExpectBodyContains(
{"\"cardNumber\": \"4111111111111111\"", "\"cardSecurityCode\": \"123\"",
"\"cardholderName\": \"Test User\"",
base::StringPrintf(
"\"expiryMonth\": \"%s\"",
base::UTF16ToUTF8(card.Expiration2DigitMonthAsString()).c_str()),
base::StringPrintf(
"\"expiryYear\": \"%s\"",
base::UTF16ToUTF8(card.Expiration4DigitYearAsString()).c_str())});
// Test that the billing address was sent to the merchant.
ExpectBodyContains(
......
......@@ -373,33 +373,29 @@ AutofillProfile GetServerProfile2() {
CreditCard GetCreditCard() {
CreditCard credit_card(base::GenerateGUID(), kEmptyOrigin);
// TODO(crbug/1059087): Change hardcoded year to NextYear.
SetCreditCardInfo(&credit_card, "Test User", "4111111111111111" /* Visa */,
"11", "2022", "1");
NextMonth().c_str(), NextYear().c_str(), "1");
return credit_card;
}
CreditCard GetCreditCard2() {
CreditCard credit_card(base::GenerateGUID(), kEmptyOrigin);
// TODO(crbug/1059087): Change hardcoded year to NextYear.
SetCreditCardInfo(&credit_card, "Someone Else", "378282246310005" /* AmEx */,
"07", "2022", "1");
NextMonth().c_str(), TenYearsFromNow().c_str(), "1");
return credit_card;
}
CreditCard GetExpiredCreditCard() {
CreditCard credit_card(base::GenerateGUID(), kEmptyOrigin);
// TODO(crbug/1059087): Change hardcoded year to NextYear.
SetCreditCardInfo(&credit_card, "Test User", "4111111111111111" /* Visa */,
"11", "2002", "1");
NextMonth().c_str(), LastYear().c_str(), "1");
return credit_card;
}
CreditCard GetIncompleteCreditCard() {
CreditCard credit_card(base::GenerateGUID(), kEmptyOrigin);
// TODO(crbug/1059087): Change hardcoded year to NextYear.
SetCreditCardInfo(&credit_card, "", "4111111111111111" /* Visa */, "11",
"2022", "1");
SetCreditCardInfo(&credit_card, "", "4111111111111111" /* Visa */,
NextMonth().c_str(), NextYear().c_str(), "1");
return credit_card;
}
......@@ -757,22 +753,22 @@ std::string ObfuscatedCardDigitsAsUTF8(const std::string& str) {
std::string NextMonth() {
base::Time::Exploded now;
base::Time::Now().LocalExplode(&now);
return base::NumberToString(now.month % 12 + 1);
AutofillClock::Now().LocalExplode(&now);
return base::StringPrintf("%02d", now.month % 12 + 1);
}
std::string LastYear() {
base::Time::Exploded now;
base::Time::Now().LocalExplode(&now);
AutofillClock::Now().LocalExplode(&now);
return base::NumberToString(now.year - 1);
}
std::string NextYear() {
base::Time::Exploded now;
base::Time::Now().LocalExplode(&now);
AutofillClock::Now().LocalExplode(&now);
return base::NumberToString(now.year + 1);
}
std::string TenYearsFromNow() {
base::Time::Exploded now;
base::Time::Now().LocalExplode(&now);
AutofillClock::Now().LocalExplode(&now);
return base::NumberToString(now.year + 10);
}
......
......@@ -275,6 +275,7 @@ void GenerateTestAutofillPopup(
std::string ObfuscatedCardDigitsAsUTF8(const std::string& str);
// Returns 2-digit month string, like "02", "10".
std::string NextMonth();
std::string LastYear();
std::string NextYear();
......
......@@ -74,8 +74,9 @@ namespace {
using UkmCardUploadDecisionType = ukm::builders::Autofill_CardUploadDecision;
using UkmDeveloperEngagementType = ukm::builders::Autofill_DeveloperEngagement;
const base::Time kArbitraryTime = base::Time::FromDoubleT(25);
const base::Time kMuchLaterTime = base::Time::FromDoubleT(5000);
// time_t representation of 9th Sep, 2001 01:46:40 GMT
const base::Time kArbitraryTime = base::Time::FromTimeT(1000000000);
const base::Time kMuchLaterTime = base::Time::FromTimeT(1234567890);
// Used to configure form for |CreateTestCreditCardFormData|.
struct CreditCardFormOptions {
......
......@@ -33,11 +33,11 @@ class PaymentResponseHelperTest : public testing::Test,
test_personal_data_manager_.AddProfile(address_);
// Set up the autofill payment app.
autofill::CreditCard visa_card = autofill::test::GetCreditCard();
visa_card.set_billing_address_id(address_.guid());
visa_card.set_use_count(5u);
visa_card_ = autofill::test::GetCreditCard();
visa_card_.set_billing_address_id(address_.guid());
visa_card_.set_use_count(5u);
autofill_app_ = std::make_unique<AutofillPaymentApp>(
"visa", visa_card, billing_addresses_, "en-US",
"visa", visa_card_, billing_addresses_, "en-US",
&test_payment_request_delegate_);
}
~PaymentResponseHelperTest() override {}
......@@ -90,6 +90,7 @@ class PaymentResponseHelperTest : public testing::Test,
PaymentRequestSpec* spec() { return spec_.get(); }
const mojom::PaymentResponsePtr& response() { return payment_response_; }
autofill::AutofillProfile* test_address() { return &address_; }
const autofill::CreditCard& test_credit_card() { return visa_card_; }
PaymentApp* test_app() { return autofill_app_.get(); }
PaymentRequestDelegate* test_payment_request_delegate() {
return &test_payment_request_delegate_;
......@@ -103,6 +104,7 @@ class PaymentResponseHelperTest : public testing::Test,
// Test data.
autofill::AutofillProfile address_;
autofill::CreditCard visa_card_;
const std::vector<autofill::AutofillProfile*> billing_addresses_;
std::unique_ptr<AutofillPaymentApp> autofill_app_;
};
......@@ -119,22 +121,27 @@ TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_SupportedMethod) {
test_address(), this);
EXPECT_EQ("visa", response()->method_name);
EXPECT_EQ(
"{\"billingAddress\":"
"{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
"\"city\":\"Elysium\","
"\"country\":\"US\","
"\"dependentLocality\":\"\","
"\"organization\":\"Underworld\","
"\"phone\":\"16502111111\","
"\"postalCode\":\"91111\","
"\"recipient\":\"John H. Doe\","
"\"region\":\"CA\","
"\"sortingCode\":\"\"},"
"\"cardNumber\":\"4111111111111111\","
"\"cardSecurityCode\":\"123\","
"\"cardholderName\":\"Test User\","
"\"expiryMonth\":\"11\","
"\"expiryYear\":\"2022\"}",
base::StringPrintf(
"{\"billingAddress\":"
"{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
"\"city\":\"Elysium\","
"\"country\":\"US\","
"\"dependentLocality\":\"\","
"\"organization\":\"Underworld\","
"\"phone\":\"16502111111\","
"\"postalCode\":\"91111\","
"\"recipient\":\"John H. Doe\","
"\"region\":\"CA\","
"\"sortingCode\":\"\"},"
"\"cardNumber\":\"4111111111111111\","
"\"cardSecurityCode\":\"123\","
"\"cardholderName\":\"Test User\","
"\"expiryMonth\":\"%s\","
"\"expiryYear\":\"%s\"}",
base::UTF16ToUTF8(test_credit_card().Expiration2DigitMonthAsString())
.c_str(),
base::UTF16ToUTF8(test_credit_card().Expiration4DigitYearAsString())
.c_str()),
response()->stringified_details);
}
......@@ -157,22 +164,27 @@ TEST_F(PaymentResponseHelperTest, GeneratePaymentResponse_BasicCard) {
test_address(), this);
EXPECT_EQ("basic-card", response()->method_name);
EXPECT_EQ(
"{\"billingAddress\":"
"{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
"\"city\":\"Elysium\","
"\"country\":\"US\","
"\"dependentLocality\":\"\","
"\"organization\":\"Underworld\","
"\"phone\":\"16502111111\","
"\"postalCode\":\"91111\","
"\"recipient\":\"John H. Doe\","
"\"region\":\"CA\","
"\"sortingCode\":\"\"},"
"\"cardNumber\":\"4111111111111111\","
"\"cardSecurityCode\":\"123\","
"\"cardholderName\":\"Test User\","
"\"expiryMonth\":\"11\","
"\"expiryYear\":\"2022\"}",
base::StringPrintf(
"{\"billingAddress\":"
"{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
"\"city\":\"Elysium\","
"\"country\":\"US\","
"\"dependentLocality\":\"\","
"\"organization\":\"Underworld\","
"\"phone\":\"16502111111\","
"\"postalCode\":\"91111\","
"\"recipient\":\"John H. Doe\","
"\"region\":\"CA\","
"\"sortingCode\":\"\"},"
"\"cardNumber\":\"4111111111111111\","
"\"cardSecurityCode\":\"123\","
"\"cardholderName\":\"Test User\","
"\"expiryMonth\":\"%s\","
"\"expiryYear\":\"%s\"}",
base::UTF16ToUTF8(test_credit_card().Expiration2DigitMonthAsString())
.c_str(),
base::UTF16ToUTF8(test_credit_card().Expiration4DigitYearAsString())
.c_str()),
response()->stringified_details);
}
......
......@@ -8,6 +8,7 @@
#include "base/json/json_writer.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
......@@ -69,24 +70,26 @@ TEST(PaymentRequestDataUtilTest, GetBasicCardResponseFromAutofillCreditCard) {
->ToDictionaryValue();
std::string json_response;
base::JSONWriter::Write(*response_value, &json_response);
EXPECT_EQ(
"{\"billingAddress\":"
"{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
"\"city\":\"Elysium\","
"\"country\":\"US\","
"\"dependentLocality\":\"\","
"\"organization\":\"Underworld\","
"\"phone\":\"16502111111\","
"\"postalCode\":\"91111\","
"\"recipient\":\"John H. Doe\","
"\"region\":\"CA\","
"\"sortingCode\":\"\"},"
"\"cardNumber\":\"4111111111111111\","
"\"cardSecurityCode\":\"123\","
"\"cardholderName\":\"Test User\","
"\"expiryMonth\":\"11\","
"\"expiryYear\":\"2022\"}",
json_response);
EXPECT_EQ(base::StringPrintf(
"{\"billingAddress\":"
"{\"addressLine\":[\"666 Erebus St.\",\"Apt 8\"],"
"\"city\":\"Elysium\","
"\"country\":\"US\","
"\"dependentLocality\":\"\","
"\"organization\":\"Underworld\","
"\"phone\":\"16502111111\","
"\"postalCode\":\"91111\","
"\"recipient\":\"John H. Doe\","
"\"region\":\"CA\","
"\"sortingCode\":\"\"},"
"\"cardNumber\":\"4111111111111111\","
"\"cardSecurityCode\":\"123\","
"\"cardholderName\":\"Test User\","
"\"expiryMonth\":\"%s\","
"\"expiryYear\":\"%s\"}",
base::UTF16ToUTF8(card.Expiration2DigitMonthAsString()).c_str(),
base::UTF16ToUTF8(card.Expiration4DigitYearAsString()).c_str()),
json_response);
}
// A test fixture to check ParseSupportedMethods() returns empty supported
......
......@@ -266,6 +266,7 @@ source_set("eg2_tests") {
deps = [
"//base",
"//base/test:test_support",
"//components/autofill/core/browser:test_support",
"//ios/chrome/app/strings:ios_strings_grit",
"//ios/chrome/browser/ui/autofill:eg_test_support+eg2",
"//ios/chrome/browser/ui/settings/autofill:feature_flags",
......
......@@ -5,6 +5,7 @@
#include "base/ios/ios_util.h"
#include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#import "ios/chrome/browser/ui/autofill/autofill_app_interface.h"
#import "ios/chrome/browser/ui/settings/autofill/features.h"
#include "ios/chrome/grit/ios_strings.h"
......@@ -44,8 +45,12 @@ const char kFormElementOtherStuff[] = "otherstuff";
NSString* kLocalCardNumber = @"4111111111111111";
NSString* kLocalCardHolder = @"Test User";
NSString* kLocalCardExpirationMonth = @"11";
NSString* kLocalCardExpirationYear = @"22";
// The local card's expiration month and year (only the last two digits) are set
// with next month and next year.
NSString* kLocalCardExpirationMonth =
base::SysUTF8ToNSString(autofill::test::NextMonth());
NSString* kLocalCardExpirationYear =
base::SysUTF8ToNSString(autofill::test::NextYear().substr(2, 2));
// Unicode characters used in card number:
// - 0x0020 - Space.
......
......@@ -149,6 +149,7 @@ source_set("eg2_tests") {
":constants",
":feature_flags",
"//base",
"//components/autofill/core/browser:test_support",
"//components/strings:components_strings_grit",
"//ios/chrome/app/strings",
"//ios/chrome/browser/ui/autofill:eg_test_support+eg2",
......
......@@ -6,6 +6,7 @@
#include "base/strings/sys_string_conversions.h"
#import "base/test/ios/wait_util.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/strings/grit/components_strings.h"
#import "ios/chrome/browser/ui/autofill/autofill_app_interface.h"
#import "ios/chrome/browser/ui/settings/autofill/autofill_constants.h"
......@@ -43,8 +44,10 @@ struct DisplayStringIDToExpectedResult {
const DisplayStringIDToExpectedResult kExpectedFields[] = {
{IDS_IOS_AUTOFILL_CARDHOLDER, @"Test User"},
{IDS_IOS_AUTOFILL_CARD_NUMBER, @"4111111111111111"},
{IDS_IOS_AUTOFILL_EXP_MONTH, @"11"},
{IDS_IOS_AUTOFILL_EXP_YEAR, @"2022"}};
{IDS_IOS_AUTOFILL_EXP_MONTH,
base::SysUTF8ToNSString(autofill::test::NextMonth())},
{IDS_IOS_AUTOFILL_EXP_YEAR,
base::SysUTF8ToNSString(autofill::test::NextYear())}};
NSString* const kCreditCardLabelTemplate = @"Test User, %@";
......
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