Skip to content

Commit

Permalink
NTP SI distance to SI view gets reset to initial count every 1 day
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed Aug 22, 2023
1 parent 010a7e4 commit 64d8f1b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
22 changes: 20 additions & 2 deletions components/ntp_background_images/browser/view_counter_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
6 changes: 6 additions & 0 deletions components/ntp_background_images/browser/view_counter_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/timer/timer.h"

class PrefService;

Expand Down Expand Up @@ -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);
Expand All @@ -66,6 +69,8 @@ class ViewCounterModel {

void RegisterPageViewForBackgroundImages();

void OnTimerCountsResetExpired();

// For NTP SI.
raw_ptr<PrefService> prefs_ = nullptr;
int count_to_branded_wallpaper_ = 0;
Expand All @@ -75,6 +80,7 @@ class ViewCounterModel {
size_t total_campaign_count_ = 0;
std::vector<size_t> campaigns_total_branded_image_count_;
std::vector<size_t> campaigns_current_branded_image_index_;
base::RepeatingTimer timer_counts_reset_;

// For NTP BI.
int current_wallpaper_image_index_ = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<base::test::FeatureRefAndParams> 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) {
Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit 64d8f1b

Please sign in to comment.