Commit bf7bae04 authored by Anne Lim's avatar Anne Lim Committed by Commit Bot

[Autofill] CreditCardSaveStrikeDatabase implementation

Created CreditCardSaveStrikeDatabase implementation of
StrikeDatabase, which is to be used to count strikes for credit
card saves.

Bug: 884817
Change-Id: I96b2b98894d06f97f6ea3bb24f3566cea273133f
Reviewed-on: https://chromium-review.googlesource.com/c/1349169
Commit-Queue: Anne Lim <annelim@google.com>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarJared Saul <jsaul@google.com>
Cr-Commit-Position: refs/heads/master@{#611466}
parent 7780f76a
......@@ -101,6 +101,8 @@ jumbo_static_library("browser") {
"credit_card_field.h",
"credit_card_save_manager.cc",
"credit_card_save_manager.h",
"credit_card_save_strike_database.cc",
"credit_card_save_strike_database.h",
"email_field.cc",
"email_field.h",
"field_candidates.cc",
......@@ -351,6 +353,8 @@ jumbo_static_library("test_support") {
"test_autofill_provider.h",
"test_credit_card_save_manager.cc",
"test_credit_card_save_manager.h",
"test_credit_card_save_strike_database.cc",
"test_credit_card_save_strike_database.h",
"test_event_waiter.h",
"test_form_data_importer.cc",
"test_form_data_importer.h",
......@@ -484,6 +488,7 @@ source_set("unit_tests") {
"country_names_unittest.cc",
"credit_card_field_unittest.cc",
"credit_card_save_manager_unittest.cc",
"credit_card_save_strike_database_unittest.cc",
"credit_card_unittest.cc",
"field_candidates_unittest.cc",
"field_filler_unittest.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/credit_card_save_strike_database.h"
#include "components/autofill/core/browser/proto/strike_data.pb.h"
namespace autofill {
CreditCardSaveStrikeDatabase::CreditCardSaveStrikeDatabase(
const base::FilePath& database_dir)
: StrikeDatabase(database_dir) {}
CreditCardSaveStrikeDatabase::~CreditCardSaveStrikeDatabase() {}
std::string CreditCardSaveStrikeDatabase::GetProjectPrefix() {
return "CreditCardSave";
}
int CreditCardSaveStrikeDatabase::GetMaxStrikesLimit() {
return 3;
}
} // namespace autofill
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_SAVE_STRIKE_DATABASE_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_SAVE_STRIKE_DATABASE_H_
#include <string>
#include "components/autofill/core/browser/strike_database.h"
namespace autofill {
// Implementation of StrikeDatabase for credit card saves (both local and
// upload).
class CreditCardSaveStrikeDatabase : public StrikeDatabase {
public:
CreditCardSaveStrikeDatabase(const base::FilePath& database_dir);
~CreditCardSaveStrikeDatabase() override;
std::string GetProjectPrefix() override;
int GetMaxStrikesLimit() override;
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_SAVE_STRIKE_DATABASE_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/credit_card_save_strike_database.h"
#include <utility>
#include <vector>
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_task_environment.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/autofill/core/browser/proto/strike_data.pb.h"
#include "components/autofill/core/browser/test_credit_card_save_strike_database.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
class CreditCardSaveStrikeDatabaseTest : public ::testing::Test {
public:
CreditCardSaveStrikeDatabaseTest() : strike_database_(InitFilePath()) {}
protected:
base::HistogramTester* GetHistogramTester() { return &histogram_tester_; }
base::test::ScopedTaskEnvironment scoped_task_environment_;
TestCreditCardSaveStrikeDatabase strike_database_;
private:
static const base::FilePath InitFilePath() {
base::ScopedTempDir temp_dir_;
EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
const base::FilePath file_path =
temp_dir_.GetPath().AppendASCII("StrikeDatabaseTest");
return file_path;
}
base::HistogramTester histogram_tester_;
};
TEST_F(CreditCardSaveStrikeDatabaseTest, GetKeyForCreditCardSaveTest) {
const std::string last_four = "1234";
EXPECT_EQ("CreditCardSave__1234", strike_database_.GetKey(last_four));
}
TEST_F(CreditCardSaveStrikeDatabaseTest, MaxStrikesLimitReachedTest) {
const std::string last_four = "1234";
EXPECT_EQ(false, strike_database_.IsMaxStrikesLimitReached(last_four));
// 1st strike added for |last_four|.
strike_database_.AddStrike(last_four);
EXPECT_EQ(false, strike_database_.IsMaxStrikesLimitReached(last_four));
// 2nd strike added for |last_four|.
strike_database_.AddStrike(last_four);
EXPECT_EQ(false, strike_database_.IsMaxStrikesLimitReached(last_four));
// 3rd strike added for |last_four|.
strike_database_.AddStrike(last_four);
EXPECT_EQ(true, strike_database_.IsMaxStrikesLimitReached(last_four));
}
TEST_F(CreditCardSaveStrikeDatabaseTest,
CreditCardSaveNthStrikeAddedHistogram) {
const std::string last_four1 = "1234";
const std::string last_four2 = "9876";
// 1st strike added for |last_four1|.
strike_database_.AddStrike(last_four1);
// 2nd strike added for |last_four1|.
strike_database_.AddStrike(last_four1);
// 1st strike added for |last_four2|.
strike_database_.AddStrike(last_four2);
std::vector<base::Bucket> buckets = GetHistogramTester()->GetAllSamples(
"Autofill.StrikeDatabase.NthStrikeAdded.CreditCardSave");
// There should be two buckets, one for 1st strike, one for 2nd strike count.
ASSERT_EQ(2U, buckets.size());
// Both |last_four1| and |last_four2| have 1st strikes recorded.
EXPECT_EQ(2, buckets[0].count);
// Only |last_four1| has 2nd strike recorded.
EXPECT_EQ(1, buckets[1].count);
}
TEST_F(CreditCardSaveStrikeDatabaseTest,
AddStrikeForZeroAndNonZeroStrikesTest) {
const std::string last_four = "1234";
EXPECT_EQ(0, strike_database_.GetStrikes(last_four));
strike_database_.AddStrike(last_four);
EXPECT_EQ(1, strike_database_.GetStrikes(last_four));
strike_database_.AddStrike(last_four);
EXPECT_EQ(2, strike_database_.GetStrikes(last_four));
}
TEST_F(CreditCardSaveStrikeDatabaseTest, ClearStrikesForNonZeroStrikesTest) {
const std::string last_four = "1234";
strike_database_.AddStrike(last_four);
EXPECT_EQ(1, strike_database_.GetStrikes(last_four));
strike_database_.ClearStrikes(last_four);
EXPECT_EQ(0, strike_database_.GetStrikes(last_four));
}
TEST_F(CreditCardSaveStrikeDatabaseTest, ClearStrikesForZeroStrikesTest) {
const std::string last_four = "1234";
strike_database_.ClearStrikes(last_four);
EXPECT_EQ(0, strike_database_.GetStrikes(last_four));
}
} // namespace autofill
......@@ -39,6 +39,10 @@ StrikeDatabase::StrikeDatabase(const base::FilePath& database_dir)
StrikeDatabase::~StrikeDatabase() {}
bool StrikeDatabase::IsMaxStrikesLimitReached(const std::string id) {
return GetStrikes(id) >= GetMaxStrikesLimit();
}
int StrikeDatabase::AddStrike(const std::string id) {
std::string key = GetKey(id);
int num_strikes = strike_map_cache_.count(key) // Cache has entry for |key|.
......@@ -50,6 +54,9 @@ int StrikeDatabase::AddStrike(const std::string id) {
base::Time::Now().ToDeltaSinceWindowsEpoch().InMicroseconds());
UpdateCache(key, data);
SetProtoStrikeData(key, data, base::DoNothing());
base::UmaHistogramCounts1000(
"Autofill.StrikeDatabase.NthStrikeAdded." + GetProjectPrefix(),
num_strikes);
return num_strikes;
}
......
......@@ -43,7 +43,7 @@ class StrikeDatabase : public KeyedService {
explicit StrikeDatabase(const base::FilePath& database_dir);
~StrikeDatabase() override;
bool IsMaxStrikesLimitReached();
bool IsMaxStrikesLimitReached(const std::string id);
// Increments in-memory cache and updates underlying ProtoDatabase.
int AddStrike(const std::string id);
......@@ -78,6 +78,8 @@ class StrikeDatabase : public KeyedService {
private:
FRIEND_TEST_ALL_PREFIXES(ChromeBrowsingDataRemoverDelegateTest,
StrikeDatabaseEmptyOnAutofillRemoveEverything);
FRIEND_TEST_ALL_PREFIXES(CreditCardSaveStrikeDatabaseTest,
GetKeyForCreditCardSaveTest);
friend class StrikeDatabaseTest;
friend class StrikeDatabaseTester;
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/test_credit_card_save_strike_database.h"
namespace autofill {
TestCreditCardSaveStrikeDatabase::TestCreditCardSaveStrikeDatabase(
const base::FilePath& database_dir)
: CreditCardSaveStrikeDatabase(database_dir) {
database_initialized_ = true;
}
} // namespace autofill
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_TEST_CREDIT_CARD_SAVE_STRIKE_DATABASE_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_TEST_CREDIT_CARD_SAVE_STRIKE_DATABASE_H_
#include "components/autofill/core/browser/credit_card_save_strike_database.h"
namespace autofill {
class TestCreditCardSaveStrikeDatabase : public CreditCardSaveStrikeDatabase {
public:
TestCreditCardSaveStrikeDatabase(const base::FilePath& database_dir);
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_CREDIT_CARD_SAVE_STRIKE_DATABASE_H_
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