From bae7670397a1d265a9417350390b4bdc2d9b10fb Mon Sep 17 00:00:00 2001 From: brave-builds Date: Mon, 29 Jan 2024 17:57:44 +0000 Subject: [PATCH] Uplift of #21751 (squashed) to release --- .../ntp_background_images/browser/features.h | 5 +++-- .../browser/view_counter_model.cc | 7 ++++--- .../browser/view_counter_model_unittest.cc | 21 +++++++++++-------- .../browser/view_counter_service_unittest.cc | 4 ++-- .../ntp_background_images_service_ios.h | 5 +++++ .../ntp_background_images_service_ios.mm | 9 ++++++++ 6 files changed, 35 insertions(+), 16 deletions(-) diff --git a/components/ntp_background_images/browser/features.h b/components/ntp_background_images/browser/features.h index 3ee0ff77057b..f7d25d3dcc5e 100644 --- a/components/ntp_background_images/browser/features.h +++ b/components/ntp_background_images/browser/features.h @@ -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 { @@ -20,11 +21,11 @@ BASE_DECLARE_FEATURE(kBraveNTPBrandedWallpaper); // Show initial branded wallpaper after nth new tab page for fresh opens. inline constexpr base::FeatureParam 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 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 kResetCounterAfter{ diff --git a/components/ntp_background_images/browser/view_counter_model.cc b/components/ntp_background_images/browser/view_counter_model.cc index 9f5ae4b85046..b2ac87a519a3 100644 --- a/components/ntp_background_images/browser/view_counter_model.cc +++ b/components/ntp_background_images/browser/view_counter_model.cc @@ -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, @@ -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_] = @@ -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); } } diff --git a/components/ntp_background_images/browser/view_counter_model_unittest.cc b/components/ntp_background_images/browser/view_counter_model_unittest.cc index b98975e3c2f9..5f41035d7f01 100644 --- a/components/ntp_background_images/browser/view_counter_model_unittest.cc +++ b/components/ntp_background_images/browser/view_counter_model_unittest.cc @@ -31,8 +31,8 @@ class ViewCounterModelTest : public testing::Test { base::FieldTrialParams parameters; std::vector 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, {}); @@ -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(); } @@ -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(); } @@ -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(); } @@ -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(); } @@ -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(); @@ -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(); } diff --git a/components/ntp_background_images/browser/view_counter_service_unittest.cc b/components/ntp_background_images/browser/view_counter_service_unittest.cc index 12708ec41230..e2d83e1e1ad2 100644 --- a/components/ntp_background_images/browser/view_counter_service_unittest.cc +++ b/components/ntp_background_images/browser/view_counter_service_unittest.cc @@ -230,7 +230,7 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test { } int GetInitialCountToBrandedWallpaper() const { - return features::kInitialCountToBrandedWallpaper.Get(); + return features::kInitialCountToBrandedWallpaper.Get() - 1; } absl::optional TryGetFirstSponsoredImageWallpaper() { @@ -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_); diff --git a/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.h b/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.h index 4f0dbb7e1f7a..4d4deecd42f1 100644 --- a/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.h +++ b/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.h @@ -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; diff --git a/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm b/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm index 885ac4ebc219..34580d342ad4 100644 --- a/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm +++ b/ios/browser/api/ntp_background_images/ntp_background_images_service_ios.mm @@ -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" @@ -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(); }