Skip to content

Commit

Permalink
[ads] Add OnceClosureTaskQueue tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Oct 16, 2024
1 parent fe56aaf commit 41a991e
Show file tree
Hide file tree
Showing 18 changed files with 212 additions and 183 deletions.
4 changes: 2 additions & 2 deletions components/brave_ads/core/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ static_library("internal") {
"common/database/database_table_util.h",
"common/database/database_transaction_util.cc",
"common/database/database_transaction_util.h",
"common/functional/once_closure_queue.cc",
"common/functional/once_closure_queue.h",
"common/functional/once_closure_task_queue.cc",
"common/functional/once_closure_task_queue.h",
"common/instance_id.cc",
"common/instance_id.h",
"common/locale/locale_util.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
#include <memory>

#include "base/functional/bind.h"
#include "brave/components/brave_ads/core/internal/common/functional/once_closure_queue.h"
#include "brave/components/brave_ads/core/internal/common/functional/once_closure_task_queue.h"
#include "url/gurl.h"

namespace brave_ads {

AdsClientNotifier::AdsClientNotifier()
: pending_task_queue_(std::make_unique<OnceClosureQueue>()) {}
: task_queue_(std::make_unique<OnceClosureTaskQueue>()) {}

AdsClientNotifier::~AdsClientNotifier() = default;

Expand All @@ -33,13 +33,12 @@ void AdsClientNotifier::RemoveObserver(
}

void AdsClientNotifier::NotifyPendingObservers() {
should_queue_ = false;
pending_task_queue_->Process();
task_queue_->FlushAndStopQueueing();
}

void AdsClientNotifier::NotifyDidInitializeAds() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyDidInitializeAds,
weak_factory_.GetWeakPtr()));
}
Expand All @@ -52,8 +51,8 @@ void AdsClientNotifier::NotifyDidInitializeAds() {
void AdsClientNotifier::NotifyRewardsWalletDidUpdate(
const std::string& payment_id,
const std::string& recovery_seed_base64) {
if (should_queue_) {
return pending_task_queue_->Add(base::BindOnce(
if (task_queue_->should_queue()) {
return task_queue_->Add(base::BindOnce(
&AdsClientNotifier::NotifyRewardsWalletDidUpdate,
weak_factory_.GetWeakPtr(), payment_id, recovery_seed_base64));
}
Expand All @@ -64,8 +63,8 @@ void AdsClientNotifier::NotifyRewardsWalletDidUpdate(
}

void AdsClientNotifier::NotifyLocaleDidChange(const std::string& locale) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyLocaleDidChange,
weak_factory_.GetWeakPtr(), locale));
}
Expand All @@ -76,8 +75,8 @@ void AdsClientNotifier::NotifyLocaleDidChange(const std::string& locale) {
}

void AdsClientNotifier::NotifyPrefDidChange(const std::string& path) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyPrefDidChange,
weak_factory_.GetWeakPtr(), path));
}
Expand All @@ -90,8 +89,8 @@ void AdsClientNotifier::NotifyPrefDidChange(const std::string& path) {
void AdsClientNotifier::NotifyResourceComponentDidChange(
const std::string& manifest_version,
const std::string& id) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyResourceComponentDidChange,
weak_factory_.GetWeakPtr(), manifest_version, id));
}
Expand All @@ -103,8 +102,8 @@ void AdsClientNotifier::NotifyResourceComponentDidChange(

void AdsClientNotifier::NotifyDidUnregisterResourceComponent(
const std::string& id) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyDidUnregisterResourceComponent,
weak_factory_.GetWeakPtr(), id));
}
Expand All @@ -118,8 +117,8 @@ void AdsClientNotifier::NotifyTabTextContentDidChange(
const int32_t tab_id,
const std::vector<GURL>& redirect_chain,
const std::string& text) {
if (should_queue_) {
return pending_task_queue_->Add(base::BindOnce(
if (task_queue_->should_queue()) {
return task_queue_->Add(base::BindOnce(
&AdsClientNotifier::NotifyTabTextContentDidChange,
weak_factory_.GetWeakPtr(), tab_id, redirect_chain, text));
}
Expand All @@ -133,8 +132,8 @@ void AdsClientNotifier::NotifyTabHtmlContentDidChange(
const int32_t tab_id,
const std::vector<GURL>& redirect_chain,
const std::string& html) {
if (should_queue_) {
return pending_task_queue_->Add(base::BindOnce(
if (task_queue_->should_queue()) {
return task_queue_->Add(base::BindOnce(
&AdsClientNotifier::NotifyTabHtmlContentDidChange,
weak_factory_.GetWeakPtr(), tab_id, redirect_chain, html));
}
Expand All @@ -145,8 +144,8 @@ void AdsClientNotifier::NotifyTabHtmlContentDidChange(
}

void AdsClientNotifier::NotifyTabDidStartPlayingMedia(const int32_t tab_id) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyTabDidStartPlayingMedia,
weak_factory_.GetWeakPtr(), tab_id));
}
Expand All @@ -157,8 +156,8 @@ void AdsClientNotifier::NotifyTabDidStartPlayingMedia(const int32_t tab_id) {
}

void AdsClientNotifier::NotifyTabDidStopPlayingMedia(const int32_t tab_id) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyTabDidStopPlayingMedia,
weak_factory_.GetWeakPtr(), tab_id));
}
Expand All @@ -174,8 +173,8 @@ void AdsClientNotifier::NotifyTabDidChange(
const bool is_new_navigation,
const bool is_restoring,
const bool is_visible) {
if (should_queue_) {
return pending_task_queue_->Add(base::BindOnce(
if (task_queue_->should_queue()) {
return task_queue_->Add(base::BindOnce(
&AdsClientNotifier::NotifyTabDidChange, weak_factory_.GetWeakPtr(),
tab_id, redirect_chain, is_new_navigation, is_restoring, is_visible));
}
Expand All @@ -188,10 +187,10 @@ void AdsClientNotifier::NotifyTabDidChange(

void AdsClientNotifier::NotifyTabDidLoad(const int32_t tab_id,
const int http_status_code) {
if (should_queue_) {
return queue_->Add(base::BindOnce(&AdsClientNotifier::NotifyTabDidLoad,
weak_factory_.GetWeakPtr(), tab_id,
http_status_code));
if (task_queue_->should_queue()) {
return task_queue_->Add(base::BindOnce(&AdsClientNotifier::NotifyTabDidLoad,
weak_factory_.GetWeakPtr(), tab_id,
http_status_code));
}

for (auto& observer : observers_) {
Expand All @@ -200,8 +199,8 @@ void AdsClientNotifier::NotifyTabDidLoad(const int32_t tab_id,
}

void AdsClientNotifier::NotifyDidCloseTab(const int32_t tab_id) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyDidCloseTab,
weak_factory_.GetWeakPtr(), tab_id));
}
Expand All @@ -213,8 +212,8 @@ void AdsClientNotifier::NotifyDidCloseTab(const int32_t tab_id) {

void AdsClientNotifier::NotifyUserGestureEventTriggered(
const int32_t page_transition_type) {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyUserGestureEventTriggered,
weak_factory_.GetWeakPtr(), page_transition_type));
}
Expand All @@ -225,8 +224,8 @@ void AdsClientNotifier::NotifyUserGestureEventTriggered(
}

void AdsClientNotifier::NotifyUserDidBecomeIdle() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyUserDidBecomeIdle,
weak_factory_.GetWeakPtr()));
}
Expand All @@ -239,8 +238,8 @@ void AdsClientNotifier::NotifyUserDidBecomeIdle() {
void AdsClientNotifier::NotifyUserDidBecomeActive(
const base::TimeDelta idle_time,
const bool screen_was_locked) {
if (should_queue_) {
return pending_task_queue_->Add(base::BindOnce(
if (task_queue_->should_queue()) {
return task_queue_->Add(base::BindOnce(
&AdsClientNotifier::NotifyUserDidBecomeActive,
weak_factory_.GetWeakPtr(), idle_time, screen_was_locked));
}
Expand All @@ -251,8 +250,8 @@ void AdsClientNotifier::NotifyUserDidBecomeActive(
}

void AdsClientNotifier::NotifyBrowserDidEnterForeground() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyBrowserDidEnterForeground,
weak_factory_.GetWeakPtr()));
}
Expand All @@ -263,8 +262,8 @@ void AdsClientNotifier::NotifyBrowserDidEnterForeground() {
}

void AdsClientNotifier::NotifyBrowserDidEnterBackground() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyBrowserDidEnterBackground,
weak_factory_.GetWeakPtr()));
}
Expand All @@ -275,8 +274,8 @@ void AdsClientNotifier::NotifyBrowserDidEnterBackground() {
}

void AdsClientNotifier::NotifyBrowserDidBecomeActive() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyBrowserDidBecomeActive,
weak_factory_.GetWeakPtr()));
}
Expand All @@ -287,8 +286,8 @@ void AdsClientNotifier::NotifyBrowserDidBecomeActive() {
}

