[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 4af4858 | 2016-04-19 00:29:35 | [diff] [blame] | 5 | #include "chrome/browser/search/instant_service.h" |
| 6 | |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
| 10 | #include "base/path_service.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 11 | #include "base/task/thread_pool/thread_pool_instance.h" |
Kristi Park | 999db91 | 2018-12-05 23:46:42 | [diff] [blame] | 12 | #include "base/test/mock_callback.h" |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 13 | #include "chrome/browser/chrome_notification_types.h" |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 14 | #include "chrome/browser/search/background/ntp_background_service.h" |
Kristi Park | 30e182e | 2019-02-09 02:15:38 | [diff] [blame] | 15 | #include "chrome/browser/search/instant_service_observer.h" |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 16 | #include "chrome/browser/search/instant_unittest_base.h" |
Kyle Milka | 848864f6e | 2018-08-02 21:59:31 | [diff] [blame] | 17 | #include "chrome/browser/search/ntp_features.h" |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 18 | #include "chrome/browser/themes/theme_properties.h" |
| 19 | #include "chrome/browser/themes/theme_service.h" |
| 20 | #include "chrome/browser/themes/theme_service_factory.h" |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 21 | #include "chrome/common/chrome_paths.h" |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 22 | #include "chrome/common/pref_names.h" |
Marc Treib | be5f3feb | 2017-09-04 09:53:16 | [diff] [blame] | 23 | #include "chrome/common/search/instant_types.h" |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
Kristi Park | 01ca125 | 2019-05-10 22:39:05 | [diff] [blame] | 25 | #include "components/ntp_tiles/features.h" |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 26 | #include "components/ntp_tiles/ntp_tile.h" |
| 27 | #include "components/ntp_tiles/section_type.h" |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 28 | #include "components/sync_preferences/testing_pref_service_syncable.h" |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 29 | #include "content/public/test/test_utils.h" |
Kristi Park | 30e182e | 2019-02-09 02:15:38 | [diff] [blame] | 30 | #include "testing/gmock/include/gmock/gmock.h" |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 31 | #include "testing/gtest/include/gtest/gtest.h" |
Kristi Park | ff3cf95 | 2019-01-28 23:00:59 | [diff] [blame] | 32 | #include "ui/native_theme/test_native_theme.h" |
[email protected] | 2309e91 | 2013-10-01 01:33:30 | [diff] [blame] | 33 | #include "url/gurl.h" |
[email protected] | c8a118e | 2013-09-24 21:33:40 | [diff] [blame] | 34 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 35 | namespace { |
Kristi Park | 30e182e | 2019-02-09 02:15:38 | [diff] [blame] | 36 | |
| 37 | class MockInstantServiceObserver : public InstantServiceObserver { |
| 38 | public: |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 39 | MOCK_METHOD1(NtpThemeChanged, void(const NtpTheme&)); |
Kristi Park | 6424e60 | 2019-06-11 18:01:43 | [diff] [blame] | 40 | MOCK_METHOD1(MostVisitedInfoChanged, void(const InstantMostVisitedInfo&)); |
Kristi Park | 30e182e | 2019-02-09 02:15:38 | [diff] [blame] | 41 | }; |
| 42 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 43 | base::DictionaryValue GetBackgroundInfoAsDict(const GURL& background_url) { |
| 44 | base::DictionaryValue background_info; |
| 45 | background_info.SetKey("background_url", base::Value(background_url.spec())); |
| 46 | background_info.SetKey("attribution_line_1", base::Value(std::string())); |
| 47 | background_info.SetKey("attribution_line_2", base::Value(std::string())); |
| 48 | background_info.SetKey("attribution_action_url", base::Value(std::string())); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 49 | background_info.SetKey("collection_id", base::Value(std::string())); |
| 50 | background_info.SetKey("resume_token", base::Value(std::string())); |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 51 | background_info.SetKey("refresh_timestamp", base::Value(0)); |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 52 | return background_info; |
| 53 | } |
Kristi Park | 30e182e | 2019-02-09 02:15:38 | [diff] [blame] | 54 | |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 55 | base::Time GetReferenceTime() { |
| 56 | base::Time::Exploded exploded_reference_time; |
| 57 | exploded_reference_time.year = 2019; |
| 58 | exploded_reference_time.month = 1; |
| 59 | exploded_reference_time.day_of_month = 1; |
| 60 | exploded_reference_time.day_of_week = 1; |
| 61 | exploded_reference_time.hour = 0; |
| 62 | exploded_reference_time.minute = 0; |
| 63 | exploded_reference_time.second = 0; |
| 64 | exploded_reference_time.millisecond = 0; |
| 65 | |
| 66 | base::Time out_time; |
| 67 | EXPECT_TRUE( |
| 68 | base::Time::FromLocalExploded(exploded_reference_time, &out_time)); |
| 69 | return out_time; |
| 70 | } |
| 71 | |
Gayane Petrosyan | 9929610 | 2019-03-06 19:07:49 | [diff] [blame] | 72 | class MockInstantService : public InstantService { |
| 73 | public: |
| 74 | explicit MockInstantService(Profile* profile) : InstantService(profile) {} |
| 75 | ~MockInstantService() override = default; |
| 76 | |
| 77 | MOCK_METHOD0(ResetCustomLinks, bool()); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 78 | MOCK_METHOD0(ResetCustomBackgroundNtpTheme, void()); |
Gayane Petrosyan | 9929610 | 2019-03-06 19:07:49 | [diff] [blame] | 79 | }; |
| 80 | |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 81 | bool CheckBackgroundColor(SkColor color, |
| 82 | const base::DictionaryValue* background_info) { |
| 83 | if (!background_info) |
| 84 | return false; |
| 85 | |
| 86 | const base::Value* background_color = |
| 87 | background_info->FindKey(kNtpCustomBackgroundMainColor); |
| 88 | if (!background_color) |
| 89 | return false; |
| 90 | |
| 91 | return color == static_cast<uint32_t>(background_color->GetInt()); |
| 92 | } |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 93 | } // namespace |
| 94 | |
Marc Treib | c4de3f1 | 2017-09-05 12:36:15 | [diff] [blame] | 95 | using InstantServiceTest = InstantUnitTestBase; |
[email protected] | c4b2af2 | 2014-05-11 19:48:53 | [diff] [blame] | 96 | |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 97 | TEST_F(InstantServiceTest, GetNTPTileSuggestion) { |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 98 | ntp_tiles::NTPTile some_tile; |
| 99 | some_tile.source = ntp_tiles::TileSource::TOP_SITES; |
| 100 | some_tile.title_source = ntp_tiles::TileTitleSource::TITLE_TAG; |
| 101 | ntp_tiles::NTPTilesVector suggestions{some_tile}; |
| 102 | |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 103 | std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector> suggestions_map; |
| 104 | suggestions_map[ntp_tiles::SectionType::PERSONALIZED] = suggestions; |
zmin | fddc3e3 | 2015-12-09 22:59:08 | [diff] [blame] | 105 | |
Marc Treib | 7b3985e | 2017-09-18 11:22:16 | [diff] [blame] | 106 | instant_service_->OnURLsAvailable(suggestions_map); |
zmin | fddc3e3 | 2015-12-09 22:59:08 | [diff] [blame] | 107 | |
Kristi Park | e6893152 | 2019-06-08 00:17:18 | [diff] [blame] | 108 | auto items = instant_service_->most_visited_info_->items; |
zmin | fddc3e3 | 2015-12-09 22:59:08 | [diff] [blame] | 109 | ASSERT_EQ(1, (int)items.size()); |
Friedrich Horschig | efec081 | 2017-09-28 18:50:17 | [diff] [blame] | 110 | EXPECT_EQ(ntp_tiles::TileSource::TOP_SITES, items[0].source); |
| 111 | EXPECT_EQ(ntp_tiles::TileTitleSource::TITLE_TAG, items[0].title_source); |
zmin | fddc3e3 | 2015-12-09 22:59:08 | [diff] [blame] | 112 | } |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 113 | |
Kristi Park | b8aec55 | 2019-04-18 21:08:56 | [diff] [blame] | 114 | TEST_F(InstantServiceTest, DoesToggleMostVisitedOrCustomLinks) { |
| 115 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 116 | profile()->GetTestingPrefService(); |
| 117 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 118 | ASSERT_FALSE(pref_service->GetBoolean(prefs::kNtpUseMostVisitedTiles)); |
Kristi Park | e6893152 | 2019-06-08 00:17:18 | [diff] [blame] | 119 | ASSERT_FALSE(instant_service_->most_visited_info_->use_most_visited); |
Kristi Park | b8aec55 | 2019-04-18 21:08:56 | [diff] [blame] | 120 | |
| 121 | // Enable most visited tiles. |
| 122 | EXPECT_TRUE(instant_service_->ToggleMostVisitedOrCustomLinks()); |
| 123 | EXPECT_TRUE(pref_service->GetBoolean(prefs::kNtpUseMostVisitedTiles)); |
Kristi Park | e6893152 | 2019-06-08 00:17:18 | [diff] [blame] | 124 | EXPECT_TRUE(instant_service_->most_visited_info_->use_most_visited); |
Kristi Park | b8aec55 | 2019-04-18 21:08:56 | [diff] [blame] | 125 | |
| 126 | // Disable most visited tiles. |
| 127 | EXPECT_TRUE(instant_service_->ToggleMostVisitedOrCustomLinks()); |
| 128 | EXPECT_FALSE(pref_service->GetBoolean(prefs::kNtpUseMostVisitedTiles)); |
Kristi Park | e6893152 | 2019-06-08 00:17:18 | [diff] [blame] | 129 | EXPECT_FALSE(instant_service_->most_visited_info_->use_most_visited); |
Kristi Park | b8aec55 | 2019-04-18 21:08:56 | [diff] [blame] | 130 | |
| 131 | // Should do nothing if this is a non-Google NTP. |
| 132 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
| 133 | EXPECT_FALSE(instant_service_->ToggleMostVisitedOrCustomLinks()); |
| 134 | EXPECT_FALSE(pref_service->GetBoolean(prefs::kNtpUseMostVisitedTiles)); |
Kristi Park | e6893152 | 2019-06-08 00:17:18 | [diff] [blame] | 135 | EXPECT_FALSE(instant_service_->most_visited_info_->use_most_visited); |
Kristi Park | b8aec55 | 2019-04-18 21:08:56 | [diff] [blame] | 136 | } |
| 137 | |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 138 | TEST_F(InstantServiceTest, DoesToggleShortcutsVisibility) { |
Kristi Park | 955ad55 | 2019-07-02 21:50:33 | [diff] [blame] | 139 | testing::StrictMock<MockInstantServiceObserver> mock_observer; |
| 140 | instant_service_->AddObserver(&mock_observer); |
| 141 | |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 142 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 143 | profile()->GetTestingPrefService(); |
| 144 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 145 | ASSERT_TRUE(pref_service->GetBoolean(prefs::kNtpShortcutsVisible)); |
| 146 | ASSERT_TRUE(instant_service_->most_visited_info_->is_visible); |
| 147 | |
| 148 | // Hide shortcuts. |
Kristi Park | 955ad55 | 2019-07-02 21:50:33 | [diff] [blame] | 149 | EXPECT_CALL(mock_observer, MostVisitedInfoChanged(testing::_)).Times(0); |
| 150 | EXPECT_TRUE(instant_service_->ToggleShortcutsVisibility(false)); |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 151 | EXPECT_FALSE(pref_service->GetBoolean(prefs::kNtpShortcutsVisible)); |
| 152 | EXPECT_FALSE(instant_service_->most_visited_info_->is_visible); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 153 | task_environment()->RunUntilIdle(); |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 154 | |
Kristi Park | 955ad55 | 2019-07-02 21:50:33 | [diff] [blame] | 155 | // Show shortcuts, and check that a notification was sent. |
| 156 | EXPECT_CALL(mock_observer, MostVisitedInfoChanged(testing::_)).Times(1); |
| 157 | EXPECT_TRUE(instant_service_->ToggleShortcutsVisibility(true)); |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 158 | EXPECT_TRUE(pref_service->GetBoolean(prefs::kNtpShortcutsVisible)); |
| 159 | EXPECT_TRUE(instant_service_->most_visited_info_->is_visible); |
| 160 | |
| 161 | // Should do nothing if this is a non-Google NTP. |
Kristi Park | 955ad55 | 2019-07-02 21:50:33 | [diff] [blame] | 162 | EXPECT_CALL(mock_observer, MostVisitedInfoChanged(testing::_)).Times(0); |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 163 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
Kristi Park | 955ad55 | 2019-07-02 21:50:33 | [diff] [blame] | 164 | EXPECT_FALSE(instant_service_->ToggleShortcutsVisibility(false)); |
Kristi Park | 30aa634 | 2019-06-11 22:43:27 | [diff] [blame] | 165 | EXPECT_TRUE(pref_service->GetBoolean(prefs::kNtpShortcutsVisible)); |
| 166 | EXPECT_TRUE(instant_service_->most_visited_info_->is_visible); |
| 167 | } |
| 168 | |
Kristi Park | 4165308 | 2019-01-09 22:20:30 | [diff] [blame] | 169 | TEST_F(InstantServiceTest, |
Ramya Nagarajan | 7673f05 | 2018-08-13 22:53:29 | [diff] [blame] | 170 | DisableUndoCustomLinkActionForNonGoogleSearchProvider) { |
| 171 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 172 | EXPECT_TRUE(instant_service_->UndoCustomLinkAction()); |
| 173 | |
| 174 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
| 175 | EXPECT_FALSE(instant_service_->UndoCustomLinkAction()); |
| 176 | } |
| 177 | |
Kristi Park | 4165308 | 2019-01-09 22:20:30 | [diff] [blame] | 178 | TEST_F(InstantServiceTest, DisableResetCustomLinksForNonGoogleSearchProvider) { |
Ramya Nagarajan | 7673f05 | 2018-08-13 22:53:29 | [diff] [blame] | 179 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 180 | EXPECT_TRUE(instant_service_->ResetCustomLinks()); |
| 181 | |
| 182 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
| 183 | EXPECT_FALSE(instant_service_->ResetCustomLinks()); |
| 184 | } |
| 185 | |
Kristi Park | b8aec55 | 2019-04-18 21:08:56 | [diff] [blame] | 186 | TEST_F(InstantServiceTest, IsCustomLinksEnabled) { |
| 187 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 188 | profile()->GetTestingPrefService(); |
| 189 | |
| 190 | // Test that custom links are only enabled when Most Visited is toggled off |
| 191 | // and this is a Google NTP. |
| 192 | pref_service->SetBoolean(prefs::kNtpUseMostVisitedTiles, false); |
| 193 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 194 | EXPECT_TRUE(instant_service_->IsCustomLinksEnabled()); |
| 195 | |
| 196 | // All other cases should return false. |
| 197 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
| 198 | EXPECT_FALSE(instant_service_->IsCustomLinksEnabled()); |
| 199 | pref_service->SetBoolean(prefs::kNtpUseMostVisitedTiles, true); |
| 200 | EXPECT_FALSE(instant_service_->IsCustomLinksEnabled()); |
| 201 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 202 | EXPECT_FALSE(instant_service_->IsCustomLinksEnabled()); |
| 203 | } |
| 204 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 205 | TEST_F(InstantServiceTest, SetCustomBackgroundURL) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 206 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 207 | const GURL kUrl("https://www.foo.com"); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 208 | |
| 209 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 210 | instant_service_->SetCustomBackgroundInfo(kUrl, "", "", GURL(), ""); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 211 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 212 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 213 | EXPECT_EQ(kUrl, theme->custom_background_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 214 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 215 | } |
| 216 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 217 | TEST_F(InstantServiceTest, SetCustomBackgroundURLInvalidURL) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 218 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 219 | const GURL kInvalidUrl("foo"); |
| 220 | const GURL kValidUrl("https://www.foo.com"); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 221 | instant_service_->AddValidBackdropUrlForTesting(kValidUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 222 | instant_service_->SetCustomBackgroundInfo(kValidUrl, "", "", GURL(), ""); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 223 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 224 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 225 | EXPECT_EQ(kValidUrl.spec(), theme->custom_background_url.spec()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 226 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 227 | instant_service_->SetCustomBackgroundInfo(kInvalidUrl, "", "", GURL(), ""); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 228 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 229 | theme = instant_service_->GetInitializedNtpTheme(); |
| 230 | EXPECT_EQ("", theme->custom_background_url.spec()); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 231 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 232 | } |
| 233 | |
Kyle Milka | afef744 | 2019-07-31 23:55:52 | [diff] [blame] | 234 | TEST_F(InstantServiceTest, SetCustomBackgroundInfo) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 235 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 236 | const GURL kUrl("https://www.foo.com"); |
| 237 | const std::string kAttributionLine1 = "foo"; |
| 238 | const std::string kAttributionLine2 = "bar"; |
| 239 | const GURL kActionUrl("https://www.bar.com"); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 240 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 241 | instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1, |
| 242 | kAttributionLine2, kActionUrl, ""); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 243 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 244 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 245 | EXPECT_EQ(kUrl, theme->custom_background_url); |
| 246 | EXPECT_EQ(kAttributionLine1, theme->custom_background_attribution_line_1); |
| 247 | EXPECT_EQ(kAttributionLine2, theme->custom_background_attribution_line_2); |
| 248 | EXPECT_EQ(kActionUrl, theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 249 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 250 | } |
| 251 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 252 | TEST_F(InstantServiceTest, ChangingSearchProviderClearsNtpThemeAndPref) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 253 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 254 | const GURL kUrl("https://www.foo.com"); |
| 255 | const std::string kAttributionLine1 = "foo"; |
| 256 | const std::string kAttributionLine2 = "bar"; |
| 257 | const GURL kActionUrl("https://www.bar.com"); |
| 258 | |
| 259 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 260 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 261 | instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1, |
| 262 | kAttributionLine2, kActionUrl, ""); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 263 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 264 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 265 | EXPECT_EQ(kUrl, theme->custom_background_url); |
| 266 | EXPECT_EQ(kAttributionLine1, theme->custom_background_attribution_line_1); |
| 267 | EXPECT_EQ(kAttributionLine2, theme->custom_background_attribution_line_2); |
| 268 | EXPECT_EQ(kActionUrl, theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 269 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 270 | |
| 271 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 272 | instant_service_->UpdateNtpTheme(); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 273 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 274 | theme = instant_service_->GetInitializedNtpTheme(); |
| 275 | EXPECT_EQ(GURL(), theme->custom_background_url); |
| 276 | EXPECT_EQ("", theme->custom_background_attribution_line_1); |
| 277 | EXPECT_EQ("", theme->custom_background_attribution_line_2); |
| 278 | EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 279 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 280 | |
| 281 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 282 | instant_service_->UpdateNtpTheme(); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 283 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 284 | theme = instant_service_->GetInitializedNtpTheme(); |
| 285 | EXPECT_EQ(GURL(), theme->custom_background_url); |
| 286 | EXPECT_EQ("", theme->custom_background_attribution_line_1); |
| 287 | EXPECT_EQ("", theme->custom_background_attribution_line_2); |
| 288 | EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 289 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 290 | } |
| 291 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 292 | TEST_F(InstantServiceTest, LocalBackgroundImageCopyCreated) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 293 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 294 | const GURL kUrl("chrome-search://local-ntp/background.jpg"); |
| 295 | |
Kyle Milka | ed7c3b3 | 2018-09-25 16:24:16 | [diff] [blame] | 296 | base::FilePath profile_path = profile()->GetPath(); |
| 297 | base::FilePath path(profile_path.AppendASCII("test_file")); |
| 298 | base::FilePath copy_path(profile_path.AppendASCII( |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 299 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 300 | |
| 301 | base::WriteFile(path, "background_image", 16); |
| 302 | |
| 303 | instant_service_->SelectLocalBackgroundImage(path); |
| 304 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 305 | task_environment()->RunUntilIdle(); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 306 | |
| 307 | bool file_exists = base::PathExists(copy_path); |
| 308 | |
| 309 | EXPECT_EQ(true, file_exists); |
| 310 | EXPECT_EQ(true, profile()->GetTestingPrefService()->GetBoolean( |
| 311 | prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 312 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 313 | } |
| 314 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 315 | TEST_F(InstantServiceTest, |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 316 | ChangingSearchProviderRemovesLocalBackgroundImageCopy) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 317 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | ed7c3b3 | 2018-09-25 16:24:16 | [diff] [blame] | 318 | base::FilePath profile_path = profile()->GetPath(); |
| 319 | base::FilePath path(profile_path.AppendASCII( |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 320 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 321 | |
| 322 | base::WriteFile(path, "background_image", 16); |
| 323 | |
| 324 | SetUserSelectedDefaultSearchProvider("https://www.search.com"); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 325 | instant_service_->UpdateNtpTheme(); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 326 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 327 | task_environment()->RunUntilIdle(); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 328 | |
| 329 | bool file_exists = base::PathExists(path); |
| 330 | |
| 331 | EXPECT_EQ(false, file_exists); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 332 | EXPECT_EQ(false, profile()->GetTestingPrefService()->GetBoolean( |
| 333 | prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 334 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 335 | } |
| 336 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 337 | TEST_F(InstantServiceTest, SettingUrlRemovesLocalBackgroundImageCopy) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 338 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 339 | const GURL kUrl("https://www.foo.com"); |
| 340 | |
Kyle Milka | ed7c3b3 | 2018-09-25 16:24:16 | [diff] [blame] | 341 | base::FilePath profile_path = profile()->GetPath(); |
| 342 | base::FilePath path(profile_path.AppendASCII( |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 343 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 344 | |
| 345 | base::WriteFile(path, "background_image", 16); |
| 346 | |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 347 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 348 | instant_service_->SetCustomBackgroundInfo(kUrl, "", "", GURL(), ""); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 349 | instant_service_->UpdateNtpTheme(); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 350 | |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 351 | task_environment()->RunUntilIdle(); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 352 | |
| 353 | bool file_exists = base::PathExists(path); |
| 354 | |
| 355 | EXPECT_EQ(false, file_exists); |
| 356 | EXPECT_EQ(false, profile()->GetTestingPrefService()->GetBoolean( |
| 357 | prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 358 | ASSERT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 6986ede | 2018-07-19 02:16:24 | [diff] [blame] | 359 | } |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 360 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 361 | TEST_F(InstantServiceTest, CustomBackgroundAttributionActionUrlReset) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 362 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 363 | const GURL kUrl("https://www.foo.com"); |
| 364 | const std::string kAttributionLine1 = "foo"; |
| 365 | const std::string kAttributionLine2 = "bar"; |
| 366 | const GURL kHttpsActionUrl("https://www.bar.com"); |
| 367 | const GURL kHttpActionUrl("http://www.bar.com"); |
| 368 | |
| 369 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 370 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 371 | instant_service_->SetCustomBackgroundInfo( |
| 372 | kUrl, kAttributionLine1, kAttributionLine2, kHttpsActionUrl, ""); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 373 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 374 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 375 | EXPECT_EQ(kHttpsActionUrl, theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 376 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 377 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 378 | instant_service_->SetCustomBackgroundInfo( |
| 379 | kUrl, kAttributionLine1, kAttributionLine2, kHttpActionUrl, ""); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 380 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 381 | theme = instant_service_->GetInitializedNtpTheme(); |
| 382 | EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 383 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 384 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 385 | instant_service_->SetCustomBackgroundInfo( |
| 386 | kUrl, kAttributionLine1, kAttributionLine2, kHttpsActionUrl, ""); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 387 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 388 | theme = instant_service_->GetInitializedNtpTheme(); |
| 389 | EXPECT_EQ(kHttpsActionUrl, theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 390 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 391 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 392 | instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1, |
| 393 | kAttributionLine2, GURL(), ""); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 394 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 395 | theme = instant_service_->GetInitializedNtpTheme(); |
| 396 | EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 397 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | d68475e | 2018-07-30 00:05:04 | [diff] [blame] | 398 | } |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 399 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 400 | TEST_F(InstantServiceTest, UpdatingPrefUpdatesNtpTheme) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 401 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 402 | const GURL kUrlFoo("https://www.foo.com"); |
| 403 | const GURL kUrlBar("https://www.bar.com"); |
| 404 | |
| 405 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 406 | profile()->GetTestingPrefService(); |
| 407 | pref_service->SetUserPref( |
| 408 | prefs::kNtpCustomBackgroundDict, |
| 409 | std::make_unique<base::Value>(GetBackgroundInfoAsDict(kUrlFoo))); |
| 410 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 411 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 412 | EXPECT_EQ(kUrlFoo, theme->custom_background_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 413 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 414 | |
| 415 | pref_service->SetUserPref( |
| 416 | prefs::kNtpCustomBackgroundDict, |
| 417 | std::make_unique<base::Value>(GetBackgroundInfoAsDict(kUrlBar))); |
| 418 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 419 | theme = instant_service_->GetInitializedNtpTheme(); |
| 420 | EXPECT_EQ(kUrlBar, theme->custom_background_url); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 421 | EXPECT_EQ(false, |
| 422 | pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 423 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 424 | } |
| 425 | |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 426 | TEST_F(InstantServiceTest, SetLocalImage) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 427 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 428 | const GURL kUrl("chrome-search://local-ntp/background.jpg?123456789"); |
| 429 | |
| 430 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 431 | profile()->GetTestingPrefService(); |
| 432 | |
Kyle Milka | ed7c3b3 | 2018-09-25 16:24:16 | [diff] [blame] | 433 | base::FilePath profile_path = profile()->GetPath(); |
| 434 | base::FilePath path(profile_path.AppendASCII( |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 435 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 436 | base::WriteFile(path, "background_image", 16); |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 437 | base::ThreadPoolInstance::Get()->FlushForTesting(); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 438 | |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 439 | instant_service_->SelectLocalBackgroundImage(path); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 440 | task_environment()->RunUntilIdle(); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 441 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 442 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 443 | EXPECT_TRUE(base::StartsWith(theme->custom_background_url.spec(), |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 444 | chrome::kChromeSearchLocalNtpBackgroundUrl, |
| 445 | base::CompareCase::SENSITIVE)); |
| 446 | EXPECT_TRUE( |
| 447 | pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 448 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 449 | } |
| 450 | |
Kyle Milka | 724ded4 | 2019-08-03 04:17:16 | [diff] [blame] | 451 | TEST_F(InstantServiceTest, SyncPrefOverridesAndRemovesLocalImage) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 452 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 453 | const GURL kUrl("https://www.foo.com/"); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 454 | |
| 455 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 456 | profile()->GetTestingPrefService(); |
| 457 | |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 458 | base::FilePath profile_path = profile()->GetPath(); |
| 459 | base::FilePath path(profile_path.AppendASCII( |
| 460 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 461 | base::WriteFile(path, "background_image", 16); |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 462 | base::ThreadPoolInstance::Get()->FlushForTesting(); |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 463 | |
| 464 | instant_service_->SelectLocalBackgroundImage(path); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 465 | task_environment()->RunUntilIdle(); |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 466 | |
| 467 | EXPECT_TRUE( |
| 468 | pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 724ded4 | 2019-08-03 04:17:16 | [diff] [blame] | 469 | EXPECT_TRUE(base::PathExists(path)); |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 470 | |
| 471 | // Update theme info via Sync. |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 472 | pref_service->SetUserPref( |
| 473 | prefs::kNtpCustomBackgroundDict, |
| 474 | std::make_unique<base::Value>(GetBackgroundInfoAsDict(kUrl))); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 475 | task_environment()->RunUntilIdle(); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 476 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 477 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 478 | EXPECT_EQ(kUrl, theme->custom_background_url); |
Kyle Milka | 0d1b376e | 2019-01-28 19:34:10 | [diff] [blame] | 479 | EXPECT_FALSE( |
| 480 | pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice)); |
Kyle Milka | 724ded4 | 2019-08-03 04:17:16 | [diff] [blame] | 481 | EXPECT_FALSE(base::PathExists(path)); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 482 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 66bb07a | 2018-09-17 17:55:56 | [diff] [blame] | 483 | } |
Kyle Milka | ed7c3b3 | 2018-09-25 16:24:16 | [diff] [blame] | 484 | |
Kyle Milka | 285005f | 2019-01-09 02:38:07 | [diff] [blame] | 485 | TEST_F(InstantServiceTest, ValidateBackdropUrls) { |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 486 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 487 | const GURL kBackdropUrl1("https://www.foo.com"); |
| 488 | const GURL kBackdropUrl2("https://www.bar.com"); |
| 489 | const GURL kNonBackdropUrl1("https://www.test.com"); |
| 490 | const GURL kNonBackdropUrl2("https://www.foo.com/path"); |
| 491 | |
| 492 | instant_service_->AddValidBackdropUrlForTesting(kBackdropUrl1); |
| 493 | instant_service_->AddValidBackdropUrlForTesting(kBackdropUrl2); |
| 494 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 495 | instant_service_->SetCustomBackgroundInfo(kBackdropUrl1, "", "", GURL(), ""); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 496 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 497 | EXPECT_EQ(kBackdropUrl1, theme->custom_background_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 498 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 499 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 500 | instant_service_->SetCustomBackgroundInfo(kNonBackdropUrl1, "", "", GURL(), |
| 501 | ""); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 502 | theme = instant_service_->GetInitializedNtpTheme(); |
| 503 | EXPECT_EQ(GURL(), theme->custom_background_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 504 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 505 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 506 | instant_service_->SetCustomBackgroundInfo(kBackdropUrl2, "", "", GURL(), ""); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 507 | theme = instant_service_->GetInitializedNtpTheme(); |
| 508 | EXPECT_EQ(kBackdropUrl2, theme->custom_background_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 509 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 510 | |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 511 | instant_service_->SetCustomBackgroundInfo(kNonBackdropUrl2, "", "", GURL(), |
| 512 | ""); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 513 | theme = instant_service_->GetInitializedNtpTheme(); |
| 514 | EXPECT_EQ(GURL(), theme->custom_background_url); |
Kyle Milka | 427c8da | 2018-11-26 21:59:05 | [diff] [blame] | 515 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
Kyle Milka | 31a76c6d | 2018-10-19 17:37:27 | [diff] [blame] | 516 | } |
Kristi Park | ff3cf95 | 2019-01-28 23:00:59 | [diff] [blame] | 517 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 518 | TEST_F(InstantServiceTest, TestNoNtpTheme) { |
| 519 | instant_service_->theme_ = nullptr; |
| 520 | EXPECT_NE(nullptr, instant_service_->GetInitializedNtpTheme()); |
Gayane Petrosyan | e85debf | 2019-03-06 06:19:13 | [diff] [blame] | 521 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 522 | instant_service_->theme_ = nullptr; |
| 523 | // As |FallbackToDefaultNtpTheme| uses |theme_| it should initialize it |
Gayane Petrosyan | e85debf | 2019-03-06 06:19:13 | [diff] [blame] | 524 | // otherwise the test should crash. |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 525 | instant_service_->FallbackToDefaultNtpTheme(); |
| 526 | EXPECT_NE(nullptr, instant_service_->theme_); |
Gayane Petrosyan | e85debf | 2019-03-06 06:19:13 | [diff] [blame] | 527 | } |
| 528 | |
Gayane Petrosyan | 9929610 | 2019-03-06 19:07:49 | [diff] [blame] | 529 | TEST_F(InstantServiceTest, TestResetToDefault) { |
| 530 | MockInstantService mock_instant_service_(profile()); |
| 531 | EXPECT_CALL(mock_instant_service_, ResetCustomLinks()); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 532 | EXPECT_CALL(mock_instant_service_, ResetCustomBackgroundNtpTheme()); |
Gayane Petrosyan | 9929610 | 2019-03-06 19:07:49 | [diff] [blame] | 533 | mock_instant_service_.ResetToDefault(); |
| 534 | } |
| 535 | |
Kristi Park | ff3cf95 | 2019-01-28 23:00:59 | [diff] [blame] | 536 | class InstantServiceThemeTest : public InstantServiceTest { |
| 537 | public: |
| 538 | InstantServiceThemeTest() {} |
| 539 | ~InstantServiceThemeTest() override {} |
| 540 | |
| 541 | ui::TestNativeTheme* theme() { return &theme_; } |
| 542 | |
| 543 | private: |
| 544 | ui::TestNativeTheme theme_; |
| 545 | |
| 546 | DISALLOW_COPY_AND_ASSIGN(InstantServiceThemeTest); |
| 547 | }; |
| 548 | |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 549 | TEST_F(InstantServiceTest, LocalImageDoesNotHaveAttribution) { |
| 550 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 551 | const GURL kUrl("https://www.foo.com"); |
| 552 | const std::string kAttributionLine1 = "foo"; |
| 553 | const std::string kAttributionLine2 = "bar"; |
| 554 | const GURL kActionUrl("https://www.bar.com"); |
| 555 | |
| 556 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 557 | profile()->GetTestingPrefService(); |
| 558 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 559 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 560 | instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1, |
| 561 | kAttributionLine2, kActionUrl, ""); |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 562 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 563 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 564 | ASSERT_EQ(kAttributionLine1, theme->custom_background_attribution_line_1); |
| 565 | ASSERT_EQ(kAttributionLine2, theme->custom_background_attribution_line_2); |
| 566 | ASSERT_EQ(kActionUrl, theme->custom_background_attribution_action_url); |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 567 | ASSERT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 568 | |
| 569 | base::FilePath profile_path = profile()->GetPath(); |
| 570 | base::FilePath path(profile_path.AppendASCII( |
| 571 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 572 | base::WriteFile(path, "background_image", 16); |
Gabriel Charette | 43fd370 | 2019-05-29 16:36:51 | [diff] [blame] | 573 | base::ThreadPoolInstance::Get()->FlushForTesting(); |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 574 | |
| 575 | instant_service_->SelectLocalBackgroundImage(path); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 576 | task_environment()->RunUntilIdle(); |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 577 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 578 | theme = instant_service_->GetInitializedNtpTheme(); |
| 579 | EXPECT_TRUE(base::StartsWith(theme->custom_background_url.spec(), |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 580 | chrome::kChromeSearchLocalNtpBackgroundUrl, |
| 581 | base::CompareCase::SENSITIVE)); |
| 582 | EXPECT_TRUE( |
| 583 | pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice)); |
| 584 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 585 | EXPECT_EQ("", theme->custom_background_attribution_line_1); |
| 586 | EXPECT_EQ("", theme->custom_background_attribution_line_2); |
| 587 | EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url); |
Kyle Milka | cb9cc66 | 2019-03-19 22:39:19 | [diff] [blame] | 588 | } |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 589 | |
| 590 | TEST_F(InstantServiceTest, TestUpdateCustomBackgroundColor) { |
| 591 | SkBitmap bitmap; |
| 592 | bitmap.allocN32Pixels(32, 32); |
| 593 | bitmap.eraseColor(SK_ColorRED); |
| 594 | gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 595 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 596 | profile()->GetTestingPrefService(); |
| 597 | |
| 598 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 599 | |
| 600 | // Background color will not update if no background is set. |
| 601 | instant_service_->UpdateCustomBackgroundColorAsync( |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 602 | base::TimeTicks::Now(), image, image_fetcher::RequestMetadata()); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 603 | task_environment()->RunUntilIdle(); |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 604 | EXPECT_FALSE(CheckBackgroundColor( |
| 605 | SK_ColorRED, |
| 606 | pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict))); |
| 607 | |
| 608 | const GURL kUrl("https://www.foo.com"); |
| 609 | const std::string kAttributionLine1 = "foo"; |
| 610 | const std::string kAttributionLine2 = "bar"; |
| 611 | const GURL kActionUrl("https://www.bar.com"); |
| 612 | |
| 613 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 614 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 615 | instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1, |
| 616 | kAttributionLine2, kActionUrl, ""); |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 617 | |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 618 | // Background color will not update if background timestamp has changed. |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 619 | instant_service_->UpdateCustomBackgroundColorAsync( |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 620 | base::TimeTicks::Now(), image, image_fetcher::RequestMetadata()); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 621 | task_environment()->RunUntilIdle(); |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 622 | EXPECT_FALSE(CheckBackgroundColor( |
| 623 | SK_ColorRED, |
| 624 | pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict))); |
| 625 | |
| 626 | // Background color should update. |
| 627 | instant_service_->UpdateCustomBackgroundColorAsync( |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 628 | instant_service_->GetBackgroundUpdatedTimestampForTesting(), image, |
| 629 | image_fetcher::RequestMetadata()); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 630 | task_environment()->RunUntilIdle(); |
Gayane Petrosyan | d257e720 | 2019-05-21 03:43:38 | [diff] [blame] | 631 | EXPECT_TRUE(CheckBackgroundColor( |
| 632 | SK_ColorRED, |
| 633 | pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict))); |
| 634 | } |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 635 | |
| 636 | TEST_F(InstantServiceTest, LocalImageDoesNotUpdateCustomBackgroundColor) { |
| 637 | SkBitmap bitmap; |
| 638 | bitmap.allocN32Pixels(32, 32); |
| 639 | bitmap.eraseColor(SK_ColorRED); |
| 640 | gfx::Image image = gfx::Image::CreateFrom1xBitmap(bitmap); |
| 641 | sync_preferences::TestingPrefServiceSyncable* pref_service = |
| 642 | profile()->GetTestingPrefService(); |
| 643 | |
| 644 | base::FilePath profile_path = profile()->GetPath(); |
| 645 | base::FilePath path(profile_path.AppendASCII("test_file")); |
| 646 | base::FilePath copy_path(profile_path.AppendASCII( |
| 647 | chrome::kChromeSearchLocalNtpBackgroundFilename)); |
| 648 | base::WriteFile(path, "background_image", 16); |
| 649 | |
| 650 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 651 | |
| 652 | const GURL kUrl("https://www.foo.com"); |
| 653 | const std::string kAttributionLine1 = "foo"; |
| 654 | const std::string kAttributionLine2 = "bar"; |
| 655 | const GURL kActionUrl("https://www.bar.com"); |
| 656 | |
| 657 | SetUserSelectedDefaultSearchProvider("{google:baseURL}"); |
| 658 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
Kyle Milka | 7d43ddcf | 2019-08-08 16:59:16 | [diff] [blame] | 659 | instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1, |
| 660 | kAttributionLine2, kActionUrl, ""); |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 661 | base::TimeTicks time_set = |
| 662 | instant_service_->GetBackgroundUpdatedTimestampForTesting(); |
| 663 | |
| 664 | instant_service_->SelectLocalBackgroundImage(path); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 665 | task_environment()->RunUntilIdle(); |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 666 | |
| 667 | // Background color will not update if a local image was uploaded in the |
| 668 | // meantime. |
| 669 | instant_service_->UpdateCustomBackgroundColorAsync( |
| 670 | time_set, image, image_fetcher::RequestMetadata()); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 671 | task_environment()->RunUntilIdle(); |
Kyle Milka | 8bfaad0 | 2019-07-12 18:31:59 | [diff] [blame] | 672 | EXPECT_FALSE(CheckBackgroundColor( |
| 673 | SK_ColorRED, |
| 674 | pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict))); |
| 675 | } |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 676 | |
| 677 | TEST_F(InstantServiceTest, SetCustomBackgroundCollectionId) { |
| 678 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 679 | const std::string kInvalidId("aarrtt"); |
| 680 | const std::string kValidId("art"); |
| 681 | |
| 682 | // A valid id should update the pref/background. |
| 683 | CollectionImage image; |
| 684 | image.collection_id = kValidId; |
| 685 | image.image_url = GURL("https://www.test.com/"); |
| 686 | instant_service_->SetNextCollectionImageForTesting(image); |
| 687 | |
| 688 | instant_service_->AddValidBackdropCollectionForTesting(kValidId); |
| 689 | instant_service_->SetCustomBackgroundInfo(GURL(), "", "", GURL(), kValidId); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 690 | task_environment()->RunUntilIdle(); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 691 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 692 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 693 | EXPECT_EQ(kValidId, theme->collection_id); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 694 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 695 | |
| 696 | // An invalid id should clear the pref/background. |
| 697 | CollectionImage image2; |
| 698 | instant_service_->SetNextCollectionImageForTesting(image2); |
| 699 | instant_service_->SetCustomBackgroundInfo(GURL(), "", "", GURL(), kInvalidId); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 700 | task_environment()->RunUntilIdle(); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 701 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 702 | theme = instant_service_->GetInitializedNtpTheme(); |
| 703 | EXPECT_EQ(std::string(), theme->collection_id); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 704 | EXPECT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 705 | } |
| 706 | |
| 707 | TEST_F(InstantServiceTest, CollectionIdTakePriorityOverBackgroundURL) { |
| 708 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 709 | const std::string kValidId("art"); |
| 710 | const GURL kUrl("https://www.foo.com/"); |
| 711 | |
| 712 | CollectionImage image; |
| 713 | image.collection_id = kValidId; |
| 714 | image.image_url = GURL("https://www.test.com/"); |
| 715 | instant_service_->SetNextCollectionImageForTesting(image); |
| 716 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
| 717 | instant_service_->AddValidBackdropCollectionForTesting(kValidId); |
| 718 | |
| 719 | instant_service_->SetCustomBackgroundInfo(kUrl, "", "", GURL(), kValidId); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 720 | task_environment()->RunUntilIdle(); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 721 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 722 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 723 | EXPECT_EQ(kValidId, theme->collection_id); |
| 724 | EXPECT_EQ("https://www.test.com/", theme->custom_background_url); |
Kyle Milka | ae360e2d | 2019-08-01 01:34:08 | [diff] [blame] | 725 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 726 | } |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 727 | |
| 728 | TEST_F(InstantServiceTest, RefreshesBackgroundAfter24Hours) { |
| 729 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 730 | const std::string kValidId("art"); |
| 731 | const GURL kImageUrl1("https://www.test.com/1/"); |
| 732 | const GURL kImageUrl2("https://www.test.com/2/"); |
| 733 | |
| 734 | instant_service_->SetClockForTesting(clock_); |
| 735 | clock_->SetNow(GetReferenceTime()); |
| 736 | |
| 737 | // A valid id should update the pref/background. |
| 738 | CollectionImage image; |
| 739 | image.collection_id = kValidId; |
| 740 | image.image_url = kImageUrl1; |
| 741 | instant_service_->SetNextCollectionImageForTesting(image); |
| 742 | |
| 743 | instant_service_->AddValidBackdropCollectionForTesting(kValidId); |
| 744 | instant_service_->SetCustomBackgroundInfo(GURL(), "", "", GURL(), kValidId); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 745 | task_environment()->RunUntilIdle(); |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 746 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 747 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
| 748 | EXPECT_EQ(kValidId, theme->collection_id); |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 749 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 750 | |
| 751 | CollectionImage image2; |
| 752 | image2.collection_id = kValidId; |
| 753 | image2.image_url = kImageUrl2; |
| 754 | instant_service_->SetNextCollectionImageForTesting(image2); |
| 755 | |
| 756 | // Should not refresh background. |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 757 | theme = instant_service_->GetInitializedNtpTheme(); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 758 | task_environment()->RunUntilIdle(); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 759 | EXPECT_EQ(kValidId, theme->collection_id); |
| 760 | EXPECT_EQ(kImageUrl1, theme->custom_background_url); |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 761 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 762 | |
| 763 | clock_->Advance(base::TimeDelta::FromHours(25)); |
| 764 | |
| 765 | // Should refresh background after >24 hours. |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 766 | theme = instant_service_->GetInitializedNtpTheme(); |
Gabriel Charette | 798fde7 | 2019-08-20 22:24:04 | [diff] [blame] | 767 | task_environment()->RunUntilIdle(); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 768 | EXPECT_EQ(kValidId, theme->collection_id); |
| 769 | EXPECT_EQ(kImageUrl2, theme->custom_background_url); |
Kyle Milka | 989bb36 | 2019-08-01 17:45:03 | [diff] [blame] | 770 | EXPECT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 771 | } |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 772 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 773 | TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) { |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 774 | const auto& theme_provider = |
| 775 | ThemeService::GetThemeProviderForProfile(profile()); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 776 | SkColor default_text_color = |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 777 | theme_provider.GetColor(ThemeProperties::COLOR_NTP_TEXT); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 778 | SkColor default_logo_color = |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 779 | theme_provider.GetColor(ThemeProperties::COLOR_NTP_LOGO); |
| 780 | SkColor default_shortcut_color = |
| 781 | theme_provider.GetColor(ThemeProperties::COLOR_NTP_SHORTCUT); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 782 | |
| 783 | ASSERT_FALSE(instant_service_->IsCustomBackgroundSet()); |
| 784 | |
| 785 | // Check defaults when no theme and no custom backgrounds is set. |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 786 | NtpTheme* theme = instant_service_->GetInitializedNtpTheme(); |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 787 | EXPECT_EQ(default_text_color, theme->text_color); |
| 788 | EXPECT_FALSE(theme->logo_alternate); |
| 789 | EXPECT_EQ(default_logo_color, theme->logo_color); |
| 790 | EXPECT_EQ(default_shortcut_color, theme->shortcut_color); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 791 | |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 792 | // Install colors, theme update should trigger SetNTPElementsNtpTheme() and |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 793 | // update NTP themed elements info. |
| 794 | ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile()); |
| 795 | content::WindowedNotificationObserver theme_change_observer( |
| 796 | chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 797 | content::Source<ThemeService>(theme_service)); |
Peter Kasting | 273d23f | 2019-11-27 19:58:04 | [diff] [blame] | 798 | theme_service->BuildAutogeneratedThemeFromColor(SK_ColorRED); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 799 | theme_change_observer.Wait(); |
| 800 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 801 | theme = instant_service_->GetInitializedNtpTheme(); |
| 802 | EXPECT_NE(default_text_color, theme->text_color); |
| 803 | EXPECT_TRUE(theme->logo_alternate); |
| 804 | EXPECT_NE(default_logo_color, theme->logo_color); |
| 805 | EXPECT_NE(default_shortcut_color, theme->shortcut_color); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 806 | |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 807 | // Setting a custom background should call SetNTPElementsNtpTheme() and |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 808 | // update NTP themed elements info. |
| 809 | const GURL kUrl("https://www.foo.com"); |
| 810 | instant_service_->AddValidBackdropUrlForTesting(kUrl); |
| 811 | instant_service_->SetCustomBackgroundInfo(kUrl, "", "", GURL(), ""); |
| 812 | ASSERT_TRUE(instant_service_->IsCustomBackgroundSet()); |
| 813 | |
Dan Beam | 19c8178 | 2019-11-12 00:09:03 | [diff] [blame] | 814 | theme = instant_service_->GetInitializedNtpTheme(); |
| 815 | EXPECT_NE(default_text_color, theme->text_color); |
| 816 | EXPECT_TRUE(theme->logo_alternate); |
| 817 | EXPECT_EQ(default_logo_color, theme->logo_color); |
Peter Kasting | 90dcda9 | 2020-02-25 15:28:48 | [diff] [blame] | 818 | // The shortcut color with a background set should always use the light mode |
| 819 | // default regardless of system setting. |
| 820 | EXPECT_EQ(ThemeProperties::GetDefaultColor( |
| 821 | ThemeProperties::COLOR_NTP_SHORTCUT, false), |
| 822 | theme->shortcut_color); |
Gayane Petrosyan | b93e0e0 | 2019-09-09 20:00:10 | [diff] [blame] | 823 | } |