Skip to content

Commit

Permalink
[ads] Unit test code health
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Nov 6, 2024
1 parent 160db53 commit 4c6d6e1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "brave/components/brave_ads/core/internal/ad_units/ad_test_constants.h"
#include "brave/components/brave_ads/core/internal/common/test/test_base.h"
#include "brave/components/brave_ads/core/internal/creatives/search_result_ads/creative_search_result_ad_test_util.h"
#include "net/http/http_status_code.h"
#include "testing/gtest/include/gtest/gtest.h"

// npm run test -- brave_unit_tests --filter=BraveAds*
Expand All @@ -34,7 +35,8 @@ TEST_F(BraveAdsCreativeAdCacheTest, AddCreativeAd) {
/*should_generate_random_uuids=*/true);

SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

// Act
creative_ad_cache_->MaybeAdd(test::kPlacementId, mojom_creative_ad->Clone());
Expand All @@ -48,7 +50,8 @@ TEST_F(BraveAdsCreativeAdCacheTest, AddCreativeAd) {
TEST_F(BraveAdsCreativeAdCacheTest, DoNotAddCreativeAd) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

// Act
creative_ad_cache_->MaybeAdd(test::kPlacementId,
Expand All @@ -63,7 +66,8 @@ TEST_F(BraveAdsCreativeAdCacheTest, DoNotAddCreativeAd) {
TEST_F(BraveAdsCreativeAdCacheTest, DoNotAddInvalidCreativeAd) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

CreativeAdVariant creative_ad_variant;

Expand Down Expand Up @@ -117,11 +121,13 @@ TEST_F(BraveAdsCreativeAdCacheTest, GetCreativeAdsAcrossMultipleTabs) {

// Act
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);
creative_ad_cache_->MaybeAdd(test::kPlacementId, mojom_creative_ad->Clone());

SimulateOpeningNewTab(/*tab_id=*/2,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);
creative_ad_cache_->MaybeAdd(test::kAnotherPlacementId,
mojom_creative_ad->Clone());

Expand All @@ -141,11 +147,13 @@ TEST_F(BraveAdsCreativeAdCacheTest, PurgePlacementsOnTabDidClose) {
/*should_generate_random_uuids=*/true);

SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);
creative_ad_cache_->MaybeAdd(test::kPlacementId, mojom_creative_ad->Clone());

SimulateOpeningNewTab(/*tab_id=*/2,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);
creative_ad_cache_->MaybeAdd(test::kAnotherPlacementId,
mojom_creative_ad->Clone());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class BraveAdsInlineContentAdIntegrationTest : public test::TestBase {
test::TestBase::SetUp(/*is_integration_test=*/true);

SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("brave://newtab")});
/*redirect_chain=*/{GURL("brave://newtab")},
net::HTTP_OK);
}

