Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple brave_ads::*AdsClientNotifierObserver from processors to resources #18311

Merged
merged 2 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/brave_ads/core/internal/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ source_set("internal") {
"resources/country_components.h",
"resources/language_components.cc",
"resources/language_components.h",
"resources/parsing_error_or.h",
"resources/resource_parsing_error_or.h",
"resources/resources_util.h",
"resources/resources_util_impl.h",
"segments/segment_alias.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool IssuerExistsForType(const IssuerType issuer_type) {

const absl::optional<IssuerInfo> issuer =
GetIssuerForType(*issuers, issuer_type);
return static_cast<bool>(issuer);
return bool{issuer};
}

absl::optional<IssuerInfo> GetIssuerForType(const IssuersInfo& issuers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ bool HasVisitedSiteOnAntiTargetingList(
} // namespace

AntiTargetingExclusionRule::AntiTargetingExclusionRule(
const AntiTargetingResource& anti_targeting_resource,
const AntiTargetingResource& resource,
BrowsingHistoryList browsing_history)
: anti_targeting_resource_(anti_targeting_resource),
browsing_history_(std::move(browsing_history)) {}
: resource_(resource), browsing_history_(std::move(browsing_history)) {}

AntiTargetingExclusionRule::~AntiTargetingExclusionRule() = default;

Expand Down Expand Up @@ -59,9 +58,8 @@ bool AntiTargetingExclusionRule::DoesRespectCap(
return true;
}

const auto iter =
anti_targeting_resource_->get().sites.find(creative_ad.creative_set_id);
if (iter == anti_targeting_resource_->get().sites.cend()) {
const auto iter = resource_->get().sites.find(creative_ad.creative_set_id);
if (iter == resource_->get().sites.cend()) {
// Always respect if creative set has no anti-targeting sites
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ struct CreativeAdInfo;
class AntiTargetingExclusionRule final
: public ExclusionRuleInterface<CreativeAdInfo> {
public:
AntiTargetingExclusionRule(
const AntiTargetingResource& anti_targeting_resource,
BrowsingHistoryList browsing_history);
AntiTargetingExclusionRule(const AntiTargetingResource& resource,
BrowsingHistoryList browsing_history);

AntiTargetingExclusionRule(const AntiTargetingExclusionRule&) = delete;
AntiTargetingExclusionRule& operator=(const AntiTargetingExclusionRule&) =
Expand All @@ -42,7 +41,7 @@ class AntiTargetingExclusionRule final
private:
bool DoesRespectCap(const CreativeAdInfo& creative_ad) const;

const raw_ref<const AntiTargetingResource> anti_targeting_resource_;
const raw_ref<const AntiTargetingResource> resource_;

BrowsingHistoryList browsing_history_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "brave/components/brave_ads/core/internal/ads/serving/eligible_ads/exclusion_rules/anti_targeting_exclusion_rule.h"

#include <memory>

#include "brave/components/brave_ads/core/internal/ads/ad_unittest_constants.h"
#include "brave/components/brave_ads/core/internal/common/unittest/unittest_base.h"
#include "brave/components/brave_ads/core/internal/creatives/creative_ad_info.h"
Expand All @@ -15,21 +17,34 @@

namespace brave_ads {

class BraveAdsAntiTargetingExclusionRuleTest : public UnitTestBase {};
class BraveAdsAntiTargetingExclusionRuleTest : public UnitTestBase {
protected:
void SetUp() override {
UnitTestBase::SetUp();

resource_ = std::make_unique<AntiTargetingResource>();
}

bool LoadResource() {
resource_->Load();
task_environment_.RunUntilIdle();
return resource_->IsInitialized();
}

std::unique_ptr<AntiTargetingResource> resource_;
};

TEST_F(BraveAdsAntiTargetingExclusionRuleTest,
AllowIfResourceIsNotInitialized) {
// Arrange
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kCreativeSetId;

AntiTargetingResource resource;

const BrowsingHistoryList history = {GURL("https://www.foo1.org"),
GURL("https://www.brave.com"),
GURL("https://www.foo2.org")};

const AntiTargetingExclusionRule exclusion_rule(resource, history);
const AntiTargetingExclusionRule exclusion_rule(*resource_, history);

// Act

Expand All @@ -39,63 +54,54 @@ TEST_F(BraveAdsAntiTargetingExclusionRuleTest,

TEST_F(BraveAdsAntiTargetingExclusionRuleTest, AllowIfCreativeSetDoesNotExist) {
// Arrange
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kMissingCreativeSetId;

AntiTargetingResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

const BrowsingHistoryList history = {GURL("https://www.foo1.org"),
GURL("https://www.brave.com"),
GURL("https://www.foo2.org")};

const AntiTargetingExclusionRule exclusion_rule(resource, history);
const AntiTargetingExclusionRule exclusion_rule(*resource_, history);

// Act

// Assert
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kMissingCreativeSetId;
EXPECT_TRUE(exclusion_rule.ShouldInclude(creative_ad).has_value());
}

TEST_F(BraveAdsAntiTargetingExclusionRuleTest, AllowIfSiteDoesNotExist) {
// Arrange
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kCreativeSetId;

AntiTargetingResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

const BrowsingHistoryList history = {GURL("https://www.foo1.org"),
GURL("https://www.foo2.org")};

const AntiTargetingExclusionRule exclusion_rule(resource, history);
const AntiTargetingExclusionRule exclusion_rule(*resource_, history);

// Act

// Assert
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kCreativeSetId;
EXPECT_TRUE(exclusion_rule.ShouldInclude(creative_ad).has_value());
}

TEST_F(BraveAdsAntiTargetingExclusionRuleTest,
DoNotAllowIfCreativeSetAndSiteMatch) {
// Arrange
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kCreativeSetId;

AntiTargetingResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

const BrowsingHistoryList history = {GURL("https://www.foo1.org"),
GURL("https://www.brave.com")};

const AntiTargetingExclusionRule exclusion_rule(resource, history);
const AntiTargetingExclusionRule exclusion_rule(*resource_, history);

// Act

// Assert
CreativeAdInfo creative_ad;
creative_ad.creative_set_id = kCreativeSetId;
EXPECT_FALSE(exclusion_rule.ShouldInclude(creative_ad).has_value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class InlineContentAdServing final {
MaybeServeInlineContentAdCallback callback);

private:
bool IsSupported() const { return static_cast<bool>(eligible_ads_); }
bool IsSupported() const { return bool{eligible_ads_}; }

void OnBuildUserModel(const std::string& dimensions,
MaybeServeInlineContentAdCallback callback,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NewTabPageAdServing final {
void MaybeServeAd(MaybeServeNewTabPageAdCallback callback);

private:
bool IsSupported() const { return static_cast<bool>(eligible_ads_); }
bool IsSupported() const { return bool{eligible_ads_}; }

void OnBuildUserModel(MaybeServeNewTabPageAdCallback callback,
const UserModelInfo& user_model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class NotificationAdServing final : public AdsClientNotifierObserver {
void MaybeServeAd();

private:
bool IsSupported() const { return static_cast<bool>(eligible_ads_); }
bool IsSupported() const { return bool{eligible_ads_}; }

void OnBuildUserModel(const UserModelInfo& user_model);
void OnGetForUserModel(const UserModelInfo& user_model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "brave/components/brave_ads/core/internal/ads/serving/targeting/behavioral/purchase_intent/purchase_intent_model.h"

#include <memory>

#include "brave/components/brave_ads/core/internal/common/unittest/unittest_base.h"
#include "brave/components/brave_ads/core/internal/processors/behavioral/purchase_intent/purchase_intent_processor.h"
#include "brave/components/brave_ads/core/internal/resources/behavioral/purchase_intent/purchase_intent_resource.h"
Expand All @@ -14,13 +16,27 @@

namespace brave_ads {

class BraveAdsPurchaseIntentModelTest : public UnitTestBase {};
class BraveAdsPurchaseIntentModelTest : public UnitTestBase {
protected:
void SetUp() override {
UnitTestBase::SetUp();

resource_ = std::make_unique<PurchaseIntentResource>();
}

bool LoadResource() {
resource_->Load();
task_environment_.RunUntilIdle();
return resource_->IsInitialized();
}

std::unique_ptr<PurchaseIntentResource> resource_;
};

TEST_F(BraveAdsPurchaseIntentModelTest,
DoNotGetSegmentsForUnitializedResource) {
// Arrange
PurchaseIntentResource resource;
PurchaseIntentProcessor processor(resource);
PurchaseIntentProcessor processor(*resource_);

const GURL url = GURL("https://www.brave.com/test?foo=bar");
processor.Process(url);
Expand All @@ -36,11 +52,9 @@ TEST_F(BraveAdsPurchaseIntentModelTest,

TEST_F(BraveAdsPurchaseIntentModelTest, DoNotGetSegmentsForExpiredSignals) {
// Arrange
PurchaseIntentResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

PurchaseIntentProcessor processor(resource);
PurchaseIntentProcessor processor(*resource_);

const GURL url_1 = GURL("https://www.brave.com/test?foo=bar");
processor.Process(url_1);
Expand All @@ -61,9 +75,7 @@ TEST_F(BraveAdsPurchaseIntentModelTest, DoNotGetSegmentsForExpiredSignals) {

TEST_F(BraveAdsPurchaseIntentModelTest, DoNotGetSegmentsIfNeverProcessed) {
// Arrange
PurchaseIntentResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

const PurchaseIntentModel model;

Expand All @@ -77,11 +89,9 @@ TEST_F(BraveAdsPurchaseIntentModelTest, DoNotGetSegmentsIfNeverProcessed) {
TEST_F(BraveAdsPurchaseIntentModelTest,
DoNotGetSegmentsIfNeverMatchedFunnelSites) {
// Arrange
PurchaseIntentResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

PurchaseIntentProcessor processor(resource);
PurchaseIntentProcessor processor(*resource_);

const GURL url = GURL("https://duckduckgo.com/?q=segment+keyword+1");
processor.Process(url);
Expand All @@ -97,11 +107,9 @@ TEST_F(BraveAdsPurchaseIntentModelTest,

TEST_F(BraveAdsPurchaseIntentModelTest, GetSegmentsForPreviouslyMatchedSite) {
// Arrange
PurchaseIntentResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

PurchaseIntentProcessor processor(resource);
PurchaseIntentProcessor processor(*resource_);

const GURL url_1 = GURL("https://www.brave.com/test?foo=bar");
processor.Process(url_1);
Expand All @@ -124,11 +132,9 @@ TEST_F(BraveAdsPurchaseIntentModelTest, GetSegmentsForPreviouslyMatchedSite) {
TEST_F(BraveAdsPurchaseIntentModelTest,
GetSegmentsForPreviouslyMatchedSegmentKeywords) {
// Arrange
PurchaseIntentResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

PurchaseIntentProcessor processor(resource);
PurchaseIntentProcessor processor(*resource_);

const GURL url = GURL("https://duckduckgo.com/?q=segment+keyword+1&foo=bar");
processor.Process(url);
Expand All @@ -148,11 +154,9 @@ TEST_F(BraveAdsPurchaseIntentModelTest,
TEST_F(BraveAdsPurchaseIntentModelTest,
GetSegmentsForPreviouslyMatchedFunnelKeywords) {
// Arrange
PurchaseIntentResource resource;
resource.Load();
task_environment_.RunUntilIdle();
ASSERT_TRUE(LoadResource());

PurchaseIntentProcessor processor(resource);
PurchaseIntentProcessor processor(*resource_);

const GURL url =
GURL("https://duckduckgo.com/?q=segment+keyword+1+funnel+keyword+2");
Expand Down
Loading