Skip to content

Commit

Permalink
Decouple brave_ads::*AdsClientNotifierObserver from processors to res…
Browse files Browse the repository at this point in the history
…ources
  • Loading branch information
tmancey committed May 3, 2023
1 parent 9f8307f commit 03e1926
Show file tree
Hide file tree
Showing 36 changed files with 443 additions and 379 deletions.
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 @@ -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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/components/brave_ads/core/internal/ads/serving/targeting/contextual/text_classification/text_classification_model.h"

#include <memory>
#include <string>
#include <vector>

Expand All @@ -21,19 +22,22 @@ class BraveAdsTextClassificationModelTest : public UnitTestBase {
void SetUp() override {
UnitTestBase::SetUp();

resource_.Load();
resource_ = std::make_unique<TextClassificationResource>();
}

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

TextClassificationResource resource_;
std::unique_ptr<TextClassificationResource> resource_;
};

TEST_F(BraveAdsTextClassificationModelTest,
DoNotGetSegmentsForUninitializedResource) {
// Arrange
TextClassificationResource resource;

TextClassificationProcessor processor(resource);
TextClassificationProcessor processor(*resource_);
processor.Process(/*text*/ "The quick brown fox jumps over the lazy dog");

const TextClassificationModel model;
Expand All @@ -47,8 +51,10 @@ TEST_F(BraveAdsTextClassificationModelTest,

TEST_F(BraveAdsTextClassificationModelTest, DoNotGetSegmentsForEmptyText) {
// Arrange
ASSERT_TRUE(LoadResource());

const std::string text;
TextClassificationProcessor processor(resource_);
TextClassificationProcessor processor(*resource_);
processor.Process(text);

const TextClassificationModel model;
Expand All @@ -63,7 +69,9 @@ TEST_F(BraveAdsTextClassificationModelTest, DoNotGetSegmentsForEmptyText) {
TEST_F(BraveAdsTextClassificationModelTest,
GetSegmentsForPreviouslyClassifiedText) {
// Arrange
TextClassificationProcessor processor(resource_);
ASSERT_TRUE(LoadResource());

TextClassificationProcessor processor(*resource_);
processor.Process(/*text*/ "Some content about technology & computing");

const TextClassificationModel model;
Expand Down Expand Up @@ -136,11 +144,13 @@ TEST_F(BraveAdsTextClassificationModelTest,
TEST_F(BraveAdsTextClassificationModelTest,
GetSegmentsForPreviouslyClassifiedTexts) {
// Arrange
ASSERT_TRUE(LoadResource());

const std::vector<std::string> texts = {
"Some content about cooking food", "Some content about finance & banking",
"Some content about technology & computing"};

TextClassificationProcessor processor(resource_);
TextClassificationProcessor processor(*resource_);
for (const auto& text : texts) {
processor.Process(text);
}
Expand Down Expand Up @@ -255,6 +265,8 @@ TEST_F(BraveAdsTextClassificationModelTest,

TEST_F(BraveAdsTextClassificationModelTest, DoNotGetSegmentsIfNeverProcessed) {
// Arrange
ASSERT_TRUE(LoadResource());

const TextClassificationModel model;

// Act
Expand Down
Loading

0 comments on commit 03e1926

Please sign in to comment.