From 64d8f1be2dbcf1e128cfe8092fe8971e8f596909 Mon Sep 17 00:00:00 2001 From: Pete Miller Date: Tue, 22 Aug 2023 15:41:29 -0700 Subject: [PATCH] NTP SI distance to SI view gets reset to initial count every 1 day --- .../browser/view_counter_model.cc | 22 +++++++++++-- .../browser/view_counter_model.h | 6 ++++ .../browser/view_counter_model_unittest.cc | 31 ++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/components/ntp_background_images/browser/view_counter_model.cc b/components/ntp_background_images/browser/view_counter_model.cc index 264f9bc8a6b8..2432a004dc2a 100644 --- a/components/ntp_background_images/browser/view_counter_model.cc +++ b/components/ntp_background_images/browser/view_counter_model.cc @@ -8,19 +8,29 @@ #include "base/check.h" #include "base/logging.h" #include "base/rand_util.h" +#include "base/time/time.h" #include "brave/components/ntp_background_images/browser/features.h" #include "brave/components/ntp_background_images/common/pref_names.h" #include "components/prefs/pref_service.h" namespace ntp_background_images { +namespace { + +constexpr base::TimeDelta kCountsResetTimeDelay = base::Days(1); + +} // namespace + ViewCounterModel::ViewCounterModel(PrefService* prefs) : prefs_(prefs) { CHECK(prefs); // When browser is restarted or component is updated, we reset to "initial" // count. - count_to_branded_wallpaper_ = - features::kInitialCountToBrandedWallpaper.Get(); + count_to_branded_wallpaper_ = features::kInitialCountToBrandedWallpaper.Get(); + + // We also reset when a specific amount of time is elapsed when in SI mode + timer_counts_reset_.Start(FROM_HERE, kCountsResetTimeDelay, this, + &ViewCounterModel::OnTimerCountsResetExpired); } ViewCounterModel::~ViewCounterModel() = default; @@ -148,4 +158,12 @@ void ViewCounterModel::Reset() { campaigns_current_branded_image_index_.clear(); } +void ViewCounterModel::OnTimerCountsResetExpired() { + // Only reset count after timer expires for SI images + if (!always_show_branded_wallpaper_ && show_branded_wallpaper_) { + count_to_branded_wallpaper_ = + features::kInitialCountToBrandedWallpaper.Get(); + } +} + } // namespace ntp_background_images diff --git a/components/ntp_background_images/browser/view_counter_model.h b/components/ntp_background_images/browser/view_counter_model.h index 3a31603c2ce2..14409e0dd1f2 100644 --- a/components/ntp_background_images/browser/view_counter_model.h +++ b/components/ntp_background_images/browser/view_counter_model.h @@ -11,6 +11,7 @@ #include "base/gtest_prod_util.h" #include "base/memory/raw_ptr.h" +#include "base/timer/timer.h" class PrefService; @@ -52,6 +53,8 @@ class ViewCounterModel { private: friend class NTPBackgroundImagesViewCounterTest; FRIEND_TEST_ALL_PREFIXES(ViewCounterModelTest, NTPSponsoredImagesTest); + FRIEND_TEST_ALL_PREFIXES(ViewCounterModelTest, + NTPSponsoredImagesCountResetTimerTest); FRIEND_TEST_ALL_PREFIXES(ViewCounterModelTest, NTPSponsoredImagesCountToBrandedWallpaperTest); FRIEND_TEST_ALL_PREFIXES(ViewCounterModelTest, NTPBackgroundImagesTest); @@ -66,6 +69,8 @@ class ViewCounterModel { void RegisterPageViewForBackgroundImages(); + void OnTimerCountsResetExpired(); + // For NTP SI. raw_ptr prefs_ = nullptr; int count_to_branded_wallpaper_ = 0; @@ -75,6 +80,7 @@ class ViewCounterModel { size_t total_campaign_count_ = 0; std::vector campaigns_total_branded_image_count_; std::vector campaigns_current_branded_image_index_; + base::RepeatingTimer timer_counts_reset_; // For NTP BI. int current_wallpaper_image_index_ = 0; 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 6b7989ca0f11..716f8b7a8c56 100644 --- a/components/ntp_background_images/browser/view_counter_model_unittest.cc +++ b/components/ntp_background_images/browser/view_counter_model_unittest.cc @@ -4,10 +4,13 @@ // you can obtain one at http://mozilla.org/MPL/2.0/. #include "brave/components/ntp_background_images/browser/view_counter_model.h" +#include "base/test/scoped_feature_list.h" +#include "base/time/time.h" #include "brave/components/ntp_background_images/browser/features.h" #include "brave/components/ntp_background_images/browser/view_counter_service.h" #include "brave/components/ntp_background_images/common/pref_names.h" #include "components/sync_preferences/testing_pref_service_syncable.h" +#include "content/public/test/browser_task_environment.h" #include "testing/gtest/include/gtest/gtest.h" namespace { @@ -19,18 +22,29 @@ namespace ntp_background_images { class ViewCounterModelTest : public testing::Test { public: - ViewCounterModelTest() = default; + ViewCounterModelTest() + : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {} ~ViewCounterModelTest() override = default; void SetUp() override { auto* registry = prefs()->registry(); ViewCounterService::RegisterProfilePrefs(registry); + + base::FieldTrialParams parameters; + std::vector enabled_features; + parameters[features::kInitialCountToBrandedWallpaper.name] = "1"; + parameters[features::kCountToBrandedWallpaper.name] = "4"; + enabled_features.emplace_back(features::kBraveNTPBrandedWallpaper, + parameters); + feature_list_.InitWithFeaturesAndParameters(enabled_features, {}); } sync_preferences::TestingPrefServiceSyncable* prefs() { return &prefs_; } protected: + content::BrowserTaskEnvironment task_environment_; sync_preferences::TestingPrefServiceSyncable prefs_; + base::test::ScopedFeatureList feature_list_; }; TEST_F(ViewCounterModelTest, NTPSponsoredImagesTest) { @@ -113,6 +127,21 @@ TEST_F(ViewCounterModelTest, NTPSponsoredImagesCountToBrandedWallpaperTest) { model.RegisterPageView(); } +TEST_F(ViewCounterModelTest, NTPSponsoredImagesCountResetTimerTest) { + ViewCounterModel model(prefs()); + model.SetCampaignsTotalBrandedImageCount(kTestCampaignsTotalImageCount); + + // Verify param value for initial count was used + EXPECT_EQ(model.count_to_branded_wallpaper_, 1); + model.RegisterPageView(); + EXPECT_TRUE(model.ShouldShowBrandedWallpaper()); + model.RegisterPageView(); + EXPECT_FALSE(model.ShouldShowBrandedWallpaper()); + EXPECT_EQ(model.count_to_branded_wallpaper_, 4); + task_environment_.FastForwardBy(base::Days(1)); + EXPECT_EQ(model.count_to_branded_wallpaper_, 1); +} + TEST_F(ViewCounterModelTest, NTPBackgroundImagesTest) { ViewCounterModel model(prefs());