From 1acdf8018643b23996a799fce82d5ba3a2914f7c Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Tue, 29 Aug 2023 21:37:22 +0900 Subject: [PATCH] Fixed first background image skipped after launch fix https://github.com/brave/brave-browser/issues/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. --- .../browser/view_counter_model.cc | 5 ++++- .../browser/view_counter_model_unittest.cc | 21 +++++++++++++++++-- 2 files changed, 23 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 fdbdf8bd689e..cef294292bf7 100644 --- a/components/ntp_background_images/browser/view_counter_model.cc +++ b/components/ntp_background_images/browser/view_counter_model.cc @@ -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() { 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 37c9a117009d..ecfa706cf888 100644 --- a/components/ntp_background_images/browser/view_counter_model_unittest.cc +++ b/components/ntp_background_images/browser/view_counter_model_unittest.cc @@ -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) @@ -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());