Skip to content

Commit

Permalink
[ads] Add ads service waiter for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Oct 15, 2024
1 parent 59551fb commit 31d5aeb
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions browser/brave_ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
34 changes: 34 additions & 0 deletions browser/brave_ads/test_ads_service_waiter.cc
Original file line number Diff line number Diff line change
@@ -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
39 changes: 39 additions & 0 deletions browser/brave_ads/test_ads_service_waiter.h
Original file line number Diff line number Diff line change
@@ -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<AdsService> ads_service_; // not owned.

base::RunLoop run_loop_;
};

} // namespace brave_ads

#endif // BRAVE_BROWSER_BRAVE_ADS_TEST_ADS_SERVICE_WAITER_H_
1 change: 1 addition & 0 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down
21 changes: 21 additions & 0 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 8 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class SharedURLLoaderFactory;

namespace brave_ads {

class AdsServiceObserver;
class AdsTooltipsDelegate;
class BatAdsServiceFactory;
class Database;
Expand Down Expand Up @@ -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<std::unique_ptr<network::SimpleURLLoader>>;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -470,6 +476,8 @@ class AdsServiceImpl final : public AdsService,
brave_rewards::RewardsServiceObserver>
rewards_service_observation_{this};

base::ObserverList<AdsServiceObserver> observers_;

mojo::Receiver<bat_ads::mojom::BatAdsObserver> bat_ads_observer_receiver_{
this};

Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#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"

Expand All @@ -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<bat_ads::mojom::BatAdsObserver>
Expand Down
21 changes: 21 additions & 0 deletions components/brave_ads/browser/ads_service_observer.h
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit 31d5aeb

Please sign in to comment.