Skip to content

Commit

Permalink
[ads] Unify iOS kBraveNTPBrandedWallpaper (uplift to 1.62.x) (#21801)
Browse files Browse the repository at this point in the history
Uplift of #21751 (squashed) to release
  • Loading branch information
brave-builds authored Jan 31, 2024
1 parent b2bcea9 commit d6dbbbb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 16 deletions.
5 changes: 3 additions & 2 deletions components/ntp_background_images/browser/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/time/time.h"

namespace ntp_background_images {
namespace features {
Expand All @@ -20,11 +21,11 @@ BASE_DECLARE_FEATURE(kBraveNTPBrandedWallpaper);

// Show initial branded wallpaper after nth new tab page for fresh opens.
inline constexpr base::FeatureParam<int> kInitialCountToBrandedWallpaper{
&kBraveNTPBrandedWallpaper, "initial_count_to_branded_wallpaper", 1};
&kBraveNTPBrandedWallpaper, "initial_count_to_branded_wallpaper", 2};

// Show branded wallpaper every nth new tab page.
inline constexpr base::FeatureParam<int> kCountToBrandedWallpaper{
&kBraveNTPBrandedWallpaper, "count_to_branded_wallpaper", 2};
&kBraveNTPBrandedWallpaper, "count_to_branded_wallpaper", 3};

// Reset counter when a specific amount of time has elapsed in SI mode.
inline constexpr base::FeatureParam<base::TimeDelta> kResetCounterAfter{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ ViewCounterModel::ViewCounterModel(PrefService* prefs) : prefs_(prefs) {

// When browser is restarted we reset to "initial" count. This will also get
// set again in the Reset() function, called e.g. when component is updated.
count_to_branded_wallpaper_ = features::kInitialCountToBrandedWallpaper.Get();
count_to_branded_wallpaper_ =
features::kInitialCountToBrandedWallpaper.Get() - 1;

// We also reset when a specific amount of time is elapsed when in SI mode
timer_counts_reset_.Start(FROM_HERE, features::kResetCounterAfter.Get(), this,
Expand Down Expand Up @@ -97,7 +98,7 @@ void ViewCounterModel::RegisterPageViewForBrandedImages() {
count_to_branded_wallpaper_--;
if (count_to_branded_wallpaper_ < 0) {
// Reset count and randomize image index for next time.
count_to_branded_wallpaper_ = features::kCountToBrandedWallpaper.Get();
count_to_branded_wallpaper_ = features::kCountToBrandedWallpaper.Get() - 1;

// Randomize SI campaign branded image index for next time.
campaigns_current_branded_image_index_[current_campaign_index_] =
Expand Down Expand Up @@ -152,7 +153,7 @@ void ViewCounterModel::MaybeResetBrandedWallpaperCount() {
if (!always_show_branded_wallpaper_ && show_branded_wallpaper_) {
count_to_branded_wallpaper_ =
std::min(count_to_branded_wallpaper_,
features::kInitialCountToBrandedWallpaper.Get());
features::kInitialCountToBrandedWallpaper.Get() - 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ViewCounterModelTest : public testing::Test {

base::FieldTrialParams parameters;
std::vector<base::test::FeatureRefAndParams> enabled_features;
parameters[features::kInitialCountToBrandedWallpaper.name] = "1";
parameters[features::kCountToBrandedWallpaper.name] = "3";
parameters[features::kInitialCountToBrandedWallpaper.name] = "2";
parameters[features::kCountToBrandedWallpaper.name] = "4";
enabled_features.emplace_back(features::kBraveNTPBrandedWallpaper,
parameters);
feature_list_.InitWithFeaturesAndParameters(enabled_features, {});
Expand Down Expand Up @@ -62,7 +62,8 @@ TEST_F(ViewCounterModelTest, NTPSponsoredImagesTest) {
EXPECT_FALSE(model.always_show_branded_wallpaper_);

// Loading initial count times.
for (int i = 0; i < features::kInitialCountToBrandedWallpaper.Get(); ++i) {
for (int i = 0; i < features::kInitialCountToBrandedWallpaper.Get() - 1;
++i) {
EXPECT_FALSE(model.ShouldShowBrandedWallpaper());
model.RegisterPageView();
}
Expand All @@ -83,7 +84,7 @@ TEST_F(ViewCounterModelTest, NTPSponsoredImagesTest) {
model.RegisterPageView();

// Loading regular-count times.
for (int j = 0; j < features::kCountToBrandedWallpaper.Get(); ++j) {
for (int j = 0; j < features::kCountToBrandedWallpaper.Get() - 1; ++j) {
EXPECT_FALSE(model.ShouldShowBrandedWallpaper());
model.RegisterPageView();
}
Expand Down Expand Up @@ -116,7 +117,7 @@ TEST_F(ViewCounterModelTest, NTPSponsoredImagesCountToBrandedWallpaperTest) {

// Loading regular-count times from kCountToBrandedWallpaper to 0 and do not
// show branded wallpaper.
for (int i = 0; i < features::kCountToBrandedWallpaper.Get(); ++i) {
for (int i = 0; i < features::kCountToBrandedWallpaper.Get() - 1; ++i) {
EXPECT_FALSE(model.ShouldShowBrandedWallpaper());
model.RegisterPageView();
}
Expand Down Expand Up @@ -183,7 +184,8 @@ TEST_F(ViewCounterModelTest, NTPBackgroundImagesTest) {
model.set_total_image_count(kTestImageCount);

// Loading initial count times.
for (int i = 0; i < features::kInitialCountToBrandedWallpaper.Get(); ++i) {
for (int i = 0; i < features::kInitialCountToBrandedWallpaper.Get() - 1;
++i) {
EXPECT_EQ(i, model.current_wallpaper_image_index());
model.RegisterPageView();
}
Expand All @@ -193,9 +195,9 @@ TEST_F(ViewCounterModelTest, NTPBackgroundImagesTest) {

// Loading regular-count times.
int expected_wallpaper_index;
for (int i = 0; i < features::kCountToBrandedWallpaper.Get(); ++i) {
for (int i = 0; i < features::kCountToBrandedWallpaper.Get() - 1; ++i) {
expected_wallpaper_index =
(i + features::kInitialCountToBrandedWallpaper.Get()) %
(i + (features::kInitialCountToBrandedWallpaper.Get() - 1)) %
model.total_image_count_;
EXPECT_EQ(expected_wallpaper_index, model.current_wallpaper_image_index());
model.RegisterPageView();
Expand Down Expand Up @@ -294,7 +296,8 @@ TEST_F(ViewCounterModelTest, NTPFailedToLoadSponsoredImagesTest) {
model.set_total_image_count(kTestImageCount);

// Loading initial count model.
for (int i = 0; i < features::kInitialCountToBrandedWallpaper.Get(); ++i) {
for (int i = 0; i < features::kInitialCountToBrandedWallpaper.Get() - 1;
++i) {
EXPECT_EQ(i, model.current_wallpaper_image_index());
model.RegisterPageView();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test {
}

int GetInitialCountToBrandedWallpaper() const {
return features::kInitialCountToBrandedWallpaper.Get();
return features::kInitialCountToBrandedWallpaper.Get() - 1;
}

absl::optional<base::Value::Dict> TryGetFirstSponsoredImageWallpaper() {
Expand Down Expand Up @@ -358,7 +358,7 @@ TEST_F(NTPBackgroundImagesViewCounterTest, IsActiveOptedIn) {
TEST_F(NTPBackgroundImagesViewCounterTest, PrefsWithModelTest) {
auto& model = view_counter_->model_;

EXPECT_EQ(features::kInitialCountToBrandedWallpaper.Get(),
EXPECT_EQ(features::kInitialCountToBrandedWallpaper.Get() - 1,
model.show_branded_wallpaper_);
EXPECT_TRUE(model.show_wallpaper_);
EXPECT_TRUE(model.show_branded_wallpaper_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ OBJC_EXPORT
@property(readonly, nullable) NTPSponsoredImageData* sponsoredImageData;
@property(readonly, nullable) NTPSponsoredImageData* superReferralImageData;

// TODO(https://github.com/brave/brave-core/pull/21559): Remove these properties
// once we have a better way to handle Griffin feature params from iOS.
@property(nonatomic, readonly) NSInteger initialCountToBrandedWallpaper;
@property(nonatomic, readonly) NSInteger countToBrandedWallpaper;

- (void)updateSponsoredImageComponentIfNeeded;

@property(readonly) NSString* superReferralCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/memory/raw_ptr.h"
#include "base/strings/sys_string_conversions.h"
#include "brave/components/ntp_background_images/browser/features.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_data.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
#include "brave/components/ntp_background_images/browser/ntp_sponsored_images_data.h"
Expand Down Expand Up @@ -103,6 +104,14 @@ - (NTPSponsoredImageData*)superReferralImageData {
return [[NTPSponsoredImageData alloc] initWithData:*data];
}

- (NSInteger)initialCountToBrandedWallpaper {
return ntp_background_images::features::kInitialCountToBrandedWallpaper.Get();
}

- (NSInteger)countToBrandedWallpaper {
return ntp_background_images::features::kCountToBrandedWallpaper.Get();
}

- (void)updateSponsoredImageComponentIfNeeded {
_service->CheckNTPSIComponentUpdateIfNeeded();
}
Expand Down

0 comments on commit d6dbbbb

Please sign in to comment.