blob: 8a4993ebaa3e9824126baec404d21c6de72b36f8 [file] [log] [blame]
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +01001// Copyright (c) 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
5#include "chrome/browser/profile_resetter/profile_resetter.h"
6
Ben Murdochd3868032013-07-31 10:55:33 +01007#include "base/json/json_string_value_serializer.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01008#include "base/prefs/pref_service.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +01009#include "base/strings/utf_string_conversions.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010010#include "chrome/browser/content_settings/host_content_settings_map.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010011#include "chrome/browser/extensions/extension_service_unittest.h"
12#include "chrome/browser/extensions/tab_helper.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010013#include "chrome/browser/notifications/desktop_notification_service.h"
14#include "chrome/browser/notifications/desktop_notification_service_factory.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010015#include "chrome/browser/prefs/session_startup_pref.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010016#include "chrome/browser/profile_resetter/brandcode_config_fetcher.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010017#include "chrome/browser/profile_resetter/profile_resetter_test_base.h"
Ben Murdochd3868032013-07-31 10:55:33 +010018#include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010019#include "chrome/browser/search_engines/template_url_service.h"
Ben Murdochca12bfa2013-07-23 11:17:05 +010020#include "chrome/browser/search_engines/template_url_service_factory.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010021#include "chrome/browser/themes/theme_service.h"
22#include "chrome/browser/themes/theme_service_factory.h"
23#include "chrome/browser/ui/tabs/tab_strip_model.h"
24#include "chrome/common/extensions/extension.h"
25#include "chrome/common/extensions/extension_manifest_constants.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010026#include "chrome/common/pref_names.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010027#include "chrome/test/base/browser_with_test_window_test.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010028#include "content/public/browser/web_contents.h"
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010029#include "net/http/http_response_headers.h"
30#include "net/url_request/test_url_fetcher_factory.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010031
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010032
33namespace {
34
Ben Murdoch7dbb3d52013-07-17 14:55:54 +010035const char kDistributionConfig[] = "{"
36 " \"homepage\" : \"http://www.foo.com\","
37 " \"homepage_is_newtabpage\" : false,"
38 " \"browser\" : {"
39 " \"show_home_button\" : true"
40 " },"
41 " \"session\" : {"
42 " \"restore_on_startup\" : 4,"
43 " \"urls_to_restore_on_startup\" : [\"http://goo.gl\", \"http://foo.de\"]"
44 " },"
45 " \"search_provider_overrides\" : ["
46 " {"
47 " \"name\" : \"first\","
48 " \"keyword\" : \"firstkey\","
49 " \"search_url\" : \"http://www.foo.com/s?q={searchTerms}\","
50 " \"favicon_url\" : \"http://www.foo.com/favicon.ico\","
51 " \"suggest_url\" : \"http://www.foo.com/s?q={searchTerms}\","
52 " \"encoding\" : \"UTF-8\","
53 " \"id\" : 1001"
54 " }"
55 " ],"
56 " \"extensions\" : {"
57 " \"settings\" : {"
58 " \"placeholder_for_id\": {"
59 " }"
60 " }"
61 " }"
62 "}";
63
64const char kXmlConfig[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
65 "<response protocol=\"3.0\" server=\"prod\">"
66 "<app appid=\"{8A69D345-D564-463C-AFF1-A69D9E530F96}\" status=\"ok\">"
67 "<data index=\"skipfirstrunui-importsearch-defaultbrowser\" "
68 "name=\"install\" status=\"ok\">"
69 "placeholder_for_data"
70 "</data>"
71 "</app>"
72 "</response>";
73
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010074using extensions::Extension;
75using extensions::Manifest;
76
Ben Murdoch58e6fbe2013-07-26 10:20:38 +010077
78// ProfileResetterTest --------------------------------------------------------
79
Ben Murdochca12bfa2013-07-23 11:17:05 +010080// ProfileResetterTest sets up the extension, WebData and TemplateURL services.
81class ProfileResetterTest : public ExtensionServiceTestBase,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010082 public ProfileResetterTestBase {
83 protected:
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010084 virtual void SetUp() OVERRIDE;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010085
Ben Murdochca12bfa2013-07-23 11:17:05 +010086 TestingProfile* profile() { return profile_.get(); }
87
88 static BrowserContextKeyedService* CreateTemplateURLService(
89 content::BrowserContext* context);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010090};
91
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010092void ProfileResetterTest::SetUp() {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010093 ExtensionServiceTestBase::SetUp();
94 InitializeEmptyExtensionService();
Ben Murdochca12bfa2013-07-23 11:17:05 +010095
96 profile()->CreateWebDataService();
97 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
98 profile(),
99 &ProfileResetterTest::CreateTemplateURLService);
100 resetter_.reset(new ProfileResetter(profile()));
101}
102
103// static
104BrowserContextKeyedService* ProfileResetterTest::CreateTemplateURLService(
105 content::BrowserContext* context) {
106 return new TemplateURLService(static_cast<Profile*>(context));
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100107}
108
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100109
110// PinnedTabsResetTest --------------------------------------------------------
111
112class PinnedTabsResetTest : public BrowserWithTestWindowTest,
113 public ProfileResetterTestBase {
114 protected:
115 virtual void SetUp() OVERRIDE;
116
117 content::WebContents* CreateWebContents();
118};
119
120void PinnedTabsResetTest::SetUp() {
121 BrowserWithTestWindowTest::SetUp();
122 resetter_.reset(new ProfileResetter(profile()));
123}
124
125content::WebContents* PinnedTabsResetTest::CreateWebContents() {
126 return content::WebContents::Create(
127 content::WebContents::CreateParams(profile()));
128}
129
130
131// ConfigParserTest -----------------------------------------------------------
132
133// URLFetcher delegate that simply records the upload data.
134struct URLFetcherRequestListener : net::URLFetcherDelegate {
135 URLFetcherRequestListener();
136 virtual ~URLFetcherRequestListener();
137
138 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
139
140 std::string upload_data;
141 net::URLFetcherDelegate* real_delegate;
142};
143
144URLFetcherRequestListener::URLFetcherRequestListener()
145 : real_delegate(NULL) {
146}
147
148URLFetcherRequestListener::~URLFetcherRequestListener() {
149}
150
151void URLFetcherRequestListener::OnURLFetchComplete(
152 const net::URLFetcher* source) {
153 const net::TestURLFetcher* test_fetcher =
154 static_cast<const net::TestURLFetcher*>(source);
155 upload_data = test_fetcher->upload_data();
156 DCHECK(real_delegate);
157 real_delegate->OnURLFetchComplete(source);
158}
159
160class ConfigParserTest : public testing::Test {
161 protected:
162 ConfigParserTest();
163 virtual ~ConfigParserTest();
164
165 scoped_ptr<BrandcodeConfigFetcher> WaitForRequest(const GURL& url);
166
167 net::FakeURLFetcherFactory& factory() { return factory_; }
168
169 private:
170 scoped_ptr<net::FakeURLFetcher> CreateFakeURLFetcher(
171 const GURL& url,
172 net::URLFetcherDelegate* fetcher_delegate,
173 const std::string& response_data,
174 bool success);
175
176 MOCK_METHOD0(Callback, void(void));
177
178 base::MessageLoop loop_;
179 content::TestBrowserThread ui_thread_;
180 content::TestBrowserThread io_thread_;
181 URLFetcherRequestListener request_listener_;
182 net::FakeURLFetcherFactory factory_;
183};
184
185ConfigParserTest::ConfigParserTest()
186 : loop_(base::MessageLoop::TYPE_IO),
187 ui_thread_(content::BrowserThread::UI, &loop_),
188 io_thread_(content::BrowserThread::IO, &loop_),
189 factory_(NULL, base::Bind(&ConfigParserTest::CreateFakeURLFetcher,
190 base::Unretained(this))) {
191}
192
193ConfigParserTest::~ConfigParserTest() {}
194
195scoped_ptr<BrandcodeConfigFetcher> ConfigParserTest::WaitForRequest(
196 const GURL& url) {
197 EXPECT_CALL(*this, Callback());
198 scoped_ptr<BrandcodeConfigFetcher> fetcher(
199 new BrandcodeConfigFetcher(base::Bind(&ConfigParserTest::Callback,
200 base::Unretained(this)),
201 url,
202 "ABCD"));
203 base::MessageLoop::current()->RunUntilIdle();
204 EXPECT_FALSE(fetcher->IsActive());
205 // Look for the brand code in the request.
206 EXPECT_NE(std::string::npos, request_listener_.upload_data.find("ABCD"));
207 return fetcher.Pass();
208}
209
210scoped_ptr<net::FakeURLFetcher> ConfigParserTest::CreateFakeURLFetcher(
211 const GURL& url,
212 net::URLFetcherDelegate* fetcher_delegate,
213 const std::string& response_data,
214 bool success) {
215 request_listener_.real_delegate = fetcher_delegate;
216 scoped_ptr<net::FakeURLFetcher> fetcher(
217 new net::FakeURLFetcher(url, &request_listener_, response_data, success));
218 scoped_refptr<net::HttpResponseHeaders> download_headers =
219 new net::HttpResponseHeaders("");
220 download_headers->AddHeader("Content-Type: text/xml");
221 fetcher->set_response_headers(download_headers);
222 return fetcher.Pass();
223}
224
225
226// helper functions -----------------------------------------------------------
227
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100228scoped_refptr<Extension> CreateExtension(const std::string& name,
229 const base::FilePath& path,
230 Manifest::Location location,
Ben Murdoch32409262013-08-07 11:04:47 +0100231 extensions::Manifest::Type type,
232 bool installed_by_default) {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100233 DictionaryValue manifest;
234 manifest.SetString(extension_manifest_keys::kVersion, "1.0.0.0");
235 manifest.SetString(extension_manifest_keys::kName, name);
Ben Murdoch32409262013-08-07 11:04:47 +0100236 switch (type) {
237 case extensions::Manifest::TYPE_THEME:
238 manifest.Set(extension_manifest_keys::kTheme, new DictionaryValue);
239 break;
240 case extensions::Manifest::TYPE_HOSTED_APP:
241 manifest.SetString(extension_manifest_keys::kLaunchWebURL,
242 "http://www.google.com");
243 manifest.SetString(extension_manifest_keys::kUpdateURL,
244 "http://clients2.google.com/service/update2/crx");
245 break;
246 case extensions::Manifest::TYPE_EXTENSION:
247 // do nothing
248 break;
249 default:
250 NOTREACHED();
251 }
Ben Murdochca12bfa2013-07-23 11:17:05 +0100252 manifest.SetString(extension_manifest_keys::kOmniboxKeyword, name);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100253 std::string error;
254 scoped_refptr<Extension> extension = Extension::Create(
255 path,
256 location,
257 manifest,
Ben Murdoch32409262013-08-07 11:04:47 +0100258 installed_by_default ? Extension::WAS_INSTALLED_BY_DEFAULT
259 : Extension::NO_FLAGS,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100260 &error);
261 EXPECT_TRUE(extension.get() != NULL) << error;
262 return extension;
263}
264
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100265void ReplaceString(std::string* str,
266 const std::string& placeholder,
267 const std::string& substitution) {
268 ASSERT_NE(static_cast<std::string*>(NULL), str);
269 size_t placeholder_pos = str->find(placeholder);
270 ASSERT_NE(std::string::npos, placeholder_pos);
271 str->replace(placeholder_pos, placeholder.size(), substitution);
272}
273
274
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100275/********************* Tests *********************/
276
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100277TEST_F(ProfileResetterTest, ResetDefaultSearchEngine) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100278 // Search engine's logic is tested by
Ben Murdochca12bfa2013-07-23 11:17:05 +0100279 // TemplateURLServiceTest.ResetURLs.
280 PrefService* prefs = profile()->GetPrefs();
Ben Murdocheb525c52013-07-10 11:40:50 +0100281 DCHECK(prefs);
282 prefs->SetString(prefs::kLastPromptedGoogleURL, "http://www.foo.com/");
283
Ben Murdochca12bfa2013-07-23 11:17:05 +0100284 scoped_refptr<Extension> extension = CreateExtension(
285 "xxx",
286 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
287 Manifest::COMPONENT,
Ben Murdoch32409262013-08-07 11:04:47 +0100288 extensions::Manifest::TYPE_EXTENSION,
Ben Murdochca12bfa2013-07-23 11:17:05 +0100289 false);
290 service_->AddExtension(extension.get());
291
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100292 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE);
293
Ben Murdochca12bfa2013-07-23 11:17:05 +0100294 // TemplateURLService should reset extension search engines.
295 TemplateURLService* model =
296 TemplateURLServiceFactory::GetForProfile(profile());
297 TemplateURL* ext_url = model->GetTemplateURLForKeyword(ASCIIToUTF16("xxx"));
298 ASSERT_TRUE(ext_url);
299 EXPECT_TRUE(ext_url->IsExtensionKeyword());
300 EXPECT_EQ(ASCIIToUTF16("xxx"), ext_url->keyword());
301 EXPECT_EQ(ASCIIToUTF16("xxx"), ext_url->short_name());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100302 EXPECT_EQ("", prefs->GetString(prefs::kLastPromptedGoogleURL));
303}
304
305TEST_F(ProfileResetterTest, ResetDefaultSearchEngineNonOrganic) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100306 PrefService* prefs = profile()->GetPrefs();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100307 DCHECK(prefs);
308 prefs->SetString(prefs::kLastPromptedGoogleURL, "http://www.foo.com/");
309
310 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE, kDistributionConfig);
311
Ben Murdochca12bfa2013-07-23 11:17:05 +0100312 TemplateURLService* model =
313 TemplateURLServiceFactory::GetForProfile(profile());
314 EXPECT_EQ(1u, model->GetTemplateURLs().size());
315 TemplateURL* default_engine = model->GetDefaultSearchProvider();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100316 ASSERT_NE(static_cast<TemplateURL*>(NULL), default_engine);
317 EXPECT_EQ(ASCIIToUTF16("first"), default_engine->short_name());
318 EXPECT_EQ(ASCIIToUTF16("firstkey"), default_engine->keyword());
319 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", default_engine->url());
Ben Murdocheb525c52013-07-10 11:40:50 +0100320
321 EXPECT_EQ("", prefs->GetString(prefs::kLastPromptedGoogleURL));
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100322}
323
324TEST_F(ProfileResetterTest, ResetHomepage) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100325 PrefService* prefs = profile()->GetPrefs();
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100326 DCHECK(prefs);
327 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
328 prefs->SetString(prefs::kHomePage, "http://google.com");
329 prefs->SetBoolean(prefs::kShowHomeButton, true);
330
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100331 ResetAndWait(ProfileResetter::HOMEPAGE);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100332
333 EXPECT_TRUE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
334 EXPECT_EQ(std::string(), prefs->GetString(prefs::kHomePage));
335 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowHomeButton));
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100336}
337
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100338TEST_F(ProfileResetterTest, ResetHomepageNonOrganic) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100339 PrefService* prefs = profile()->GetPrefs();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100340 DCHECK(prefs);
341 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, true);
342 prefs->SetString(prefs::kHomePage, "http://google.com");
343 prefs->SetBoolean(prefs::kShowHomeButton, false);
344
345 ResetAndWait(ProfileResetter::HOMEPAGE, kDistributionConfig);
346
347 EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
348 EXPECT_EQ("http://www.foo.com", prefs->GetString(prefs::kHomePage));
349 EXPECT_TRUE(prefs->GetBoolean(prefs::kShowHomeButton));
350}
351
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100352TEST_F(ProfileResetterTest, ResetContentSettings) {
Ben Murdocheb525c52013-07-10 11:40:50 +0100353 HostContentSettingsMap* host_content_settings_map =
Ben Murdochca12bfa2013-07-23 11:17:05 +0100354 profile()->GetHostContentSettingsMap();
Ben Murdocheb525c52013-07-10 11:40:50 +0100355 DesktopNotificationService* notification_service =
Ben Murdochca12bfa2013-07-23 11:17:05 +0100356 DesktopNotificationServiceFactory::GetForProfile(profile());
Ben Murdocheb525c52013-07-10 11:40:50 +0100357 ContentSettingsPattern pattern =
358 ContentSettingsPattern::FromString("[*.]example.org");
359 std::map<ContentSettingsType, ContentSetting> default_settings;
360
361 for (int type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
362 if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
363 notification_service->SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
364 notification_service->GrantPermission(GURL("http://foo.de"));
365 } else if (type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
366 type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT ||
367 type == CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS) {
368 // These types are excluded because one can't call
369 // GetDefaultContentSetting() for them.
370 } else {
371 ContentSettingsType content_type = static_cast<ContentSettingsType>(type);
372 ContentSetting default_setting =
373 host_content_settings_map->GetDefaultContentSetting(content_type,
374 NULL);
375 default_settings[content_type] = default_setting;
376 ContentSetting wildcard_setting =
377 default_setting == CONTENT_SETTING_BLOCK ? CONTENT_SETTING_ALLOW
378 : CONTENT_SETTING_BLOCK;
379 ContentSetting site_setting =
380 default_setting == CONTENT_SETTING_ALLOW ? CONTENT_SETTING_ALLOW
381 : CONTENT_SETTING_BLOCK;
382 if (HostContentSettingsMap::IsSettingAllowedForType(
Ben Murdochca12bfa2013-07-23 11:17:05 +0100383 profile()->GetPrefs(),
Ben Murdocheb525c52013-07-10 11:40:50 +0100384 wildcard_setting,
385 content_type)) {
386 host_content_settings_map->SetDefaultContentSetting(
387 content_type,
388 wildcard_setting);
389 }
390 if (!HostContentSettingsMap::ContentTypeHasCompoundValue(content_type) &&
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100391 HostContentSettingsMap::IsSettingAllowedForType(
Ben Murdochca12bfa2013-07-23 11:17:05 +0100392 profile()->GetPrefs(),
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100393 site_setting,
Ben Murdocheb525c52013-07-10 11:40:50 +0100394 content_type)) {
395 host_content_settings_map->SetContentSetting(
396 pattern,
397 ContentSettingsPattern::Wildcard(),
398 content_type,
399 std::string(),
400 site_setting);
401 ContentSettingsForOneType host_settings;
402 host_content_settings_map->GetSettingsForOneType(
403 content_type, std::string(), &host_settings);
404 EXPECT_EQ(2U, host_settings.size());
405 }
406 }
407 }
408
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100409 ResetAndWait(ProfileResetter::CONTENT_SETTINGS);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100410
Ben Murdocheb525c52013-07-10 11:40:50 +0100411 for (int type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
412 if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
413 EXPECT_EQ(CONTENT_SETTING_ASK,
414 notification_service->GetDefaultContentSetting(NULL));
415 EXPECT_EQ(CONTENT_SETTING_ASK,
416 notification_service->GetContentSetting(GURL("http://foo.de")));
417 } else {
418 ContentSettingsType content_type = static_cast<ContentSettingsType>(type);
419 if (HostContentSettingsMap::ContentTypeHasCompoundValue(content_type) ||
420 type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
421 content_type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT ||
422 content_type == CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS)
423 continue;
424 ContentSetting default_setting =
425 host_content_settings_map->GetDefaultContentSetting(content_type,
426 NULL);
427 EXPECT_TRUE(default_settings.count(content_type));
428 EXPECT_EQ(default_settings[content_type], default_setting);
429 if (!HostContentSettingsMap::ContentTypeHasCompoundValue(content_type)) {
430 ContentSetting site_setting =
431 host_content_settings_map->GetContentSetting(
432 GURL("example.org"),
433 GURL(),
434 content_type,
435 std::string());
436 EXPECT_EQ(default_setting, site_setting);
437 }
438
439 ContentSettingsForOneType host_settings;
440 host_content_settings_map->GetSettingsForOneType(
441 content_type, std::string(), &host_settings);
442 EXPECT_EQ(1U, host_settings.size());
443 }
444 }
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100445}
446
Ben Murdochca12bfa2013-07-23 11:17:05 +0100447TEST_F(ProfileResetterTest, ResetExtensionsByDisabling) {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100448 base::ScopedTempDir temp_dir;
449 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100450
Ben Murdoch32409262013-08-07 11:04:47 +0100451 scoped_refptr<Extension> theme =
452 CreateExtension("example1",
453 temp_dir.path(),
454 Manifest::INVALID_LOCATION,
455 extensions::Manifest::TYPE_THEME,
456 false);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100457 service_->FinishInstallationForTest(theme.get());
458 // Let ThemeService finish creating the theme pack.
459 base::MessageLoop::current()->RunUntilIdle();
460
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100461 ThemeService* theme_service =
Ben Murdochca12bfa2013-07-23 11:17:05 +0100462 ThemeServiceFactory::GetForProfile(profile());
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100463 EXPECT_FALSE(theme_service->UsingDefaultTheme());
464
465 scoped_refptr<Extension> ext2 = CreateExtension(
466 "example2",
467 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
468 Manifest::INVALID_LOCATION,
Ben Murdoch32409262013-08-07 11:04:47 +0100469 extensions::Manifest::TYPE_EXTENSION,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100470 false);
471 service_->AddExtension(ext2.get());
472 // Components and external policy extensions shouldn't be deleted.
473 scoped_refptr<Extension> ext3 = CreateExtension(
474 "example3",
475 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
476 Manifest::COMPONENT,
Ben Murdoch32409262013-08-07 11:04:47 +0100477 extensions::Manifest::TYPE_EXTENSION,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100478 false);
479 service_->AddExtension(ext3.get());
480 scoped_refptr<Extension> ext4 =
481 CreateExtension("example4",
482 base::FilePath(FILE_PATH_LITERAL("//nonexistent3")),
483 Manifest::EXTERNAL_POLICY_DOWNLOAD,
Ben Murdoch32409262013-08-07 11:04:47 +0100484 extensions::Manifest::TYPE_EXTENSION,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100485 false);
486 service_->AddExtension(ext4.get());
487 EXPECT_EQ(4u, service_->extensions()->size());
488
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100489 ResetAndWait(ProfileResetter::EXTENSIONS);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100490 EXPECT_EQ(2u, service_->extensions()->size());
Ben Murdoch32409262013-08-07 11:04:47 +0100491 EXPECT_FALSE(service_->extensions()->Contains(theme->id()));
492 EXPECT_FALSE(service_->extensions()->Contains(ext2->id()));
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100493 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
494 EXPECT_TRUE(service_->extensions()->Contains(ext4->id()));
495 EXPECT_TRUE(theme_service->UsingDefaultTheme());
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100496}
497
Ben Murdochca12bfa2013-07-23 11:17:05 +0100498TEST_F(ProfileResetterTest, ResetExtensionsByDisablingNonOrganic) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100499 scoped_refptr<Extension> ext2 = CreateExtension(
500 "example2",
501 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
502 Manifest::INVALID_LOCATION,
Ben Murdoch32409262013-08-07 11:04:47 +0100503 extensions::Manifest::TYPE_EXTENSION,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100504 false);
505 service_->AddExtension(ext2.get());
506 // Components and external policy extensions shouldn't be deleted.
507 scoped_refptr<Extension> ext3 = CreateExtension(
508 "example3",
509 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
510 Manifest::INVALID_LOCATION,
Ben Murdoch32409262013-08-07 11:04:47 +0100511 extensions::Manifest::TYPE_EXTENSION,
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100512 false);
513 service_->AddExtension(ext3.get());
514 EXPECT_EQ(2u, service_->extensions()->size());
515
516 std::string master_prefs(kDistributionConfig);
517 ReplaceString(&master_prefs, "placeholder_for_id", ext3->id());
518
519 ResetAndWait(ProfileResetter::EXTENSIONS, master_prefs);
520
521 EXPECT_EQ(1u, service_->extensions()->size());
522 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
523}
524
Ben Murdoch32409262013-08-07 11:04:47 +0100525TEST_F(ProfileResetterTest, ResetExtensionsAndDefaultApps) {
526 service_->Init();
527
528 base::ScopedTempDir temp_dir;
529 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
530
531 scoped_refptr<Extension> ext1 =
532 CreateExtension("example1",
533 temp_dir.path(),
534 Manifest::INVALID_LOCATION,
535 extensions::Manifest::TYPE_THEME,
536 false);
537 service_->FinishInstallationForTest(ext1.get());
538 // Let ThemeService finish creating the theme pack.
539 base::MessageLoop::current()->RunUntilIdle();
540
541 ThemeService* theme_service =
542 ThemeServiceFactory::GetForProfile(profile());
543 EXPECT_FALSE(theme_service->UsingDefaultTheme());
544
545 scoped_refptr<Extension> ext2 =
546 CreateExtension("example2",
547 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
548 Manifest::INVALID_LOCATION,
549 extensions::Manifest::TYPE_EXTENSION,
550 false);
551 service_->AddExtension(ext2.get());
552
553 scoped_refptr<Extension> ext3 =
554 CreateExtension("example2",
555 base::FilePath(FILE_PATH_LITERAL("//nonexistent3")),
556 Manifest::INVALID_LOCATION,
557 extensions::Manifest::TYPE_HOSTED_APP,
558 true);
559 service_->AddExtension(ext3.get());
560 EXPECT_EQ(3u, service_->extensions()->size());
561
562 ResetAndWait(ProfileResetter::EXTENSIONS);
563
564 EXPECT_EQ(1u, service_->extensions()->size());
565 EXPECT_FALSE(service_->extensions()->Contains(ext1->id()));
566 EXPECT_FALSE(service_->extensions()->Contains(ext2->id()));
567 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
568 EXPECT_TRUE(theme_service->UsingDefaultTheme());
569}
570
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100571TEST_F(ProfileResetterTest, ResetStartPage) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100572 PrefService* prefs = profile()->GetPrefs();
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100573 DCHECK(prefs);
574
575 SessionStartupPref startup_pref(SessionStartupPref::URLS);
576 startup_pref.urls.push_back(GURL("http://foo"));
577 startup_pref.urls.push_back(GURL("http://bar"));
578 SessionStartupPref::SetStartupPref(prefs, startup_pref);
579
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100580 ResetAndWait(ProfileResetter::STARTUP_PAGES);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100581
582 startup_pref = SessionStartupPref::GetStartupPref(prefs);
583 EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(), startup_pref.type);
584 EXPECT_EQ(std::vector<GURL>(), startup_pref.urls);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100585}
586
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100587TEST_F(ProfileResetterTest, ResetStartPageNonOrganic) {
Ben Murdochca12bfa2013-07-23 11:17:05 +0100588 PrefService* prefs = profile()->GetPrefs();
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100589 DCHECK(prefs);
590
591 SessionStartupPref startup_pref(SessionStartupPref::LAST);
592 SessionStartupPref::SetStartupPref(prefs, startup_pref);
593
594 ResetAndWait(ProfileResetter::STARTUP_PAGES, kDistributionConfig);
595
596 startup_pref = SessionStartupPref::GetStartupPref(prefs);
597 EXPECT_EQ(SessionStartupPref::URLS, startup_pref.type);
598 const GURL urls[] = {GURL("http://goo.gl"), GURL("http://foo.de")};
599 EXPECT_EQ(std::vector<GURL>(urls, urls + arraysize(urls)), startup_pref.urls);
600}
601
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100602TEST_F(PinnedTabsResetTest, ResetPinnedTabs) {
603 scoped_refptr<Extension> extension_app = CreateExtension(
604 "hello!",
605 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
606 Manifest::INVALID_LOCATION,
Ben Murdoch32409262013-08-07 11:04:47 +0100607 extensions::Manifest::TYPE_HOSTED_APP,
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100608 false);
Ben Murdocheb525c52013-07-10 11:40:50 +0100609 scoped_ptr<content::WebContents> contents1(CreateWebContents());
610 extensions::TabHelper::CreateForWebContents(contents1.get());
611 extensions::TabHelper::FromWebContents(contents1.get())->
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100612 SetExtensionApp(extension_app.get());
Ben Murdocheb525c52013-07-10 11:40:50 +0100613 scoped_ptr<content::WebContents> contents2(CreateWebContents());
614 scoped_ptr<content::WebContents> contents3(CreateWebContents());
615 scoped_ptr<content::WebContents> contents4(CreateWebContents());
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100616 TabStripModel* tab_strip_model = browser()->tab_strip_model();
617
Ben Murdocheb525c52013-07-10 11:40:50 +0100618 tab_strip_model->AppendWebContents(contents4.get(), true);
619 tab_strip_model->AppendWebContents(contents3.get(), true);
620 tab_strip_model->AppendWebContents(contents2.get(), true);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100621 tab_strip_model->SetTabPinned(2, true);
Ben Murdocheb525c52013-07-10 11:40:50 +0100622 tab_strip_model->AppendWebContents(contents1.get(), true);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100623 tab_strip_model->SetTabPinned(3, true);
624
625 EXPECT_EQ(contents2, tab_strip_model->GetWebContentsAt(0));
626 EXPECT_EQ(contents1, tab_strip_model->GetWebContentsAt(1));
627 EXPECT_EQ(contents3, tab_strip_model->GetWebContentsAt(2));
628 EXPECT_EQ(contents4, tab_strip_model->GetWebContentsAt(3));
629 EXPECT_EQ(3, tab_strip_model->IndexOfFirstNonMiniTab());
630
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100631 ResetAndWait(ProfileResetter::PINNED_TABS);
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100632
633 EXPECT_EQ(contents1, tab_strip_model->GetWebContentsAt(0));
634 EXPECT_EQ(contents2, tab_strip_model->GetWebContentsAt(1));
635 EXPECT_EQ(contents3, tab_strip_model->GetWebContentsAt(2));
636 EXPECT_EQ(contents4, tab_strip_model->GetWebContentsAt(3));
637 EXPECT_EQ(1, tab_strip_model->IndexOfFirstNonMiniTab());
638}
639
640TEST_F(ProfileResetterTest, ResetFewFlags) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100641 // mock_object_ is a StrictMock, so we verify that it is called only once.
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100642 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
Ben Murdochca12bfa2013-07-23 11:17:05 +0100643 ProfileResetter::HOMEPAGE |
644 ProfileResetter::CONTENT_SETTINGS);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100645}
646
647// Tries to load unavailable config file.
648TEST_F(ConfigParserTest, NoConnectivity) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100649 const std::string url("http://test");
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100650 factory().SetFakeResponse(url, "", false);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100651
652 scoped_ptr<BrandcodeConfigFetcher> fetcher = WaitForRequest(GURL(url));
653 EXPECT_FALSE(fetcher->GetSettings());
654}
655
656// Tries to load available config file.
657TEST_F(ConfigParserTest, ParseConfig) {
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100658 const std::string url("http://test");
659 std::string xml_config(kXmlConfig);
660 ReplaceString(&xml_config, "placeholder_for_data", kDistributionConfig);
661 ReplaceString(&xml_config,
662 "placeholder_for_id",
663 "abbaabbaabbaabbaabbaabbaabbaabba");
Ben Murdoch58e6fbe2013-07-26 10:20:38 +0100664 factory().SetFakeResponse(url, xml_config, true);
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100665
666 scoped_ptr<BrandcodeConfigFetcher> fetcher = WaitForRequest(GURL(url));
667 scoped_ptr<BrandcodedDefaultSettings> settings = fetcher->GetSettings();
668 ASSERT_TRUE(settings);
669
670 std::vector<std::string> extension_ids;
671 EXPECT_TRUE(settings->GetExtensions(&extension_ids));
672 EXPECT_EQ(1u, extension_ids.size());
673 EXPECT_EQ("abbaabbaabbaabbaabbaabbaabbaabba", extension_ids[0]);
674
675 std::string homepage;
676 EXPECT_TRUE(settings->GetHomepage(&homepage));
677 EXPECT_EQ("http://www.foo.com", homepage);
678
679 scoped_ptr<base::ListValue> startup_list(
680 settings->GetUrlsToRestoreOnStartup());
681 EXPECT_TRUE(startup_list);
682 std::vector<std::string> startup_pages;
683 for (base::ListValue::iterator i = startup_list->begin();
684 i != startup_list->end(); ++i) {
685 std::string url;
686 EXPECT_TRUE((*i)->GetAsString(&url));
687 startup_pages.push_back(url);
688 }
Ben Murdochd3868032013-07-31 10:55:33 +0100689 ASSERT_EQ(2u, startup_pages.size());
Ben Murdoch7dbb3d52013-07-17 14:55:54 +0100690 EXPECT_EQ("http://goo.gl", startup_pages[0]);
691 EXPECT_EQ("http://foo.de", startup_pages[1]);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100692}
693
Ben Murdochd3868032013-07-31 10:55:33 +0100694TEST_F(ProfileResetterTest, CheckSnapshots) {
695 ResettableSettingsSnapshot empty_snap(profile());
696 EXPECT_EQ(0, empty_snap.FindDifferentFields(empty_snap));
697
698 // Reset to non organic defaults.
699 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
700 ProfileResetter::HOMEPAGE |
701 ProfileResetter::STARTUP_PAGES, kDistributionConfig);
702
703 ResettableSettingsSnapshot nonorganic_snap(profile());
704 EXPECT_EQ(ResettableSettingsSnapshot::STARTUP_URLS |
705 ResettableSettingsSnapshot::STARTUP_TYPE |
706 ResettableSettingsSnapshot::HOMEPAGE |
707 ResettableSettingsSnapshot::HOMEPAGE_IS_NTP |
708 ResettableSettingsSnapshot::DSE_URL,
709 empty_snap.FindDifferentFields(nonorganic_snap));
710 empty_snap.SubtractStartupURLs(nonorganic_snap);
711 EXPECT_TRUE(empty_snap.startup_urls().empty());
712 EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(),
713 empty_snap.startup_type());
714 EXPECT_TRUE(empty_snap.homepage().empty());
715 EXPECT_TRUE(empty_snap.homepage_is_ntp());
716 EXPECT_NE(std::string::npos, empty_snap.dse_url().find("{google:baseURL}"));
717
718 // Reset to organic defaults.
719 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
720 ProfileResetter::HOMEPAGE |
721 ProfileResetter::STARTUP_PAGES);
722
723 ResettableSettingsSnapshot organic_snap(profile());
724 EXPECT_EQ(ResettableSettingsSnapshot::STARTUP_URLS |
725 ResettableSettingsSnapshot::STARTUP_TYPE |
726 ResettableSettingsSnapshot::HOMEPAGE |
727 ResettableSettingsSnapshot::HOMEPAGE_IS_NTP |
728 ResettableSettingsSnapshot::DSE_URL,
729 nonorganic_snap.FindDifferentFields(organic_snap));
730 nonorganic_snap.SubtractStartupURLs(organic_snap);
731 const GURL urls[] = {GURL("http://foo.de"), GURL("http://goo.gl")};
732 EXPECT_EQ(std::vector<GURL>(urls, urls + arraysize(urls)),
733 nonorganic_snap.startup_urls());
734 EXPECT_EQ(SessionStartupPref::URLS, nonorganic_snap.startup_type());
735 EXPECT_EQ("http://www.foo.com", nonorganic_snap.homepage());
736 EXPECT_FALSE(nonorganic_snap.homepage_is_ntp());
737 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", nonorganic_snap.dse_url());
738}
739
740TEST_F(ProfileResetterTest, FeedbackSerializtionTest) {
741 // Reset to non organic defaults.
742 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
743 ProfileResetter::HOMEPAGE |
744 ProfileResetter::STARTUP_PAGES, kDistributionConfig);
745
746 const ResettableSettingsSnapshot nonorganic_snap(profile());
747
748 COMPILE_ASSERT(ResettableSettingsSnapshot::ALL_FIELDS == 31,
749 expand_this_test);
750 for (int field_mask = 0; field_mask <= ResettableSettingsSnapshot::ALL_FIELDS;
751 ++field_mask) {
752 std::string report = SerializeSettingsReport(nonorganic_snap, field_mask);
753 JSONStringValueSerializer json(report);
754 std::string error;
755 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
756 ASSERT_TRUE(root) << error;
757 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY)) << error;
758
759 base::DictionaryValue* dict =
760 static_cast<base::DictionaryValue*>(root.get());
761
762 ListValue* startup_urls = NULL;
763 int startup_type = 0;
764 std::string homepage;
765 bool homepage_is_ntp = true;
766 std::string default_search_engine;
767
768 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::STARTUP_URLS),
769 dict->GetList("startup_urls", &startup_urls));
770 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::STARTUP_TYPE),
771 dict->GetInteger("startup_type", &startup_type));
772 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::HOMEPAGE),
773 dict->GetString("homepage", &homepage));
774 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::HOMEPAGE_IS_NTP),
775 dict->GetBoolean("homepage_is_ntp", &homepage_is_ntp));
776 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::DSE_URL),
777 dict->GetString("default_search_engine", &default_search_engine));
778 }
779}
780
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100781} // namespace