From 31d5aebecdeb6d7cf07abaa2fdf20ebe56176872 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Thu, 10 Oct 2024 22:24:14 -0500 Subject: [PATCH] [ads] Add ads service waiter for testing --- browser/brave_ads/BUILD.gn | 2 + browser/brave_ads/test_ads_service_waiter.cc | 34 ++++++++++++++++ browser/brave_ads/test_ads_service_waiter.h | 39 +++++++++++++++++++ components/brave_ads/browser/BUILD.gn | 1 + components/brave_ads/browser/ads_service.h | 4 ++ .../brave_ads/browser/ads_service_impl.cc | 21 ++++++++++ .../brave_ads/browser/ads_service_impl.h | 8 ++++ .../brave_ads/browser/ads_service_mock.h | 4 ++ .../brave_ads/browser/ads_service_observer.h | 21 ++++++++++ 9 files changed, 134 insertions(+) create mode 100644 browser/brave_ads/test_ads_service_waiter.cc create mode 100644 browser/brave_ads/test_ads_service_waiter.h create mode 100644 components/brave_ads/browser/ads_service_observer.h diff --git a/browser/brave_ads/BUILD.gn b/browser/brave_ads/BUILD.gn index 139d18045f9d..02acaa00427f 100644 --- a/browser/brave_ads/BUILD.gn +++ b/browser/brave_ads/BUILD.gn @@ -144,6 +144,8 @@ source_set("browser_tests") { "analytics/p3a/brave_stats_helper_browsertest.cc", "application_state/notification_helper/notification_helper_impl_mock.cc", "application_state/notification_helper/notification_helper_impl_mock.h", + "test_ads_service_waiter.cc", + "test_ads_service_waiter.h", ] deps = [ diff --git a/browser/brave_ads/test_ads_service_waiter.cc b/browser/brave_ads/test_ads_service_waiter.cc new file mode 100644 index 000000000000..c46325675288 --- /dev/null +++ b/browser/brave_ads/test_ads_service_waiter.cc @@ -0,0 +1,34 @@ +/* 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/browser/brave_ads/test_ads_service_waiter.h" + +#include "base/check.h" +#include "brave/components/brave_ads/browser/ads_service.h" + +namespace brave_ads { + +AdsServiceWaiter::AdsServiceWaiter(AdsService* const ads_service) + : ads_service_(ads_service) { + CHECK(ads_service_); + + ads_service_->AddObserver(this); +} + +AdsServiceWaiter::~AdsServiceWaiter() { + ads_service_->RemoveObserver(this); +} + +void AdsServiceWaiter::Wait() { + run_loop_.Run(); +} + +/////////////////////////////////////////////////////////////////////////////// + +void AdsServiceWaiter::OnAdsServiceInitialized() { + run_loop_.Quit(); +} + +} // namespace brave_ads diff --git a/browser/brave_ads/test_ads_service_waiter.h b/browser/brave_ads/test_ads_service_waiter.h new file mode 100644 index 000000000000..6e0fc99ee24e --- /dev/null +++ b/browser/brave_ads/test_ads_service_waiter.h @@ -0,0 +1,39 @@ +/* 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_BROWSER_BRAVE_ADS_TEST_ADS_SERVICE_WAITER_H_ +#define BRAVE_BROWSER_BRAVE_ADS_TEST_ADS_SERVICE_WAITER_H_ + +#include "base/memory/raw_ptr.h" +#include "base/run_loop.h" +#include "brave/components/brave_ads/browser/ads_service_observer.h" + +namespace brave_ads { + +class AdsService; + +// This class waits for the ads service to be initialized. +class AdsServiceWaiter : public AdsServiceObserver { + public: + explicit AdsServiceWaiter(AdsService* const ads_service); + ~AdsServiceWaiter() override; + + AdsServiceWaiter(const AdsServiceWaiter&) = delete; + AdsServiceWaiter& operator=(const AdsServiceWaiter&) = delete; + + void Wait(); + + private: + // AdsServiceObserver: + void OnAdsServiceInitialized() override; + + const raw_ptr ads_service_; // not owned. + + base::RunLoop run_loop_; +}; + +} // namespace brave_ads + +#endif // BRAVE_BROWSER_BRAVE_ADS_TEST_ADS_SERVICE_WAITER_H_ diff --git a/components/brave_ads/browser/BUILD.gn b/components/brave_ads/browser/BUILD.gn index 78beb71b898d..38ac03770d75 100644 --- a/components/brave_ads/browser/BUILD.gn +++ b/components/brave_ads/browser/BUILD.gn @@ -12,6 +12,7 @@ static_library("browser") { "ad_units/notification_ad/custom_notification_ad_feature.h", "ads_service.cc", "ads_service.h", + "ads_service_observer.h", "analytics/p2a/p2a.cc", "analytics/p2a/p2a.h", "analytics/p2a/p2a_constants.h", diff --git a/components/brave_ads/browser/ads_service.h b/components/brave_ads/browser/ads_service.h index 2219552aad6c..07084ba881ba 100644 --- a/components/brave_ads/browser/ads_service.h +++ b/components/brave_ads/browser/ads_service.h @@ -14,6 +14,7 @@ #include "base/memory/raw_ptr.h" #include "base/time/time.h" #include "brave/components/brave_ads/browser/ads_service_callback.h" +#include "brave/components/brave_ads/browser/ads_service_observer.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" #include "brave/components/brave_ads/core/public/ad_units/new_tab_page_ad/new_tab_page_ad_info.h" #include "brave/components/brave_ads/core/public/ads_callback.h" @@ -67,6 +68,9 @@ class AdsService : public KeyedService { ~AdsService() override; + virtual void AddObserver(AdsServiceObserver* observer) = 0; + virtual void RemoveObserver(AdsServiceObserver* observer) = 0; + // Returns true if a browser upgrade is required to serve ads. virtual bool IsBrowserUpgradeRequiredToServeAds() const = 0; diff --git a/components/brave_ads/browser/ads_service_impl.cc b/components/brave_ads/browser/ads_service_impl.cc index 55d585c2605c..43c2128d6ace 100644 --- a/components/brave_ads/browser/ads_service_impl.cc +++ b/components/brave_ads/browser/ads_service_impl.cc @@ -29,6 +29,7 @@ #include "base/timer/timer.h" #include "brave/components/brave_adaptive_captcha/pref_names.h" #include "brave/components/brave_ads/browser/ad_units/notification_ad/custom_notification_ad_feature.h" +#include "brave/components/brave_ads/browser/ads_service_observer.h" #include "brave/components/brave_ads/browser/analytics/p2a/p2a.h" #include "brave/components/brave_ads/browser/analytics/p3a/notification_ad.h" #include "brave/components/brave_ads/browser/bat_ads_service_factory.h" @@ -233,6 +234,18 @@ AdsServiceImpl::AdsServiceImpl( AdsServiceImpl::~AdsServiceImpl() = default; +void AdsServiceImpl::AddObserver(AdsServiceObserver* const observer) { + CHECK(observer); + + observers_.AddObserver(observer); +} + +void AdsServiceImpl::RemoveObserver(AdsServiceObserver* const observer) { + CHECK(observer); + + observers_.RemoveObserver(observer); +} + /////////////////////////////////////////////////////////////////////////////// bool AdsServiceImpl::IsBatAdsServiceBound() const { @@ -491,6 +504,14 @@ void AdsServiceImpl::InitializeBatAdsCallback(const bool success) { if (bat_ads_client_notifier_remote_.is_bound()) { bat_ads_client_notifier_remote_->NotifyDidInitializeAds(); } + + NotifyAdsServiceInitialized(); +} + +void AdsServiceImpl::NotifyAdsServiceInitialized() const { + for (AdsServiceObserver& observer : observers_) { + observer.OnAdsServiceInitialized(); + } } void AdsServiceImpl::ShutdownAndClearData() { diff --git a/components/brave_ads/browser/ads_service_impl.h b/components/brave_ads/browser/ads_service_impl.h index 8c8e9dfc9d87..756519b7ab06 100644 --- a/components/brave_ads/browser/ads_service_impl.h +++ b/components/brave_ads/browser/ads_service_impl.h @@ -57,6 +57,7 @@ class SharedURLLoaderFactory; namespace brave_ads { +class AdsServiceObserver; class AdsTooltipsDelegate; class BatAdsServiceFactory; class Database; @@ -93,6 +94,9 @@ class AdsServiceImpl final : public AdsService, ~AdsServiceImpl() override; + void AddObserver(AdsServiceObserver* observer) override; + void RemoveObserver(AdsServiceObserver* observer) override; + private: using SimpleURLLoaderList = std::list>; @@ -134,6 +138,8 @@ class AdsServiceImpl final : public AdsService, brave_rewards::mojom::RewardsWalletPtr mojom_rewards_wallet); void InitializeBatAdsCallback(bool success); + void NotifyAdsServiceInitialized() const; + void ShutdownAndClearData(); void ShutdownAndClearDataCallback(bool success); @@ -470,6 +476,8 @@ class AdsServiceImpl final : public AdsService, brave_rewards::RewardsServiceObserver> rewards_service_observation_{this}; + base::ObserverList observers_; + mojo::Receiver bat_ads_observer_receiver_{ this}; diff --git a/components/brave_ads/browser/ads_service_mock.h b/components/brave_ads/browser/ads_service_mock.h index 013c5eff21b5..1833074c80a1 100644 --- a/components/brave_ads/browser/ads_service_mock.h +++ b/components/brave_ads/browser/ads_service_mock.h @@ -12,6 +12,7 @@ #include #include "brave/components/brave_ads/browser/ads_service.h" +#include "brave/components/brave_ads/browser/ads_service_observer.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h" #include "testing/gmock/include/gmock/gmock.h" @@ -29,6 +30,9 @@ class AdsServiceMock : public AdsService { ~AdsServiceMock() override; + MOCK_METHOD(void, AddObserver, (AdsServiceObserver * observer)); + MOCK_METHOD(void, RemoveObserver, (AdsServiceObserver * observer)); + MOCK_METHOD(void, AddBatAdsObserver, (mojo::PendingRemote diff --git a/components/brave_ads/browser/ads_service_observer.h b/components/brave_ads/browser/ads_service_observer.h new file mode 100644 index 000000000000..362b92879adf --- /dev/null +++ b/components/brave_ads/browser/ads_service_observer.h @@ -0,0 +1,21 @@ +/* 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_BROWSER_ADS_SERVICE_OBSERVER_H_ +#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_ADS_SERVICE_OBSERVER_H_ + +#include "base/observer_list_types.h" + +namespace brave_ads { + +class AdsServiceObserver : public base::CheckedObserver { + public: + // Invoked when the ads service has initialized. + virtual void OnAdsServiceInitialized() {} +}; + +} // namespace brave_ads + +#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_ADS_SERVICE_OBSERVER_H_