blob: 10a5c798763681c9cded4390d21ef94d9f0b825a [file] [log] [blame]
[email protected]c8a118e2013-09-24 21:33:401// 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
dcheng4af48582016-04-19 00:29:355#include "chrome/browser/search/instant_service.h"
6
[email protected]c8a118e2013-09-24 21:33:407#include <vector>
8
Kyle Milka6986ede2018-07-19 02:16:249#include "base/files/file_util.h"
10#include "base/path_service.h"
Hans Wennborg5ffd1392019-10-16 11:00:0211#include "base/task/thread_pool/thread_pool_instance.h"
Kristi Park999db912018-12-05 23:46:4212#include "base/test/mock_callback.h"
Gayane Petrosyanb93e0e02019-09-09 20:00:1013#include "chrome/browser/chrome_notification_types.h"
Kyle Milka31a76c6d2018-10-19 17:37:2714#include "chrome/browser/search/background/ntp_background_service.h"
Kristi Park30e182e2019-02-09 02:15:3815#include "chrome/browser/search/instant_service_observer.h"
[email protected]c8a118e2013-09-24 21:33:4016#include "chrome/browser/search/instant_unittest_base.h"
Kyle Milka848864f6e2018-08-02 21:59:3117#include "chrome/browser/search/ntp_features.h"
Gayane Petrosyanb93e0e02019-09-09 20:00:1018#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 Milka6986ede2018-07-19 02:16:2421#include "chrome/common/chrome_paths.h"
Kyle Milka66bb07a2018-09-17 17:55:5622#include "chrome/common/pref_names.h"
Marc Treibbe5f3feb2017-09-04 09:53:1623#include "chrome/common/search/instant_types.h"
Kyle Milka6986ede2018-07-19 02:16:2424#include "chrome/common/url_constants.h"
Kristi Park01ca1252019-05-10 22:39:0525#include "components/ntp_tiles/features.h"
Marc Treib7b3985e2017-09-18 11:22:1626#include "components/ntp_tiles/ntp_tile.h"
27#include "components/ntp_tiles/section_type.h"
Kyle Milka66bb07a2018-09-17 17:55:5628#include "components/sync_preferences/testing_pref_service_syncable.h"
Gayane Petrosyanb93e0e02019-09-09 20:00:1029#include "content/public/test/test_utils.h"
Kristi Park30e182e2019-02-09 02:15:3830#include "testing/gmock/include/gmock/gmock.h"
Marc Treib7b3985e2017-09-18 11:22:1631#include "testing/gtest/include/gtest/gtest.h"
Kristi Parkff3cf952019-01-28 23:00:5932#include "ui/native_theme/test_native_theme.h"
[email protected]2309e912013-10-01 01:33:3033#include "url/gurl.h"
[email protected]c8a118e2013-09-24 21:33:4034
Kyle Milka285005f2019-01-09 02:38:0735namespace {
Kristi Park30e182e2019-02-09 02:15:3836
37class MockInstantServiceObserver : public InstantServiceObserver {
38 public:
Dan Beam19c81782019-11-12 00:09:0339 MOCK_METHOD1(NtpThemeChanged, void(const NtpTheme&));
Kristi Park6424e602019-06-11 18:01:4340 MOCK_METHOD1(MostVisitedInfoChanged, void(const InstantMostVisitedInfo&));
Kristi Park30e182e2019-02-09 02:15:3841};
42
Kyle Milka285005f2019-01-09 02:38:0743base::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 Milkaae360e2d2019-08-01 01:34:0849 background_info.SetKey("collection_id", base::Value(std::string()));
50 background_info.SetKey("resume_token", base::Value(std::string()));
Kyle Milka989bb362019-08-01 17:45:0351 background_info.SetKey("refresh_timestamp", base::Value(0));
Kyle Milka285005f2019-01-09 02:38:0752 return background_info;
53}
Kristi Park30e182e2019-02-09 02:15:3854
Kyle Milka989bb362019-08-01 17:45:0355base::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 Petrosyan99296102019-03-06 19:07:4972class MockInstantService : public InstantService {
73 public:
74 explicit MockInstantService(Profile* profile) : InstantService(profile) {}
75 ~MockInstantService() override = default;
76
77 MOCK_METHOD0(ResetCustomLinks, bool());
Dan Beam19c81782019-11-12 00:09:0378 MOCK_METHOD0(ResetCustomBackgroundNtpTheme, void());
Gayane Petrosyan99296102019-03-06 19:07:4979};
80
Gayane Petrosyand257e7202019-05-21 03:43:3881bool 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 Milka285005f2019-01-09 02:38:0793} // namespace
94
Marc Treibc4de3f12017-09-05 12:36:1595using InstantServiceTest = InstantUnitTestBase;
[email protected]c4b2af22014-05-11 19:48:5396
Marc Treib7b3985e2017-09-18 11:22:1697TEST_F(InstantServiceTest, GetNTPTileSuggestion) {
Friedrich Horschigefec0812017-09-28 18:50:1798 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 Treib7b3985e2017-09-18 11:22:16103 std::map<ntp_tiles::SectionType, ntp_tiles::NTPTilesVector> suggestions_map;
104 suggestions_map[ntp_tiles::SectionType::PERSONALIZED] = suggestions;
zminfddc3e32015-12-09 22:59:08105
Marc Treib7b3985e2017-09-18 11:22:16106 instant_service_->OnURLsAvailable(suggestions_map);
zminfddc3e32015-12-09 22:59:08107
Kristi Parke68931522019-06-08 00:17:18108 auto items = instant_service_->most_visited_info_->items;
zminfddc3e32015-12-09 22:59:08109 ASSERT_EQ(1, (int)items.size());
Friedrich Horschigefec0812017-09-28 18:50:17110 EXPECT_EQ(ntp_tiles::TileSource::TOP_SITES, items[0].source);
111 EXPECT_EQ(ntp_tiles::TileTitleSource::TITLE_TAG, items[0].title_source);
zminfddc3e32015-12-09 22:59:08112}
Kyle Milka6986ede2018-07-19 02:16:24113
Kristi Parkb8aec552019-04-18 21:08:56114TEST_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 Parke68931522019-06-08 00:17:18119 ASSERT_FALSE(instant_service_->most_visited_info_->use_most_visited);
Kristi Parkb8aec552019-04-18 21:08:56120
121 // Enable most visited tiles.
122 EXPECT_TRUE(instant_service_->ToggleMostVisitedOrCustomLinks());
123 EXPECT_TRUE(pref_service->GetBoolean(prefs::kNtpUseMostVisitedTiles));
Kristi Parke68931522019-06-08 00:17:18124 EXPECT_TRUE(instant_service_->most_visited_info_->use_most_visited);
Kristi Parkb8aec552019-04-18 21:08:56125
126 // Disable most visited tiles.
127 EXPECT_TRUE(instant_service_->ToggleMostVisitedOrCustomLinks());
128 EXPECT_FALSE(pref_service->GetBoolean(prefs::kNtpUseMostVisitedTiles));
Kristi Parke68931522019-06-08 00:17:18129 EXPECT_FALSE(instant_service_->most_visited_info_->use_most_visited);
Kristi Parkb8aec552019-04-18 21:08:56130
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 Parke68931522019-06-08 00:17:18135 EXPECT_FALSE(instant_service_->most_visited_info_->use_most_visited);
Kristi Parkb8aec552019-04-18 21:08:56136}
137
Kristi Park30aa6342019-06-11 22:43:27138TEST_F(InstantServiceTest, DoesToggleShortcutsVisibility) {
Kristi Park955ad552019-07-02 21:50:33139 testing::StrictMock<MockInstantServiceObserver> mock_observer;
140 instant_service_->AddObserver(&mock_observer);
141
Kristi Park30aa6342019-06-11 22:43:27142 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 Park955ad552019-07-02 21:50:33149 EXPECT_CALL(mock_observer, MostVisitedInfoChanged(testing::_)).Times(0);
150 EXPECT_TRUE(instant_service_->ToggleShortcutsVisibility(false));
Kristi Park30aa6342019-06-11 22:43:27151 EXPECT_FALSE(pref_service->GetBoolean(prefs::kNtpShortcutsVisible));
152 EXPECT_FALSE(instant_service_->most_visited_info_->is_visible);
Gabriel Charette798fde72019-08-20 22:24:04153 task_environment()->RunUntilIdle();
Kristi Park30aa6342019-06-11 22:43:27154
Kristi Park955ad552019-07-02 21:50:33155 // 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 Park30aa6342019-06-11 22:43:27158 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 Park955ad552019-07-02 21:50:33162 EXPECT_CALL(mock_observer, MostVisitedInfoChanged(testing::_)).Times(0);
Kristi Park30aa6342019-06-11 22:43:27163 SetUserSelectedDefaultSearchProvider("https://www.search.com");
Kristi Park955ad552019-07-02 21:50:33164 EXPECT_FALSE(instant_service_->ToggleShortcutsVisibility(false));
Kristi Park30aa6342019-06-11 22:43:27165 EXPECT_TRUE(pref_service->GetBoolean(prefs::kNtpShortcutsVisible));
166 EXPECT_TRUE(instant_service_->most_visited_info_->is_visible);
167}
168
Kristi Park41653082019-01-09 22:20:30169TEST_F(InstantServiceTest,
Ramya Nagarajan7673f052018-08-13 22:53:29170 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 Park41653082019-01-09 22:20:30178TEST_F(InstantServiceTest, DisableResetCustomLinksForNonGoogleSearchProvider) {
Ramya Nagarajan7673f052018-08-13 22:53:29179 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 Parkb8aec552019-04-18 21:08:56186TEST_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 Milka285005f2019-01-09 02:38:07205TEST_F(InstantServiceTest, SetCustomBackgroundURL) {
Kyle Milka427c8da2018-11-26 21:59:05206 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24207 const GURL kUrl("https://www.foo.com");
Kyle Milka31a76c6d2018-10-19 17:37:27208
209 instant_service_->AddValidBackdropUrlForTesting(kUrl);
Kyle Milka7d43ddcf2019-08-08 16:59:16210 instant_service_->SetCustomBackgroundInfo(kUrl, "", "", GURL(), "");
Kyle Milka6986ede2018-07-19 02:16:24211
Dan Beam19c81782019-11-12 00:09:03212 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
213 EXPECT_EQ(kUrl, theme->custom_background_url);
Kyle Milka427c8da2018-11-26 21:59:05214 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24215}
216
Kyle Milka285005f2019-01-09 02:38:07217TEST_F(InstantServiceTest, SetCustomBackgroundURLInvalidURL) {
Kyle Milka427c8da2018-11-26 21:59:05218 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56219 const GURL kInvalidUrl("foo");
220 const GURL kValidUrl("https://www.foo.com");
Kyle Milka31a76c6d2018-10-19 17:37:27221 instant_service_->AddValidBackdropUrlForTesting(kValidUrl);
Kyle Milka7d43ddcf2019-08-08 16:59:16222 instant_service_->SetCustomBackgroundInfo(kValidUrl, "", "", GURL(), "");
Kyle Milka6986ede2018-07-19 02:16:24223
Dan Beam19c81782019-11-12 00:09:03224 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
225 EXPECT_EQ(kValidUrl.spec(), theme->custom_background_url.spec());
Kyle Milka66bb07a2018-09-17 17:55:56226
Kyle Milka7d43ddcf2019-08-08 16:59:16227 instant_service_->SetCustomBackgroundInfo(kInvalidUrl, "", "", GURL(), "");
Kyle Milka66bb07a2018-09-17 17:55:56228
Dan Beam19c81782019-11-12 00:09:03229 theme = instant_service_->GetInitializedNtpTheme();
230 EXPECT_EQ("", theme->custom_background_url.spec());
Kyle Milka427c8da2018-11-26 21:59:05231 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24232}
233
Kyle Milkaafef7442019-07-31 23:55:52234TEST_F(InstantServiceTest, SetCustomBackgroundInfo) {
Kyle Milka427c8da2018-11-26 21:59:05235 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24236 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 Milka31a76c6d2018-10-19 17:37:27240 instant_service_->AddValidBackdropUrlForTesting(kUrl);
Kyle Milka7d43ddcf2019-08-08 16:59:16241 instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1,
242 kAttributionLine2, kActionUrl, "");
Kyle Milka6986ede2018-07-19 02:16:24243
Dan Beam19c81782019-11-12 00:09:03244 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 Milka427c8da2018-11-26 21:59:05249 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24250}
251
Dan Beam19c81782019-11-12 00:09:03252TEST_F(InstantServiceTest, ChangingSearchProviderClearsNtpThemeAndPref) {
Kyle Milka427c8da2018-11-26 21:59:05253 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24254 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 Milka31a76c6d2018-10-19 17:37:27260 instant_service_->AddValidBackdropUrlForTesting(kUrl);
Kyle Milka7d43ddcf2019-08-08 16:59:16261 instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1,
262 kAttributionLine2, kActionUrl, "");
Kyle Milka6986ede2018-07-19 02:16:24263
Dan Beam19c81782019-11-12 00:09:03264 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 Milka427c8da2018-11-26 21:59:05269 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24270
271 SetUserSelectedDefaultSearchProvider("https://www.search.com");
Dan Beam19c81782019-11-12 00:09:03272 instant_service_->UpdateNtpTheme();
Kyle Milka6986ede2018-07-19 02:16:24273
Dan Beam19c81782019-11-12 00:09:03274 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 Milka427c8da2018-11-26 21:59:05279 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24280
281 SetUserSelectedDefaultSearchProvider("{google:baseURL}");
Dan Beam19c81782019-11-12 00:09:03282 instant_service_->UpdateNtpTheme();
Kyle Milka6986ede2018-07-19 02:16:24283
Dan Beam19c81782019-11-12 00:09:03284 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 Milka427c8da2018-11-26 21:59:05289 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24290}
291
Kyle Milka285005f2019-01-09 02:38:07292TEST_F(InstantServiceTest, LocalBackgroundImageCopyCreated) {
Kyle Milka427c8da2018-11-26 21:59:05293 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56294 const GURL kUrl("chrome-search://local-ntp/background.jpg");
295
Kyle Milkaed7c3b32018-09-25 16:24:16296 base::FilePath profile_path = profile()->GetPath();
297 base::FilePath path(profile_path.AppendASCII("test_file"));
298 base::FilePath copy_path(profile_path.AppendASCII(
Kyle Milka66bb07a2018-09-17 17:55:56299 chrome::kChromeSearchLocalNtpBackgroundFilename));
300
301 base::WriteFile(path, "background_image", 16);
302
303 instant_service_->SelectLocalBackgroundImage(path);
304
Gabriel Charette798fde72019-08-20 22:24:04305 task_environment()->RunUntilIdle();
Kyle Milka66bb07a2018-09-17 17:55:56306
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 Milka427c8da2018-11-26 21:59:05312 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56313}
314
Kyle Milka285005f2019-01-09 02:38:07315TEST_F(InstantServiceTest,
Kyle Milka6986ede2018-07-19 02:16:24316 ChangingSearchProviderRemovesLocalBackgroundImageCopy) {
Kyle Milka427c8da2018-11-26 21:59:05317 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milkaed7c3b32018-09-25 16:24:16318 base::FilePath profile_path = profile()->GetPath();
319 base::FilePath path(profile_path.AppendASCII(
Kyle Milka6986ede2018-07-19 02:16:24320 chrome::kChromeSearchLocalNtpBackgroundFilename));
321
322 base::WriteFile(path, "background_image", 16);
323
324 SetUserSelectedDefaultSearchProvider("https://www.search.com");
Dan Beam19c81782019-11-12 00:09:03325 instant_service_->UpdateNtpTheme();
Kyle Milka6986ede2018-07-19 02:16:24326
Gabriel Charette798fde72019-08-20 22:24:04327 task_environment()->RunUntilIdle();
Kyle Milka6986ede2018-07-19 02:16:24328
329 bool file_exists = base::PathExists(path);
330
331 EXPECT_EQ(false, file_exists);
Kyle Milka66bb07a2018-09-17 17:55:56332 EXPECT_EQ(false, profile()->GetTestingPrefService()->GetBoolean(
333 prefs::kNtpCustomBackgroundLocalToDevice));
Kyle Milka427c8da2018-11-26 21:59:05334 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56335}
336
Kyle Milka285005f2019-01-09 02:38:07337TEST_F(InstantServiceTest, SettingUrlRemovesLocalBackgroundImageCopy) {
Kyle Milka427c8da2018-11-26 21:59:05338 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56339 const GURL kUrl("https://www.foo.com");
340
Kyle Milkaed7c3b32018-09-25 16:24:16341 base::FilePath profile_path = profile()->GetPath();
342 base::FilePath path(profile_path.AppendASCII(
Kyle Milka66bb07a2018-09-17 17:55:56343 chrome::kChromeSearchLocalNtpBackgroundFilename));
344
345 base::WriteFile(path, "background_image", 16);
346
Kyle Milka427c8da2018-11-26 21:59:05347 instant_service_->AddValidBackdropUrlForTesting(kUrl);
Kyle Milka7d43ddcf2019-08-08 16:59:16348 instant_service_->SetCustomBackgroundInfo(kUrl, "", "", GURL(), "");
Dan Beam19c81782019-11-12 00:09:03349 instant_service_->UpdateNtpTheme();
Kyle Milka66bb07a2018-09-17 17:55:56350
Gabriel Charette798fde72019-08-20 22:24:04351 task_environment()->RunUntilIdle();
Kyle Milka66bb07a2018-09-17 17:55:56352
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 Milka427c8da2018-11-26 21:59:05358 ASSERT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka6986ede2018-07-19 02:16:24359}
Kyle Milkad68475e2018-07-30 00:05:04360
Kyle Milka285005f2019-01-09 02:38:07361TEST_F(InstantServiceTest, CustomBackgroundAttributionActionUrlReset) {
Kyle Milka427c8da2018-11-26 21:59:05362 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milkad68475e2018-07-30 00:05:04363 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 Milka31a76c6d2018-10-19 17:37:27370 instant_service_->AddValidBackdropUrlForTesting(kUrl);
Kyle Milka7d43ddcf2019-08-08 16:59:16371 instant_service_->SetCustomBackgroundInfo(
372 kUrl, kAttributionLine1, kAttributionLine2, kHttpsActionUrl, "");
Kyle Milkad68475e2018-07-30 00:05:04373
Dan Beam19c81782019-11-12 00:09:03374 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
375 EXPECT_EQ(kHttpsActionUrl, theme->custom_background_attribution_action_url);
Kyle Milka427c8da2018-11-26 21:59:05376 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milkad68475e2018-07-30 00:05:04377
Kyle Milka7d43ddcf2019-08-08 16:59:16378 instant_service_->SetCustomBackgroundInfo(
379 kUrl, kAttributionLine1, kAttributionLine2, kHttpActionUrl, "");
Kyle Milkad68475e2018-07-30 00:05:04380
Dan Beam19c81782019-11-12 00:09:03381 theme = instant_service_->GetInitializedNtpTheme();
382 EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url);
Kyle Milka427c8da2018-11-26 21:59:05383 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milkad68475e2018-07-30 00:05:04384
Kyle Milka7d43ddcf2019-08-08 16:59:16385 instant_service_->SetCustomBackgroundInfo(
386 kUrl, kAttributionLine1, kAttributionLine2, kHttpsActionUrl, "");
Kyle Milkad68475e2018-07-30 00:05:04387
Dan Beam19c81782019-11-12 00:09:03388 theme = instant_service_->GetInitializedNtpTheme();
389 EXPECT_EQ(kHttpsActionUrl, theme->custom_background_attribution_action_url);
Kyle Milka427c8da2018-11-26 21:59:05390 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milkad68475e2018-07-30 00:05:04391
Kyle Milka7d43ddcf2019-08-08 16:59:16392 instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1,
393 kAttributionLine2, GURL(), "");
Kyle Milkad68475e2018-07-30 00:05:04394
Dan Beam19c81782019-11-12 00:09:03395 theme = instant_service_->GetInitializedNtpTheme();
396 EXPECT_EQ(GURL(), theme->custom_background_attribution_action_url);
Kyle Milka427c8da2018-11-26 21:59:05397 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milkad68475e2018-07-30 00:05:04398}
Kyle Milka66bb07a2018-09-17 17:55:56399
Dan Beam19c81782019-11-12 00:09:03400TEST_F(InstantServiceTest, UpdatingPrefUpdatesNtpTheme) {
Kyle Milka427c8da2018-11-26 21:59:05401 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56402 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 Beam19c81782019-11-12 00:09:03411 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
412 EXPECT_EQ(kUrlFoo, theme->custom_background_url);
Kyle Milka427c8da2018-11-26 21:59:05413 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56414
415 pref_service->SetUserPref(
416 prefs::kNtpCustomBackgroundDict,
417 std::make_unique<base::Value>(GetBackgroundInfoAsDict(kUrlBar)));
418
Dan Beam19c81782019-11-12 00:09:03419 theme = instant_service_->GetInitializedNtpTheme();
420 EXPECT_EQ(kUrlBar, theme->custom_background_url);
Kyle Milka66bb07a2018-09-17 17:55:56421 EXPECT_EQ(false,
422 pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice));
Kyle Milka427c8da2018-11-26 21:59:05423 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56424}
425
Kyle Milka0d1b376e2019-01-28 19:34:10426TEST_F(InstantServiceTest, SetLocalImage) {
Kyle Milka427c8da2018-11-26 21:59:05427 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56428 const GURL kUrl("chrome-search://local-ntp/background.jpg?123456789");
429
430 sync_preferences::TestingPrefServiceSyncable* pref_service =
431 profile()->GetTestingPrefService();
432
Kyle Milkaed7c3b32018-09-25 16:24:16433 base::FilePath profile_path = profile()->GetPath();
434 base::FilePath path(profile_path.AppendASCII(
Kyle Milka66bb07a2018-09-17 17:55:56435 chrome::kChromeSearchLocalNtpBackgroundFilename));
436 base::WriteFile(path, "background_image", 16);
Gabriel Charette43fd3702019-05-29 16:36:51437 base::ThreadPoolInstance::Get()->FlushForTesting();
Kyle Milka66bb07a2018-09-17 17:55:56438
Kyle Milka0d1b376e2019-01-28 19:34:10439 instant_service_->SelectLocalBackgroundImage(path);
Gabriel Charette798fde72019-08-20 22:24:04440 task_environment()->RunUntilIdle();
Kyle Milka66bb07a2018-09-17 17:55:56441
Dan Beam19c81782019-11-12 00:09:03442 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
443 EXPECT_TRUE(base::StartsWith(theme->custom_background_url.spec(),
Kyle Milka0d1b376e2019-01-28 19:34:10444 chrome::kChromeSearchLocalNtpBackgroundUrl,
445 base::CompareCase::SENSITIVE));
446 EXPECT_TRUE(
447 pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice));
Kyle Milka427c8da2018-11-26 21:59:05448 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56449}
450
Kyle Milka724ded42019-08-03 04:17:16451TEST_F(InstantServiceTest, SyncPrefOverridesAndRemovesLocalImage) {
Kyle Milka427c8da2018-11-26 21:59:05452 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka0d1b376e2019-01-28 19:34:10453 const GURL kUrl("https://www.foo.com/");
Kyle Milka66bb07a2018-09-17 17:55:56454
455 sync_preferences::TestingPrefServiceSyncable* pref_service =
456 profile()->GetTestingPrefService();
457
Kyle Milka0d1b376e2019-01-28 19:34:10458 base::FilePath profile_path = profile()->GetPath();
459 base::FilePath path(profile_path.AppendASCII(
460 chrome::kChromeSearchLocalNtpBackgroundFilename));
461 base::WriteFile(path, "background_image", 16);
Gabriel Charette43fd3702019-05-29 16:36:51462 base::ThreadPoolInstance::Get()->FlushForTesting();
Kyle Milka0d1b376e2019-01-28 19:34:10463
464 instant_service_->SelectLocalBackgroundImage(path);
Gabriel Charette798fde72019-08-20 22:24:04465 task_environment()->RunUntilIdle();
Kyle Milka0d1b376e2019-01-28 19:34:10466
467 EXPECT_TRUE(
468 pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice));
Kyle Milka724ded42019-08-03 04:17:16469 EXPECT_TRUE(base::PathExists(path));
Kyle Milka0d1b376e2019-01-28 19:34:10470
471 // Update theme info via Sync.
Kyle Milka66bb07a2018-09-17 17:55:56472 pref_service->SetUserPref(
473 prefs::kNtpCustomBackgroundDict,
474 std::make_unique<base::Value>(GetBackgroundInfoAsDict(kUrl)));
Gabriel Charette798fde72019-08-20 22:24:04475 task_environment()->RunUntilIdle();
Kyle Milka66bb07a2018-09-17 17:55:56476
Dan Beam19c81782019-11-12 00:09:03477 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
478 EXPECT_EQ(kUrl, theme->custom_background_url);
Kyle Milka0d1b376e2019-01-28 19:34:10479 EXPECT_FALSE(
480 pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice));
Kyle Milka724ded42019-08-03 04:17:16481 EXPECT_FALSE(base::PathExists(path));
Kyle Milka427c8da2018-11-26 21:59:05482 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka66bb07a2018-09-17 17:55:56483}
Kyle Milkaed7c3b32018-09-25 16:24:16484
Kyle Milka285005f2019-01-09 02:38:07485TEST_F(InstantServiceTest, ValidateBackdropUrls) {
Kyle Milka427c8da2018-11-26 21:59:05486 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka31a76c6d2018-10-19 17:37:27487 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 Milka7d43ddcf2019-08-08 16:59:16495 instant_service_->SetCustomBackgroundInfo(kBackdropUrl1, "", "", GURL(), "");
Dan Beam19c81782019-11-12 00:09:03496 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
497 EXPECT_EQ(kBackdropUrl1, theme->custom_background_url);
Kyle Milka427c8da2018-11-26 21:59:05498 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka31a76c6d2018-10-19 17:37:27499
Kyle Milka7d43ddcf2019-08-08 16:59:16500 instant_service_->SetCustomBackgroundInfo(kNonBackdropUrl1, "", "", GURL(),
501 "");
Dan Beam19c81782019-11-12 00:09:03502 theme = instant_service_->GetInitializedNtpTheme();
503 EXPECT_EQ(GURL(), theme->custom_background_url);
Kyle Milka427c8da2018-11-26 21:59:05504 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka31a76c6d2018-10-19 17:37:27505
Kyle Milka7d43ddcf2019-08-08 16:59:16506 instant_service_->SetCustomBackgroundInfo(kBackdropUrl2, "", "", GURL(), "");
Dan Beam19c81782019-11-12 00:09:03507 theme = instant_service_->GetInitializedNtpTheme();
508 EXPECT_EQ(kBackdropUrl2, theme->custom_background_url);
Kyle Milka427c8da2018-11-26 21:59:05509 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Kyle Milka31a76c6d2018-10-19 17:37:27510
Kyle Milka7d43ddcf2019-08-08 16:59:16511 instant_service_->SetCustomBackgroundInfo(kNonBackdropUrl2, "", "", GURL(),
512 "");
Dan Beam19c81782019-11-12 00:09:03513 theme = instant_service_->GetInitializedNtpTheme();
514 EXPECT_EQ(GURL(), theme->custom_background_url);
Kyle Milka427c8da2018-11-26 21:59:05515 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
Kyle Milka31a76c6d2018-10-19 17:37:27516}
Kristi Parkff3cf952019-01-28 23:00:59517
Dan Beam19c81782019-11-12 00:09:03518TEST_F(InstantServiceTest, TestNoNtpTheme) {
519 instant_service_->theme_ = nullptr;
520 EXPECT_NE(nullptr, instant_service_->GetInitializedNtpTheme());
Gayane Petrosyane85debf2019-03-06 06:19:13521
Dan Beam19c81782019-11-12 00:09:03522 instant_service_->theme_ = nullptr;
523 // As |FallbackToDefaultNtpTheme| uses |theme_| it should initialize it
Gayane Petrosyane85debf2019-03-06 06:19:13524 // otherwise the test should crash.
Dan Beam19c81782019-11-12 00:09:03525 instant_service_->FallbackToDefaultNtpTheme();
526 EXPECT_NE(nullptr, instant_service_->theme_);
Gayane Petrosyane85debf2019-03-06 06:19:13527}
528
Gayane Petrosyan99296102019-03-06 19:07:49529TEST_F(InstantServiceTest, TestResetToDefault) {
530 MockInstantService mock_instant_service_(profile());
531 EXPECT_CALL(mock_instant_service_, ResetCustomLinks());
Dan Beam19c81782019-11-12 00:09:03532 EXPECT_CALL(mock_instant_service_, ResetCustomBackgroundNtpTheme());
Gayane Petrosyan99296102019-03-06 19:07:49533 mock_instant_service_.ResetToDefault();
534}
535
Kristi Parkff3cf952019-01-28 23:00:59536class 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 Milkacb9cc662019-03-19 22:39:19549TEST_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 Milka7d43ddcf2019-08-08 16:59:16560 instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1,
561 kAttributionLine2, kActionUrl, "");
Kyle Milkacb9cc662019-03-19 22:39:19562
Dan Beam19c81782019-11-12 00:09:03563 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 Milkacb9cc662019-03-19 22:39:19567 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 Charette43fd3702019-05-29 16:36:51573 base::ThreadPoolInstance::Get()->FlushForTesting();
Kyle Milkacb9cc662019-03-19 22:39:19574
575 instant_service_->SelectLocalBackgroundImage(path);
Gabriel Charette798fde72019-08-20 22:24:04576 task_environment()->RunUntilIdle();
Kyle Milkacb9cc662019-03-19 22:39:19577
Dan Beam19c81782019-11-12 00:09:03578 theme = instant_service_->GetInitializedNtpTheme();
579 EXPECT_TRUE(base::StartsWith(theme->custom_background_url.spec(),
Kyle Milkacb9cc662019-03-19 22:39:19580 chrome::kChromeSearchLocalNtpBackgroundUrl,
581 base::CompareCase::SENSITIVE));
582 EXPECT_TRUE(
583 pref_service->GetBoolean(prefs::kNtpCustomBackgroundLocalToDevice));
584 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
Dan Beam19c81782019-11-12 00:09:03585 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 Milkacb9cc662019-03-19 22:39:19588}
Gayane Petrosyand257e7202019-05-21 03:43:38589
590TEST_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 Milka8bfaad02019-07-12 18:31:59602 base::TimeTicks::Now(), image, image_fetcher::RequestMetadata());
Gabriel Charette798fde72019-08-20 22:24:04603 task_environment()->RunUntilIdle();
Gayane Petrosyand257e7202019-05-21 03:43:38604 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 Milka7d43ddcf2019-08-08 16:59:16615 instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1,
616 kAttributionLine2, kActionUrl, "");
Gayane Petrosyand257e7202019-05-21 03:43:38617
Kyle Milka8bfaad02019-07-12 18:31:59618 // Background color will not update if background timestamp has changed.
Gayane Petrosyand257e7202019-05-21 03:43:38619 instant_service_->UpdateCustomBackgroundColorAsync(
Kyle Milka8bfaad02019-07-12 18:31:59620 base::TimeTicks::Now(), image, image_fetcher::RequestMetadata());
Gabriel Charette798fde72019-08-20 22:24:04621 task_environment()->RunUntilIdle();
Gayane Petrosyand257e7202019-05-21 03:43:38622 EXPECT_FALSE(CheckBackgroundColor(
623 SK_ColorRED,
624 pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict)));
625
626 // Background color should update.
627 instant_service_->UpdateCustomBackgroundColorAsync(
Kyle Milka8bfaad02019-07-12 18:31:59628 instant_service_->GetBackgroundUpdatedTimestampForTesting(), image,
629 image_fetcher::RequestMetadata());
Gabriel Charette798fde72019-08-20 22:24:04630 task_environment()->RunUntilIdle();
Gayane Petrosyand257e7202019-05-21 03:43:38631 EXPECT_TRUE(CheckBackgroundColor(
632 SK_ColorRED,
633 pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict)));
634}
Kyle Milka8bfaad02019-07-12 18:31:59635
636TEST_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 Milka7d43ddcf2019-08-08 16:59:16659 instant_service_->SetCustomBackgroundInfo(kUrl, kAttributionLine1,
660 kAttributionLine2, kActionUrl, "");
Kyle Milka8bfaad02019-07-12 18:31:59661 base::TimeTicks time_set =
662 instant_service_->GetBackgroundUpdatedTimestampForTesting();
663
664 instant_service_->SelectLocalBackgroundImage(path);
Gabriel Charette798fde72019-08-20 22:24:04665 task_environment()->RunUntilIdle();
Kyle Milka8bfaad02019-07-12 18:31:59666
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 Charette798fde72019-08-20 22:24:04671 task_environment()->RunUntilIdle();
Kyle Milka8bfaad02019-07-12 18:31:59672 EXPECT_FALSE(CheckBackgroundColor(
673 SK_ColorRED,
674 pref_service->GetDictionary(prefs::kNtpCustomBackgroundDict)));
675}
Kyle Milkaae360e2d2019-08-01 01:34:08676
677TEST_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 Charette798fde72019-08-20 22:24:04690 task_environment()->RunUntilIdle();
Kyle Milkaae360e2d2019-08-01 01:34:08691
Dan Beam19c81782019-11-12 00:09:03692 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
693 EXPECT_EQ(kValidId, theme->collection_id);
Kyle Milkaae360e2d2019-08-01 01:34:08694 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 Charette798fde72019-08-20 22:24:04700 task_environment()->RunUntilIdle();
Kyle Milkaae360e2d2019-08-01 01:34:08701
Dan Beam19c81782019-11-12 00:09:03702 theme = instant_service_->GetInitializedNtpTheme();
703 EXPECT_EQ(std::string(), theme->collection_id);
Kyle Milkaae360e2d2019-08-01 01:34:08704 EXPECT_FALSE(instant_service_->IsCustomBackgroundSet());
705}
706
707TEST_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 Charette798fde72019-08-20 22:24:04720 task_environment()->RunUntilIdle();
Kyle Milkaae360e2d2019-08-01 01:34:08721
Dan Beam19c81782019-11-12 00:09:03722 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
723 EXPECT_EQ(kValidId, theme->collection_id);
724 EXPECT_EQ("https://www.test.com/", theme->custom_background_url);
Kyle Milkaae360e2d2019-08-01 01:34:08725 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
726}
Kyle Milka989bb362019-08-01 17:45:03727
728TEST_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 Charette798fde72019-08-20 22:24:04745 task_environment()->RunUntilIdle();
Kyle Milka989bb362019-08-01 17:45:03746
Dan Beam19c81782019-11-12 00:09:03747 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
748 EXPECT_EQ(kValidId, theme->collection_id);
Kyle Milka989bb362019-08-01 17:45:03749 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 Beam19c81782019-11-12 00:09:03757 theme = instant_service_->GetInitializedNtpTheme();
Gabriel Charette798fde72019-08-20 22:24:04758 task_environment()->RunUntilIdle();
Dan Beam19c81782019-11-12 00:09:03759 EXPECT_EQ(kValidId, theme->collection_id);
760 EXPECT_EQ(kImageUrl1, theme->custom_background_url);
Kyle Milka989bb362019-08-01 17:45:03761 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
762
763 clock_->Advance(base::TimeDelta::FromHours(25));
764
765 // Should refresh background after >24 hours.
Dan Beam19c81782019-11-12 00:09:03766 theme = instant_service_->GetInitializedNtpTheme();
Gabriel Charette798fde72019-08-20 22:24:04767 task_environment()->RunUntilIdle();
Dan Beam19c81782019-11-12 00:09:03768 EXPECT_EQ(kValidId, theme->collection_id);
769 EXPECT_EQ(kImageUrl2, theme->custom_background_url);
Kyle Milka989bb362019-08-01 17:45:03770 EXPECT_TRUE(instant_service_->IsCustomBackgroundSet());
771}
Gayane Petrosyanb93e0e02019-09-09 20:00:10772
Dan Beam19c81782019-11-12 00:09:03773TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) {
Peter Kasting90dcda92020-02-25 15:28:48774 const auto& theme_provider =
775 ThemeService::GetThemeProviderForProfile(profile());
Gayane Petrosyanb93e0e02019-09-09 20:00:10776 SkColor default_text_color =
Peter Kasting90dcda92020-02-25 15:28:48777 theme_provider.GetColor(ThemeProperties::COLOR_NTP_TEXT);
Gayane Petrosyanb93e0e02019-09-09 20:00:10778 SkColor default_logo_color =
Peter Kasting90dcda92020-02-25 15:28:48779 theme_provider.GetColor(ThemeProperties::COLOR_NTP_LOGO);
780 SkColor default_shortcut_color =
781 theme_provider.GetColor(ThemeProperties::COLOR_NTP_SHORTCUT);
Gayane Petrosyanb93e0e02019-09-09 20:00:10782
783 ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());
784
785 // Check defaults when no theme and no custom backgrounds is set.
Peter Kasting90dcda92020-02-25 15:28:48786 NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
Dan Beam19c81782019-11-12 00:09:03787 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 Petrosyanb93e0e02019-09-09 20:00:10791
Peter Kasting90dcda92020-02-25 15:28:48792 // Install colors, theme update should trigger SetNTPElementsNtpTheme() and
Gayane Petrosyanb93e0e02019-09-09 20:00:10793 // 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 Kasting273d23f2019-11-27 19:58:04798 theme_service->BuildAutogeneratedThemeFromColor(SK_ColorRED);
Gayane Petrosyanb93e0e02019-09-09 20:00:10799 theme_change_observer.Wait();
800
Dan Beam19c81782019-11-12 00:09:03801 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 Petrosyanb93e0e02019-09-09 20:00:10806
Peter Kasting90dcda92020-02-25 15:28:48807 // Setting a custom background should call SetNTPElementsNtpTheme() and
Gayane Petrosyanb93e0e02019-09-09 20:00:10808 // 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 Beam19c81782019-11-12 00:09:03814 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 Kasting90dcda92020-02-25 15:28:48818 // 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 Petrosyanb93e0e02019-09-09 20:00:10823}