Skip to content

Commit

Permalink
Fixed first background image skipped after launch
Browse files Browse the repository at this point in the history
fix brave/brave-browser#32577

When NTP is shown after launch and it shows branded image,
next loading should show first background image.
So far background image index is also increased when NTP loads
branded image. It should not be updated to show proper background
for next NTP loading/opening.
  • Loading branch information
simonhong committed Aug 29, 2023
1 parent 7685a58 commit 1acdf80
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ bool ViewCounterModel::ShouldShowBrandedWallpaper() const {
}

void ViewCounterModel::RegisterPageView() {
RegisterPageViewForBrandedImages();
// Call BG images first to know this calling is after showing
// branded image or not. If this calling is from branded image
// showing, background image index should not be changed.
RegisterPageViewForBackgroundImages();
RegisterPageViewForBrandedImages();
}

void ViewCounterModel::RegisterPageViewForBrandedImages() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ TEST_F(ViewCounterModelTest, NTPBackgroundImagesTest) {
EXPECT_EQ(expected_wallpaper_index, model.current_wallpaper_image_index());
model.RegisterPageView();
}

// It's time for sponsored image.
EXPECT_EQ(0, model.count_to_branded_wallpaper_);
const auto bg_index = model.current_wallpaper_image_index();
model.RegisterPageView();

// Check bg image index is not changed if sponsored image is shown.
// Only |count_to_branded_wallpapaer_| is reset.
EXPECT_NE(0, model.count_to_branded_wallpaper_);
EXPECT_EQ(bg_index, model.current_wallpaper_image_index());
}

// Test for background images only case (SI option is disabled)
Expand Down Expand Up @@ -234,10 +244,17 @@ TEST_F(ViewCounterModelTest, NTPFailedToLoadSponsoredImagesTest) {
const int initial_wallpaper_image_index =
model.current_wallpaper_image_index();

// Simulate that sponsored image ad was frequency capped by ads service. In
// this case next background wallpaper will be shown.
// Simulate that sponsored image ad was frequency capped by ads service.
// If |count_to_branded_wallpaper_| is zero when RegisterPageView() is called,
// only |count_to_branded_wallpaper_| is reset and background image index is
// not changed because it's time to show branded image. So, need to increase
// background image explicitely when background image is shown as ads was
// frequency capped. Client(ViewCounterService) calls this increase method
// when it's capped.
model.IncreaseBackgroundWallpaperImageIndex();
model.RegisterPageView();

// Check background index is increased properly.
int expected_image_index =
(initial_wallpaper_image_index + 1) % model.total_image_count_;
EXPECT_EQ(expected_image_index, model.current_wallpaper_image_index());
Expand Down

0 comments on commit 1acdf80

Please sign in to comment.