diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 9bdc90c5a94c..af38141b0f7d 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -45,7 +45,6 @@ #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_value_util.h" #include "brave/components/brave_ads/core/public/ads_constants.h" #include "brave/components/brave_ads/core/public/ads_feature.h" -#include "brave/components/brave_ads/core/public/database/database.h" #include "brave/components/brave_ads/core/public/flags/flags_util.h" #include "brave/components/brave_ads/core/public/history/site_history.h" #include "brave/components/brave_ads/core/public/prefs/pref_names.h" @@ -349,6 +348,7 @@ void AdsServiceImpl::StartBatAdsService() { } bat_ads_service_remote_->Create( + ads_service_path_, bat_ads_client_associated_receiver_.BindNewEndpointAndPassRemote(), bat_ads_associated_remote_.BindNewEndpointAndPassReceiver(), std::move(bat_ads_client_notifier_pending_receiver_), @@ -408,18 +408,9 @@ void AdsServiceImpl::Initialize(size_t current_start_number) { return; } - InitializeDatabase(); - InitializeRewardsWallet(current_start_number); } -void AdsServiceImpl::InitializeDatabase() { - CHECK(!database_); - - database_ = base::SequenceBound( - file_task_runner_, ads_service_path_.AppendASCII(kDatabaseFilename)); -} - void AdsServiceImpl::InitializeRewardsWallet(size_t current_start_number) { rewards_service_observation_.GetSource()->GetRewardsWallet( base::BindOnce(&AdsServiceImpl::InitializeRewardsWalletCallback, @@ -1096,8 +1087,6 @@ void AdsServiceImpl::ShutdownAdsService() { CloseAdaptiveCaptcha(); - database_.Reset(); - if (is_bat_ads_initialized_) { VLOG(2) << "Shutdown Bat Ads Service"; } @@ -1733,17 +1722,6 @@ void AdsServiceImpl::ShowScheduledCaptcha(const std::string& payment_id, #endif // !BUILDFLAG(IS_ANDROID) } -void AdsServiceImpl::RunDBTransaction( - mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) { - CHECK(mojom_db_transaction); - CHECK(database_); - - database_.AsyncCall(&Database::RunDBTransaction) - .WithArgs(std::move(mojom_db_transaction)) - .Then(std::move(callback)); -} - void AdsServiceImpl::RecordP2AEvents(const std::vector& events) { for (const auto& event : events) { RecordAndEmitP2AHistogramName(prefs_, diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index 5743e1f8ada6..3e2c6e7ef173 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -19,7 +19,6 @@ #include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/task/cancelable_task_tracker.h" -#include "base/threading/sequence_bound.h" #include "base/time/time.h" #include "base/timer/timer.h" #include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h" @@ -61,7 +60,6 @@ namespace brave_ads { class AdsServiceObserver; class AdsTooltipsDelegate; class BatAdsServiceFactory; -class Database; class DeviceId; class ResourceComponent; struct NewTabPageAdInfo; @@ -124,7 +122,6 @@ class AdsServiceImpl final : public AdsService, void InitializeBasePathDirectoryCallback(size_t current_start_number, bool success); void Initialize(size_t current_start_number); - void InitializeDatabase(); void InitializeRewardsWallet(size_t current_start_number); void InitializeRewardsWalletCallback( size_t current_start_number, @@ -357,9 +354,6 @@ class AdsServiceImpl final : public AdsService, void ShowScheduledCaptcha(const std::string& payment_id, const std::string& captcha_id) override; - void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) override; - // TODO(https://github.com/brave/brave-browser/issues/14666) Decouple P2A // business logic. void RecordP2AEvents(const std::vector& events) override; @@ -427,8 +421,6 @@ class AdsServiceImpl final : public AdsService, mojom::SysInfo sys_info_; - base::SequenceBound database_; - base::RepeatingTimer idle_state_timer_; ui::IdleState last_idle_state_ = ui::IdleState::IDLE_STATE_ACTIVE; base::TimeDelta last_idle_time_; diff --git a/components/brave_ads/core/internal/BUILD.gn b/components/brave_ads/core/internal/BUILD.gn index 4b39be6da65f..065164a88f7c 100644 --- a/components/brave_ads/core/internal/BUILD.gn +++ b/components/brave_ads/core/internal/BUILD.gn @@ -583,6 +583,7 @@ static_library("internal") { "creatives/segments_database_util.cc", "creatives/segments_database_util.h", "database/database.cc", + "database/database.h", "database/database_maintenance.cc", "database/database_maintenance.h", "database/database_manager.cc", diff --git a/components/brave_ads/core/internal/account/account_unittest.cc b/components/brave_ads/core/internal/account/account_unittest.cc index cc1123efee35..34d48c7d1f5e 100644 --- a/components/brave_ads/core/internal/account/account_unittest.cc +++ b/components/brave_ads/core/internal/account/account_unittest.cc @@ -6,6 +6,8 @@ #include "brave/components/brave_ads/core/internal/account/account.h" #include "base/memory/raw_ptr.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/account_observer_mock.h" #include "brave/components/brave_ads/core/internal/account/issuers/issuers_test_util.h" @@ -180,8 +182,11 @@ TEST_F(BraveAdsAccountTest, GetStatementForRewardsUser) { {mojom::AdType::kNotificationAd, 3}}; base::MockCallback callback; - EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_mojom_statement)))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_mojom_statement)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAccount().GetStatement(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAccountTest, DoNotGetStatementForNonRewardsUser) { @@ -190,8 +195,11 @@ TEST_F(BraveAdsAccountTest, DoNotGetStatementForNonRewardsUser) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*statement=*/::testing::IsFalse())); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*statement=*/::testing::IsFalse())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAccount().GetStatement(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAccountTest, DepositForCash) { @@ -214,18 +222,24 @@ TEST_F(BraveAdsAccountTest, DepositForCash) { database::SaveCreativeNotificationAds({creative_ad}); // Act & Assert + base::RunLoop run_loop1; EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, test::kValue, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); - EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange); + base::RunLoop run_loop2; + EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); GetAccount().Deposit(test::kCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression); + run_loop1.Run(); + run_loop2.Run(); } TEST_F(BraveAdsAccountTest, DepositForCashWithUserData) { @@ -248,19 +262,25 @@ TEST_F(BraveAdsAccountTest, DepositForCashWithUserData) { database::SaveCreativeNotificationAds({creative_ad}); // Act & Assert + base::RunLoop run_loop1; EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, test::kValue, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); - EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange); + base::RunLoop run_loop2; + EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); GetAccount().DepositWithUserData(test::kCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression, /*user_data=*/{}); + run_loop1.Run(); + run_loop2.Run(); } TEST_F(BraveAdsAccountTest, DepositForNonCash) { @@ -268,18 +288,24 @@ TEST_F(BraveAdsAccountTest, DepositForNonCash) { test::MockTokenGenerator(/*count=*/1); // Act & Assert + base::RunLoop run_loop1; EXPECT_CALL( account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kClicked, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); - EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange); + base::RunLoop run_loop2; + EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); GetAccount().Deposit(test::kCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kClicked); + run_loop1.Run(); + run_loop2.Run(); } TEST_F(BraveAdsAccountTest, DepositForNonCashWithUserData) { @@ -287,18 +313,24 @@ TEST_F(BraveAdsAccountTest, DepositForNonCashWithUserData) { test::MockTokenGenerator(/*count=*/1); // Act & Assert + base::RunLoop run_loop1; EXPECT_CALL( account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kClicked, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); - EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange); + base::RunLoop run_loop2; + EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); GetAccount().DepositWithUserData( test::kCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kClicked, /*user_data=*/{}); + run_loop1.Run(); + run_loop2.Run(); } TEST_F(BraveAdsAccountTest, DoNotDepositCashIfCreativeInstanceIdDoesNotExist) { @@ -310,12 +342,15 @@ TEST_F(BraveAdsAccountTest, DoNotDepositCashIfCreativeInstanceIdDoesNotExist) { database::SaveCreativeNotificationAds({creative_ad}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit).Times(0); - EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit); + EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange).Times(0); GetAccount().Deposit(test::kMissingCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression); + run_loop.Run(); } TEST_F(BraveAdsAccountTest, AddTransactionWhenDepositingCashForRewardsUser) { @@ -327,28 +362,37 @@ TEST_F(BraveAdsAccountTest, AddTransactionWhenDepositingCashForRewardsUser) { database::SaveCreativeNotificationAds({creative_ad}); // Act + base::RunLoop run_loop1; EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, test::kValue, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); - EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange); + base::RunLoop run_loop2; + EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); GetAccount().Deposit(test::kCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kViewedImpression); + run_loop1.Run(); + run_loop2.Run(); // Assert base::MockCallback callback; + base::RunLoop run_loop3; EXPECT_CALL(callback, - Run(/*success=*/true, /*transactions=*/::testing::SizeIs(1))); + Run(/*success=*/true, /*transactions=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop3.QuitClosure())); const database::table::Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop3.Run(); } TEST_F(BraveAdsAccountTest, AddTransactionWhenDepositingNonCashForRewardsUser) { @@ -360,28 +404,37 @@ TEST_F(BraveAdsAccountTest, AddTransactionWhenDepositingNonCashForRewardsUser) { database::SaveCreativeNotificationAds({creative_ad}); // Act + base::RunLoop run_loop1; EXPECT_CALL( account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kClicked, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); - EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange); + base::RunLoop run_loop2; + EXPECT_CALL(*ads_observer_mock_, OnAdRewardsDidChange) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); GetAccount().Deposit(test::kCreativeInstanceId, test::kSegment, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kClicked); + run_loop1.Run(); + run_loop2.Run(); // Assert base::MockCallback callback; + base::RunLoop run_loop3; EXPECT_CALL(callback, - Run(/*success=*/true, /*transactions=*/::testing::SizeIs(1))); + Run(/*success=*/true, /*transactions=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop3.QuitClosure())); const database::table::Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop3.Run(); } TEST_F(BraveAdsAccountTest, @@ -403,12 +456,15 @@ TEST_F(BraveAdsAccountTest, // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, /*transactions=*/::testing::IsEmpty())); + Run(/*success=*/true, /*transactions=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAccountTest, @@ -430,12 +486,15 @@ TEST_F(BraveAdsAccountTest, // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, /*transactions=*/::testing::IsEmpty())); + Run(/*success=*/true, /*transactions=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/account/confirmations/confirmations_unittest.cc b/components/brave_ads/core/internal/account/confirmations/confirmations_unittest.cc index 903c78e3c58d..966496df44f5 100644 --- a/components/brave_ads/core/internal/account/confirmations/confirmations_unittest.cc +++ b/components/brave_ads/core/internal/account/confirmations/confirmations_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.h" #include "brave/components/brave_ads/core/internal/account/tokens/confirmation_tokens/confirmation_tokens_test_util.h" @@ -49,9 +51,12 @@ TEST_F(BraveAdsConfirmationsTest, ConfirmForRewardsUser) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - /*confirmation_queue_items=*/::testing::SizeIs(1))); + /*confirmation_queue_items=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); confirmation_queue_database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationsTest, ConfirmForNonRewardsUser) { @@ -68,9 +73,12 @@ TEST_F(BraveAdsConfirmationsTest, ConfirmForNonRewardsUser) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - /*confirmation_queue_items=*/::testing::SizeIs(1))); + /*confirmation_queue_items=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); confirmation_queue_database_table_.GetAll(callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc index 2ea7a5110135..1bb8ac3bc014 100644 --- a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc +++ b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.cc @@ -18,7 +18,6 @@ #include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_builder_util.h" #include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_util.h" #include "brave/components/brave_ads/core/internal/account/confirmations/reward/reward_confirmation_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/blinded_token.h" #include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/public_key.h" #include "brave/components/brave_ads/core/internal/common/challenge_bypass_ristretto/token.h" @@ -33,7 +32,6 @@ #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" #include "brave/components/brave_ads/core/public/ad_units/ad_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -395,9 +393,8 @@ void ConfirmationQueue::GetAll(GetConfirmationQueueCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void ConfirmationQueue::GetNext(GetConfirmationQueueCallback callback) const { @@ -432,9 +429,8 @@ void ConfirmationQueue::GetNext(GetConfirmationQueueCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } std::string ConfirmationQueue::GetTableName() const { diff --git a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table_unittest.cc b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table_unittest.cc index 300e6507fff7..2b87d778bdd1 100644 --- a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table_unittest.cc +++ b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/confirmations/confirmation_info.h" #include "brave/components/brave_ads/core/internal/account/confirmations/queue/queue_item/confirmation_queue_item_builder.h" @@ -44,9 +46,12 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, SaveEmptyConfirmationQueue) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - /*confirmation_queue_items=*/::testing::IsEmpty())); + /*confirmation_queue_items=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, SaveConfirmationQueueItems) { @@ -67,8 +72,11 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, SaveConfirmationQueueItems) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -92,9 +100,12 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, const ConfirmationQueueItemList expected_confirmation_queue_items = { confirmation_queue_items.front(), confirmation_queue_items.front()}; base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, expected_confirmation_queue_items)); + Run(/*success=*/true, expected_confirmation_queue_items)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -117,8 +128,11 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -149,9 +163,13 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ConfirmationQueueItemList{ - confirmation_queue_item_1})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + ConfirmationQueueItemList{confirmation_queue_item_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetNext(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -192,11 +210,15 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ConfirmationQueueItemList{ - confirmation_queue_item_2, - confirmation_queue_item_3, - confirmation_queue_item_1})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + ConfirmationQueueItemList{confirmation_queue_item_2, + confirmation_queue_item_3, + confirmation_queue_item_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -226,17 +248,24 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, test::SaveConfirmationQueueItems(confirmation_queue_items); base::MockCallback delete_callback; - EXPECT_CALL(delete_callback, Run(/*success=*/true)); + base::RunLoop run_loop_delete; + EXPECT_CALL(delete_callback, Run(/*success=*/true)) + .WillOnce(base::test::RunOnceClosure(run_loop_delete.QuitClosure())); // Act database_table_.Delete(confirmation_queue_item_1.confirmation.transaction_id, delete_callback.Get()); + run_loop_delete.Run(); // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ConfirmationQueueItemList{ - confirmation_queue_item_2})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + ConfirmationQueueItemList{confirmation_queue_item_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -273,16 +302,22 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, BuildConfirmationQueueItem(*confirmation_3, /*process_at=*/test::Now()); base::MockCallback delete_callback; - EXPECT_CALL(delete_callback, Run(/*success=*/true)); + base::RunLoop run_loop_delete; + EXPECT_CALL(delete_callback, Run(/*success=*/true)) + .WillOnce(base::test::RunOnceClosure(run_loop_delete.QuitClosure())); // Act database_table_.Delete(confirmation_queue_item_3.confirmation.transaction_id, delete_callback.Get()); + run_loop_delete.Run(); // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, RetryConfirmationQueueItem) { @@ -303,7 +338,9 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, RetryConfirmationQueueItem) { test::SaveConfirmationQueueItems(confirmation_queue_items); base::MockCallback retry_callback; - EXPECT_CALL(retry_callback, Run(/*success=*/true)); + base::RunLoop run_loop_retry; + EXPECT_CALL(retry_callback, Run(/*success=*/true)) + .WillOnce(base::test::RunOnceClosure(run_loop_retry.QuitClosure())); const ScopedRandTimeDeltaSetterForTesting scoped_rand_time_delta( base::Minutes(7)); @@ -311,14 +348,18 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, RetryConfirmationQueueItem) { // Act database_table_.Retry(confirmation_queue_item.confirmation.transaction_id, retry_callback.Get()); + run_loop_retry.Run(); // Assert confirmation_queue_item.process_at = test::Now() + base::Minutes(7); confirmation_queue_item.retry_count = 1; base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ConfirmationQueueItemList{ - confirmation_queue_item})); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + ConfirmationQueueItemList{confirmation_queue_item})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, @@ -355,16 +396,22 @@ TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, BuildConfirmationQueueItem(*confirmation_3, /*process_at=*/test::Now()); base::MockCallback retry_callback; - EXPECT_CALL(retry_callback, Run(/*success=*/true)); + base::RunLoop run_loop_retry; + EXPECT_CALL(retry_callback, Run(/*success=*/true)) + .WillOnce(base::test::RunOnceClosure(run_loop_retry.QuitClosure())); // Act database_table_.Retry(confirmation_queue_item_3.confirmation.transaction_id, retry_callback.Get()); + run_loop_retry.Run(); // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, confirmation_queue_items)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsConfirmationQueueDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_unittest.cc b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_unittest.cc index 8d8e2c2de88c..8d42ad0c0270 100644 --- a/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_unittest.cc +++ b/components/brave_ads/core/internal/account/confirmations/queue/confirmation_queue_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/account/confirmations/confirmation_info.h" #include "brave/components/brave_ads/core/internal/account/confirmations/non_reward/non_reward_confirmation_test_util.h" @@ -61,16 +63,22 @@ TEST_F(BraveAdsConfirmationQueueTest, AddConfirmation) { test::BuildRewardConfirmation(/*should_generate_random_uuids=*/false); ASSERT_TRUE(confirmation); - EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation)); - EXPECT_CALL(delegate_mock_, OnWillProcessConfirmationQueue( - *confirmation, /*process_at=*/test::Now() + - base::Minutes(5))); + base::RunLoop run_loop_add; + EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation)) + .WillOnce(base::test::RunOnceClosure(run_loop_add.QuitClosure())); + base::RunLoop run_loop_process; + EXPECT_CALL(delegate_mock_, + OnWillProcessConfirmationQueue( + *confirmation, /*process_at=*/test::Now() + base::Minutes(5))) + .WillOnce(base::test::RunOnceClosure(run_loop_process.QuitClosure())); const ScopedDelayBeforeProcessingConfirmationQueueItemForTesting scoped_delay_before_processing_confirmation_queue_item(base::Minutes(5)); // Act confirmation_queue_->Add(*confirmation); + run_loop_add.Run(); + run_loop_process.Run(); // Assert EXPECT_TRUE(HasPendingTasks()); @@ -100,19 +108,29 @@ TEST_F(BraveAdsConfirmationQueueTest, ProcessConfirmation) { test::BuildRewardConfirmation(/*should_generate_random_uuids=*/false); ASSERT_TRUE(confirmation); - EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation)); + base::RunLoop run_loop_add; + EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation)) + .WillOnce(base::test::RunOnceClosure(run_loop_add.QuitClosure())); + base::RunLoop run_loop_process; EXPECT_CALL(delegate_mock_, OnWillProcessConfirmationQueue( *confirmation, /*process_at=*/test::Now() + - base::Minutes(21))); + base::Minutes(21))) + .WillOnce(base::test::RunOnceClosure(run_loop_process.QuitClosure())); const ScopedDelayBeforeProcessingConfirmationQueueItemForTesting scoped_delay_before_processing_confirmation_queue_item(base::Minutes(21)); confirmation_queue_->Add(*confirmation); + run_loop_add.Run(); + run_loop_process.Run(); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidProcessConfirmationQueue); - EXPECT_CALL(delegate_mock_, OnDidExhaustConfirmationQueue); + base::RunLoop run_loop_did_process; + EXPECT_CALL(delegate_mock_, OnDidProcessConfirmationQueue) + .WillOnce(base::test::RunOnceClosure(run_loop_did_process.QuitClosure())); + EXPECT_CALL(delegate_mock_, OnDidExhaustConfirmationQueue) + .WillOnce(base::test::RunOnceClosure(run_loop_did_process.QuitClosure())); FastForwardClockToNextPendingTask(); + run_loop_did_process.Run(); } TEST_F(BraveAdsConfirmationQueueTest, ProcessMultipleConfirmations) { @@ -123,16 +141,22 @@ TEST_F(BraveAdsConfirmationQueueTest, ProcessMultipleConfirmations) { test::BuildNonRewardConfirmation(/*should_generate_random_uuids=*/true); ASSERT_TRUE(confirmation_1); { - EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation_1)); + base::RunLoop run_loop_add_1; + EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation_1)) + .WillOnce(base::test::RunOnceClosure(run_loop_add_1.QuitClosure())); + base::RunLoop run_loop_process_1; EXPECT_CALL( delegate_mock_, OnWillProcessConfirmationQueue( - *confirmation_1, /*process_at=*/test::Now() + base::Minutes(7))); + *confirmation_1, /*process_at=*/test::Now() + base::Minutes(7))) + .WillOnce(base::test::RunOnceClosure(run_loop_process_1.QuitClosure())); const ScopedDelayBeforeProcessingConfirmationQueueItemForTesting scoped_delay_before_processing_confirmation_queue_item( base::Minutes(7)); confirmation_queue_->Add(*confirmation_1); + run_loop_add_1.Run(); + run_loop_process_1.Run(); EXPECT_TRUE(::testing::Mock::VerifyAndClearExpectations(&delegate_mock_)); } @@ -143,9 +167,12 @@ TEST_F(BraveAdsConfirmationQueueTest, ProcessMultipleConfirmations) { test::BuildNonRewardConfirmation(/*should_generate_random_uuids=*/true); ASSERT_TRUE(confirmation_2); { - EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation_2)); + base::RunLoop run_loop_add_2; + EXPECT_CALL(delegate_mock_, OnDidAddConfirmationToQueue(*confirmation_2)) + .WillOnce(base::test::RunOnceClosure(run_loop_add_2.QuitClosure())); confirmation_queue_->Add(*confirmation_2); + run_loop_add_2.Run(); EXPECT_TRUE(::testing::Mock::VerifyAndClearExpectations(&delegate_mock_)); } @@ -160,20 +187,24 @@ TEST_F(BraveAdsConfirmationQueueTest, ProcessMultipleConfirmations) { test::MockUrlResponses(ads_client_mock_, url_responses); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidProcessConfirmationQueue(*confirmation_1)); - + base::RunLoop run_loop_process_1; + EXPECT_CALL(delegate_mock_, OnDidProcessConfirmationQueue(*confirmation_1)) + .WillOnce(base::test::RunOnceClosure(run_loop_process_1.QuitClosure())); EXPECT_CALL(delegate_mock_, OnWillProcessConfirmationQueue( *confirmation_2, /*process_at=*/test::Now() + - base::Minutes(7) + base::Minutes(21))); - + base::Minutes(7) + base::Minutes(21))) + .WillOnce(base::test::RunOnceClosure(run_loop_process_1.QuitClosure())); FastForwardClockToNextPendingTask(); + run_loop_process_1.Run(); - EXPECT_CALL(delegate_mock_, OnDidProcessConfirmationQueue(*confirmation_2)); - - EXPECT_CALL(delegate_mock_, OnDidExhaustConfirmationQueue); - + base::RunLoop run_loop_process_2; + EXPECT_CALL(delegate_mock_, OnDidProcessConfirmationQueue(*confirmation_2)) + .WillOnce(base::test::RunOnceClosure(run_loop_process_2.QuitClosure())); + EXPECT_CALL(delegate_mock_, OnDidExhaustConfirmationQueue) + .WillOnce(base::test::RunOnceClosure(run_loop_process_2.QuitClosure())); FastForwardClockToNextPendingTask(); + run_loop_process_2.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/account/deposits/cash_deposit_test.cc b/components/brave_ads/core/internal/account/deposits/cash_deposit_test.cc index 2331922b8214..98c2e2a9768b 100644 --- a/components/brave_ads/core/internal/account/deposits/cash_deposit_test.cc +++ b/components/brave_ads/core/internal/account/deposits/cash_deposit_test.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/account/deposits/cash_deposit.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/deposits/deposit_interface.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" @@ -35,8 +37,11 @@ TEST_F(BraveAdsCashDepositIntegrationTest, GetValue) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, test::kValue)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, test::kValue)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); deposit.GetValue(test::kCreativeInstanceId, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCashDepositIntegrationTest, @@ -46,8 +51,11 @@ TEST_F(BraveAdsCashDepositIntegrationTest, // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/false, /*value=*/0.0)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/false, /*value=*/0.0)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); deposit.GetValue(test::kMissingCreativeInstanceId, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc b/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc index d4d6f10460ae..bdfe48b7bdc6 100644 --- a/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc +++ b/components/brave_ads/core/internal/account/deposits/deposits_database_table.cc @@ -11,7 +11,6 @@ #include "base/check.h" #include "base/functional/bind.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_table_util.h" @@ -19,7 +18,6 @@ #include "brave/components/brave_ads/core/internal/common/logging_util.h" #include "brave/components/brave_ads/core/internal/common/time/time_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -188,10 +186,9 @@ void Deposits::GetForCreativeInstanceId(const std::string& creative_instance_id, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void Deposits::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/account/statement/statement_unittest.cc b/components/brave_ads/core/internal/account/statement/statement_unittest.cc index 73b31bdfe405..76f6dc03ba93 100644 --- a/components/brave_ads/core/internal/account/statement/statement_unittest.cc +++ b/components/brave_ads/core/internal/account/statement/statement_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/account/statement/statement.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/statement/statement_feature.h" #include "brave/components/brave_ads/core/internal/account/transactions/transaction_info.h" @@ -55,8 +57,11 @@ TEST_F(BraveAdsStatementTest, GetForTransactionsThisMonth) { {mojom::AdType::kNotificationAd, 2}}; base::MockCallback callback; - EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_mojom_statement)))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_mojom_statement)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); BuildStatement(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsStatementTest, @@ -136,8 +141,11 @@ TEST_F(BraveAdsStatementTest, {mojom::AdType::kNotificationAd, 3}}; base::MockCallback callback; - EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); BuildStatement(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsStatementTest, GetForTransactionsSplitOverTwoYears) { @@ -202,8 +210,11 @@ TEST_F(BraveAdsStatementTest, GetForTransactionsSplitOverTwoYears) { {mojom::AdType::kNotificationAd, 3}}; base::MockCallback callback; - EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); BuildStatement(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsStatementTest, GetForNoTransactions) { @@ -223,8 +234,11 @@ TEST_F(BraveAdsStatementTest, GetForNoTransactions) { expected_statement->ads_summary_this_month.clear(); base::MockCallback callback; - EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); BuildStatement(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsStatementTest, GetWithFilteredTransactions) { @@ -278,8 +292,11 @@ TEST_F(BraveAdsStatementTest, GetWithFilteredTransactions) { {mojom::AdType::kNotificationAd, 1}, {mojom::AdType::kNewTabPageAd, 1}}; base::MockCallback callback; - EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(::testing::Eq(std::ref(expected_statement)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); BuildStatement(callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc b/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc index fd4747c601f0..72f4191c5cfe 100644 --- a/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc +++ b/components/brave_ads/core/internal/account/transactions/transactions_database_table.cc @@ -241,9 +241,8 @@ void Transactions::GetForDateRange(base::Time from_time, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void Transactions::Reconcile(const PaymentTokenList& payment_tokens, diff --git a/components/brave_ads/core/internal/account/transactions/transactions_database_table_unittest.cc b/components/brave_ads/core/internal/account/transactions/transactions_database_table_unittest.cc index 6e35b5acc651..1d3e20219258 100644 --- a/components/brave_ads/core/internal/account/transactions/transactions_database_table_unittest.cc +++ b/components/brave_ads/core/internal/account/transactions/transactions_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/account/transactions/transactions_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/time/time.h" #include "brave/components/brave_ads/core/internal/account/tokens/payment_tokens/payment_token_info.h" @@ -28,12 +30,15 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, SaveEmptyTransactions) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - /*transactions=*/::testing::IsEmpty())); + /*transactions=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, SaveTransactions) { @@ -60,12 +65,15 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, SaveTransactions) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, ::testing::ElementsAreArray(transactions))); + Run(/*success=*/true, ::testing::ElementsAreArray(transactions))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, DoNotSaveDuplicateTransactions) { @@ -85,11 +93,14 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, DoNotSaveDuplicateTransactions) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, transactions)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, transactions)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, GetTransactionsForDateRange) { @@ -117,10 +128,13 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, GetTransactionsForDateRange) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, TransactionList{transaction_2})); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, TransactionList{transaction_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForDateRange(/*from_time=*/test::Now(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, ReconcileTransactions) { @@ -159,13 +173,15 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, ReconcileTransactions) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, - Run(/*success=*/true, - ::testing::UnorderedElementsAreArray( - TransactionList{transaction_1, transaction_2}))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + ::testing::UnorderedElementsAreArray( + TransactionList{transaction_1, transaction_2}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, PurgeExpired) { @@ -206,13 +222,15 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, PurgeExpired) { database_table.PurgeExpired(purge_expired_callback.Get()); base::MockCallback callback; - EXPECT_CALL(callback, - Run(/*success=*/true, - ::testing::UnorderedElementsAreArray( - TransactionList{transaction_2, transaction_3}))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + ::testing::UnorderedElementsAreArray( + TransactionList{transaction_2, transaction_3}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, @@ -248,13 +266,15 @@ TEST_F(BraveAdsTransactionsDatabaseTableTest, database_table.PurgeExpired(purge_expired_callback.Get()); base::MockCallback callback; - EXPECT_CALL(callback, - Run(/*success=*/true, - ::testing::UnorderedElementsAreArray( - TransactionList{transaction_1, transaction_2}))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + ::testing::UnorderedElementsAreArray( + TransactionList{transaction_1, transaction_2}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/account/transactions/transactions_unittest.cc b/components/brave_ads/core/internal/account/transactions/transactions_unittest.cc index e4688b1f1ed9..839f719771fe 100644 --- a/components/brave_ads/core/internal/account/transactions/transactions_unittest.cc +++ b/components/brave_ads/core/internal/account/transactions/transactions_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/account/transactions/transactions.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/transactions/transaction_info.h" #include "brave/components/brave_ads/core/internal/account/transactions/transactions_database_table.h" @@ -36,11 +38,14 @@ TEST_F(BraveAdsTransactionsTest, Add) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, TransactionList{transaction})); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, TransactionList{transaction})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::Transactions database_table; database_table.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsTransactionsTest, GetForDateRange) { @@ -74,11 +79,14 @@ TEST_F(BraveAdsTransactionsTest, GetForDateRange) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - TransactionList{transaction_2, transaction_3})); + TransactionList{transaction_2, transaction_3})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetTransactionsForDateRange(/*from_time=*/test::Now(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/ad_units/inline_content_ad/inline_content_ad_test.cc b/components/brave_ads/core/internal/ad_units/inline_content_ad/inline_content_ad_test.cc index f3a890c38cb1..aaf1b95095c2 100644 --- a/components/brave_ads/core/internal/ad_units/inline_content_ad/inline_content_ad_test.cc +++ b/components/brave_ads/core/internal/ad_units/inline_content_ad/inline_content_ad_test.cc @@ -5,6 +5,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/types/optional_ref.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" @@ -51,10 +53,13 @@ class BraveAdsInlineContentAdIntegrationTest : public test::TestBase { const std::string& creative_instance_id, mojom::InlineContentAdEventType mojom_ad_event_type, bool should_fire_event) { + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/should_fire_event)); + EXPECT_CALL(callback, Run(/*success=*/should_fire_event)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().TriggerInlineContentAdEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } }; @@ -67,9 +72,12 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, ServeAd) { RecordP2AEvents(BuildP2AAdOpportunityEvents( mojom::AdType::kInlineContentAd, /*segments=*/{}))); + base::RunLoop run_loop; base::MockCallback callback; - EXPECT_CALL(callback, Run(kDimensions, /*ad=*/::testing::Ne(std::nullopt))); + EXPECT_CALL(callback, Run(kDimensions, /*ad=*/::testing::Ne(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeInlineContentAd(kDimensions, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdIntegrationTest, @@ -77,9 +85,12 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, // Act & Assert EXPECT_CALL(ads_client_mock_, RecordP2AEvents).Times(0); + base::RunLoop run_loop; base::MockCallback callback; - EXPECT_CALL(callback, Run(kDimensions, /*ad=*/::testing::Eq(std::nullopt))); + EXPECT_CALL(callback, Run(kDimensions, /*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeInlineContentAd(kDimensions, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdIntegrationTest, @@ -92,15 +103,19 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, // Act & Assert EXPECT_CALL(ads_client_mock_, RecordP2AEvents).Times(0); + base::RunLoop run_loop; base::MockCallback callback; - EXPECT_CALL(callback, Run(kDimensions, /*ad=*/::testing::Eq(std::nullopt))); + EXPECT_CALL(callback, Run(kDimensions, /*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeInlineContentAd(kDimensions, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdIntegrationTest, TriggerViewedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run) .WillOnce([&](const std::string& dimensions, @@ -114,15 +129,18 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, TriggerViewedEvent) { ad->placement_id, ad->creative_instance_id, mojom::InlineContentAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Quit(); }); GetAds().MaybeServeInlineContentAd(kDimensions, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdIntegrationTest, TriggerClickedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run) .WillOnce([&](const std::string& dimensions, @@ -141,9 +159,11 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, TriggerClickedEvent) { ad->placement_id, ad->creative_instance_id, mojom::InlineContentAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Quit(); }); GetAds().MaybeServeInlineContentAd(kDimensions, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdIntegrationTest, @@ -151,6 +171,7 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run) .WillOnce([&](const std::string& dimensions, @@ -164,9 +185,11 @@ TEST_F(BraveAdsInlineContentAdIntegrationTest, ad->placement_id, test::kInvalidCreativeInstanceId, mojom::InlineContentAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Quit(); }); GetAds().MaybeServeInlineContentAd(kDimensions, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test.cc b/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test.cc index 969853369d13..74dcffe2722f 100644 --- a/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test.cc +++ b/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test.cc @@ -5,6 +5,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "base/types/optional_ref.h" @@ -43,9 +45,12 @@ class BraveAdsNewTabPageAdIntegrationTest : public test::TestBase { mojom::NewTabPageAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/should_fire_event)); + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/should_fire_event)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().TriggerNewTabPageAdEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } }; @@ -62,8 +67,11 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, ServeAd) { mojom::AdType::kNewTabPageAd, /*segments=*/{}))); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*statement=*/::testing::Ne(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*statement=*/::testing::Ne(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdIntegrationTest, @@ -76,8 +84,11 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, EXPECT_CALL(ads_client_mock_, RecordP2AEvents).Times(0); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdIntegrationTest, @@ -94,8 +105,11 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, EXPECT_CALL(ads_client_mock_, RecordP2AEvents).Times(0); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } TEST_F( @@ -110,8 +124,11 @@ TEST_F( EXPECT_CALL(ads_client_mock_, RecordP2AEvents).Times(0); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*statement=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*statement=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdIntegrationTest, TriggerViewedEvent) { @@ -122,6 +139,7 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, TriggerViewedEvent) { test::ForcePermissionRules(); base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run) .WillOnce([&](base::optional_ref ad) { ASSERT_TRUE(ad); @@ -132,9 +150,11 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, TriggerViewedEvent) { ad->placement_id, ad->creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Quit(); }); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdIntegrationTest, @@ -173,6 +193,7 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, TriggerClickedEvent) { test::ForcePermissionRules(); base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run) .WillOnce([&](base::optional_ref ad) { ASSERT_TRUE(ad); @@ -188,9 +209,11 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, TriggerClickedEvent) { ad->placement_id, ad->creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Quit(); }); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdIntegrationTest, @@ -244,6 +267,7 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, test::ForcePermissionRules(); base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run) .WillOnce([&](base::optional_ref ad) { ASSERT_TRUE(ad); @@ -254,9 +278,11 @@ TEST_F(BraveAdsNewTabPageAdIntegrationTest, ad->placement_id, test::kInvalidCreativeInstanceId, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Quit(); }); GetAds().MaybeServeNewTabPageAd(callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_for_mobile_test.cc b/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_for_mobile_test.cc index 5725f2a3d151..b38ecaf323b6 100644 --- a/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_for_mobile_test.cc +++ b/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_for_mobile_test.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/analytics/p2a/opportunities/p2a_opportunity_util.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" @@ -49,13 +51,16 @@ TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, RecordP2AEvents(BuildP2AAdOpportunityEvents( mojom::AdType::kNotificationAd, /*segments=*/{}))); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) - .WillOnce(::testing::Invoke([](const NotificationAdInfo& ad) { + .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { EXPECT_TRUE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, @@ -78,6 +83,7 @@ TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerViewedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -85,22 +91,30 @@ TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerViewedEvent) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kViewedImpression, callback.Get()); + ad_event_run_loop.Run(); EXPECT_TRUE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerClickedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -110,22 +124,30 @@ TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerClickedEvent) { EXPECT_CALL(ads_client_mock_, CloseNotificationAd(ad.placement_id)); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kClicked, callback.Get()); + ad_event_run_loop.Run(); EXPECT_FALSE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerDismissedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -133,22 +155,30 @@ TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerDismissedEvent) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kDismissed, callback.Get()); + ad_event_run_loop.Run(); EXPECT_FALSE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerTimedOutEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -156,16 +186,23 @@ TEST_F(BraveAdsNotificationAdForMobileIntegrationTest, TriggerTimedOutEvent) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kTimedOut, callback.Get()); + ad_event_run_loop.Run(); EXPECT_FALSE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_test.cc b/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_test.cc index 2dfb3ddb23c8..38dda4c7be8a 100644 --- a/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_test.cc +++ b/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_test.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/time/time.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" @@ -44,9 +46,12 @@ TEST_F(BraveAdsNotificationAdIntegrationTest, ServeAd) { // Act & Assert EXPECT_CALL(ads_client_mock_, RecordP2AEvents); - EXPECT_CALL(ads_client_mock_, ShowNotificationAd); + base::RunLoop run_loop; + EXPECT_CALL(ads_client_mock_, ShowNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdIntegrationTest, @@ -69,6 +74,7 @@ TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerViewedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -76,47 +82,64 @@ TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerViewedEvent) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kViewedImpression, callback.Get()); + ad_event_run_loop.Run(); EXPECT_TRUE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerClickedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); // Act & Assert - EXPECT_CALL(ads_client_mock_, CloseNotificationAd(ad.placement_id)); + EXPECT_CALL(ads_client_mock_, CloseNotificationAd(ad.placement_id)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kClicked, callback.Get()); + ad_event_run_loop.Run(); EXPECT_FALSE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerDismissedEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -124,22 +147,30 @@ TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerDismissedEvent) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kDismissed, callback.Get()); + ad_event_run_loop.Run(); EXPECT_FALSE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerTimedOutEvent) { // Arrange test::ForcePermissionRules(); + base::RunLoop run_loop; EXPECT_CALL(ads_client_mock_, ShowNotificationAd) .WillOnce(::testing::Invoke([&](const NotificationAdInfo& ad) { ASSERT_TRUE( @@ -147,16 +178,23 @@ TEST_F(BraveAdsNotificationAdIntegrationTest, TriggerTimedOutEvent) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true)); + base::RunLoop ad_event_run_loop( + base::RunLoop::Type::kNestableTasksAllowed); + EXPECT_CALL(callback, Run(/*success=*/true)) + .WillOnce( + base::test::RunOnceClosure(ad_event_run_loop.QuitClosure())); GetAds().TriggerNotificationAdEvent( ad.placement_id, mojom::NotificationAdEventType::kTimedOut, callback.Get()); + ad_event_run_loop.Run(); EXPECT_FALSE( NotificationAdManager::GetInstance().Exists(ad.placement_id)); + run_loop.Quit(); })); ServeAd(); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/ad_units/promoted_content_ad/promoted_content_ad_test.cc b/components/brave_ads/core/internal/ad_units/promoted_content_ad/promoted_content_ad_test.cc index 8a7eb1580570..dfff9c8ca623 100644 --- a/components/brave_ads/core/internal/ad_units/promoted_content_ad/promoted_content_ad_test.cc +++ b/components/brave_ads/core/internal/ad_units/promoted_content_ad/promoted_content_ad_test.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" @@ -42,9 +44,12 @@ class BraveAdsPromotedContentAdIntegrationTest : public test::TestBase { mojom::PromotedContentAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/should_fire_event)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/should_fire_event)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().TriggerPromotedContentAdEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } }; diff --git a/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_non_rewards_test.cc b/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_non_rewards_test.cc index 7a8ef5132fdd..11ce49d810e1 100644 --- a/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_non_rewards_test.cc +++ b/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_non_rewards_test.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_handler.h" @@ -39,10 +41,13 @@ class BraveAdsSearchResultAdForNonRewardsIntegrationTest mojom::CreativeSearchResultAdInfoPtr mojom_creative_ad, mojom::SearchResultAdEventType mojom_ad_event_type, bool should_fire_event) { + base::RunLoop run_loop; base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/should_fire_event)); + EXPECT_CALL(callback, Run(/*success=*/should_fire_event)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().TriggerSearchResultAdEvent(std::move(mojom_creative_ad), mojom_ad_event_type, callback.Get()); + run_loop.Run(); } base::test::ScopedFeatureList scoped_feature_list_; diff --git a/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_rewards_test.cc b/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_rewards_test.cc index b572b259aa27..631d47920222 100644 --- a/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_rewards_test.cc +++ b/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_for_rewards_test.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_handler.h" @@ -35,10 +37,13 @@ class BraveAdsSearchResultAdForRewardsIntegrationTest : public test::TestBase { mojom::CreativeSearchResultAdInfoPtr mojom_creative_ad, mojom::SearchResultAdEventType mojom_ad_event_type, bool should_fire_event) { + base::RunLoop run_loop; base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/should_fire_event)); + EXPECT_CALL(callback, Run(/*success=*/should_fire_event)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); GetAds().TriggerSearchResultAdEvent(std::move(mojom_creative_ad), mojom_ad_event_type, callback.Get()); + run_loop.Run(); } base::test::ScopedFeatureList scoped_feature_list_; diff --git a/components/brave_ads/core/internal/ads.cc b/components/brave_ads/core/internal/ads.cc index 8a23294e7054..036331cb86f9 100644 --- a/components/brave_ads/core/internal/ads.cc +++ b/components/brave_ads/core/internal/ads.cc @@ -11,8 +11,9 @@ namespace brave_ads { // static -std::unique_ptr Ads::CreateInstance(AdsClient& ads_client) { - return std::make_unique(ads_client, +std::unique_ptr Ads::CreateInstance(AdsClient& ads_client, + const base::FilePath& database_path) { + return std::make_unique(ads_client, database_path, std::make_unique()); } diff --git a/components/brave_ads/core/internal/ads_client/ads_client_mock.h b/components/brave_ads/core/internal/ads_client/ads_client_mock.h index 3f6ed5f5f121..177357dd6ba9 100644 --- a/components/brave_ads/core/internal/ads_client/ads_client_mock.h +++ b/components/brave_ads/core/internal/ads_client/ads_client_mock.h @@ -68,10 +68,6 @@ class AdsClientMock : public AdsClient { ShowScheduledCaptcha, (const std::string& payment_id, const std::string& captcha_id)); - MOCK_METHOD(void, - RunDBTransaction, - (mojom::DBTransactionInfoPtr, RunDBTransactionCallback)); - MOCK_METHOD(void, RecordP2AEvents, (const std::vector& events)); MOCK_METHOD(bool, FindProfilePref, (const std::string& path), (const)); diff --git a/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.cc b/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.cc new file mode 100644 index 000000000000..5020574ec30b --- /dev/null +++ b/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.cc @@ -0,0 +1,36 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.h" + +#include + +#include "base/run_loop.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_notifier.h" + +namespace brave_ads::test { + +AdsClientNotifierWaiter::AdsClientNotifierWaiter(AdsClientNotifier& notifier) + : notifier_(notifier) { + notifier_->AddObserver(this); +} + +AdsClientNotifierWaiter::~AdsClientNotifierWaiter() { + notifier_->RemoveObserver(this); +} + +void AdsClientNotifierWaiter::WaitForAdsInitialization() { + base::RunLoop run_loop; + did_initialize_ads_closure_ = run_loop.QuitClosure(); + run_loop.Run(); +} + +void AdsClientNotifierWaiter::OnNotifyDidInitializeAds() { + if (did_initialize_ads_closure_) { + std::move(did_initialize_ads_closure_).Run(); + } +} + +} // namespace brave_ads::test diff --git a/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.h b/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.h new file mode 100644 index 000000000000..6f09074a1dfc --- /dev/null +++ b/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.h @@ -0,0 +1,37 @@ +/* Copyright (c) 2024 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ADS_CLIENT_ADS_CLIENT_NOTIFIER_WAITER_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ADS_CLIENT_ADS_CLIENT_NOTIFIER_WAITER_H_ + +#include "base/functional/callback.h" +#include "base/memory/raw_ref.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_notifier_observer.h" + +namespace brave_ads { +class AdsClientNotifier; +} // namespace brave_ads + +namespace brave_ads::test { + +class AdsClientNotifierWaiter : public AdsClientNotifierObserver { + public: + explicit AdsClientNotifierWaiter(AdsClientNotifier& notifier); + + ~AdsClientNotifierWaiter() override; + + // AdsClientNotifierObserver overrides: + void OnNotifyDidInitializeAds() override; + + void WaitForAdsInitialization(); + + private: + const base::raw_ref notifier_; + base::OnceClosure did_initialize_ads_closure_; +}; + +} // namespace brave_ads::test + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_ADS_CLIENT_ADS_CLIENT_NOTIFIER_WAITER_H_ diff --git a/components/brave_ads/core/internal/ads_impl.cc b/components/brave_ads/core/internal/ads_impl.cc index bbe7c4cb734d..c91fef9b5d38 100644 --- a/components/brave_ads/core/internal/ads_impl.cc +++ b/components/brave_ads/core/internal/ads_impl.cc @@ -5,6 +5,7 @@ #include "brave/components/brave_ads/core/internal/ads_impl.h" +#include #include #include @@ -41,8 +42,9 @@ void FailedToInitialize(InitializeCallback callback) { } // namespace AdsImpl::AdsImpl(AdsClient& ads_client, + const base::FilePath& database_path, std::unique_ptr token_generator) - : global_state_(ads_client, std::move(token_generator)), + : global_state_(ads_client, database_path, std::move(token_generator)), database_maintenance_(std::make_unique()) {} AdsImpl::~AdsImpl() = default; diff --git a/components/brave_ads/core/internal/ads_impl.h b/components/brave_ads/core/internal/ads_impl.h index 9a43499e8ac8..e86112c36b3d 100644 --- a/components/brave_ads/core/internal/ads_impl.h +++ b/components/brave_ads/core/internal/ads_impl.h @@ -32,6 +32,7 @@ class Maintenance; class AdsImpl final : public Ads { public: AdsImpl(AdsClient& ads_client, + const base::FilePath& database_path, std::unique_ptr token_generator); AdsImpl(const AdsImpl&) = delete; diff --git a/components/brave_ads/core/internal/common/database/database_transaction_util.cc b/components/brave_ads/core/internal/common/database/database_transaction_util.cc index 0f047b7ee5d3..22edbc38e5b3 100644 --- a/components/brave_ads/core/internal/common/database/database_transaction_util.cc +++ b/components/brave_ads/core/internal/common/database/database_transaction_util.cc @@ -9,9 +9,10 @@ #include "base/functional/bind.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" +#include "brave/components/brave_ads/core/internal/database/database_manager.h" +#include "brave/components/brave_ads/core/internal/global_state/global_state.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" +#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" namespace brave_ads::database { @@ -43,9 +44,15 @@ bool IsError( mojom::DBTransactionResultInfo::StatusCode::kSuccess; } +void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + ::brave_ads::RunDBTransactionCallback callback) { + GlobalState::GetInstance()->GetDatabaseManager().RunDBTransaction( + std::move(mojom_db_transaction), std::move(callback)); +} + void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, ResultCallback callback) { - GetAdsClient().RunDBTransaction( + GlobalState::GetInstance()->GetDatabaseManager().RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&RunDBTransactionCallback, std::move(callback))); } diff --git a/components/brave_ads/core/internal/common/database/database_transaction_util.h b/components/brave_ads/core/internal/common/database/database_transaction_util.h index 8d34e50aabc5..ba1705afcba3 100644 --- a/components/brave_ads/core/internal/common/database/database_transaction_util.h +++ b/components/brave_ads/core/internal/common/database/database_transaction_util.h @@ -22,6 +22,11 @@ bool IsSuccess( bool IsError( const mojom::DBTransactionResultInfoPtr& mojom_db_transaction_result); +// Run a database transaction. The callback takes one argument - +// `mojom::DBTransactionResultInfoPtr` containing the info of the transaction. +void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback); + // Run a database transaction. void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, ResultCallback callback); diff --git a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc index 5757e8a3081e..0e6016692426 100644 --- a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc +++ b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.cc @@ -26,7 +26,6 @@ #include "brave/components/brave_ads/core/internal/global_state/global_state.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_info.h" -#include "brave/components/brave_ads/core/public/database/database.h" #include "brave/components/brave_ads/core/public/flags/flags_util.h" namespace brave_ads::test { @@ -140,20 +139,6 @@ void MockLoadDataResource(AdsClientMock& ads_client_mock) { })); } -void MockRunDBTransaction(AdsClientMock& ads_client_mock, Database& database) { - ON_CALL(ads_client_mock, RunDBTransaction) - .WillByDefault(::testing::Invoke( - [&database](mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) { - CHECK(mojom_db_transaction); - - mojom::DBTransactionResultInfoPtr mojom_db_transaction_result = - database.RunDBTransaction(std::move(mojom_db_transaction)); - - std::move(callback).Run(std::move(mojom_db_transaction_result)); - })); -} - void MockFindProfilePref(const AdsClientMock& ads_client_mock) { ON_CALL(ads_client_mock, FindProfilePref) .WillByDefault(::testing::Invoke([](const std::string& path) -> bool { diff --git a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h index 5777abf680b8..da9e1c5e902e 100644 --- a/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h +++ b/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h @@ -8,7 +8,6 @@ #include "base/files/file_path.h" #include "brave/components/brave_ads/core/internal/ads_client/ads_client_mock.h" -#include "brave/components/brave_ads/core/public/database/database.h" namespace brave_ads::test { @@ -33,8 +32,6 @@ void MockLoadResourceComponent(AdsClientMock& ads_client_mock, void MockLoadDataResource(AdsClientMock& ads_client_mock); -void MockRunDBTransaction(AdsClientMock& ads_client_mock, Database& database); - void MockFindProfilePref(const AdsClientMock& ads_client_mock); void MockGetProfilePref(const AdsClientMock& ads_client_mock); void MockSetProfilePref(const AdsClientMock& ads_client_mock, diff --git a/components/brave_ads/core/internal/common/test/test_base.cc b/components/brave_ads/core/internal/common/test/test_base.cc index 81cb245b21aa..5af58ef80761 100644 --- a/components/brave_ads/core/internal/common/test/test_base.cc +++ b/components/brave_ads/core/internal/common/test/test_base.cc @@ -16,6 +16,7 @@ #include "base/test/task_environment.h" #include "brave/components/brave_ads/core/internal/account/tokens/token_generator_mock.h" #include "brave/components/brave_ads/core/internal/account/wallet/wallet_test_util.h" +#include "brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.h" #include "brave/components/brave_ads/core/internal/common/test/file_path_test_util.h" #include "brave/components/brave_ads/core/internal/common/test/internal/command_line_switch_test_util_internal.h" #include "brave/components/brave_ads/core/internal/common/test/internal/mock_test_util_internal.h" @@ -26,13 +27,13 @@ #include "brave/components/brave_ads/core/internal/common/test/test_constants.h" #include "brave/components/brave_ads/core/internal/common/test/test_types.h" #include "brave/components/brave_ads/core/internal/common/test/time_test_util.h" +#include "brave/components/brave_ads/core/internal/database/database.h" #include "brave/components/brave_ads/core/internal/database/database_manager.h" #include "brave/components/brave_ads/core/internal/deprecated/client/client_state_manager.h" #include "brave/components/brave_ads/core/internal/deprecated/confirmations/confirmation_state_manager.h" #include "brave/components/brave_ads/core/internal/global_state/global_state.h" #include "brave/components/brave_ads/core/public/ads.h" #include "brave/components/brave_ads/core/public/ads_constants.h" -#include "brave/components/brave_ads/core/public/database/database.h" namespace brave_ads::test { @@ -204,6 +205,10 @@ void TestBase::SimulateProfile() { std::cout << "SIMULATED PROFILE PATH: " << ProfilePath() << std::endl; } +base::FilePath TestBase::DatabasePath() const { + return ProfilePath().AppendASCII(kDatabaseFilename); +} + void TestBase::MockAdsClientNotifier() { MockAdsClientNotifierAddObserver(ads_client_mock_, *this); } @@ -234,10 +239,6 @@ void TestBase::MockAdsClient() { MockLoadDataResource(ads_client_mock_); - database_ = - std::make_unique(ProfilePath().AppendASCII(kDatabaseFilename)); - MockRunDBTransaction(ads_client_mock_, *database_); - MockFindProfilePref(ads_client_mock_); MockGetProfilePref(ads_client_mock_); MockSetProfilePref(ads_client_mock_, *this); @@ -298,7 +299,7 @@ void TestBase::SetUpIntegrationTest() { << "SetUpIntegrationTest should only be called if SetUp is initialized " "for integration testing"; - ads_ = Ads::CreateInstance(ads_client_mock_); + ads_ = Ads::CreateInstance(ads_client_mock_, DatabasePath()); CHECK(ads_) << "Failed to create ads instance"; // Must be called after `Ads` is instantiated but prior to `Initialize`. @@ -307,6 +308,8 @@ void TestBase::SetUpIntegrationTest() { ads_->Initialize(WalletAsPtr(), base::BindOnce(&TestBase::SetUpIntegrationTestCallback, weak_factory_.GetWeakPtr())); + + AdsClientNotifierWaiter(*this).WaitForAdsInitialization(); } void TestBase::SetUpIntegrationTestCallback(bool success) { @@ -328,7 +331,7 @@ void TestBase::SetUpUnitTest() { "SetUp is initialized for unit testing"; global_state_ = std::make_unique( - ads_client_mock_, std::make_unique()); + ads_client_mock_, DatabasePath(), std::make_unique()); // Must be called after `GlobalState` is instantiated but prior to // `MockDefaultAdsServiceState`. diff --git a/components/brave_ads/core/internal/common/test/test_base.h b/components/brave_ads/core/internal/common/test/test_base.h index 48f38795a3ca..8149e0d5f7b1 100644 --- a/components/brave_ads/core/internal/common/test/test_base.h +++ b/components/brave_ads/core/internal/common/test/test_base.h @@ -10,6 +10,7 @@ #include #include +#include "base/files/file_path.h" #include "base/files/scoped_temp_dir.h" #include "base/memory/weak_ptr.h" #include "base/test/task_environment.h" @@ -137,6 +138,7 @@ class TestBase : public AdsClientNotifierForTesting, public ::testing::Test { private: void SimulateProfile(); const base::FilePath& ProfilePath() const { return profile_dir_.GetPath(); } + base::FilePath DatabasePath() const; void MockAdsClientNotifier(); void MockAdsClient(); diff --git a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc index 6bd0187c0cf9..24ebf2c78025 100644 --- a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc +++ b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.cc @@ -23,7 +23,6 @@ #include "brave/components/brave_ads/core/internal/common/time/time_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -219,9 +218,8 @@ void CreativeSetConversions::GetUnexpired( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void CreativeSetConversions::GetActive( @@ -251,9 +249,8 @@ void CreativeSetConversions::GetActive( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void CreativeSetConversions::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_test.cc b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_test.cc index 6b8c57a7b390..b569045cbf5f 100644 --- a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_test.cc +++ b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_test.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" #include "brave/components/brave_ads/core/internal/common/test/mock_test_util.h" @@ -35,9 +37,12 @@ TEST_F(BraveAdsConversionsDatabaseTableIntegrationTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - /*creative_set_conversions=*/::testing::SizeIs(2))); + /*creative_set_conversions=*/::testing::SizeIs(2))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetUnexpired(callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_unittest.cc b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_unittest.cc index 919b27e76ccd..7a8e0d107117 100644 --- a/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_unittest.cc +++ b/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -28,9 +30,12 @@ TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, EmptySave) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - /*creative_set_conversions=*/::testing::IsEmpty())); + /*creative_set_conversions=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, @@ -58,8 +63,11 @@ TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, creative_set_conversions)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, creative_set_conversions)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, @@ -82,8 +90,11 @@ TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, creative_set_conversions)); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, creative_set_conversions)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, @@ -115,9 +126,13 @@ TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, CreativeSetConversionList{ - creative_set_conversion_1})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + CreativeSetConversionList{creative_set_conversion_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, @@ -151,9 +166,13 @@ TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, CreativeSetConversionList{ - creative_set_conversion_2})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + CreativeSetConversionList{creative_set_conversion_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeSetConversionDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc b/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc index 17cad5f39bbf..01f43e304f36 100644 --- a/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/creative_ads_database_table.cc @@ -14,13 +14,11 @@ #include "base/functional/bind.h" #include "base/strings/strcat.h" #include "base/strings/string_util.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_table_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_transaction_util.h" #include "brave/components/brave_ads/core/internal/common/logging_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -216,10 +214,9 @@ void CreativeAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } std::string CreativeAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc index 2338e4b150a7..f5d3e748f681 100644 --- a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.cc @@ -15,7 +15,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -26,7 +25,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -348,10 +346,9 @@ void CreativeInlineContentAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void CreativeInlineContentAds::GetForSegmentsAndDimensions( @@ -420,10 +417,9 @@ void CreativeInlineContentAds::GetForSegmentsAndDimensions( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForSegmentsAndDimensionsCallback, segments, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForSegmentsAndDimensionsCallback, + segments, std::move(callback))); } void CreativeInlineContentAds::GetForDimensions( @@ -481,7 +477,7 @@ void CreativeInlineContentAds::GetForDimensions( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForDimensionsCallback, std::move(callback))); } @@ -534,9 +530,8 @@ void CreativeInlineContentAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativeInlineContentAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_test.cc b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_test.cc index 9346e97267e3..48106390e300 100644 --- a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_test.cc +++ b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_test.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" #include "brave/components/brave_ads/core/internal/common/test/mock_test_util.h" @@ -34,14 +36,17 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableIntegrationTest, const database::table::CreativeInlineContentAds database_table; // Act & Assert + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/SegmentList{"technology & computing"}, - /*creative_ads=*/::testing::SizeIs(1))); + /*creative_ads=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForSegmentsAndDimensions( /*segments=*/{"technology & computing"}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableIntegrationTest, @@ -50,12 +55,15 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableIntegrationTest, const database::table::CreativeInlineContentAds database_table; // Act & Assert + base::RunLoop run_loop; base::MockCallback< database::table::GetCreativeInlineContentAdsForDimensionsCallback> callback; EXPECT_CALL(callback, Run(/*success=*/true, - /*creative_ads=*/::testing::SizeIs(1))); + /*creative_ads=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForDimensions("200x100", callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_unittest.cc b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_unittest.cc index 31f7bc377d2b..ee0373b0ce82 100644 --- a/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_unittest.cc +++ b/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -30,9 +32,12 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, SaveEmpty) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, Save) { @@ -46,11 +51,13 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, Save) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, SegmentList{"architecture", "arts & entertainment"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, SaveInBatches) { @@ -66,12 +73,14 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, SaveInBatches) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, - SegmentList{"architecture", "arts & entertainment", "automotive"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment", + "automotive"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, DoNotSaveDuplicates) { @@ -86,9 +95,12 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, DoNotSaveDuplicates) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)); + Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, @@ -119,11 +131,14 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"food & drink"}, - CreativeInlineContentAdList{creative_ad_1})); + CreativeInlineContentAdList{creative_ad_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegmentsAndDimensions(SegmentList{"food & drink"}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, GetForEmptySegments) { @@ -135,11 +150,14 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, GetForEmptySegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegmentsAndDimensions( /*segments=*/{}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, @@ -152,10 +170,13 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"NON_EXISTENT"}, - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegmentsAndDimensions( /*segments=*/{"NON_EXISTENT"}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, @@ -183,14 +204,17 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL( callback, Run(/*success=*/true, SegmentList{"technology & computing", "automotive"}, ::testing::UnorderedElementsAreArray( - CreativeInlineContentAdList{creative_ad_1, creative_ad_3}))); + CreativeInlineContentAdList{creative_ad_1, creative_ad_3}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegmentsAndDimensions( /*segments=*/{"technology & computing", "automotive"}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, @@ -211,10 +235,13 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - creative_ad_1.creative_instance_id, creative_ad_1)); + creative_ad_1.creative_instance_id, creative_ad_1)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(creative_ad_1.creative_instance_id, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, @@ -231,10 +258,13 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/false, test::kMissingCreativeInstanceId, - CreativeInlineContentAdInfo{})); + CreativeInlineContentAdInfo{})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(test::kMissingCreativeInstanceId, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, GetNonExpired) { @@ -260,10 +290,13 @@ TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, GetNonExpired) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{creative_ad_2.segment}, - CreativeInlineContentAdList{creative_ad_2})); + CreativeInlineContentAdList{creative_ad_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeInlineContentAdsDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc index d77b8122c4a3..b9a42da8afbd 100644 --- a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.cc @@ -15,7 +15,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_split.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -26,7 +25,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "brave/components/brave_ads/core/public/serving/targeting/condition_matcher/condition_matcher_util.h" #include "url/gurl.h" @@ -387,10 +385,9 @@ void CreativeNewTabPageAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void CreativeNewTabPageAds::GetForSegments( @@ -460,7 +457,7 @@ void CreativeNewTabPageAds::GetForSegments( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForSegmentsCallback, segments, std::move(callback))); } @@ -516,9 +513,8 @@ void CreativeNewTabPageAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativeNewTabPageAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_test.cc b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_test.cc index a9ae7016368b..f2ce719c1e0e 100644 --- a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_test.cc +++ b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_test.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" #include "brave/components/brave_ads/core/internal/common/test/mock_test_util.h" @@ -36,11 +38,14 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableIntegrationTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"technology & computing"}, - ::testing::SizeIs(1))); + ::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForSegments( /*segments=*/{"technology & computing"}, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_unittest.cc b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_unittest.cc index ec4384a7f114..74f5114aa044 100644 --- a/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_unittest.cc +++ b/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -29,9 +31,12 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, SaveEmpty) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, Save) { @@ -45,11 +50,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, Save) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, SegmentList{"architecture", "arts & entertainment"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, SaveInBatches) { @@ -65,12 +72,14 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, SaveInBatches) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, - SegmentList{"architecture", "arts & entertainment", "automotive"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment", + "automotive"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, DoNotSaveDuplicates) { @@ -85,9 +94,12 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, DoNotSaveDuplicates) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)); + Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetForSegments) { @@ -109,10 +121,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetForSegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"food & drink"}, - CreativeNewTabPageAdList{creative_ad_1})); + CreativeNewTabPageAdList{creative_ad_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"food & drink"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetForEmptySegments) { @@ -124,10 +139,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetForEmptySegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments(/*segments=*/{}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, @@ -140,10 +158,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"NON_EXISTENT"}, - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"NON_EXISTENT"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetForMultipleSegments) { @@ -170,13 +191,16 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetForMultipleSegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"technology & computing", "food & drink"}, ::testing::UnorderedElementsAreArray( - CreativeNewTabPageAdList{creative_ad_1, creative_ad_2}))); + CreativeNewTabPageAdList{creative_ad_1, creative_ad_2}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"technology & computing", "food & drink"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, @@ -196,10 +220,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - creative_ad_1.creative_instance_id, creative_ad_1)); + creative_ad_1.creative_instance_id, creative_ad_1)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(creative_ad_1.creative_instance_id, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, @@ -211,10 +238,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/false, test::kMissingCreativeInstanceId, - CreativeNewTabPageAdInfo{})); + CreativeNewTabPageAdInfo{})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(test::kMissingCreativeInstanceId, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetNonExpired) { @@ -240,10 +270,13 @@ TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetNonExpired) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{creative_ad_2.segment}, - CreativeNewTabPageAdList{creative_ad_2})); + CreativeNewTabPageAdList{creative_ad_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNewTabPageAdsDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc index 3ea29afede75..e31d21df809b 100644 --- a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.cc @@ -14,7 +14,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -25,7 +24,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -308,7 +306,7 @@ void CreativeNotificationAds::GetForSegments( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForSegmentsCallback, segments, std::move(callback))); } @@ -358,9 +356,8 @@ void CreativeNotificationAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativeNotificationAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_test.cc b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_test.cc index df29b2418d1a..e9790c8f2c9e 100644 --- a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_test.cc +++ b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_test.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" #include "brave/components/brave_ads/core/internal/common/test/mock_test_util.h" @@ -36,11 +38,14 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableIntegrationTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"technology & computing"}, - ::testing::SizeIs(2))); + ::testing::SizeIs(2))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForSegments( /*segments=*/{"technology & computing"}, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_unittest.cc b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_unittest.cc index 3527a5fd906e..3f6bb7a4d75e 100644 --- a/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_unittest.cc +++ b/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/common/test/time_test_util.h" @@ -27,9 +29,12 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, SaveEmpty) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, Save) { @@ -43,11 +48,13 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, Save) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, SegmentList{"architecture", "arts & entertainment"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, SaveInBatches) { @@ -63,12 +70,14 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, SaveInBatches) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, - SegmentList{"architecture", "arts & entertainment", "automotive"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment", + "automotive"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, DoNotSaveDuplicates) { @@ -83,9 +92,12 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, DoNotSaveDuplicates) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)); + Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetForSegments) { @@ -107,9 +119,12 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetForSegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"food & drink"}, - CreativeNotificationAdList{creative_ad_1})); + CreativeNotificationAdList{creative_ad_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments(/*segments=*/{"food & drink"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetForEmptySegments) { @@ -121,10 +136,13 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetForEmptySegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments(/*segments=*/{}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, @@ -137,10 +155,13 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"NON_EXISTENT"}, - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"NON_EXISTENT"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, @@ -168,14 +189,17 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL( callback, Run(/*success=*/true, SegmentList{"technology & computing", "food & drink"}, ::testing::UnorderedElementsAreArray( - CreativeNotificationAdList{creative_ad_1, creative_ad_2}))); + CreativeNotificationAdList{creative_ad_1, creative_ad_2}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"technology & computing", "food & drink"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetNonExpired) { @@ -201,10 +225,13 @@ TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetNonExpired) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{creative_ad_2.segment}, - CreativeNotificationAdList{creative_ad_2})); + CreativeNotificationAdList{creative_ad_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativeNotificationAdsDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc index efddadbac21e..0235499532be 100644 --- a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc +++ b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.cc @@ -14,7 +14,6 @@ #include "base/strings/strcat.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -25,7 +24,6 @@ #include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h" #include "brave/components/brave_ads/core/internal/segments/segment_util.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "url/gurl.h" namespace brave_ads::database::table { @@ -322,10 +320,9 @@ void CreativePromotedContentAds::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetForCreativeInstanceIdCallback, creative_instance_id, - std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetForCreativeInstanceIdCallback, + creative_instance_id, std::move(callback))); } void CreativePromotedContentAds::GetForSegments( @@ -390,7 +387,7 @@ void CreativePromotedContentAds::GetForSegments( mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&GetForSegmentsCallback, segments, std::move(callback))); } @@ -440,9 +437,8 @@ void CreativePromotedContentAds::GetForActiveCampaigns( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetAllCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetAllCallback, std::move(callback))); } std::string CreativePromotedContentAds::GetTableName() const { diff --git a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_test.cc b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_test.cc index ac616fb433ab..113159312c45 100644 --- a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_test.cc +++ b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_test.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/catalog/catalog_url_request_builder_util.h" #include "brave/components/brave_ads/core/internal/common/test/mock_test_util.h" @@ -34,13 +36,16 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableIntegrationTest, const database::table::CreativePromotedContentAds database_table; // Act & Assert + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"technology & computing"}, - ::testing::SizeIs(1))); + ::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table.GetForSegments( /*segments=*/{"technology & computing"}, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_unittest.cc b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_unittest.cc index 73c9c91c3cc1..fc7f946bb8b4 100644 --- a/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_unittest.cc +++ b/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/creatives/promoted_content_ads/creative_promoted_content_ads_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -29,9 +31,12 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, SaveEmpty) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, Save) { @@ -45,11 +50,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, Save) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, SegmentList{"architecture", "arts & entertainment"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, SaveInBatches) { @@ -65,12 +72,14 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, SaveInBatches) { // Assert base::MockCallback callback; - EXPECT_CALL( - callback, - Run(/*success=*/true, - SegmentList{"architecture", "arts & entertainment", "automotive"}, - ::testing::UnorderedElementsAreArray(creative_ads))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, + SegmentList{"architecture", "arts & entertainment", + "automotive"}, + ::testing::UnorderedElementsAreArray(creative_ads))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, @@ -86,9 +95,12 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)); + Run(/*success=*/true, SegmentList{"architecture"}, creative_ads)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, GetForSegments) { @@ -112,10 +124,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, GetForSegments) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"food & drink"}, - CreativePromotedContentAdList{creative_ad_1})); + CreativePromotedContentAdList{creative_ad_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"food & drink"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, @@ -128,10 +143,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, /*segments=*/::testing::IsEmpty(), - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments(/*segments=*/{}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, @@ -144,10 +162,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{"NON_EXISTENT"}, - /*creative_ads=*/::testing::IsEmpty())); + /*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"NON_EXISTENT"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, @@ -178,14 +199,17 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL( callback, Run(/*success=*/true, SegmentList{"technology & computing", "food & drink"}, ::testing::UnorderedElementsAreArray( - CreativePromotedContentAdList{creative_ad_1, creative_ad_2}))); + CreativePromotedContentAdList{creative_ad_1, creative_ad_2}))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForSegments( /*segments=*/{"technology & computing", "food & drink"}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, @@ -208,10 +232,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, - creative_ad_1.creative_instance_id, creative_ad_1)); + creative_ad_1.creative_instance_id, creative_ad_1)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(creative_ad_1.creative_instance_id, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, @@ -224,10 +251,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/false, test::kMissingCreativeInstanceId, - CreativePromotedContentAdInfo{})); + CreativePromotedContentAdInfo{})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(test::kMissingCreativeInstanceId, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, GetNonExpired) { @@ -255,10 +285,13 @@ TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, GetNonExpired) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(/*success=*/true, SegmentList{creative_ad_2.segment}, - CreativePromotedContentAdList{creative_ad_2})); + CreativePromotedContentAdList{creative_ad_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForActiveCampaigns(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsCreativePromotedContentAdsDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/database/database.cc b/components/brave_ads/core/internal/database/database.cc index 7af25532f6b6..f525dcbdb416 100644 --- a/components/brave_ads/core/internal/database/database.cc +++ b/components/brave_ads/core/internal/database/database.cc @@ -3,7 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#include "brave/components/brave_ads/core/public/database/database.h" +#include "brave/components/brave_ads/core/internal/database/database.h" #include #include diff --git a/components/brave_ads/core/public/database/database.h b/components/brave_ads/core/internal/database/database.h similarity index 92% rename from components/brave_ads/core/public/database/database.h rename to components/brave_ads/core/internal/database/database.h index 97a10195829f..923c94df91e8 100644 --- a/components/brave_ads/core/public/database/database.h +++ b/components/brave_ads/core/internal/database/database.h @@ -3,8 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ -#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_DATABASE_H_ -#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_DATABASE_H_ +#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_H_ #include @@ -80,4 +80,4 @@ class ADS_EXPORT Database final { } // namespace brave_ads -#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_PUBLIC_DATABASE_DATABASE_H_ +#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_H_ diff --git a/components/brave_ads/core/internal/database/database_manager.cc b/components/brave_ads/core/internal/database/database_manager.cc index 4ba35e4eaf8a..d925ff2b73e3 100644 --- a/components/brave_ads/core/internal/database/database_manager.cc +++ b/components/brave_ads/core/internal/database/database_manager.cc @@ -10,20 +10,25 @@ #include "base/check_op.h" #include "base/debug/dump_without_crashing.h" #include "base/functional/bind.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" +#include "base/task/task_traits.h" +#include "base/task/thread_pool.h" #include "brave/components/brave_ads/core/internal/common/database/database_transaction_util.h" #include "brave/components/brave_ads/core/internal/common/logging_util.h" +#include "brave/components/brave_ads/core/internal/database/database.h" #include "brave/components/brave_ads/core/internal/global_state/global_state.h" #include "brave/components/brave_ads/core/internal/legacy_migration/database/database_constants.h" #include "brave/components/brave_ads/core/internal/legacy_migration/database/database_creation.h" #include "brave/components/brave_ads/core/internal/legacy_migration/database/database_migration.h" #include "brave/components/brave_ads/core/internal/legacy_migration/database/database_raze.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads { -DatabaseManager::DatabaseManager() = default; +DatabaseManager::DatabaseManager(const base::FilePath& path) + : database_task_runner_(base::ThreadPool::CreateSequencedTaskRunner( + {base::MayBlock(), base::TaskPriority::BEST_EFFORT, + base::TaskShutdownBehavior::BLOCK_SHUTDOWN})), + database_(base::SequenceBound(database_task_runner_, path)) {} DatabaseManager::~DatabaseManager() = default; @@ -53,12 +58,23 @@ void DatabaseManager::CreateOrOpen(ResultCallback callback) { mojom_db_action->type = mojom::DBActionInfo::Type::kInitialize; mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( + RunDBTransaction( std::move(mojom_db_transaction), base::BindOnce(&DatabaseManager::CreateOrOpenCallback, weak_factory_.GetWeakPtr(), std::move(callback))); } +void DatabaseManager::RunDBTransaction( + mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback) { + CHECK(mojom_db_transaction); + CHECK(callback); + + database_.AsyncCall(&Database::RunDBTransaction) + .WithArgs(std::move(mojom_db_transaction)) + .Then(std::move(callback)); +} + /////////////////////////////////////////////////////////////////////////////// void DatabaseManager::CreateOrOpenCallback( diff --git a/components/brave_ads/core/internal/database/database_manager.h b/components/brave_ads/core/internal/database/database_manager.h index defdfb052714..a2dffdfa2ede 100644 --- a/components/brave_ads/core/internal/database/database_manager.h +++ b/components/brave_ads/core/internal/database/database_manager.h @@ -6,16 +6,26 @@ #ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_MANAGER_H_ #define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_DATABASE_DATABASE_MANAGER_H_ +#include "base/memory/scoped_refptr.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" +#include "base/threading/sequence_bound.h" #include "brave/components/brave_ads/core/internal/database/database_manager_observer.h" +#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" #include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h" +namespace base { +class FilePath; +class SequencedTaskRunner; +} // namespace base + namespace brave_ads { +class Database; + class DatabaseManager final { public: - DatabaseManager(); + explicit DatabaseManager(const base::FilePath& path); DatabaseManager(const DatabaseManager&) = delete; DatabaseManager& operator=(const DatabaseManager&) = delete; @@ -30,6 +40,11 @@ class DatabaseManager final { // Create or open the database. void CreateOrOpen(ResultCallback callback); + // Run a database transaction. The callback takes one argument - + // `mojom::DBTransactionResultInfoPtr` containing the info of the transaction. + void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction, + RunDBTransactionCallback callback); + private: void CreateOrOpenCallback( ResultCallback callback, @@ -60,6 +75,9 @@ class DatabaseManager final { void NotifyFailedToMigrateDatabase(int from_version, int to_version) const; void NotifyDatabaseIsReady() const; + const scoped_refptr database_task_runner_; + const base::SequenceBound database_; + base::ObserverList observers_; base::WeakPtrFactory weak_factory_{this}; diff --git a/components/brave_ads/core/internal/global_state/global_state.cc b/components/brave_ads/core/internal/global_state/global_state.cc index 57f93ceb808b..2b45e6cef6d6 100644 --- a/components/brave_ads/core/internal/global_state/global_state.cc +++ b/components/brave_ads/core/internal/global_state/global_state.cc @@ -28,6 +28,7 @@ namespace brave_ads { GlobalState::GlobalState( AdsClient& ads_client, + const base::FilePath& database_path, std::unique_ptr token_generator) : ads_client_(ads_client), global_state_holder_(std::make_unique(this)) { @@ -35,7 +36,7 @@ GlobalState::GlobalState( browser_manager_ = std::make_unique(); client_state_manager_ = std::make_unique(); confirmation_state_manager_ = std::make_unique(); - database_manager_ = std::make_unique(); + database_manager_ = std::make_unique(database_path); diagnostic_manager_ = std::make_unique(); ad_history_manager_ = std::make_unique(); notification_ad_manager_ = std::make_unique(); diff --git a/components/brave_ads/core/internal/global_state/global_state.h b/components/brave_ads/core/internal/global_state/global_state.h index 09cf350da87b..27a11a1b8a4e 100644 --- a/components/brave_ads/core/internal/global_state/global_state.h +++ b/components/brave_ads/core/internal/global_state/global_state.h @@ -8,6 +8,7 @@ #include +#include "base/files/file_path.h" #include "base/memory/raw_ref.h" #include "base/memory/weak_ptr.h" #include "base/sequence_checker.h" @@ -35,9 +36,9 @@ class UserActivityManager; class GlobalState final { public: - explicit GlobalState( - AdsClient& ads_client, - std::unique_ptr token_generator); + GlobalState(AdsClient& ads_client, + const base::FilePath& database_path, + std::unique_ptr token_generator); GlobalState(const GlobalState& other) = delete; GlobalState& operator=(const GlobalState& other) = delete; diff --git a/components/brave_ads/core/internal/history/ad_history_database_table.cc b/components/brave_ads/core/internal/history/ad_history_database_table.cc index 2048a725e201..f0d861718f7c 100644 --- a/components/brave_ads/core/internal/history/ad_history_database_table.cc +++ b/components/brave_ads/core/internal/history/ad_history_database_table.cc @@ -12,7 +12,6 @@ #include "base/debug/dump_without_crashing.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/containers/container_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" @@ -23,7 +22,6 @@ #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" #include "brave/components/brave_ads/core/public/ad_units/ad_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "brave/components/brave_ads/core/public/history/ad_history_feature.h" namespace brave_ads::database::table { @@ -264,9 +262,8 @@ void AdHistory::GetForDateRange(base::Time from_time, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdHistory::GetHighestRankedPlacementsForDateRange( @@ -368,9 +365,8 @@ void AdHistory::GetHighestRankedPlacementsForDateRange( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdHistory::GetForCreativeInstanceId( @@ -403,9 +399,8 @@ void AdHistory::GetForCreativeInstanceId( BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdHistory::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/history/ad_history_database_table_unittest.cc b/components/brave_ads/core/internal/history/ad_history_database_table_unittest.cc index 473cedd5af53..77bc85ab0b98 100644 --- a/components/brave_ads/core/internal/history/ad_history_database_table_unittest.cc +++ b/components/brave_ads/core/internal/history/ad_history_database_table_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/time/time.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" @@ -59,11 +61,14 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, Save) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(ad_history)))); + ::testing::UnorderedElementsAreArray(ad_history)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, SaveEmpty) { @@ -72,11 +77,14 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, SaveEmpty) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*ad_history=*/::testing::Optional(::testing::IsEmpty()))); + Run(/*ad_history=*/::testing::Optional(::testing::IsEmpty()))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, SaveInBatches) { @@ -95,11 +103,14 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, SaveInBatches) { // Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(ad_history)))); + ::testing::UnorderedElementsAreArray(ad_history)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, GetForDateRange) { @@ -124,11 +135,14 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, GetForDateRange) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(ad_history_2)))); + ::testing::UnorderedElementsAreArray(ad_history_2)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForDateRange(from_time, /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, @@ -171,11 +185,14 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, ASSERT_THAT(expected_ad_history, ::testing::SizeIs(3)); base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(expected_ad_history)))); + ::testing::UnorderedElementsAreArray(expected_ad_history)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetHighestRankedPlacementsForDateRange( from_time, /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, GetForCreativeInstanceId) { @@ -189,10 +206,13 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, GetForCreativeInstanceId) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(ad_history)))); + ::testing::UnorderedElementsAreArray(ad_history)))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(test::kCreativeInstanceId, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, @@ -207,10 +227,13 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*ad_history=*/::testing::Optional(::testing::IsEmpty()))); + Run(/*ad_history=*/::testing::Optional(::testing::IsEmpty()))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetForCreativeInstanceId(test::kCreativeInstanceId, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, PurgeExpired) { @@ -233,16 +256,22 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, PurgeExpired) { // Act & Assert base::MockCallback purge_expired_callback; - EXPECT_CALL(purge_expired_callback, Run(/*success=*/true)); + base::RunLoop run_loop; + EXPECT_CALL(purge_expired_callback, Run(/*success=*/true)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.PurgeExpired(purge_expired_callback.Get()); + run_loop.Run(); base::MockCallback callback; + base::RunLoop run_loop2; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(ad_history_2)))); + ::testing::UnorderedElementsAreArray(ad_history_2)))) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, DoNotPurgeOnTheCuspOfExpiration) { @@ -258,15 +287,21 @@ TEST_F(BraveAdsAdHistoryDatabaseTableTest, DoNotPurgeOnTheCuspOfExpiration) { // Act & Assert base::MockCallback purge_expired_callback; - EXPECT_CALL(purge_expired_callback, Run(/*success=*/true)); + base::RunLoop run_loop; + EXPECT_CALL(purge_expired_callback, Run(/*success=*/true)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.PurgeExpired(purge_expired_callback.Get()); + run_loop.Run(); base::MockCallback callback; + base::RunLoop run_loop2; EXPECT_CALL(callback, Run(::testing::Optional( - ::testing::UnorderedElementsAreArray(ad_history)))); + ::testing::UnorderedElementsAreArray(ad_history)))) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetForDateRange(/*from_time=*/test::DistantPast(), /*to_time=*/test::DistantFuture(), callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdHistoryDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/serving/eligible_ads/pipelines/inline_content_ads/eligible_inline_content_ads_v2_unittest.cc b/components/brave_ads/core/internal/serving/eligible_ads/pipelines/inline_content_ads/eligible_inline_content_ads_v2_unittest.cc index f1cd90baf667..8bbe4e44f5ff 100644 --- a/components/brave_ads/core/internal/serving/eligible_ads/pipelines/inline_content_ads/eligible_inline_content_ads_v2_unittest.cc +++ b/components/brave_ads/core/internal/serving/eligible_ads/pipelines/inline_content_ads/eligible_inline_content_ads_v2_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/creatives/inline_content_ads/creative_inline_content_ad_info.h" @@ -58,8 +60,10 @@ TEST_F(BraveAdsEligibleInlineContentAdsV2Test, GetAds) { database::SaveCreativeInlineContentAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(CreativeInlineContentAdList{creative_ad_1})); + EXPECT_CALL(callback, Run(CreativeInlineContentAdList{creative_ad_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel( UserModelInfo{ IntentUserModelInfo{}, @@ -67,6 +71,7 @@ TEST_F(BraveAdsEligibleInlineContentAdsV2Test, GetAds) { InterestUserModelInfo{SegmentList{"untargeted"}}, }, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleInlineContentAdsV2Test, GetAdsForNoMatchingSegments) { @@ -86,10 +91,13 @@ TEST_F(BraveAdsEligibleInlineContentAdsV2Test, GetAdsForNoMatchingSegments) { database::SaveCreativeInlineContentAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel(/*user_model=*/{}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleInlineContentAdsV2Test, @@ -104,22 +112,28 @@ TEST_F(BraveAdsEligibleInlineContentAdsV2Test, database::SaveCreativeInlineContentAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel(UserModelInfo{}, /*dimensions=*/"?x?", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleInlineContentAdsV2Test, DoNotGetAdsIfNoEligibleAds) { // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel( UserModelInfo{ IntentUserModelInfo{SegmentList{"parent-child", "parent"}}, LatentInterestUserModelInfo{}, InterestUserModelInfo{SegmentList{"parent-child", "parent"}}}, /*dimensions=*/"200x100", callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/serving/eligible_ads/pipelines/new_tab_page_ads/eligible_new_tab_page_ads_v2_unittest.cc b/components/brave_ads/core/internal/serving/eligible_ads/pipelines/new_tab_page_ads/eligible_new_tab_page_ads_v2_unittest.cc index 565ccacb589f..6948def36d0e 100644 --- a/components/brave_ads/core/internal/serving/eligible_ads/pipelines/new_tab_page_ads/eligible_new_tab_page_ads_v2_unittest.cc +++ b/components/brave_ads/core/internal/serving/eligible_ads/pipelines/new_tab_page_ads/eligible_new_tab_page_ads_v2_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ad_info.h" @@ -57,12 +59,15 @@ TEST_F(BraveAdsEligibleNewTabPageAdsV2Test, GetAds) { database::SaveCreativeNewTabPageAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(CreativeNewTabPageAdList{creative_ad_1})); + EXPECT_CALL(callback, Run(CreativeNewTabPageAdList{creative_ad_1})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel( UserModelInfo{IntentUserModelInfo{}, LatentInterestUserModelInfo{}, InterestUserModelInfo{SegmentList{"untargeted"}}}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleNewTabPageAdsV2Test, GetAdsForNoMatchingSegments) { @@ -82,21 +87,27 @@ TEST_F(BraveAdsEligibleNewTabPageAdsV2Test, GetAdsForNoMatchingSegments) { database::SaveCreativeNewTabPageAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel(/*user_model=*/{}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleNewTabPageAdsV2Test, DoNotGetAdsIfNoEligibleAds) { // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel( UserModelInfo{ IntentUserModelInfo{SegmentList{"parent-child", "parent"}}, LatentInterestUserModelInfo{}, InterestUserModelInfo{SegmentList{"parent-child", "parent"}}}, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/serving/eligible_ads/pipelines/notification_ads/eligible_notification_ads_v2_unittest.cc b/components/brave_ads/core/internal/serving/eligible_ads/pipelines/notification_ads/eligible_notification_ads_v2_unittest.cc index 514cf0da9464..90c48c0218a7 100644 --- a/components/brave_ads/core/internal/serving/eligible_ads/pipelines/notification_ads/eligible_notification_ads_v2_unittest.cc +++ b/components/brave_ads/core/internal/serving/eligible_ads/pipelines/notification_ads/eligible_notification_ads_v2_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ad_info.h" @@ -53,14 +55,17 @@ TEST_F(BraveAdsEligibleNotificationAdsV2Test, GetAds) { database::SaveCreativeNotificationAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::SizeIs(1))); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::SizeIs(1))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel( UserModelInfo{ IntentUserModelInfo{SegmentList{"parent-child-1", "parent-child-2"}}, LatentInterestUserModelInfo{}, InterestUserModelInfo{SegmentList{"parent-child-3"}}}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleNotificationAdsV2Test, GetAdsForNoMatchingSegments) { @@ -80,21 +85,27 @@ TEST_F(BraveAdsEligibleNotificationAdsV2Test, GetAdsForNoMatchingSegments) { database::SaveCreativeNotificationAds(creative_ads); // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel(/*user_model=*/{}, callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsEligibleNotificationAdsV2Test, DoNotGetAdsIfNoEligibleAds) { // Act & Assert + base::RunLoop run_loop; base::MockCallback> callback; - EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())); + EXPECT_CALL(callback, Run(/*creative_ads=*/::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); eligible_ads_->GetForUserModel( UserModelInfo{ IntentUserModelInfo{SegmentList{"parent-child", "parent"}}, LatentInterestUserModelInfo{}, InterestUserModelInfo{SegmentList{"parent-child", "parent"}}}, callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/serving/inline_content_ad_serving_unittest.cc b/components/brave_ads/core/internal/serving/inline_content_ad_serving_unittest.cc index 86d323cd6f69..b20f61eb1d4c 100644 --- a/components/brave_ads/core/internal/serving/inline_content_ad_serving_unittest.cc +++ b/components/brave_ads/core/internal/serving/inline_content_ad_serving_unittest.cc @@ -5,8 +5,11 @@ #include "brave/components/brave_ads/core/internal/serving/inline_content_ad_serving.h" +#include #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -36,14 +39,15 @@ class BraveAdsInlineContentAdServingTest : public test::TestBase { SubdivisionTargeting subdivision_targeting; AntiTargetingResource anti_targeting_resource; - InlineContentAdServing ad_serving(subdivision_targeting, - anti_targeting_resource); - ad_serving.SetDelegate(&delegate_mock_); + ad_serving_ = std::make_unique( + subdivision_targeting, anti_targeting_resource); + ad_serving_->SetDelegate(&delegate_mock_); - ad_serving.MaybeServeAd(dimensions, std::move(callback)); + ad_serving_->MaybeServeAd(dimensions, std::move(callback)); } ::testing::StrictMock delegate_mock_; + std::unique_ptr ad_serving_; }; TEST_F(BraveAdsInlineContentAdServingTest, DoNotServeAdForUnsupportedVersion) { @@ -61,10 +65,13 @@ TEST_F(BraveAdsInlineContentAdServingTest, DoNotServeAdForUnsupportedVersion) { // Act & Assert EXPECT_CALL(delegate_mock_, OnFailedToServeInlineContentAd); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run(/*dimensions=*/"200x100", - /*ad=*/::testing::Eq(std::nullopt))); + /*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd("200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdServingTest, ServeAd) { @@ -81,10 +88,13 @@ TEST_F(BraveAdsInlineContentAdServingTest, ServeAd) { EXPECT_CALL(delegate_mock_, OnDidServeInlineContentAd); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run(/*dimensions=*/"200x100", - /*ad=*/::testing::Ne(std::nullopt))); + /*ad=*/::testing::Ne(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd("200x100", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdServingTest, @@ -101,10 +111,13 @@ TEST_F(BraveAdsInlineContentAdServingTest, EXPECT_CALL(delegate_mock_, OnFailedToServeInlineContentAd); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, - Run(/*dimensions=*/"?x?", /*ad=*/::testing::Eq(std::nullopt))); + Run(/*dimensions=*/"?x?", /*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd("?x?", callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdServingTest, @@ -117,10 +130,13 @@ TEST_F(BraveAdsInlineContentAdServingTest, // Act & Assert EXPECT_CALL(delegate_mock_, OnFailedToServeInlineContentAd); + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run(/*dimensions=*/"200x100", - /*ad=*/::testing::Eq(std::nullopt))); + /*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd("200x100", callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/serving/new_tab_page_ad_serving_unittest.cc b/components/brave_ads/core/internal/serving/new_tab_page_ad_serving_unittest.cc index 8ef2c8e089ca..b12ca316735d 100644 --- a/components/brave_ads/core/internal/serving/new_tab_page_ad_serving_unittest.cc +++ b/components/brave_ads/core/internal/serving/new_tab_page_ad_serving_unittest.cc @@ -7,6 +7,8 @@ #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -31,14 +33,15 @@ class BraveAdsNewTabPageAdServingTest : public test::TestBase { void MaybeServeAd(MaybeServeNewTabPageAdCallback callback) { SubdivisionTargeting subdivision_targeting; AntiTargetingResource anti_targeting_resource; - NewTabPageAdServing ad_serving(subdivision_targeting, - anti_targeting_resource); - ad_serving.SetDelegate(&delegate_mock_); + ad_serving_ = std::make_unique( + subdivision_targeting, anti_targeting_resource); + ad_serving_->SetDelegate(&delegate_mock_); - ad_serving.MaybeServeAd(std::move(callback)); + ad_serving_->MaybeServeAd(std::move(callback)); } ::testing::StrictMock delegate_mock_; + std::unique_ptr ad_serving_; }; TEST_F(BraveAdsNewTabPageAdServingTest, DoNotServeAdForUnsupportedVersion) { @@ -57,8 +60,11 @@ TEST_F(BraveAdsNewTabPageAdServingTest, DoNotServeAdForUnsupportedVersion) { EXPECT_CALL(delegate_mock_, OnFailedToServeNewTabPageAd); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdServingTest, ServeAd) { @@ -76,8 +82,11 @@ TEST_F(BraveAdsNewTabPageAdServingTest, ServeAd) { EXPECT_CALL(delegate_mock_, OnDidServeNewTabPageAd); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Ne(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Ne(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdServingTest, DoNotServeAdIfMissingWallpapers) { @@ -95,8 +104,11 @@ TEST_F(BraveAdsNewTabPageAdServingTest, DoNotServeAdIfMissingWallpapers) { EXPECT_CALL(delegate_mock_, OnFailedToServeNewTabPageAd); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdServingTest, DoNotServeAdIfNoEligibleAdsFound) { @@ -109,8 +121,11 @@ TEST_F(BraveAdsNewTabPageAdServingTest, DoNotServeAdIfNoEligibleAdsFound) { EXPECT_CALL(delegate_mock_, OnFailedToServeNewTabPageAd); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdServingTest, @@ -124,8 +139,11 @@ TEST_F(BraveAdsNewTabPageAdServingTest, EXPECT_CALL(delegate_mock_, OnFailedToServeNewTabPageAd); base::MockCallback callback; - EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*ad=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/serving/notification_ad_serving_unittest.cc b/components/brave_ads/core/internal/serving/notification_ad_serving_unittest.cc index 70bf37c5ce49..555fdf3753eb 100644 --- a/components/brave_ads/core/internal/serving/notification_ad_serving_unittest.cc +++ b/components/brave_ads/core/internal/serving/notification_ad_serving_unittest.cc @@ -5,6 +5,10 @@ #include "brave/components/brave_ads/core/internal/serving/notification_ad_serving.h" +#include + +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/creatives/notification_ads/creative_notification_ad_test_util.h" @@ -32,14 +36,15 @@ class BraveAdsNotificationAdServingTest : public test::TestBase { void MaybeServeAd() { SubdivisionTargeting subdivision_targeting; AntiTargetingResource anti_targeting_resource; - NotificationAdServing ad_serving(subdivision_targeting, - anti_targeting_resource); - ad_serving.SetDelegate(&delegate_mock_); + ad_serving_ = std::make_unique( + subdivision_targeting, anti_targeting_resource); + ad_serving_->SetDelegate(&delegate_mock_); - ad_serving.MaybeServeAd(); + ad_serving_->MaybeServeAd(); } ::testing::StrictMock delegate_mock_; + std::unique_ptr ad_serving_; }; TEST_F(BraveAdsNotificationAdServingTest, DoNotServeAdForUnsupportedVersion) { @@ -55,8 +60,11 @@ TEST_F(BraveAdsNotificationAdServingTest, DoNotServeAdForUnsupportedVersion) { database::SaveCreativeNotificationAds({creative_ad}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnFailedToServeNotificationAd); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToServeNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdServingTest, ServeAd) { @@ -68,9 +76,15 @@ TEST_F(BraveAdsNotificationAdServingTest, ServeAd) { database::SaveCreativeNotificationAds({creative_ad}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnOpportunityAroseToServeNotificationAd); - EXPECT_CALL(delegate_mock_, OnDidServeNotificationAd); + base::RunLoop run_loop1; + EXPECT_CALL(delegate_mock_, OnOpportunityAroseToServeNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); + base::RunLoop run_loop2; + EXPECT_CALL(delegate_mock_, OnDidServeNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); MaybeServeAd(); + run_loop1.Run(); + run_loop2.Run(); } TEST_F(BraveAdsNotificationAdServingTest, DoNotServeAdIfNoEligibleAdsFound) { @@ -78,9 +92,15 @@ TEST_F(BraveAdsNotificationAdServingTest, DoNotServeAdIfNoEligibleAdsFound) { test::ForcePermissionRules(); // Act & Assert - EXPECT_CALL(delegate_mock_, OnOpportunityAroseToServeNotificationAd); - EXPECT_CALL(delegate_mock_, OnFailedToServeNotificationAd); + base::RunLoop run_loop1; + EXPECT_CALL(delegate_mock_, OnOpportunityAroseToServeNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop1.QuitClosure())); + base::RunLoop run_loop2; + EXPECT_CALL(delegate_mock_, OnFailedToServeNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); MaybeServeAd(); + run_loop1.Run(); + run_loop2.Run(); } TEST_F(BraveAdsNotificationAdServingTest, @@ -91,8 +111,11 @@ TEST_F(BraveAdsNotificationAdServingTest, database::SaveCreativeNotificationAds({creative_ad}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnFailedToServeNotificationAd); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToServeNotificationAd) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); MaybeServeAd(); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc index 71cca2ad2d28..1c9ff2fba76e 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.cc @@ -12,7 +12,6 @@ #include "base/functional/bind.h" #include "base/strings/string_util.h" #include "base/time/time.h" -#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_column_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_statement_util.h" #include "brave/components/brave_ads/core/internal/common/database/database_table_util.h" @@ -23,7 +22,6 @@ #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_ads/core/public/account/confirmations/confirmation_type.h" #include "brave/components/brave_ads/core/public/ad_units/ad_type.h" -#include "brave/components/brave_ads/core/public/ads_client/ads_client.h" namespace brave_ads::database::table { @@ -205,9 +203,8 @@ void AdEvents::GetAll(GetAdEventsCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::Get(mojom::AdType mojom_ad_type, @@ -245,9 +242,8 @@ void AdEvents::Get(mojom::AdType mojom_ad_type, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::GetUnexpired(GetAdEventsCallback callback) const { @@ -285,9 +281,8 @@ void AdEvents::GetUnexpired(GetAdEventsCallback callback) const { BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::GetUnexpired(mojom::AdType mojom_ad_type, @@ -329,9 +324,8 @@ void AdEvents::GetUnexpired(mojom::AdType mojom_ad_type, BindColumnTypes(mojom_db_action); mojom_db_transaction->actions.push_back(std::move(mojom_db_action)); - GetAdsClient().RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce(&GetCallback, std::move(callback))); + RunDBTransaction(std::move(mojom_db_transaction), + base::BindOnce(&GetCallback, std::move(callback))); } void AdEvents::PurgeExpired(ResultCallback callback) const { diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table_unittest.cc index 6ca85dd7b8b1..ca0afdba81f4 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_database_table.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test_util.h" #include "brave/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_test_util.h" @@ -45,8 +47,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, RecordEvent) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, Get) { @@ -106,11 +111,14 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, Get) { // Act & Assert base::MockCallback callback; + base::RunLoop run_loop; EXPECT_CALL(callback, - Run(/*success=*/true, AdEventList{ad_event_2, ad_event_5})); + Run(/*success=*/true, AdEventList{ad_event_2, ad_event_5})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.Get(mojom::AdType::kNotificationAd, mojom::ConfirmationType::kServedImpression, /*time_window=*/base::Days(1), callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, GetUnexpired) { @@ -143,8 +151,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, GetUnexpired) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_2})); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_2})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, @@ -178,8 +189,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, GetUnexpiredOnTheCuspOfExpiry) { @@ -203,8 +217,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, GetUnexpiredOnTheCuspOfExpiry) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetUnexpired(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, GetUnexpiredForAdType) { @@ -246,8 +263,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, GetUnexpiredForAdType) { // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_3})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_3})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetUnexpired(mojom::AdType::kNewTabPageAd, callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, @@ -291,8 +311,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, // Act & Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_1})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_1})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetUnexpired(mojom::AdType::kNotificationAd, callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeExpired) { @@ -328,8 +351,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeExpired) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_2})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_2})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeExpiredForNonRewardsUser) { @@ -367,8 +393,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeExpiredForNonRewardsUser) { // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_2})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event_2})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, @@ -406,8 +435,11 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})); + base::RunLoop run_loop2; + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeOrphanedForType) { @@ -459,12 +491,15 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeOrphanedForType) { // Assert base::MockCallback callback; + base::RunLoop run_loop2; EXPECT_CALL( callback, Run(/*success=*/true, ::testing::UnorderedElementsAreArray(AdEventList{ - ad_event_1_served, ad_event_1_viewed, ad_event_2_served}))); + ad_event_1_served, ad_event_1_viewed, ad_event_2_served}))) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeOrphaned) { @@ -514,12 +549,15 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeOrphaned) { // Assert base::MockCallback callback; + base::RunLoop run_loop2; EXPECT_CALL( callback, Run(/*success=*/true, ::testing::UnorderedElementsAreArray(AdEventList{ - ad_event_1_served, ad_event_1_viewed, ad_event_3_served}))); + ad_event_1_served, ad_event_1_viewed, ad_event_3_served}))) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeAllOrphaned) { @@ -567,10 +605,13 @@ TEST_F(BraveAdsAdEventsDatabaseTableTest, PurgeAllOrphaned) { // Assert base::MockCallback callback; + base::RunLoop run_loop2; EXPECT_CALL(callback, Run(/*success=*/true, ::testing::UnorderedElementsAreArray(AdEventList{ - ad_event_1_served, ad_event_1_viewed}))); + ad_event_1_served, ad_event_1_viewed}))) + .WillOnce(base::test::RunOnceClosure(run_loop2.QuitClosure())); database_table_.GetAll(callback.Get()); + run_loop2.Run(); } TEST_F(BraveAdsAdEventsDatabaseTableTest, GetTableName) { diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_unittest.cc index e71c33f07110..02844a29036e 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/ad_events_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/ad_events.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_util.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" @@ -37,10 +39,13 @@ TEST_F(BraveAdsAdEventsTest, RecordAdEvent) { record_ad_event_callback.Get()); // Assert + base::RunLoop run_loop; base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})); + EXPECT_CALL(callback, Run(/*success=*/true, AdEventList{ad_event})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::AdEvents database_table; database_table.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsAdEventsTest, PurgeOrphanedAdEvents) { @@ -90,11 +95,14 @@ TEST_F(BraveAdsAdEventsTest, PurgeOrphanedAdEvents) { purge_orphaned_ad_events_callback.Get()); // Assert + base::RunLoop run_loop; base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true, - AdEventList{ad_event_2a, ad_event_2b, ad_event_3})); + AdEventList{ad_event_2a, ad_event_2b, ad_event_3})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::AdEvents database_table; database_table.GetUnexpired(callback.Get()); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/inline_content_ads/inline_content_ad_event_handler_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/inline_content_ads/inline_content_ad_event_handler_unittest.cc index cfd29c9d7f26..46d7c9bd701d 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/inline_content_ads/inline_content_ad_event_handler_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/inline_content_ads/inline_content_ad_event_handler_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/inline_content_ads/inline_content_ad_event_handler.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/ad_units/inline_content_ad/inline_content_ad_test_util.h" @@ -32,10 +34,13 @@ class BraveAdsInlineContentAdEventHandlerTest : public test::TestBase { mojom::InlineContentAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, placement_id, - mojom_ad_event_type)); + mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } InlineContentAdEventHandler event_handler_; @@ -48,11 +53,14 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, FireServedEvent) { test::BuildAndSaveInlineContentAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireInlineContentAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireInlineContentAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, FireViewedEvent) { @@ -62,11 +70,14 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, FireViewedEvent) { test::RecordAdEvent(ad, mojom::ConfirmationType::kServedImpression); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireInlineContentAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireInlineContentAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, @@ -78,14 +89,17 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::InlineContentAdEventType::kViewedImpression)); + mojom::InlineContentAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, @@ -95,14 +109,17 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, test::BuildAndSaveInlineContentAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::InlineContentAdEventType::kViewedImpression)); + mojom::InlineContentAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, FireClickedEvent) { @@ -113,10 +130,13 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, FireClickedEvent) { mojom::ConfirmationType::kViewedImpression}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireInlineContentAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireInlineContentAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, @@ -129,12 +149,15 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, mojom::ConfirmationType::kClicked}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::InlineContentAdEventType::kClicked)); + mojom::InlineContentAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, @@ -144,38 +167,47 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, test::BuildAndSaveInlineContentAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::InlineContentAdEventType::kClicked)); + mojom::InlineContentAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::InlineContentAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, DoNotFireEventWithInvalidPlacementId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( test::kInvalidPlacementId, test::kCreativeInstanceId, - mojom::InlineContentAdEventType::kServedImpression)); + mojom::InlineContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kInvalidPlacementId, test::kCreativeInstanceId, mojom::InlineContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, DoNotFireEventWithInvalidCreativeInstanceId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( test::kPlacementId, test::kInvalidCreativeInstanceId, - mojom::InlineContentAdEventType::kServedImpression)); + mojom::InlineContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kPlacementId, test::kInvalidCreativeInstanceId, mojom::InlineContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsInlineContentAdEventHandlerTest, @@ -185,14 +217,17 @@ TEST_F(BraveAdsInlineContentAdEventHandlerTest, test::BuildAndSaveInlineContentAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireInlineContentAdEvent( ad.placement_id, test::kMissingCreativeInstanceId, - mojom::InlineContentAdEventType::kServedImpression)); + mojom::InlineContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, test::kMissingCreativeInstanceId, mojom::InlineContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_non_rewards_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_non_rewards_unittest.cc index d056b329c893..a2e8832a820f 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_non_rewards_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_non_rewards_unittest.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test_util.h" @@ -36,10 +38,13 @@ class BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest mojom::NewTabPageAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, placement_id, - mojom_ad_event_type)); + mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } NewTabPageAdEventHandler event_handler_; @@ -53,11 +58,14 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -68,11 +76,14 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, test::RecordAdEvent(ad, mojom::ConfirmationType::kServedImpression); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -84,14 +95,17 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kViewedImpression)); + mojom::NewTabPageAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -101,14 +115,17 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kViewedImpression)); + mojom::NewTabPageAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -120,10 +137,13 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -136,12 +156,15 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, mojom::ConfirmationType::kClicked}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kClicked)); + mojom::NewTabPageAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -151,38 +174,47 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kClicked)); + mojom::NewTabPageAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, DoNotFireEventWithInvalidPlacementId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( test::kInvalidPlacementId, test::kCreativeInstanceId, - mojom::NewTabPageAdEventType::kServedImpression)); + mojom::NewTabPageAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kInvalidPlacementId, test::kCreativeInstanceId, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, DoNotFireEventWithInvalidCreativeInstanceId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( test::kPlacementId, test::kInvalidCreativeInstanceId, - mojom::NewTabPageAdEventType::kServedImpression)); + mojom::NewTabPageAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kPlacementId, test::kInvalidCreativeInstanceId, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, @@ -192,14 +224,17 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerIfUserHasNotJoinedBraveRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, test::kMissingCreativeInstanceId, - mojom::NewTabPageAdEventType::kServedImpression)); + mojom::NewTabPageAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, test::kMissingCreativeInstanceId, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_rewards_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_rewards_unittest.cc index ae8709646cd0..6aa58edcbbe2 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_rewards_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/new_tab_page_ads/new_tab_page_ad_event_handler_for_rewards_unittest.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/ad_units/new_tab_page_ad/new_tab_page_ad_test_util.h" @@ -32,10 +34,13 @@ class BraveAdsNewTabPageAdEventHandlerForRewardsTest : public test::TestBase { mojom::NewTabPageAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, placement_id, - mojom_ad_event_type)); + mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } NewTabPageAdEventHandler event_handler_; @@ -48,11 +53,14 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, FireServedEvent) { test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, FireViewedEvent) { @@ -62,11 +70,14 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, FireViewedEvent) { test::RecordAdEvent(ad, mojom::ConfirmationType::kServedImpression); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, @@ -78,14 +89,17 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kViewedImpression)); + mojom::NewTabPageAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, @@ -95,14 +109,17 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kViewedImpression)); + mojom::NewTabPageAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, FireClickedEvent) { @@ -113,10 +130,13 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, FireClickedEvent) { mojom::ConfirmationType::kViewedImpression}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNewTabPageAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, @@ -129,12 +149,15 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, mojom::ConfirmationType::kClicked}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kClicked)); + mojom::NewTabPageAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, @@ -144,38 +167,47 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::NewTabPageAdEventType::kClicked)); + mojom::NewTabPageAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::NewTabPageAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, DoNotFireEventWithInvalidPlacementId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( test::kInvalidPlacementId, test::kCreativeInstanceId, - mojom::NewTabPageAdEventType::kServedImpression)); + mojom::NewTabPageAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kInvalidPlacementId, test::kCreativeInstanceId, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, DoNotFireEventWithInvalidCreativeInstanceId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( test::kPlacementId, test::kInvalidCreativeInstanceId, - mojom::NewTabPageAdEventType::kServedImpression)); + mojom::NewTabPageAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kPlacementId, test::kInvalidCreativeInstanceId, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, @@ -185,14 +217,17 @@ TEST_F(BraveAdsNewTabPageAdEventHandlerForRewardsTest, test::BuildAndSaveNewTabPageAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNewTabPageAdEvent( ad.placement_id, test::kMissingCreativeInstanceId, - mojom::NewTabPageAdEventType::kServedImpression)); + mojom::NewTabPageAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, test::kMissingCreativeInstanceId, mojom::NewTabPageAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/notification_ads/notification_ad_event_handler_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/notification_ads/notification_ad_event_handler_unittest.cc index e28f562982e6..6c4fe8c9de36 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/notification_ads/notification_ad_event_handler_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/notification_ads/notification_ad_event_handler_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/notification_ads/notification_ad_event_handler.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/ad_units/notification_ad/notification_ad_test_util.h" @@ -32,9 +34,12 @@ class BraveAdsNotificationAdEventHandlerTest : public test::TestBase { mojom::NotificationAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, placement_id, - mojom_ad_event_type)); + mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(placement_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } NotificationAdEventHandler event_handler_; @@ -47,10 +52,13 @@ TEST_F(BraveAdsNotificationAdEventHandlerTest, FireServedEvent) { test::BuildAndSaveNotificationAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, mojom::NotificationAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdEventHandlerTest, FireViewedEvent) { @@ -59,10 +67,13 @@ TEST_F(BraveAdsNotificationAdEventHandlerTest, FireViewedEvent) { test::BuildAndSaveNotificationAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, mojom::NotificationAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdEventHandlerTest, FireClickedEvent) { @@ -71,10 +82,13 @@ TEST_F(BraveAdsNotificationAdEventHandlerTest, FireClickedEvent) { test::BuildAndSaveNotificationAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, mojom::NotificationAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdEventHandlerTest, FireDismissedEvent) { @@ -83,10 +97,13 @@ TEST_F(BraveAdsNotificationAdEventHandlerTest, FireDismissedEvent) { test::BuildAndSaveNotificationAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdDismissedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdDismissedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, mojom::NotificationAdEventType::kDismissed, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdEventHandlerTest, FireTimedOutEvent) { @@ -95,10 +112,13 @@ TEST_F(BraveAdsNotificationAdEventHandlerTest, FireTimedOutEvent) { test::BuildAndSaveNotificationAd(/*should_generate_random_uuids=*/false); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdTimedOutEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireNotificationAdTimedOutEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, mojom::NotificationAdEventType::kTimedOut, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsNotificationAdEventHandlerTest, @@ -108,14 +128,17 @@ TEST_F(BraveAdsNotificationAdEventHandlerTest, test::BuildAndSaveNotificationAd(/*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireNotificationAdEvent( test::kMissingPlacementId, - mojom::NotificationAdEventType::kViewedImpression)); + mojom::NotificationAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kMissingPlacementId, mojom::NotificationAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/promoted_content_ads/promoted_content_ad_event_handler_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/promoted_content_ads/promoted_content_ad_event_handler_unittest.cc index 9330f6434011..f411edf61bd2 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/promoted_content_ads/promoted_content_ad_event_handler_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/promoted_content_ads/promoted_content_ad_event_handler_unittest.cc @@ -5,6 +5,8 @@ #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/promoted_content_ads/promoted_content_ad_event_handler.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" #include "brave/components/brave_ads/core/internal/ad_units/promoted_content_ad/promoted_content_ad_feature.h" @@ -37,10 +39,13 @@ class BraveAdsPromotedContentAdEventHandlerTest : public test::TestBase { mojom::PromotedContentAdEventType mojom_ad_event_type, bool should_fire_event) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, placement_id, - mojom_ad_event_type)); + mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(placement_id, creative_instance_id, mojom_ad_event_type, callback.Get()); + run_loop.Run(); } PromotedContentAdEventHandler event_handler_; @@ -55,11 +60,14 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, FireViewedEvent) { test::RecordAdEvent(ad, mojom::ConfirmationType::kServedImpression); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -71,14 +79,17 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::PromotedContentAdEventType::kViewedImpression)); + mojom::PromotedContentAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -88,14 +99,17 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, /*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::PromotedContentAdEventType::kViewedImpression)); + mojom::PromotedContentAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, FireClickedEvent) { @@ -106,10 +120,13 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, FireClickedEvent) { mojom::ConfirmationType::kViewedImpression}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -122,12 +139,15 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, mojom::ConfirmationType::kClicked}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::PromotedContentAdEventType::kClicked)); + mojom::PromotedContentAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -137,38 +157,47 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, /*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::PromotedContentAdEventType::kClicked)); + mojom::PromotedContentAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, DoNotFireEventWithInvalidPlacementId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( test::kInvalidPlacementId, test::kCreativeInstanceId, - mojom::PromotedContentAdEventType::kServedImpression)); + mojom::PromotedContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kInvalidPlacementId, test::kCreativeInstanceId, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, DoNotFireEventWithInvalidCreativeInstanceId) { // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( test::kPlacementId, test::kInvalidCreativeInstanceId, - mojom::PromotedContentAdEventType::kServedImpression)); + mojom::PromotedContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( test::kPlacementId, test::kInvalidCreativeInstanceId, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -178,14 +207,17 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, /*should_generate_random_uuids=*/false); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, test::kMissingCreativeInstanceId, - mojom::PromotedContentAdEventType::kServedImpression)); + mojom::PromotedContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, test::kMissingCreativeInstanceId, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -199,11 +231,14 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, AdvanceClockBy(base::Hours(1) - base::Milliseconds(1)); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -217,14 +252,17 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, AdvanceClockBy(base::Hours(1) - base::Milliseconds(1)); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::PromotedContentAdEventType::kServedImpression)); + mojom::PromotedContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -238,11 +276,14 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, AdvanceClockBy(base::Days(1) - base::Milliseconds(1)); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFirePromotedContentAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsPromotedContentAdEventHandlerTest, @@ -256,14 +297,17 @@ TEST_F(BraveAdsPromotedContentAdEventHandlerTest, AdvanceClockBy(base::Days(1) - base::Milliseconds(1)); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFirePromotedContentAdEvent( ad.placement_id, ad.creative_instance_id, - mojom::PromotedContentAdEventType::kServedImpression)); + mojom::PromotedContentAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( ad.placement_id, ad.creative_instance_id, mojom::PromotedContentAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_non_rewards_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_non_rewards_unittest.cc index 189134f7852b..c319a8e30592 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_non_rewards_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_non_rewards_unittest.cc @@ -7,6 +7,8 @@ #include #include "base/check.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/account/deposits/deposits_database_table.h" @@ -33,21 +35,27 @@ namespace { void VerifyDepositForCreativeInstanceIdExpectation( const std::string& creative_instance_id) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/::testing::_, - /*deposit=*/::testing::Eq(std::nullopt))); + /*deposit=*/::testing::Eq(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::Deposits database_table; database_table.GetForCreativeInstanceId(creative_instance_id, callback.Get()); + run_loop.Run(); } void VerifyCreativeSetConversionExpectation(size_t expected_count) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL( callback, Run(/*success=*/::testing::_, - /*creative_set_conversions=*/::testing::SizeIs(expected_count))); + /*creative_set_conversions=*/::testing::SizeIs(expected_count))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::CreativeSetConversions database_table; database_table.GetUnexpired(callback.Get()); + run_loop.Run(); } } // namespace @@ -73,11 +81,14 @@ class BraveAdsSearchResultAdEventHandlerForNonRewardsTest CHECK(mojom_creative_ad); base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, - mojom_creative_ad->placement_id, mojom_ad_event_type)); + mojom_creative_ad->placement_id, mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(mojom_creative_ad.Clone(), mojom_ad_event_type, callback.Get()); + run_loop.Run(); size_t expected_count = 0; @@ -112,12 +123,14 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, - OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kClicked)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( + ad, mojom::SearchResultAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -129,12 +142,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kServedImpression)); + ad, mojom::SearchResultAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -146,12 +162,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kServedImpression)); + ad, mojom::SearchResultAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -163,12 +182,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kViewedImpression)); + ad, mojom::SearchResultAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -180,12 +202,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kViewedImpression)); + ad, mojom::SearchResultAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -196,12 +221,14 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, - OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kClicked)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( + ad, mojom::SearchResultAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -213,10 +240,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -229,12 +259,14 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, test::RecordAdEvent(ad, mojom::ConfirmationType::kClicked); // Act & Assert - EXPECT_CALL(delegate_mock_, - OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kClicked)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( + ad, mojom::SearchResultAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -247,12 +279,14 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, - OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kClicked)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( + ad, mojom::SearchResultAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, @@ -265,12 +299,14 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForNonRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, - OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kClicked)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( + ad, mojom::SearchResultAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_rewards_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_rewards_unittest.cc index 5ce4ea65a882..d9085b9e4c0e 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_rewards_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_for_rewards_unittest.cc @@ -7,6 +7,8 @@ #include #include "base/check.h" +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/account/deposits/deposits_database_table.h" @@ -33,21 +35,27 @@ namespace { void VerifyDepositForCreativeInstanceIdExpectation( const std::string& creative_instance_id) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/::testing::_, - /*deposit=*/::testing::Ne(std::nullopt))); + /*deposit=*/::testing::Ne(std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::Deposits database_table; database_table.GetForCreativeInstanceId(creative_instance_id, callback.Get()); + run_loop.Run(); } void VerifyCreativeSetConversionExpectation(size_t expected_count) { base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL( callback, Run(/*success=*/::testing::_, - /*creative_set_conversions=*/::testing::SizeIs(expected_count))); + /*creative_set_conversions=*/::testing::SizeIs(expected_count))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); const database::table::CreativeSetConversions database_table; database_table.GetUnexpired(callback.Get()); + run_loop.Run(); } } // namespace @@ -72,11 +80,14 @@ class BraveAdsSearchResultAdEventHandlerForRewardsTest : public test::TestBase { CHECK(mojom_creative_ad); base::MockCallback callback; + base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed); EXPECT_CALL(callback, Run(/*success=*/should_fire_event, - mojom_creative_ad->placement_id, mojom_ad_event_type)); + mojom_creative_ad->placement_id, mojom_ad_event_type)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); event_handler_.FireEvent(mojom_creative_ad.Clone(), mojom_ad_event_type, callback.Get()); + run_loop.Run(); size_t expected_count = 0; @@ -110,10 +121,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -124,10 +138,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -140,10 +157,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, test::RecordAdEvent(ad, mojom::ConfirmationType::kServedImpression); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -155,10 +175,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, test::RecordAdEvent(ad, mojom::ConfirmationType::kServedImpression); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdViewedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdViewedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -172,12 +195,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kViewedImpression)); + ad, mojom::SearchResultAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -189,12 +215,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kViewedImpression)); + ad, mojom::SearchResultAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -208,10 +237,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -224,10 +256,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdClickedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdClickedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -242,12 +277,14 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, mojom::ConfirmationType::kClicked}); // Act & Assert - EXPECT_CALL(delegate_mock_, - OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kClicked)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( + ad, mojom::SearchResultAdEventType::kClicked)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations(mojom_creative_ad, mojom::SearchResultAdEventType::kClicked, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -260,13 +297,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kViewedImpression)); - + ad, mojom::SearchResultAdEventType::kViewedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kViewedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -279,12 +318,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kServedImpression)); + ad, mojom::SearchResultAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -297,12 +339,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, const SearchResultAdInfo ad = FromMojomBuildSearchResultAd(mojom_creative_ad); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kServedImpression)); + ad, mojom::SearchResultAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -322,10 +367,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, AdvanceClockBy(base::Hours(1) - base::Milliseconds(1)); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -345,12 +393,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, AdvanceClockBy(base::Hours(1) - base::Milliseconds(1)); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kServedImpression)); + ad, mojom::SearchResultAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -370,10 +421,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, AdvanceClockBy(base::Days(1) - base::Milliseconds(1)); // Act & Assert - EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)); + base::RunLoop run_loop; + EXPECT_CALL(delegate_mock_, OnDidFireSearchResultAdServedEvent(ad)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/true); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, @@ -393,12 +447,15 @@ TEST_F(BraveAdsSearchResultAdEventHandlerForRewardsTest, AdvanceClockBy(base::Days(1) - base::Milliseconds(1)); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(delegate_mock_, OnFailedToFireSearchResultAdEvent( - ad, mojom::SearchResultAdEventType::kServedImpression)); + ad, mojom::SearchResultAdEventType::kServedImpression)) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); FireEventAndVerifyExpectations( mojom_creative_ad, mojom::SearchResultAdEventType::kServedImpression, /*should_fire_event=*/false); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_non_rewards_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_non_rewards_unittest.cc index e9f850270c08..208ac862c8e9 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_non_rewards_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_non_rewards_unittest.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_builder.h" @@ -58,9 +60,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerUtilForNonRewardsTest, base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, CreativeSetConversionList{ - *creative_set_conversion})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + CreativeSetConversionList{*creative_set_conversion})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); creative_set_conversions_database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerUtilForNonRewardsTest, @@ -84,8 +90,11 @@ TEST_F(BraveAdsSearchResultAdEventHandlerUtilForNonRewardsTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); creative_set_conversions_database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerUtilForNonRewardsTest, @@ -101,8 +110,11 @@ TEST_F(BraveAdsSearchResultAdEventHandlerUtilForNonRewardsTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); creative_set_conversions_database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerUtilForNonRewardsTest, diff --git a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_rewards_unittest.cc b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_rewards_unittest.cc index 7a5432ac7039..d979bb85f06f 100644 --- a/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_rewards_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/ad_events/search_result_ads/search_result_ad_event_handler_util_for_rewards_unittest.cc @@ -3,6 +3,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "base/test/scoped_feature_list.h" #include "brave/components/brave_ads/core/internal/ad_units/search_result_ad/search_result_ad_builder.h" @@ -56,9 +58,13 @@ TEST_F(BraveAdsSearchResultAdEventHandlerUtilForRewardsTest, base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, CreativeSetConversionList{ - *creative_set_conversion})); + base::RunLoop run_loop; + EXPECT_CALL(callback, + Run(/*success=*/true, + CreativeSetConversionList{*creative_set_conversion})) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); creative_set_conversions_database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerUtilForRewardsTest, @@ -83,8 +89,11 @@ TEST_F(BraveAdsSearchResultAdEventHandlerUtilForRewardsTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); creative_set_conversions_database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerUtilForRewardsTest, @@ -100,8 +109,11 @@ TEST_F(BraveAdsSearchResultAdEventHandlerUtilForRewardsTest, // Assert base::MockCallback callback; - EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())); + base::RunLoop run_loop; + EXPECT_CALL(callback, Run(/*success=*/true, ::testing::IsEmpty())) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); creative_set_conversions_database_table_.GetUnexpired(callback.Get()); + run_loop.Run(); } TEST_F(BraveAdsSearchResultAdEventHandlerUtilForRewardsTest, diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_inline_content_ad_unittest.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_inline_content_ad_unittest.cc index f0fffdaceafe..e66cbf19f647 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_inline_content_ad_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_inline_content_ad_unittest.cc @@ -3,6 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at https://mozilla.org/MPL/2.0/. */ +#include "base/run_loop.h" #include "base/time/time.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_util.h" #include "brave/components/brave_ads/core/internal/creatives/conversions/creative_set_conversion_test_util.h" @@ -35,9 +36,12 @@ TEST_F(BraveAdsConversionsInlineContentAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsInlineContentAdTest, @@ -73,9 +77,12 @@ TEST_F(BraveAdsConversionsInlineContentAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsInlineContentAdTest, @@ -91,9 +98,12 @@ TEST_F(BraveAdsConversionsInlineContentAdTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsInlineContentAdTest, @@ -131,9 +141,12 @@ TEST_F(BraveAdsConversionsInlineContentAdTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_new_tab_page_ad_unittest.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_new_tab_page_ad_unittest.cc index df41e8bcfb11..c476746ab3db 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_new_tab_page_ad_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_new_tab_page_ad_unittest.cc @@ -35,9 +35,12 @@ TEST_F(BraveAdsConversionsNewTabPageAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsNewTabPageAdTest, @@ -92,9 +95,12 @@ TEST_F(BraveAdsConversionsNewTabPageAdTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsNewTabPageAdTest, diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_notification_ad_unittest.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_notification_ad_unittest.cc index e3cc8284bb22..22ca8e2c309a 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_notification_ad_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_notification_ad_unittest.cc @@ -35,9 +35,12 @@ TEST_F(BraveAdsConversionsNotificationAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsNotificationAdTest, @@ -92,9 +95,12 @@ TEST_F(BraveAdsConversionsNotificationAdTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsNotificationAdTest, diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_promoted_content_ad_unittest.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_promoted_content_ad_unittest.cc index ea1bbc8c71fc..fbed43b97224 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_promoted_content_ad_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_promoted_content_ad_unittest.cc @@ -37,9 +37,12 @@ TEST_F(BraveAdsConversionsPromotedContentAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsPromotedContentAdTest, @@ -75,9 +78,12 @@ TEST_F(BraveAdsConversionsPromotedContentAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsPromotedContentAdTest, @@ -93,9 +99,12 @@ TEST_F(BraveAdsConversionsPromotedContentAdTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsPromotedContentAdTest, @@ -138,9 +147,12 @@ TEST_F(BraveAdsConversionsPromotedContentAdTest, /*verifiable_conversion=*/std::nullopt); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_search_result_ad_unittest.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_search_result_ad_unittest.cc index 32a4f3022d25..5f2c7158aded 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_search_result_ad_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_search_result_ad_unittest.cc @@ -37,9 +37,12 @@ TEST_F(BraveAdsConversionsSearchResultAdTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsSearchResultAdTest, @@ -74,9 +77,12 @@ TEST_F(BraveAdsConversionsSearchResultAdTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsSearchResultAdTest, @@ -141,9 +147,12 @@ TEST_F( test::RecordAdEvent(ad, mojom::ConfirmationType::kClicked); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F( diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.cc index 9e1521ee3582..ed4d627ef038 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.cc @@ -5,6 +5,9 @@ #include "brave/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.h" +#include + +#include "base/test/gmock_callback_support.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/user_engagement/ad_events/ad_event_test_util.h" #include "brave/components/brave_ads/core/internal/user_engagement/conversions/conversion/conversion_info.h" // IWYU pragma: keep @@ -33,12 +36,14 @@ void BraveAdsConversionsTestBase::TearDown() { void BraveAdsConversionsTestBase::VerifyOnDidConvertAdExpectation( const AdInfo& ad, - ConversionActionType action_type) { + ConversionActionType action_type, + base::OnceClosure did_convert_ad_closure) { EXPECT_CALL(conversions_observer_mock_, OnDidConvertAd(/*conversion=*/::testing::FieldsAre( ad.type, ad.creative_instance_id, ad.creative_set_id, ad.campaign_id, ad.advertiser_id, ad.segment, action_type, - /*verifiable*/ std::nullopt))); + /*verifiable*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(std::move(did_convert_ad_closure))); } void BraveAdsConversionsTestBase::VerifyOnDidNotConvertAdExpectation() { @@ -48,12 +53,14 @@ void BraveAdsConversionsTestBase::VerifyOnDidNotConvertAdExpectation() { void BraveAdsConversionsTestBase::VerifyOnDidConvertVerifiableAdExpectation( const AdInfo& ad, ConversionActionType action_type, - const VerifiableConversionInfo& verifiable_conversion) { + const VerifiableConversionInfo& verifiable_conversion, + base::OnceClosure did_convert_ad_closure) { EXPECT_CALL( conversions_observer_mock_, OnDidConvertAd(/*conversion=*/::testing::FieldsAre( ad.type, ad.creative_instance_id, ad.creative_set_id, ad.campaign_id, - ad.advertiser_id, ad.segment, action_type, verifiable_conversion))); + ad.advertiser_id, ad.segment, action_type, verifiable_conversion))) + .WillOnce(base::test::RunOnceClosure(std::move(did_convert_ad_closure))); } } // namespace brave_ads::test diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.h b/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.h index 4c872e2fa5b3..8cd13f1967e8 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.h +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_test_base.h @@ -8,6 +8,7 @@ #include +#include "base/functional/callback.h" #include "brave/components/brave_ads/core/internal/common/test/test_base.h" #include "brave/components/brave_ads/core/internal/user_engagement/conversions/actions/conversion_action_types.h" #include "brave/components/brave_ads/core/internal/user_engagement/conversions/conversions_observer_mock.h" @@ -35,15 +36,18 @@ class BraveAdsConversionsTestBase : public TestBase { void TearDown() override; protected: - void VerifyOnDidConvertAdExpectation(const AdInfo& ad, - ConversionActionType action_type); + void VerifyOnDidConvertAdExpectation( + const AdInfo& ad, + ConversionActionType action_type, + base::OnceClosure did_convert_ad_closure); void VerifyOnDidNotConvertAdExpectation(); void VerifyOnDidConvertVerifiableAdExpectation( const AdInfo& ad, ConversionActionType action_type, - const VerifiableConversionInfo& verifiable_conversion); + const VerifiableConversionInfo& verifiable_conversion, + base::OnceClosure did_convert_ad_closure); std::unique_ptr conversions_; diff --git a/components/brave_ads/core/internal/user_engagement/conversions/conversions_unittest.cc b/components/brave_ads/core/internal/user_engagement/conversions/conversions_unittest.cc index 4ff17e944657..6751c218dd42 100644 --- a/components/brave_ads/core/internal/user_engagement/conversions/conversions_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/conversions/conversions_unittest.cc @@ -5,6 +5,7 @@ #include "brave/components/brave_ads/core/internal/user_engagement/conversions/conversions.h" +#include "base/run_loop.h" #include "base/test/scoped_feature_list.h" #include "base/time/time.h" #include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h" @@ -54,10 +55,16 @@ TEST_F(BraveAdsConversionsTest, mojom::ConfirmationType::kClicked}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad_1, ConversionActionType::kViewThrough); - VerifyOnDidConvertAdExpectation(ad_2, ConversionActionType::kClickThrough); + base::RunLoop view_through_run_loop; + VerifyOnDidConvertAdExpectation(ad_1, ConversionActionType::kViewThrough, + view_through_run_loop.QuitClosure()); + base::RunLoop click_through_run_loop; + VerifyOnDidConvertAdExpectation(ad_2, ConversionActionType::kClickThrough, + click_through_run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + view_through_run_loop.Run(); + click_through_run_loop.Run(); } TEST_F(BraveAdsConversionsTest, DoNotCapConversionsWithinTheSameCreativeSet) { @@ -74,14 +81,20 @@ TEST_F(BraveAdsConversionsTest, DoNotCapConversionsWithinTheSameCreativeSet) { test::RecordAdEvents(ad, {mojom::ConfirmationType::kServedImpression, mojom::ConfirmationType::kViewedImpression}); - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop view_through_run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + view_through_run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + view_through_run_loop.Run(); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop click_through_run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + click_through_run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + click_through_run_loop.Run(); } TEST_F(BraveAdsConversionsTest, CapConversionsWithinTheSameCreativeSet) { @@ -98,13 +111,19 @@ TEST_F(BraveAdsConversionsTest, CapConversionsWithinTheSameCreativeSet) { test::RecordAdEvents(ad, {mojom::ConfirmationType::kServedImpression, mojom::ConfirmationType::kViewedImpression}); - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop view_through_run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + view_through_run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + view_through_run_loop.Run(); - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop click_through_run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + click_through_run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + click_through_run_loop.Run(); // Act & Assert VerifyOnDidNotConvertAdExpectation(); @@ -124,9 +143,12 @@ TEST_F(BraveAdsConversionsTest, ConvertViewedAdAfterTheSameAdWasDismissed) { mojom::ConfirmationType::kDismissed}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, DoNotConvertNonViewedOrClickedAds) { @@ -196,9 +218,12 @@ TEST_F(BraveAdsConversionsTest, mojom::ConfirmationType::kViewedImpression, mojom::ConfirmationType::kDismissed}); - VerifyOnDidConvertAdExpectation(ad_1, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad_1, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); AdInfo ad_2 = ad_1; ad_2.creative_instance_id = test::kAnotherCreativeInstanceId; @@ -242,9 +267,12 @@ TEST_F(BraveAdsConversionsTest, AdvanceClockBy(base::Days(3) - base::Milliseconds(1)); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, @@ -281,9 +309,12 @@ TEST_F(BraveAdsConversionsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildVerifiableConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F( @@ -303,9 +334,12 @@ TEST_F( mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, @@ -324,9 +358,12 @@ TEST_F(BraveAdsConversionsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert( /*redirect_chain=*/{GURL("https://foo.com/bar?qux=quux")}, /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, ConvertAdIfVerifiableUrlConversionIdExists) { @@ -344,13 +381,15 @@ TEST_F(BraveAdsConversionsTest, ConvertAdIfVerifiableUrlConversionIdExists) { mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; VerifyOnDidConvertVerifiableAdExpectation( ad, ConversionActionType::kViewThrough, VerifiableConversionInfo{ - /*id=*/"xyzzy", - test::kVerifiableConversionAdvertiserPublicKeyBase64}); + /*id=*/"xyzzy", test::kVerifiableConversionAdvertiserPublicKeyBase64}, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildVerifiableConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, @@ -369,9 +408,12 @@ TEST_F(BraveAdsConversionsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert(test::BuildDefaultConversionRedirectChain(), /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, ConvertAdIfVerifiableHtmlConversionIdExists) { @@ -389,14 +431,16 @@ TEST_F(BraveAdsConversionsTest, ConvertAdIfVerifiableHtmlConversionIdExists) { mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; VerifyOnDidConvertVerifiableAdExpectation( ad, ConversionActionType::kViewThrough, VerifiableConversionInfo{ - /*id=*/"waldo", - test::kVerifiableConversionAdvertiserPublicKeyBase64}); + /*id=*/"waldo", test::kVerifiableConversionAdvertiserPublicKeyBase64}, + run_loop.QuitClosure()); conversions_->MaybeConvert( test::BuildDefaultConversionRedirectChain(), /*html=*/R"(
waldo
)"); + run_loop.Run(); } TEST_F( @@ -416,9 +460,12 @@ TEST_F( mojom::ConfirmationType::kViewedImpression}); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kViewThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert( /*redirect_chain=*/{GURL("https://qux.com/quux/corge")}, /*html=*/""); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, @@ -437,13 +484,16 @@ TEST_F(BraveAdsConversionsTest, mojom::ConfirmationType::kViewedImpression}); // Act & Assert + base::RunLoop run_loop; VerifyOnDidConvertVerifiableAdExpectation( ad, ConversionActionType::kViewThrough, VerifiableConversionInfo{ - /*id=*/"fred", test::kVerifiableConversionAdvertiserPublicKeyBase64}); + /*id=*/"fred", test::kVerifiableConversionAdvertiserPublicKeyBase64}, + run_loop.QuitClosure()); conversions_->MaybeConvert( /*redirect_chain=*/{GURL("https://qux.com/quux/corge")}, /*html=*/R"()"); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, VerifiableConversion) { @@ -462,13 +512,16 @@ TEST_F(BraveAdsConversionsTest, VerifiableConversion) { mojom::ConfirmationType::kClicked}); // Act & Assert + base::RunLoop run_loop; VerifyOnDidConvertVerifiableAdExpectation( ad, ConversionActionType::kClickThrough, VerifiableConversionInfo{ - /*id=*/"fred", test::kVerifiableConversionAdvertiserPublicKeyBase64}); + /*id=*/"fred", test::kVerifiableConversionAdvertiserPublicKeyBase64}, + run_loop.QuitClosure()); conversions_->MaybeConvert( test::BuildDefaultConversionRedirectChain(), /*html=*/R"()"); + run_loop.Run(); } TEST_F(BraveAdsConversionsTest, FallbackToDefaultConversionForNonRewardsUser) { @@ -492,10 +545,13 @@ TEST_F(BraveAdsConversionsTest, FallbackToDefaultConversionForNonRewardsUser) { test::RecordAdEvent(ad, mojom::ConfirmationType::kClicked); // Act & Assert - VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough); + base::RunLoop run_loop; + VerifyOnDidConvertAdExpectation(ad, ConversionActionType::kClickThrough, + run_loop.QuitClosure()); conversions_->MaybeConvert( test::BuildDefaultConversionRedirectChain(), /*html=*/R"()"); + run_loop.Run(); } } // namespace brave_ads diff --git a/components/brave_ads/core/internal/user_engagement/reactions/reactions_unittest.cc b/components/brave_ads/core/internal/user_engagement/reactions/reactions_unittest.cc index 7cd45f9b4eeb..bead50e1043d 100644 --- a/components/brave_ads/core/internal/user_engagement/reactions/reactions_unittest.cc +++ b/components/brave_ads/core/internal/user_engagement/reactions/reactions_unittest.cc @@ -8,6 +8,8 @@ #include #include +#include "base/run_loop.h" +#include "base/test/gmock_callback_support.h" #include "base/test/mock_callback.h" #include "brave/components/brave_ads/core/internal/account/account.h" #include "brave/components/brave_ads/core/internal/account/account_observer_mock.h" @@ -49,18 +51,21 @@ TEST_F(BraveAdsReactionsTest, ToggleLikeAd) { test::BuildReaction(mojom::AdType::kNotificationAd); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL( account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kLikedAd, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true)); GetReactions().ToggleLikeAd(std::move(mojom_reaction), callback.Get()); + run_loop.Run(); EXPECT_EQ(mojom::ReactionType::kLiked, GetReactions().AdReactionTypeForId(test::kAdvertiserId)); } @@ -73,18 +78,21 @@ TEST_F(BraveAdsReactionsTest, ToggleDislikeAd) { test::BuildReaction(mojom::AdType::kNotificationAd); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL( account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kDislikedAd, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true)); GetReactions().ToggleDislikeAd(std::move(mojom_reaction), callback.Get()); + run_loop.Run(); EXPECT_EQ(mojom::ReactionType::kDisliked, GetReactions().AdReactionTypeForId(test::kAdvertiserId)); } @@ -232,18 +240,21 @@ TEST_F(BraveAdsReactionsTest, ToggleSaveAd) { test::BuildReaction(mojom::AdType::kNotificationAd); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL( account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kSavedAd, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true)); GetReactions().ToggleSaveAd(std::move(mojom_reaction), callback.Get()); + run_loop.Run(); EXPECT_TRUE(GetReactions().IsAdSaved(test::kCreativeInstanceId)); } @@ -260,19 +271,22 @@ TEST_F(BraveAdsReactionsTest, ToggleMarkAdAsInappropriate) { test::BuildReaction(mojom::AdType::kNotificationAd); // Act & Assert + base::RunLoop run_loop; EXPECT_CALL(account_observer_mock_, OnDidProcessDeposit(/*transaction=*/::testing::FieldsAre( /*id*/ ::testing::_, /*created_at*/ test::Now(), test::kCreativeInstanceId, test::kSegment, /*value*/ 0.0, mojom::AdType::kNotificationAd, mojom::ConfirmationType::kMarkAdAsInappropriate, - /*reconciled_at*/ std::nullopt))); + /*reconciled_at*/ std::nullopt))) + .WillOnce(base::test::RunOnceClosure(run_loop.QuitClosure())); EXPECT_CALL(account_observer_mock_, OnFailedToProcessDeposit).Times(0); base::MockCallback callback; EXPECT_CALL(callback, Run(/*success=*/true)); GetReactions().ToggleMarkAdAsInappropriate(std::move(mojom_reaction), callback.Get()); + run_loop.Run(); EXPECT_TRUE(GetReactions().IsAdMarkedAsInappropriate(test::kCreativeSetId)); } diff --git a/components/brave_ads/core/public/BUILD.gn b/components/brave_ads/core/public/BUILD.gn index 6bb1c3211d03..8d5d10f35482 100644 --- a/components/brave_ads/core/public/BUILD.gn +++ b/components/brave_ads/core/public/BUILD.gn @@ -31,7 +31,6 @@ source_set("headers") { "ads_feature.h", "ads_observer_interface.h", "ads_util.h", - "database/database.h", "export.h", "flags/flags_util.h", "history/ad_history_feature.h", @@ -53,7 +52,6 @@ source_set("headers") { deps = [ "//base", - "//sql", "//url", ] diff --git a/components/brave_ads/core/public/ads.h b/components/brave_ads/core/public/ads.h index e6ea703463a4..2859432bd017 100644 --- a/components/brave_ads/core/public/ads.h +++ b/components/brave_ads/core/public/ads.h @@ -33,7 +33,9 @@ class ADS_EXPORT Ads { virtual ~Ads() = default; - static std::unique_ptr CreateInstance(AdsClient& ads_client); + static std::unique_ptr CreateInstance( + AdsClient& ads_client, + const base::FilePath& database_path); virtual void AddObserver( std::unique_ptr ads_observer) = 0; diff --git a/components/brave_ads/core/public/ads_client/ads_client.h b/components/brave_ads/core/public/ads_client/ads_client.h index 4e3909080172..29ffb338f939 100644 --- a/components/brave_ads/core/public/ads_client/ads_client.h +++ b/components/brave_ads/core/public/ads_client/ads_client.h @@ -97,12 +97,6 @@ class ADS_EXPORT AdsClient { virtual void ShowScheduledCaptcha(const std::string& payment_id, const std::string& captcha_id) = 0; - // Run a database transaction. The callback takes one argument - - // `mojom::DBTransactionResultInfoPtr` containing the info of the transaction. - virtual void RunDBTransaction( - mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) = 0; - // Record P2A (Private Advertising Analytics) `events`. virtual void RecordP2AEvents(const std::vector& events) = 0; diff --git a/components/brave_ads/core/test/BUILD.gn b/components/brave_ads/core/test/BUILD.gn index e901ea2ade0d..bfad995e665d 100644 --- a/components/brave_ads/core/test/BUILD.gn +++ b/components/brave_ads/core/test/BUILD.gn @@ -196,6 +196,8 @@ source_set("brave_ads_unit_tests") { "//brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_observer_mock.cc", "//brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_observer_mock.h", "//brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_unittest.cc", + "//brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.cc", + "//brave/components/brave_ads/core/internal/ads_client/ads_client_notifier_waiter.h", "//brave/components/brave_ads/core/internal/ads_feature_unittest.cc", "//brave/components/brave_ads/core/internal/ads_observer_mock.cc", "//brave/components/brave_ads/core/internal/ads_observer_mock.h", diff --git a/components/services/bat_ads/bat_ads_client_mojo_bridge.cc b/components/services/bat_ads/bat_ads_client_mojo_bridge.cc index 3a7464fd5e34..0e9d5728e613 100644 --- a/components/services/bat_ads/bat_ads_client_mojo_bridge.cc +++ b/components/services/bat_ads/bat_ads_client_mojo_bridge.cc @@ -210,18 +210,6 @@ std::string BatAdsClientMojoBridge::LoadDataResource(const std::string& name) { return value; } -void BatAdsClientMojoBridge::RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) { - if (!bat_ads_client_associated_remote_.is_bound()) { - std::move(callback).Run(brave_ads::mojom::DBTransactionResultInfoPtr()); - return; - } - - bat_ads_client_associated_remote_->RunDBTransaction( - std::move(mojom_db_transaction), std::move(callback)); -} - void BatAdsClientMojoBridge::ShowScheduledCaptcha( const std::string& payment_id, const std::string& captcha_id) { diff --git a/components/services/bat_ads/bat_ads_client_mojo_bridge.h b/components/services/bat_ads/bat_ads_client_mojo_bridge.h index fe999b31381b..210d400d3d81 100644 --- a/components/services/bat_ads/bat_ads_client_mojo_bridge.h +++ b/components/services/bat_ads/bat_ads_client_mojo_bridge.h @@ -79,10 +79,6 @@ class BatAdsClientMojoBridge : public brave_ads::AdsClient { void ShowScheduledCaptcha(const std::string& payment_id, const std::string& captcha_id) override; - void RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) override; - void RecordP2AEvents(const std::vector& events) override; bool FindProfilePref(const std::string& path) const override; diff --git a/components/services/bat_ads/bat_ads_impl.cc b/components/services/bat_ads/bat_ads_impl.cc index 5c06309f1a24..9b776b1b483a 100644 --- a/components/services/bat_ads/bat_ads_impl.cc +++ b/components/services/bat_ads/bat_ads_impl.cc @@ -19,6 +19,7 @@ #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_info.h" #include "brave/components/brave_ads/core/public/ad_units/notification_ad/notification_ad_value_util.h" #include "brave/components/brave_ads/core/public/ads.h" +#include "brave/components/brave_ads/core/public/ads_constants.h" #include "brave/components/brave_ads/core/public/ads_observer_interface.h" #include "brave/components/services/bat_ads/bat_ads_client_mojo_bridge.h" #include "brave/components/services/bat_ads/bat_ads_observer.h" @@ -28,13 +29,16 @@ namespace bat_ads { class BatAdsImpl::AdsInstance final { public: AdsInstance( + const base::FilePath& service_path, mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingReceiver client_notifier) : bat_ads_client_mojo_proxy_(std::make_unique( std::move(bat_ads_client_pending_associated_remote), std::move(client_notifier))), - ads_(brave_ads::Ads::CreateInstance(*bat_ads_client_mojo_proxy_)) {} + ads_(brave_ads::Ads::CreateInstance( + *bat_ads_client_mojo_proxy_, + service_path.AppendASCII(brave_ads::kDatabaseFilename))) {} AdsInstance(const AdsInstance&) = delete; AdsInstance& operator=(const AdsInstance&) = delete; @@ -51,12 +55,14 @@ class BatAdsImpl::AdsInstance final { std::unique_ptr ads_; }; -BatAdsImpl::BatAdsImpl(mojo::PendingAssociatedRemote +BatAdsImpl::BatAdsImpl(const base::FilePath& service_path, + mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingReceiver bat_ads_client_notifier_pending_receiver) : ads_instance_(std::unique_ptr( - new AdsInstance(std::move(bat_ads_client_pending_associated_remote), + new AdsInstance(service_path, + std::move(bat_ads_client_pending_associated_remote), std::move(bat_ads_client_notifier_pending_receiver)), base::OnTaskRunnerDeleter( base::SequencedTaskRunner::GetCurrentDefault()))) {} diff --git a/components/services/bat_ads/bat_ads_impl.h b/components/services/bat_ads/bat_ads_impl.h index 2fbb639cf2c1..502d27f25113 100644 --- a/components/services/bat_ads/bat_ads_impl.h +++ b/components/services/bat_ads/bat_ads_impl.h @@ -25,7 +25,8 @@ class BatAdsClientMojoBridge; class BatAdsImpl : public mojom::BatAds { public: - BatAdsImpl(mojo::PendingAssociatedRemote + BatAdsImpl(const base::FilePath& service_path, + mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingReceiver bat_ads_client_notifier_pending_receiver); diff --git a/components/services/bat_ads/bat_ads_service_impl.cc b/components/services/bat_ads/bat_ads_service_impl.cc index e583ec2572a6..0faf10cfd212 100644 --- a/components/services/bat_ads/bat_ads_service_impl.cc +++ b/components/services/bat_ads/bat_ads_service_impl.cc @@ -8,6 +8,7 @@ #include #include +#include "base/files/file_path.h" #include "brave/components/services/bat_ads/bat_ads_impl.h" #include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h" #include "mojo/public/cpp/bindings/sync_call_restrictions.h" @@ -33,6 +34,7 @@ BatAdsServiceImpl::BatAdsServiceImpl(mojo::PendingReceiver BatAdsServiceImpl::~BatAdsServiceImpl() = default; void BatAdsServiceImpl::Create( + const base::FilePath& service_path, mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingAssociatedReceiver @@ -44,7 +46,7 @@ void BatAdsServiceImpl::Create( bat_ads_associated_receivers_.Add( std::make_unique( - std::move(bat_ads_client_pending_associated_remote), + service_path, std::move(bat_ads_client_pending_associated_remote), std::move(bat_ads_client_notifier_pending_receiver)), std::move(bat_ads_pending_associated_receiver)); diff --git a/components/services/bat_ads/bat_ads_service_impl.h b/components/services/bat_ads/bat_ads_service_impl.h index bc530fbf5907..bec5e2fc5486 100644 --- a/components/services/bat_ads/bat_ads_service_impl.h +++ b/components/services/bat_ads/bat_ads_service_impl.h @@ -35,7 +35,8 @@ class BatAdsServiceImpl : public mojom::BatAdsService { ~BatAdsServiceImpl() override; // BatAdsService: - void Create(mojo::PendingAssociatedRemote + void Create(const base::FilePath& service_path, + mojo::PendingAssociatedRemote bat_ads_client_pending_associated_remote, mojo::PendingAssociatedReceiver bat_ads_pending_associated_receiver, diff --git a/components/services/bat_ads/public/interfaces/bat_ads.mojom b/components/services/bat_ads/public/interfaces/bat_ads.mojom index 35a12c2ef183..38503e4829c5 100644 --- a/components/services/bat_ads/public/interfaces/bat_ads.mojom +++ b/components/services/bat_ads/public/interfaces/bat_ads.mojom @@ -7,12 +7,14 @@ module bat_ads.mojom; import "brave/components/brave_ads/core/mojom/brave_ads.mojom"; import "mojo/public/mojom/base/big_string.mojom"; import "mojo/public/mojom/base/file.mojom"; +import "mojo/public/mojom/base/file_path.mojom"; import "mojo/public/mojom/base/time.mojom"; import "mojo/public/mojom/base/values.mojom"; import "url/mojom/url.mojom"; interface BatAdsService { - Create(pending_associated_remote + Create(mojo_base.mojom.FilePath service_path, + pending_associated_remote bat_ads_client_pending_associated_remote, pending_associated_receiver bat_ads_pending_associated_receiver, @@ -100,9 +102,6 @@ interface BatAdsClient { ShowScheduledCaptcha(string payment_id, string captcha_id); - RunDBTransaction(brave_ads.mojom.DBTransactionInfo mojom_db_transaction) => - (brave_ads.mojom.DBTransactionResultInfo mojom_db_transaction_result); - RecordP2AEvents(array events); [Sync] diff --git a/ios/browser/api/ads/ads_client_bridge.h b/ios/browser/api/ads/ads_client_bridge.h index 28a868e3fdc0..f47365d17f8c 100644 --- a/ios/browser/api/ads/ads_client_bridge.h +++ b/ios/browser/api/ads/ads_client_bridge.h @@ -49,9 +49,6 @@ - (void)closeNotificationAd:(const std::string&)placement_id; - (void)UrlRequest:(brave_ads::mojom::UrlRequestInfoPtr)url_request callback:(brave_ads::UrlRequestCallback)callback; -- (void)runDBTransaction: - (brave_ads::mojom::DBTransactionInfoPtr)mojom_db_transaction - callback:(brave_ads::RunDBTransactionCallback)callback; - (void)setProfilePref:(const std::string&)path value:(base::Value)value; - (bool)findProfilePref:(const std::string&)path; - (std::optional)getProfilePref:(const std::string&)path; diff --git a/ios/browser/api/ads/ads_client_ios.h b/ios/browser/api/ads/ads_client_ios.h index d7c32c8331c3..56dc84a51d2f 100644 --- a/ios/browser/api/ads/ads_client_ios.h +++ b/ios/browser/api/ads/ads_client_ios.h @@ -54,9 +54,6 @@ class AdsClientIOS : public brave_ads::AdsClient { int line, int verbose_level, const std::string& message) override; - void RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) override; void SetProfilePref(const std::string& path, base::Value value) override; bool FindProfilePref(const std::string& path) const override; std::optional GetProfilePref(const std::string& path) override; diff --git a/ios/browser/api/ads/ads_client_ios.mm b/ios/browser/api/ads/ads_client_ios.mm index b68a6284d87b..a7c6089bacd5 100644 --- a/ios/browser/api/ads/ads_client_ios.mm +++ b/ios/browser/api/ads/ads_client_ios.mm @@ -113,13 +113,6 @@ [bridge_ log:file line:line verboseLevel:verbose_level message:message]; } -void AdsClientIOS::RunDBTransaction( - brave_ads::mojom::DBTransactionInfoPtr mojom_db_transaction, - brave_ads::RunDBTransactionCallback callback) { - [bridge_ runDBTransaction:std::move(mojom_db_transaction) - callback:std::move(callback)]; -} - void AdsClientIOS::SetProfilePref(const std::string& path, base::Value value) { [bridge_ setProfilePref:path value:std::move(value)]; } diff --git a/ios/browser/api/ads/brave_ads.mm b/ios/browser/api/ads/brave_ads.mm index 66321d1417bc..023b9ebaad55 100644 --- a/ios/browser/api/ads/brave_ads.mm +++ b/ios/browser/api/ads/brave_ads.mm @@ -1370,25 +1370,6 @@ - (void)showScheduledCaptcha:(const std::string&)payment_id captchaId:base::SysUTF8ToNSString(captcha_id)]; } -- (void)runDBTransaction: - (brave_ads::mojom::DBTransactionInfoPtr)mojom_db_transaction - callback:(brave_ads::RunDBTransactionCallback)completion { - if (![self isServiceRunning]) { - return std::move(completion) - .Run(brave_ads::mojom::DBTransactionResultInfo::New()); - } - - adsService->RunDBTransaction( - std::move(mojom_db_transaction), - base::BindOnce( - ^(brave_ads::RunDBTransactionCallback callback, - brave_ads::mojom::DBTransactionResultInfoPtr - mojom_db_transaction_result) { - std::move(callback).Run(std::move(mojom_db_transaction_result)); - }, - std::move(completion))); -} - - (void)recordP2AEvents:(const std::vector&)events { // TODO(https://github.com/brave/brave-browser/issues/33786): Unify Brave Ads // P3A analytics. diff --git a/ios/browser/brave_ads/ads_service_impl_ios.h b/ios/browser/brave_ads/ads_service_impl_ios.h index 6d587564fa70..14e16d4e1b45 100644 --- a/ios/browser/brave_ads/ads_service_impl_ios.h +++ b/ios/browser/brave_ads/ads_service_impl_ios.h @@ -11,7 +11,6 @@ #include #include "base/memory/weak_ptr.h" -#include "base/threading/sequence_bound.h" #include "brave/components/brave_ads/core/browser/service/ads_service.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" #include "brave/components/brave_ads/core/public/ads_callback.h" @@ -28,7 +27,6 @@ namespace brave_ads { class Ads; class AdsClient; -class Database; class AdsServiceImplIOS : public AdsService { public: @@ -163,7 +161,6 @@ class AdsServiceImplIOS : public AdsService { void InitializeAds(InitializeCallback callback); void InitializeAdsCallback(InitializeCallback callback, bool success); - void InitializeDatabase(); void ShutdownAdsCallback(ShutdownCallback callback, bool success); @@ -172,7 +169,7 @@ class AdsServiceImplIOS : public AdsService { const raw_ptr prefs_ = nullptr; // Not owned. - const scoped_refptr database_queue_; + const scoped_refptr file_task_runner_; base::FilePath storage_path_; std::unique_ptr ads_client_; @@ -180,8 +177,6 @@ class AdsServiceImplIOS : public AdsService { mojom::BuildChannelInfoPtr mojom_build_channel_; mojom::WalletInfoPtr mojom_wallet_; - base::SequenceBound database_; - std::unique_ptr ads_; base::WeakPtrFactory weak_ptr_factory_{this}; diff --git a/ios/browser/brave_ads/ads_service_impl_ios.mm b/ios/browser/brave_ads/ads_service_impl_ios.mm index 43a8fd4d1330..17e45cfd24ce 100644 --- a/ios/browser/brave_ads/ads_service_impl_ios.mm +++ b/ios/browser/brave_ads/ads_service_impl_ios.mm @@ -23,7 +23,6 @@ #include "brave/components/brave_ads/core/public/ads.h" #include "brave/components/brave_ads/core/public/ads_client/ads_client.h" #include "brave/components/brave_ads/core/public/ads_constants.h" -#include "brave/components/brave_ads/core/public/database/database.h" #include "brave/components/brave_ads/core/public/flags/flags_util.h" #include "components/prefs/pref_service.h" #include "sql/database.h" @@ -40,7 +39,7 @@ AdsServiceImplIOS::AdsServiceImplIOS(PrefService* prefs) : AdsService(/*delegate=*/nullptr), prefs_(prefs), - database_queue_(base::ThreadPool::CreateSequencedTaskRunner( + file_task_runner_(base::ThreadPool::CreateSequencedTaskRunner( {base::MayBlock(), base::TaskPriority::USER_VISIBLE, base::TaskShutdownBehavior::BLOCK_SHUTDOWN})) { CHECK(prefs_); @@ -82,14 +81,6 @@ std::move(callback))); } -void AdsServiceImplIOS::RunDBTransaction( - mojom::DBTransactionInfoPtr mojom_db_transaction, - RunDBTransactionCallback callback) { - database_.AsyncCall(&brave_ads::Database::RunDBTransaction) - .WithArgs(std::move(mojom_db_transaction)) - .Then(std::move(callback)); -} - void AdsServiceImplIOS::MaybeGetNotificationAd( const std::string& placement_id, MaybeGetNotificationAdCallback callback) { @@ -441,15 +432,13 @@ void AdsServiceImplIOS::Shutdown() { ads_.reset(); - database_.Reset(); } void AdsServiceImplIOS::InitializeAds(InitializeCallback callback) { CHECK(!IsInitialized()); - InitializeDatabase(); - - ads_ = Ads::CreateInstance(*ads_client_); + ads_ = Ads::CreateInstance(*ads_client_, + storage_path_.AppendASCII(kAdsDatabaseFilename)); ads_->SetSysInfo(mojom_sys_info_.Clone()); ads_->SetBuildChannel(mojom_build_channel_.Clone()); @@ -470,12 +459,6 @@ std::move(callback).Run(success); } -void AdsServiceImplIOS::InitializeDatabase() { - database_ = base::SequenceBound( - database_queue_, - base::FilePath(storage_path_.Append(kAdsDatabaseFilename))); -} - void AdsServiceImplIOS::ShutdownAdsCallback(ShutdownCallback callback, bool success) { Shutdown(); @@ -493,7 +476,7 @@ return std::move(callback).Run(/*success=*/false); } - database_queue_->PostTaskAndReply( + file_task_runner_->PostTaskAndReply( FROM_HERE, base::BindOnce( [](const base::FilePath& storage_path) {