void AdsClientNotifier::NotifyBrowserDidResignActive() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyBrowserDidResignActive,
weak_factory_.GetWeakPtr()));
}
Expand All @@ -299,8 +298,8 @@ void AdsClientNotifier::NotifyBrowserDidResignActive() {
}

void AdsClientNotifier::NotifyDidSolveAdaptiveCaptcha() {
if (should_queue_) {
return pending_task_queue_->Add(
if (task_queue_->should_queue()) {
return task_queue_->Add(
base::BindOnce(&AdsClientNotifier::NotifyDidSolveAdaptiveCaptcha,
weak_factory_.GetWeakPtr()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ class BraveAdsAdsClientNotifierTest : public ::testing::Test {
};

TEST_F(BraveAdsAdsClientNotifierTest, FireQueuedAdsClientNotifications) {
// Arrange

// Act & Assert
ExpectAdsClientNotifierCallCount(0);
FireAdsClientNotifiers(); // Queue notifications.
Expand All @@ -196,8 +194,6 @@ TEST_F(BraveAdsAdsClientNotifierTest, FireQueuedAdsClientNotifications) {
TEST_F(
BraveAdsAdsClientNotifierTest,
DoNotFireQueuedAdsClientNotificationsIfNotifyPendingObserversIsNotCalled) {
// Arrange

// Act & Assert
ExpectAdsClientNotifierCallCount(0);
FireAdsClientNotifiers();
Expand Down
Loading

0 comments on commit 41a991e

Please sign in to comment.