Commit ae7b9e8d authored by Yue Zhang's avatar Yue Zhang Committed by Chromium LUCI CQ

[ChromeCart] Expose CartDB methods to CartService

Bug: 1157892
Change-Id: Ibae1711b252cf824ee5da91b7f39ecb12df03002
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2634132
Commit-Queue: Yue Zhang <yuezhanggg@chromium.org>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#845907}
parent 49290ced
...@@ -50,6 +50,27 @@ bool CartService::IsRemoved() { ...@@ -50,6 +50,27 @@ bool CartService::IsRemoved() {
return profile_->GetPrefs()->GetBoolean(prefs::kCartModuleRemoved); return profile_->GetPrefs()->GetBoolean(prefs::kCartModuleRemoved);
} }
void CartService::LoadCart(const std::string& domain,
CartDB::LoadCallback callback) {
cart_db_->LoadCart(domain, std::move(callback));
}
void CartService::LoadAllCarts(CartDB::LoadCallback callback) {
cart_db_->LoadAllCarts(std::move(callback));
}
void CartService::AddCart(const std::string& domain,
const cart_db::ChromeCartContentProto& proto) {
cart_db_->AddCart(domain, proto,
base::BindOnce(&CartService::OnOperationFinished,
weak_ptr_factory_.GetWeakPtr()));
}
void CartService::DeleteCart(const std::string& domain) {
cart_db_->DeleteCart(domain, base::BindOnce(&CartService::OnOperationFinished,
weak_ptr_factory_.GetWeakPtr()));
}
void CartService::OnOperationFinished(bool success) { void CartService::OnOperationFinished(bool success) {
DCHECK(success) << "database operation failed."; DCHECK(success) << "database operation failed.";
} }
......
...@@ -39,6 +39,15 @@ class CartService : public history::HistoryServiceObserver, ...@@ -39,6 +39,15 @@ class CartService : public history::HistoryServiceObserver,
bool IsRemoved(); bool IsRemoved();
// Get the proto database owned by the service. // Get the proto database owned by the service.
CartDB* GetDB(); CartDB* GetDB();
// Load the cart for a domain.
void LoadCart(const std::string& domain, CartDB::LoadCallback callback);
// Load all carts in this service.
void LoadAllCarts(CartDB::LoadCallback callback);
// Add a cart to the cart service.
void AddCart(const std::string& domain,
const cart_db::ChromeCartContentProto& proto);
// Delete the cart from certain domain in the cart service.
void DeleteCart(const std::string& domain);
// history::HistoryServiceObserver: // history::HistoryServiceObserver:
void OnURLsDeleted(history::HistoryService* history_service, void OnURLsDeleted(history::HistoryService* history_service,
const history::DeletionInfo& deletion_info) override; const history::DeletionInfo& deletion_info) override;
......
...@@ -13,17 +13,29 @@ ...@@ -13,17 +13,29 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace { namespace {
cart_db::ChromeCartContentProto BuildProto(const char* domain) { cart_db::ChromeCartContentProto BuildProto(const char* domain,
const char* merchant_url) {
cart_db::ChromeCartContentProto proto; cart_db::ChromeCartContentProto proto;
proto.set_key(domain); proto.set_key(domain);
proto.set_merchant_cart_url("www.foo.com"); proto.set_merchant_cart_url(merchant_url);
return proto; return proto;
} }
const char kMockMerchantA[] = "A_merchant"; const char kMockMerchantA[] = "A_merchant";
const cart_db::ChromeCartContentProto kMockProtoA = BuildProto(kMockMerchantA); const char kMockMerchantURLA[] = "www.foo.com";
const char kMockMerchantB[] = "B_merchant";
const char kMockMerchantURLB[] = "www.bar.com";
const cart_db::ChromeCartContentProto kMockProtoA =
BuildProto(kMockMerchantA, kMockMerchantURLA);
const cart_db::ChromeCartContentProto kMockProtoB =
BuildProto(kMockMerchantB, kMockMerchantURLB);
const std::vector<ProfileProtoDB<cart_db::ChromeCartContentProto>::KeyAndValue> const std::vector<ProfileProtoDB<cart_db::ChromeCartContentProto>::KeyAndValue>
kExpectedA = {{kMockMerchantA, kMockProtoA}}; kExpectedA = {{kMockMerchantA, kMockProtoA}};
const std::vector<ProfileProtoDB<cart_db::ChromeCartContentProto>::KeyAndValue>
kExpectedB = {{kMockMerchantB, kMockProtoB}};
const std::vector<ProfileProtoDB<cart_db::ChromeCartContentProto>::KeyAndValue>
kExpectedAB = {{kMockMerchantA, kMockProtoA},
{kMockMerchantB, kMockProtoB}};
const std::vector<ProfileProtoDB<cart_db::ChromeCartContentProto>::KeyAndValue> const std::vector<ProfileProtoDB<cart_db::ChromeCartContentProto>::KeyAndValue>
kEmptyExpected = {}; kEmptyExpected = {};
} // namespace } // namespace
...@@ -94,6 +106,105 @@ TEST_F(CartServiceTest, TestRemoveStatusChange) { ...@@ -94,6 +106,105 @@ TEST_F(CartServiceTest, TestRemoveStatusChange) {
ASSERT_FALSE(service_->IsRemoved()); ASSERT_FALSE(service_->IsRemoved());
} }
// Tests adding one cart to the service.
TEST_F(CartServiceTest, TestAddCart) {
CartDB* cart_db_ = service_->GetDB();
base::RunLoop run_loop[3];
cart_db_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[0].QuitClosure(), kEmptyExpected));
run_loop[0].Run();
service_->AddCart(kMockMerchantA, kMockProtoA);
cart_db_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[1].QuitClosure(), kExpectedA));
run_loop[1].Run();
service_->AddCart(kMockMerchantA, kMockProtoB);
cart_db_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[2].QuitClosure(), kExpectedB));
run_loop[2].Run();
}
// Tests deleting one cart from the service.
TEST_F(CartServiceTest, TestDeleteCart) {
CartDB* cart_db_ = service_->GetDB();
base::RunLoop run_loop[3];
cart_db_->AddCart(
kMockMerchantA, kMockProtoA,
base::BindOnce(&CartServiceTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
run_loop[0].Run();
cart_db_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[1].QuitClosure(), kExpectedA));
run_loop[1].Run();
service_->DeleteCart(kMockMerchantA);
cart_db_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[2].QuitClosure(), kEmptyExpected));
run_loop[2].Run();
}
// Tests loading one cart from the service.
TEST_F(CartServiceTest, TestLoadCart) {
CartDB* cart_db_ = service_->GetDB();
base::RunLoop run_loop[3];
cart_db_->AddCart(
kMockMerchantA, kMockProtoA,
base::BindOnce(&CartServiceTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
run_loop[0].Run();
service_->LoadCart(
kMockMerchantB,
base::BindOnce(&CartServiceTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[1].QuitClosure(),
kEmptyExpected));
run_loop[1].Run();
service_->LoadCart(
kMockMerchantA,
base::BindOnce(&CartServiceTest::GetEvaluationPersistedStateDB,
base::Unretained(this), run_loop[2].QuitClosure(),
kExpectedA));
run_loop[2].Run();
}
// Tests loading all carts from the service.
TEST_F(CartServiceTest, TestLoadAllCarts) {
CartDB* cart_db_ = service_->GetDB();
base::RunLoop run_loop[4];
cart_db_->AddCart(
kMockMerchantA, kMockProtoA,
base::BindOnce(&CartServiceTest::OperationEvaluation,
base::Unretained(this), run_loop[0].QuitClosure(), true));
run_loop[0].Run();
service_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[1].QuitClosure(), kExpectedA));
run_loop[1].Run();
cart_db_->AddCart(
kMockMerchantB, kMockProtoB,
base::BindOnce(&CartServiceTest::OperationEvaluation,
base::Unretained(this), run_loop[2].QuitClosure(), true));
run_loop[2].Run();
service_->LoadAllCarts(base::BindOnce(
&CartServiceTest::GetEvaluationPersistedStateDB, base::Unretained(this),
run_loop[3].QuitClosure(), kExpectedAB));
run_loop[3].Run();
}
// Verifies the database is cleared when detected history deletion. // Verifies the database is cleared when detected history deletion.
TEST_F(CartServiceTest, TestOnHistoryDeletion) { TEST_F(CartServiceTest, TestOnHistoryDeletion) {
CartDB* cart_db_ = service_->GetDB(); CartDB* cart_db_ = service_->GetDB();
......
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