Skip to content

Commit

Permalink
[ads] Add test namespace for *_util_test.[h|cc] and *_util_constants.h
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Jul 9, 2024
1 parent cf7f2d8 commit 36bc156
Show file tree
Hide file tree
Showing 138 changed files with 882 additions and 812 deletions.
5 changes: 3 additions & 2 deletions components/brave_ads/core/internal/account/account.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ void Account::RemoveObserver(AccountObserver* const observer) {
}

void Account::SetWallet(const std::string& payment_id,
const std::string& recovery_seed) {
const std::optional<WalletInfo> wallet = ToWallet(payment_id, recovery_seed);
const std::string& recovery_seed_base64) {
const std::optional<WalletInfo> wallet =
ToWallet(payment_id, recovery_seed_base64);
if (!wallet) {
// TODO(https://github.com/brave/brave-browser/issues/32066):
// Detect potential defects using `DumpWithoutCrashing`.
Expand Down
2 changes: 1 addition & 1 deletion components/brave_ads/core/internal/account/account.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Account final : public AdsClientNotifierObserver,
bool IsUserRewardsSupported() const { return !!user_rewards_; }

void SetWallet(const std::string& payment_id,
const std::string& recovery_seed);
const std::string& recovery_seed_base64);

static void GetStatement(GetStatementOfAccountsCallback callback);

Expand Down
51 changes: 27 additions & 24 deletions components/brave_ads/core/internal/account/account_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class BraveAdsAccountTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

ads_observer_mock_ = AddAdsObserverMock();
ads_observer_mock_ = test::AddAdsObserverMock();

account_ = std::make_unique<Account>(&token_generator_mock_);
account_->AddObserver(&account_observer_mock_);
Expand All @@ -66,7 +66,7 @@ class BraveAdsAccountTest : public UnitTestBase {

TEST_F(BraveAdsAccountTest, SupportUserRewardsForRewardsUser) {
// Arrange
account_->SetWallet(kWalletPaymentId, kWalletRecoverySeed);
account_->SetWallet(test::kWalletPaymentId, test::kWalletRecoverySeedBase64);

NotifyDidInitializeAds();

Expand All @@ -88,28 +88,28 @@ TEST_F(BraveAdsAccountTest, SetWallet) {
// Act & Assert
EXPECT_CALL(account_observer_mock_, OnDidInitializeWallet);
EXPECT_CALL(account_observer_mock_, OnFailedToInitializeWallet).Times(0);
account_->SetWallet(kWalletPaymentId, kWalletRecoverySeed);
account_->SetWallet(test::kWalletPaymentId, test::kWalletRecoverySeedBase64);
}

TEST_F(BraveAdsAccountTest, DoNotSetWalletWithEmptyPaymentId) {
// Act & Assert
EXPECT_CALL(account_observer_mock_, OnDidInitializeWallet).Times(0);
EXPECT_CALL(account_observer_mock_, OnFailedToInitializeWallet);
account_->SetWallet(/*payment_id=*/{}, kWalletRecoverySeed);
account_->SetWallet(/*payment_id=*/"", test::kWalletRecoverySeedBase64);
}

TEST_F(BraveAdsAccountTest, DoNotSetWalletWithInvalidRecoverySeed) {
// Act & Assert
EXPECT_CALL(account_observer_mock_, OnDidInitializeWallet).Times(0);
EXPECT_CALL(account_observer_mock_, OnFailedToInitializeWallet);
account_->SetWallet(kWalletPaymentId, kInvalidWalletRecoverySeed);
account_->SetWallet(test::kWalletPaymentId, test::kInvalidWalletRecoverySeed);
}

TEST_F(BraveAdsAccountTest, DoNotSetWalletWithEmptyRecoverySeed) {
// Act & Assert
EXPECT_CALL(account_observer_mock_, OnDidInitializeWallet).Times(0);
EXPECT_CALL(account_observer_mock_, OnFailedToInitializeWallet);
account_->SetWallet(kWalletPaymentId, /*recovery_seed=*/"");
account_->SetWallet(test::kWalletPaymentId, /*recovery_seed_base64=*/"");
}

TEST_F(BraveAdsAccountTest, GetStatementForRewardsUser) {
Expand Down Expand Up @@ -204,11 +204,11 @@ TEST_F(BraveAdsAccountTest, DepositForCash) {
test::RefillConfirmationTokens(/*count=*/1);

const URLResponseMap url_responses = {
{BuildCreateRewardConfirmationUrlPath(
kTransactionId, kCreateRewardConfirmationCredential),
{BuildCreateRewardConfirmationUrlPath(test::kTransactionId,
test::kCredentialBase64Url),
{{net::HTTP_CREATED,
test::BuildCreateRewardConfirmationUrlResponseBody()}}},
{BuildFetchPaymentTokenUrlPath(kTransactionId),
{BuildFetchPaymentTokenUrlPath(test::kTransactionId),
{{net::HTTP_OK, test::BuildFetchPaymentTokenUrlResponseBody()}}}};
MockUrlResponses(ads_client_mock_, url_responses);

Expand All @@ -220,7 +220,8 @@ TEST_F(BraveAdsAccountTest, DepositForCash) {
EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit);
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);
account_->Deposit(kCreativeInstanceId, kSegment, AdType::kNotificationAd,
account_->Deposit(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd,
ConfirmationType::kViewedImpression);
}

Expand All @@ -233,11 +234,11 @@ TEST_F(BraveAdsAccountTest, DepositForCashWithUserData) {
test::RefillConfirmationTokens(/*count=*/1);

const URLResponseMap url_responses = {
{BuildCreateRewardConfirmationUrlPath(
kTransactionId, kCreateRewardConfirmationCredential),
{BuildCreateRewardConfirmationUrlPath(test::kTransactionId,
test::kCredentialBase64Url),
{{net::HTTP_CREATED,
test::BuildCreateRewardConfirmationUrlResponseBody()}}},
{BuildFetchPaymentTokenUrlPath(kTransactionId),
{BuildFetchPaymentTokenUrlPath(test::kTransactionId),
{{net::HTTP_OK, test::BuildFetchPaymentTokenUrlResponseBody()}}}};
MockUrlResponses(ads_client_mock_, url_responses);

Expand All @@ -249,7 +250,7 @@ TEST_F(BraveAdsAccountTest, DepositForCashWithUserData) {
EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit);
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);
account_->DepositWithUserData(kCreativeInstanceId, kSegment,
account_->DepositWithUserData(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd,
ConfirmationType::kViewedImpression,
/*user_data=*/{});
Expand All @@ -265,8 +266,8 @@ TEST_F(BraveAdsAccountTest, DepositForNonCash) {
EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit);
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);
account_->Deposit(kCreativeInstanceId, kSegment, AdType::kNotificationAd,
ConfirmationType::kClicked);
account_->Deposit(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd, ConfirmationType::kClicked);
}

TEST_F(BraveAdsAccountTest, DepositForNonCashWithUserData) {
Expand All @@ -279,7 +280,7 @@ TEST_F(BraveAdsAccountTest, DepositForNonCashWithUserData) {
EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit);
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);
account_->DepositWithUserData(kCreativeInstanceId, kSegment,
account_->DepositWithUserData(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd,
ConfirmationType::kClicked, /*user_data=*/{});
}
Expand All @@ -296,7 +297,7 @@ TEST_F(BraveAdsAccountTest, DoNotDepositCashIfCreativeInstanceIdDoesNotExist) {
EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit).Times(0);
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange).Times(0);
account_->Deposit(kMissingCreativeInstanceId, kSegment,
account_->Deposit(test::kMissingCreativeInstanceId, test::kSegment,
AdType::kNotificationAd,
ConfirmationType::kViewedImpression);
}
Expand All @@ -314,7 +315,8 @@ TEST_F(BraveAdsAccountTest, AddTransactionWhenDepositingCashForRewardsUser) {
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);

account_->Deposit(kCreativeInstanceId, kSegment, AdType::kNotificationAd,
account_->Deposit(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd,
ConfirmationType::kViewedImpression);

// Assert
Expand All @@ -339,8 +341,8 @@ TEST_F(BraveAdsAccountTest, AddTransactionWhenDepositingNonCashForRewardsUser) {
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);

account_->Deposit(kCreativeInstanceId, kSegment, AdType::kNotificationAd,
ConfirmationType::kClicked);
account_->Deposit(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd, ConfirmationType::kClicked);

// Assert
base::MockCallback<database::table::GetTransactionsCallback> callback;
Expand All @@ -365,7 +367,8 @@ TEST_F(BraveAdsAccountTest,
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);

account_->Deposit(kCreativeInstanceId, kSegment, AdType::kNotificationAd,
account_->Deposit(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd,
ConfirmationType::kViewedImpression);

// Assert
Expand All @@ -391,8 +394,8 @@ TEST_F(BraveAdsAccountTest,
EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0);
EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange);

account_->Deposit(kCreativeInstanceId, kSegment, AdType::kNotificationAd,
ConfirmationType::kClicked);
account_->Deposit(test::kCreativeInstanceId, test::kSegment,
AdType::kNotificationAd, ConfirmationType::kClicked);

// Assert
base::MockCallback<database::table::GetTransactionsCallback> callback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ std::optional<ConfirmationInfo> BuildNonRewardConfirmation(
const bool should_generate_random_uuids) {
CHECK(!UserHasJoinedBraveRewards());

const TransactionInfo transaction = test::BuildUnreconciledTransaction(
const TransactionInfo transaction = BuildUnreconciledTransaction(
/*value=*/0.01, AdType::kNotificationAd,
ConfirmationType::kViewedImpression, should_generate_random_uuids);
return BuildNonRewardConfirmation(transaction, /*user_data=*/{});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BraveAdsNonRewardConfirmationUtilTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();
}
};

Expand All @@ -46,11 +46,11 @@ TEST_F(BraveAdsNonRewardConfirmationUtilTest, BuildNonRewardConfirmation) {

// Assert
EXPECT_THAT(*confirmation,
::testing::FieldsAre(kTransactionId, kCreativeInstanceId,
ConfirmationType::kViewedImpression,
AdType::kNotificationAd,
/*created_at*/ Now(),
/*reward*/ std::nullopt, UserDataInfo{}));
::testing::FieldsAre(
test::kTransactionId, test::kCreativeInstanceId,
ConfirmationType::kViewedImpression, AdType::kNotificationAd,
/*created_at*/ Now(),
/*reward*/ std::nullopt, UserDataInfo{}));
}

TEST_F(BraveAdsNonRewardConfirmationUtilTest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BraveAdsConfirmationPayloadJsonWriterTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();

AdvanceClockTo(TimeFromUTCString("Mon, 8 Jul 1996 09:25"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BraveAdsConfirmationQueueDatabaseTableTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();

AdvanceClockTo(TimeFromUTCString("Mon, 8 Jul 1996 09:25"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ TEST_F(BraveAdsConfirmationQueueTest, ProcessConfirmation) {
test::RefillConfirmationTokens(/*count=*/1);

const URLResponseMap url_responses = {
{BuildCreateRewardConfirmationUrlPath(
kTransactionId, kCreateRewardConfirmationCredential),
{BuildCreateRewardConfirmationUrlPath(test::kTransactionId,
test::kCredentialBase64Url),
{{net::HTTP_CREATED,
test::BuildCreateRewardConfirmationUrlResponseBody()}}},
{BuildFetchPaymentTokenUrlPath(kTransactionId),
{BuildFetchPaymentTokenUrlPath(test::kTransactionId),
{{net::HTTP_OK, test::BuildFetchPaymentTokenUrlResponseBody()}}}};
MockUrlResponses(ads_client_mock_, url_responses);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ std::optional<ConfirmationInfo> BuildRewardConfirmation(
CHECK(token_generator);
CHECK(UserHasJoinedBraveRewards());

const TransactionInfo transaction = test::BuildUnreconciledTransaction(
const TransactionInfo transaction = BuildUnreconciledTransaction(
/*value=*/0.01, AdType::kNotificationAd,
ConfirmationType::kViewedImpression, should_generate_random_uuids);
return BuildRewardConfirmation(token_generator, transaction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BraveAdsRewardConfirmationUtilTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();

AdvanceClockTo(TimeFromUTCString("Mon, 8 Jul 1996 09:25"));
}
Expand Down Expand Up @@ -110,7 +110,7 @@ TEST_F(BraveAdsRewardConfirmationUtilTest, BuildRewardConfirmation) {

EXPECT_THAT(*confirmation,
::testing::FieldsAre(
kTransactionId, kCreativeInstanceId,
test::kTransactionId, test::kCreativeInstanceId,
ConfirmationType::kViewedImpression, AdType::kNotificationAd,
/*created_at*/ Now(), expected_reward, expected_user_data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "brave/components/brave_ads/core/internal/common/unittest/unittest_profile_pref_value.h"
#include "brave/components/brave_ads/core/public/prefs/pref_names.h"

namespace brave_ads {
namespace brave_ads::test {

namespace {

Expand All @@ -30,4 +30,4 @@ void MockConfirmationUserData() {
SetCatalogId(kCatalogId);
}

} // namespace brave_ads
} // namespace brave_ads::test
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_CONFIRMATIONS_USER_DATA_BUILDER_CONFIRMATION_USER_DATA_BUILDER_TEST_UTIL_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_CONFIRMATIONS_USER_DATA_BUILDER_CONFIRMATION_USER_DATA_BUILDER_TEST_UTIL_H_

namespace brave_ads {
namespace brave_ads::test {

void MockConfirmationUserData();

} // namespace brave_ads
} // namespace brave_ads::test

#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ACCOUNT_CONFIRMATIONS_USER_DATA_BUILDER_CONFIRMATION_USER_DATA_BUILDER_TEST_UTIL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BraveAdsConfirmationUserDataBuilderTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();

AdvanceClockTo(TimeFromUTCString("November 18 2020 12:34:56.789"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BraveAdsConfirmationDynamicUserDataBuilderTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();

AdvanceClockTo(TimeFromUTCString("November 18 2020 12:34:56.789"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BraveAdsFixedUserDataBuilderTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

MockConfirmationUserData();
test::MockConfirmationUserData();

AdvanceClockTo(TimeFromUTCString("November 18 2020 12:34:56.789"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ TEST_F(BraveAdsCashDepositIntegrationTest, GetValue) {
// Act & Assert
base::MockCallback<GetDepositCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/true, /*value=*/1.0));
deposit.GetValue(kCreativeInstanceId, callback.Get());
deposit.GetValue(test::kCreativeInstanceId, callback.Get());
}

TEST_F(BraveAdsCashDepositIntegrationTest,
Expand All @@ -47,7 +47,7 @@ TEST_F(BraveAdsCashDepositIntegrationTest,
// Act & Assert
base::MockCallback<GetDepositCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/false, /*value=*/0.0));
deposit.GetValue(kMissingCreativeInstanceId, callback.Get());
deposit.GetValue(test::kMissingCreativeInstanceId, callback.Get());
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_F(BraveAdsNonCashDepositTest, GetValue) {
// Act & Assert
base::MockCallback<GetDepositCallback> callback;
EXPECT_CALL(callback, Run(/*success=*/true, /*value=*/0.0));
deposit.GetValue(kCreativeInstanceId, callback.Get());
deposit.GetValue(test::kCreativeInstanceId, callback.Get());
}

} // namespace brave_ads
Loading

0 comments on commit 36bc156

Please sign in to comment.