void SetUpMocks() override {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include "base/check_op.h"
#include "base/containers/contains.h"
#include "net/http/http_status_code.h"
#include "url/gurl.h"

namespace brave_ads {
Expand Down Expand Up @@ -177,19 +176,21 @@ void AdsClientNotifierForTesting::NotifyDidSolveAdaptiveCaptcha() {

void AdsClientNotifierForTesting::SimulateOpeningNewTab(
const int32_t tab_id,
const std::vector<GURL>& redirect_chain) {
const std::vector<GURL>& redirect_chain,
const int http_status_code) {
CHECK(!base::Contains(redirect_chains_, tab_id)) << "Tab already open";

redirect_chains_[tab_id] = redirect_chain;

SimulateSelectTab(tab_id);

SimulateNavigateToURL(tab_id, redirect_chain);
SimulateNavigateToURL(tab_id, redirect_chain, http_status_code);
}

void AdsClientNotifierForTesting::SimulateNavigateToURL(
const int32_t tab_id,
const std::vector<GURL>& redirect_chain) {
const std::vector<GURL>& redirect_chain,
const int http_status_code) {
CHECK(base::Contains(redirect_chains_, tab_id)) << "Tab does not exist";

redirect_chains_[tab_id] = redirect_chain;
Expand All @@ -198,7 +199,7 @@ void AdsClientNotifierForTesting::SimulateNavigateToURL(

NotifyTabDidChange(tab_id, redirect_chain, /*is_new_navigation=*/true,
/*is_restoring=*/false, is_visible);
NotifyTabDidLoad(tab_id, net::HTTP_OK);
NotifyTabDidLoad(tab_id, http_status_code);
}

void AdsClientNotifierForTesting::SimulateSelectTab(const int32_t tab_id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ class AdsClientNotifierForTesting : public AdsClientNotifier {

// Simulate helper functions.
void SimulateOpeningNewTab(int32_t tab_id,
const std::vector<GURL>& redirect_chain);
const std::vector<GURL>& redirect_chain,
int http_status_code);
void SimulateNavigateToURL(int32_t tab_id,
const std::vector<GURL>& redirect_chain);
const std::vector<GURL>& redirect_chain,
int http_status_code);
void SimulateSelectTab(int32_t tab_id);
void SimulateClosingTab(int32_t tab_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "brave/components/brave_ads/core/internal/targeting/behavioral/anti_targeting/resource/anti_targeting_resource.h"
#include "brave/components/brave_ads/core/internal/targeting/geographical/subdivision/subdivision_targeting.h"
#include "brave/components/brave_ads/core/public/ad_units/inline_content_ad/inline_content_ad_info.h"
#include "net/http/http_status_code.h"

// npm run test -- brave_unit_tests --filter=BraveAds*

Expand All @@ -30,7 +31,8 @@ class BraveAdsInlineContentAdServingTest : public test::TestBase {
void MaybeServeAd(const std::string& dimensions,
MaybeServeInlineContentAdCallback callback) {
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("brave://newtab")});
/*redirect_chain=*/{GURL("brave://newtab")},
net::HTTP_OK);

SubdivisionTargeting subdivision_targeting;
AntiTargetingResource anti_targeting_resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/test/scoped_feature_list.h"
#include "brave/components/brave_ads/core/internal/common/test/test_base.h"
#include "brave/components/brave_ads/core/internal/serving/permission_rules/permission_rule_feature.h"
#include "net/http/http_status_code.h"
#include "url/gurl.h"

// npm run test -- brave_unit_tests --filter=BraveAds*
Expand All @@ -25,7 +26,8 @@ TEST_F(BraveAdsMediaPermissionRuleTest,
ShouldAllowIfMediaIsStoppedForSingleTab) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);

Expand All @@ -39,7 +41,8 @@ TEST_F(BraveAdsMediaPermissionRuleTest,
ShouldAllowIfMediaIsStoppedOnMultipleTabs) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);
NotifyTabDidStartPlayingMedia(/*tab_id=*/2);
Expand All @@ -55,7 +58,8 @@ TEST_F(BraveAdsMediaPermissionRuleTest,
ShouldAllowIfMediaIsPlayingOnMultipleTabsButStoppedForVisibleTab) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);
NotifyTabDidStartPlayingMedia(/*tab_id=*/2);
Expand All @@ -70,7 +74,8 @@ TEST_F(BraveAdsMediaPermissionRuleTest,
ShouldNotAllowIfMediaIsPlayingOnVisibleTab) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);

Expand All @@ -88,7 +93,8 @@ TEST_F(
{{"should_only_serve_ads_if_media_is_not_playing", "false"}});

SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);

Expand All @@ -100,7 +106,8 @@ TEST_F(BraveAdsMediaPermissionRuleTest,
ShouldNotAllowIfMediaIsPlayingOnMultipleTabs) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);
NotifyTabDidStartPlayingMedia(/*tab_id=*/2);
Expand All @@ -113,7 +120,8 @@ TEST_F(BraveAdsMediaPermissionRuleTest,
ShouldNotAllowIfMediaIsPlayingOnMultipleTabsButStoppedForOccludedTab) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyTabDidStartPlayingMedia(/*tab_id=*/1);
NotifyTabDidStartPlayingMedia(/*tab_id=*/2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "brave/components/brave_ads/core/internal/settings/settings_test_util.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"
#include "brave/components/brave_ads/core/public/ads_feature.h"
#include "net/http/http_status_code.h"

// npm run test -- brave_unit_tests --filter=BraveAds*

Expand Down Expand Up @@ -222,7 +223,8 @@ TEST_F(BraveAdsSiteVisitUtilTest,
TEST_F(BraveAdsSiteVisitUtilTest, DidLandOnPage) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

// Act & Assert
EXPECT_TRUE(DidLandOnPage(/*tab_id=*/1, GURL("https://brave.com")));
Expand All @@ -231,7 +233,8 @@ TEST_F(BraveAdsSiteVisitUtilTest, DidLandOnPage) {
TEST_F(BraveAdsSiteVisitUtilTest, DoNotLandOnPageForClosedTab) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://brave.com")});
/*redirect_chain=*/{GURL("https://brave.com")},
net::HTTP_OK);

NotifyDidCloseTab(/*tab_id=*/1);

Expand All @@ -242,7 +245,8 @@ TEST_F(BraveAdsSiteVisitUtilTest, DoNotLandOnPageForClosedTab) {
TEST_F(BraveAdsSiteVisitUtilTest, DoNotLandOnPageForDomainOrHostMismatch) {
// Arrange
SimulateOpeningNewTab(/*tab_id=*/1,
/*redirect_chain=*/{GURL("https://foo.com")});
/*redirect_chain=*/{GURL("https://foo.com")},
net::HTTP_OK);

// Act & Assert
EXPECT_FALSE(DidLandOnPage(/*tab_id=*/1, GURL("https://brave.com")));
Expand Down

0 comments on commit 4c6d6e1

Please sign in to comment.