Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / profile_resetter / profile_resetter_unittest.cc
blob91312aa54988beac452d6898a6b928bbcbcc870b
1 // 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.
5 #include "chrome/browser/profile_resetter/profile_resetter.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/browser/extensions/extension_service_unittest.h"
12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "chrome/browser/notifications/desktop_notification_service.h"
14 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
15 #include "chrome/browser/prefs/session_startup_pref.h"
16 #include "chrome/browser/profile_resetter/brandcode_config_fetcher.h"
17 #include "chrome/browser/profile_resetter/profile_resetter_test_base.h"
18 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
19 #include "chrome/browser/search_engines/template_url_service.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #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/pref_names.h"
25 #include "chrome/test/base/browser_with_test_window_test.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/test/test_browser_thread.h"
28 #include "extensions/common/extension.h"
29 #include "extensions/common/manifest_constants.h"
30 #include "net/http/http_response_headers.h"
31 #include "net/http/http_status_code.h"
32 #include "net/url_request/test_url_fetcher_factory.h"
33 #include "net/url_request/url_request_status.h"
34 #include "url/gurl.h"
36 using base::ASCIIToUTF16;
38 namespace {
40 const char kDistributionConfig[] = "{"
41 " \"homepage\" : \"http://www.foo.com\","
42 " \"homepage_is_newtabpage\" : false,"
43 " \"browser\" : {"
44 " \"show_home_button\" : true"
45 " },"
46 " \"session\" : {"
47 " \"restore_on_startup\" : 4,"
48 " \"startup_urls\" : [\"http://goo.gl\", \"http://foo.de\"]"
49 " },"
50 " \"search_provider_overrides\" : ["
51 " {"
52 " \"name\" : \"first\","
53 " \"keyword\" : \"firstkey\","
54 " \"search_url\" : \"http://www.foo.com/s?q={searchTerms}\","
55 " \"favicon_url\" : \"http://www.foo.com/favicon.ico\","
56 " \"suggest_url\" : \"http://www.foo.com/s?q={searchTerms}\","
57 " \"encoding\" : \"UTF-8\","
58 " \"id\" : 1001"
59 " }"
60 " ],"
61 " \"extensions\" : {"
62 " \"settings\" : {"
63 " \"placeholder_for_id\": {"
64 " }"
65 " }"
66 " }"
67 "}";
69 const char kXmlConfig[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
70 "<response protocol=\"3.0\" server=\"prod\">"
71 "<app appid=\"{8A69D345-D564-463C-AFF1-A69D9E530F96}\" status=\"ok\">"
72 "<data index=\"skipfirstrunui-importsearch-defaultbrowser\" "
73 "name=\"install\" status=\"ok\">"
74 "placeholder_for_data"
75 "</data>"
76 "</app>"
77 "</response>";
79 using extensions::Extension;
80 using extensions::Manifest;
83 // ProfileResetterTest --------------------------------------------------------
85 // ProfileResetterTest sets up the extension, WebData and TemplateURL services.
86 class ProfileResetterTest : public ExtensionServiceTestBase,
87 public ProfileResetterTestBase {
88 protected:
89 virtual void SetUp() OVERRIDE;
91 TestingProfile* profile() { return profile_.get(); }
93 static BrowserContextKeyedService* CreateTemplateURLService(
94 content::BrowserContext* context);
97 void ProfileResetterTest::SetUp() {
98 ExtensionServiceTestBase::SetUp();
99 InitializeEmptyExtensionService();
101 profile()->CreateWebDataService();
102 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
103 profile(),
104 &ProfileResetterTest::CreateTemplateURLService);
105 resetter_.reset(new ProfileResetter(profile()));
108 // static
109 BrowserContextKeyedService* ProfileResetterTest::CreateTemplateURLService(
110 content::BrowserContext* context) {
111 return new TemplateURLService(static_cast<Profile*>(context));
115 // PinnedTabsResetTest --------------------------------------------------------
117 class PinnedTabsResetTest : public BrowserWithTestWindowTest,
118 public ProfileResetterTestBase {
119 protected:
120 virtual void SetUp() OVERRIDE;
122 content::WebContents* CreateWebContents();
125 void PinnedTabsResetTest::SetUp() {
126 BrowserWithTestWindowTest::SetUp();
127 resetter_.reset(new ProfileResetter(profile()));
130 content::WebContents* PinnedTabsResetTest::CreateWebContents() {
131 return content::WebContents::Create(
132 content::WebContents::CreateParams(profile()));
136 // ConfigParserTest -----------------------------------------------------------
138 // URLFetcher delegate that simply records the upload data.
139 struct URLFetcherRequestListener : net::URLFetcherDelegate {
140 URLFetcherRequestListener();
141 virtual ~URLFetcherRequestListener();
143 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
145 std::string upload_data;
146 net::URLFetcherDelegate* real_delegate;
149 URLFetcherRequestListener::URLFetcherRequestListener()
150 : real_delegate(NULL) {
153 URLFetcherRequestListener::~URLFetcherRequestListener() {
156 void URLFetcherRequestListener::OnURLFetchComplete(
157 const net::URLFetcher* source) {
158 const net::TestURLFetcher* test_fetcher =
159 static_cast<const net::TestURLFetcher*>(source);
160 upload_data = test_fetcher->upload_data();
161 DCHECK(real_delegate);
162 real_delegate->OnURLFetchComplete(source);
165 class ConfigParserTest : public testing::Test {
166 protected:
167 ConfigParserTest();
168 virtual ~ConfigParserTest();
170 scoped_ptr<BrandcodeConfigFetcher> WaitForRequest(const GURL& url);
172 net::FakeURLFetcherFactory& factory() { return factory_; }
174 private:
175 scoped_ptr<net::FakeURLFetcher> CreateFakeURLFetcher(
176 const GURL& url,
177 net::URLFetcherDelegate* fetcher_delegate,
178 const std::string& response_data,
179 net::HttpStatusCode response_code,
180 net::URLRequestStatus::Status status);
182 MOCK_METHOD0(Callback, void(void));
184 base::MessageLoopForIO loop_;
185 content::TestBrowserThread ui_thread_;
186 content::TestBrowserThread io_thread_;
187 URLFetcherRequestListener request_listener_;
188 net::FakeURLFetcherFactory factory_;
191 ConfigParserTest::ConfigParserTest()
192 : ui_thread_(content::BrowserThread::UI, &loop_),
193 io_thread_(content::BrowserThread::IO, &loop_),
194 factory_(NULL, base::Bind(&ConfigParserTest::CreateFakeURLFetcher,
195 base::Unretained(this))) {
198 ConfigParserTest::~ConfigParserTest() {}
200 scoped_ptr<BrandcodeConfigFetcher> ConfigParserTest::WaitForRequest(
201 const GURL& url) {
202 EXPECT_CALL(*this, Callback());
203 scoped_ptr<BrandcodeConfigFetcher> fetcher(
204 new BrandcodeConfigFetcher(base::Bind(&ConfigParserTest::Callback,
205 base::Unretained(this)),
206 url,
207 "ABCD"));
208 base::MessageLoop::current()->RunUntilIdle();
209 EXPECT_FALSE(fetcher->IsActive());
210 // Look for the brand code in the request.
211 EXPECT_NE(std::string::npos, request_listener_.upload_data.find("ABCD"));
212 return fetcher.Pass();
215 scoped_ptr<net::FakeURLFetcher> ConfigParserTest::CreateFakeURLFetcher(
216 const GURL& url,
217 net::URLFetcherDelegate* fetcher_delegate,
218 const std::string& response_data,
219 net::HttpStatusCode response_code,
220 net::URLRequestStatus::Status status) {
221 request_listener_.real_delegate = fetcher_delegate;
222 scoped_ptr<net::FakeURLFetcher> fetcher(
223 new net::FakeURLFetcher(
224 url, &request_listener_, response_data, response_code, status));
225 scoped_refptr<net::HttpResponseHeaders> download_headers =
226 new net::HttpResponseHeaders("");
227 download_headers->AddHeader("Content-Type: text/xml");
228 fetcher->set_response_headers(download_headers);
229 return fetcher.Pass();
233 // helper functions -----------------------------------------------------------
235 scoped_refptr<Extension> CreateExtension(const base::string16& name,
236 const base::FilePath& path,
237 Manifest::Location location,
238 extensions::Manifest::Type type,
239 bool installed_by_default) {
240 base::DictionaryValue manifest;
241 manifest.SetString(extensions::manifest_keys::kVersion, "1.0.0.0");
242 manifest.SetString(extensions::manifest_keys::kName, name);
243 switch (type) {
244 case extensions::Manifest::TYPE_THEME:
245 manifest.Set(extensions::manifest_keys::kTheme,
246 new base::DictionaryValue);
247 break;
248 case extensions::Manifest::TYPE_HOSTED_APP:
249 manifest.SetString(extensions::manifest_keys::kLaunchWebURL,
250 "http://www.google.com");
251 manifest.SetString(extensions::manifest_keys::kUpdateURL,
252 "http://clients2.google.com/service/update2/crx");
253 break;
254 case extensions::Manifest::TYPE_EXTENSION:
255 // do nothing
256 break;
257 default:
258 NOTREACHED();
260 manifest.SetString(extensions::manifest_keys::kOmniboxKeyword, name);
261 std::string error;
262 scoped_refptr<Extension> extension = Extension::Create(
263 path,
264 location,
265 manifest,
266 installed_by_default ? Extension::WAS_INSTALLED_BY_DEFAULT
267 : Extension::NO_FLAGS,
268 &error);
269 EXPECT_TRUE(extension.get() != NULL) << error;
270 return extension;
273 void ReplaceString(std::string* str,
274 const std::string& placeholder,
275 const std::string& substitution) {
276 ASSERT_NE(static_cast<std::string*>(NULL), str);
277 size_t placeholder_pos = str->find(placeholder);
278 ASSERT_NE(std::string::npos, placeholder_pos);
279 str->replace(placeholder_pos, placeholder.size(), substitution);
283 /********************* Tests *********************/
285 TEST_F(ProfileResetterTest, ResetNothing) {
286 // The callback should be called even if there is nothing to reset.
287 ResetAndWait(0);
290 TEST_F(ProfileResetterTest, ResetDefaultSearchEngineNonOrganic) {
291 PrefService* prefs = profile()->GetPrefs();
292 DCHECK(prefs);
293 prefs->SetString(prefs::kLastPromptedGoogleURL, "http://www.foo.com/");
295 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE, kDistributionConfig);
297 TemplateURLService* model =
298 TemplateURLServiceFactory::GetForProfile(profile());
299 TemplateURL* default_engine = model->GetDefaultSearchProvider();
300 ASSERT_NE(static_cast<TemplateURL*>(NULL), default_engine);
301 EXPECT_EQ(ASCIIToUTF16("first"), default_engine->short_name());
302 EXPECT_EQ(ASCIIToUTF16("firstkey"), default_engine->keyword());
303 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", default_engine->url());
305 EXPECT_EQ("", prefs->GetString(prefs::kLastPromptedGoogleURL));
308 TEST_F(ProfileResetterTest, ResetDefaultSearchEnginePartially) {
309 // Search engine's logic is tested by
310 // TemplateURLServiceTest.RepairPrepopulatedSearchEngines.
311 PrefService* prefs = profile()->GetPrefs();
312 DCHECK(prefs);
313 prefs->SetString(prefs::kLastPromptedGoogleURL, "http://www.foo.com/");
315 // Make sure TemplateURLService has loaded.
316 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE);
318 TemplateURLService* model =
319 TemplateURLServiceFactory::GetForProfile(profile());
320 TemplateURLService::TemplateURLVector urls = model->GetTemplateURLs();
322 // The second call should produce no effect.
323 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE);
325 EXPECT_EQ(urls, model->GetTemplateURLs());
326 EXPECT_EQ(std::string(), prefs->GetString(prefs::kLastPromptedGoogleURL));
329 TEST_F(ProfileResetterTest, ResetHomepageNonOrganic) {
330 PrefService* prefs = profile()->GetPrefs();
331 DCHECK(prefs);
332 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, true);
333 prefs->SetString(prefs::kHomePage, "http://google.com");
334 prefs->SetBoolean(prefs::kShowHomeButton, false);
336 ResetAndWait(ProfileResetter::HOMEPAGE, kDistributionConfig);
338 EXPECT_FALSE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
339 EXPECT_EQ("http://www.foo.com", prefs->GetString(prefs::kHomePage));
340 EXPECT_TRUE(prefs->GetBoolean(prefs::kShowHomeButton));
343 TEST_F(ProfileResetterTest, ResetHomepagePartially) {
344 PrefService* prefs = profile()->GetPrefs();
345 DCHECK(prefs);
346 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
347 prefs->SetString(prefs::kHomePage, "http://www.foo.com");
348 prefs->SetBoolean(prefs::kShowHomeButton, true);
350 ResetAndWait(ProfileResetter::HOMEPAGE);
352 EXPECT_TRUE(prefs->GetBoolean(prefs::kHomePageIsNewTabPage));
353 EXPECT_EQ("http://www.foo.com", prefs->GetString(prefs::kHomePage));
354 EXPECT_FALSE(prefs->GetBoolean(prefs::kShowHomeButton));
357 TEST_F(ProfileResetterTest, ResetContentSettings) {
358 HostContentSettingsMap* host_content_settings_map =
359 profile()->GetHostContentSettingsMap();
360 DesktopNotificationService* notification_service =
361 DesktopNotificationServiceFactory::GetForProfile(profile());
362 ContentSettingsPattern pattern =
363 ContentSettingsPattern::FromString("[*.]example.org");
364 std::map<ContentSettingsType, ContentSetting> default_settings;
366 for (int type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
367 if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
368 notification_service->SetDefaultContentSetting(CONTENT_SETTING_BLOCK);
369 notification_service->GrantPermission(GURL("http://foo.de"));
370 } else if (type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
371 type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT ||
372 type == CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS) {
373 // These types are excluded because one can't call
374 // GetDefaultContentSetting() for them.
375 } else {
376 ContentSettingsType content_type = static_cast<ContentSettingsType>(type);
377 ContentSetting default_setting =
378 host_content_settings_map->GetDefaultContentSetting(content_type,
379 NULL);
380 default_settings[content_type] = default_setting;
381 ContentSetting wildcard_setting =
382 default_setting == CONTENT_SETTING_BLOCK ? CONTENT_SETTING_ALLOW
383 : CONTENT_SETTING_BLOCK;
384 ContentSetting site_setting =
385 default_setting == CONTENT_SETTING_ALLOW ? CONTENT_SETTING_ALLOW
386 : CONTENT_SETTING_BLOCK;
387 if (HostContentSettingsMap::IsSettingAllowedForType(
388 profile()->GetPrefs(),
389 wildcard_setting,
390 content_type)) {
391 host_content_settings_map->SetDefaultContentSetting(
392 content_type,
393 wildcard_setting);
395 if (!HostContentSettingsMap::ContentTypeHasCompoundValue(content_type) &&
396 HostContentSettingsMap::IsSettingAllowedForType(
397 profile()->GetPrefs(),
398 site_setting,
399 content_type)) {
400 host_content_settings_map->SetContentSetting(
401 pattern,
402 ContentSettingsPattern::Wildcard(),
403 content_type,
404 std::string(),
405 site_setting);
406 ContentSettingsForOneType host_settings;
407 host_content_settings_map->GetSettingsForOneType(
408 content_type, std::string(), &host_settings);
409 EXPECT_EQ(2U, host_settings.size());
414 ResetAndWait(ProfileResetter::CONTENT_SETTINGS);
416 for (int type = 0; type < CONTENT_SETTINGS_NUM_TYPES; ++type) {
417 if (type == CONTENT_SETTINGS_TYPE_NOTIFICATIONS) {
418 EXPECT_EQ(CONTENT_SETTING_ASK,
419 notification_service->GetDefaultContentSetting(NULL));
420 EXPECT_EQ(CONTENT_SETTING_ASK,
421 notification_service->GetContentSetting(GURL("http://foo.de")));
422 } else {
423 ContentSettingsType content_type = static_cast<ContentSettingsType>(type);
424 if (HostContentSettingsMap::ContentTypeHasCompoundValue(content_type) ||
425 type == CONTENT_SETTINGS_TYPE_AUTO_SELECT_CERTIFICATE ||
426 content_type == CONTENT_SETTINGS_TYPE_MIXEDSCRIPT ||
427 content_type == CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS)
428 continue;
429 ContentSetting default_setting =
430 host_content_settings_map->GetDefaultContentSetting(content_type,
431 NULL);
432 EXPECT_TRUE(default_settings.count(content_type));
433 EXPECT_EQ(default_settings[content_type], default_setting);
434 if (!HostContentSettingsMap::ContentTypeHasCompoundValue(content_type)) {
435 ContentSetting site_setting =
436 host_content_settings_map->GetContentSetting(
437 GURL("example.org"),
438 GURL(),
439 content_type,
440 std::string());
441 EXPECT_EQ(default_setting, site_setting);
444 ContentSettingsForOneType host_settings;
445 host_content_settings_map->GetSettingsForOneType(
446 content_type, std::string(), &host_settings);
447 EXPECT_EQ(1U, host_settings.size());
452 TEST_F(ProfileResetterTest, ResetExtensionsByDisabling) {
453 service_->Init();
455 base::ScopedTempDir temp_dir;
456 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
458 scoped_refptr<Extension> theme =
459 CreateExtension(ASCIIToUTF16("example1"),
460 temp_dir.path(),
461 Manifest::INVALID_LOCATION,
462 extensions::Manifest::TYPE_THEME,
463 false);
464 service_->FinishInstallationForTest(theme.get());
465 // Let ThemeService finish creating the theme pack.
466 base::MessageLoop::current()->RunUntilIdle();
468 ThemeService* theme_service =
469 ThemeServiceFactory::GetForProfile(profile());
470 EXPECT_FALSE(theme_service->UsingDefaultTheme());
472 scoped_refptr<Extension> ext2 = CreateExtension(
473 ASCIIToUTF16("example2"),
474 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
475 Manifest::INVALID_LOCATION,
476 extensions::Manifest::TYPE_EXTENSION,
477 false);
478 service_->AddExtension(ext2.get());
479 // Component extensions and policy-managed extensions shouldn't be disabled.
480 scoped_refptr<Extension> ext3 = CreateExtension(
481 ASCIIToUTF16("example3"),
482 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
483 Manifest::COMPONENT,
484 extensions::Manifest::TYPE_EXTENSION,
485 false);
486 service_->AddExtension(ext3.get());
487 scoped_refptr<Extension> ext4 = CreateExtension(
488 ASCIIToUTF16("example4"),
489 base::FilePath(FILE_PATH_LITERAL("//nonexistent3")),
490 Manifest::EXTERNAL_POLICY_DOWNLOAD,
491 extensions::Manifest::TYPE_EXTENSION,
492 false);
493 service_->AddExtension(ext4.get());
494 scoped_refptr<Extension> ext5 = CreateExtension(
495 ASCIIToUTF16("example5"),
496 base::FilePath(FILE_PATH_LITERAL("//nonexistent4")),
497 Manifest::EXTERNAL_COMPONENT,
498 extensions::Manifest::TYPE_EXTENSION,
499 false);
500 service_->AddExtension(ext5.get());
501 scoped_refptr<Extension> ext6 = CreateExtension(
502 ASCIIToUTF16("example6"),
503 base::FilePath(FILE_PATH_LITERAL("//nonexistent5")),
504 Manifest::EXTERNAL_POLICY,
505 extensions::Manifest::TYPE_EXTENSION,
506 false);
507 service_->AddExtension(ext6.get());
508 EXPECT_EQ(6u, service_->extensions()->size());
510 ResetAndWait(ProfileResetter::EXTENSIONS);
511 EXPECT_EQ(4u, service_->extensions()->size());
512 EXPECT_FALSE(service_->extensions()->Contains(theme->id()));
513 EXPECT_FALSE(service_->extensions()->Contains(ext2->id()));
514 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
515 EXPECT_TRUE(service_->extensions()->Contains(ext4->id()));
516 EXPECT_TRUE(service_->extensions()->Contains(ext5->id()));
517 EXPECT_TRUE(service_->extensions()->Contains(ext6->id()));
518 EXPECT_TRUE(theme_service->UsingDefaultTheme());
521 TEST_F(ProfileResetterTest, ResetExtensionsByDisablingNonOrganic) {
522 scoped_refptr<Extension> ext2 = CreateExtension(
523 ASCIIToUTF16("example2"),
524 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
525 Manifest::INVALID_LOCATION,
526 extensions::Manifest::TYPE_EXTENSION,
527 false);
528 service_->AddExtension(ext2.get());
529 // Components and external policy extensions shouldn't be deleted.
530 scoped_refptr<Extension> ext3 = CreateExtension(
531 ASCIIToUTF16("example3"),
532 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
533 Manifest::INVALID_LOCATION,
534 extensions::Manifest::TYPE_EXTENSION,
535 false);
536 service_->AddExtension(ext3.get());
537 EXPECT_EQ(2u, service_->extensions()->size());
539 std::string master_prefs(kDistributionConfig);
540 ReplaceString(&master_prefs, "placeholder_for_id", ext3->id());
542 ResetAndWait(ProfileResetter::EXTENSIONS, master_prefs);
544 EXPECT_EQ(1u, service_->extensions()->size());
545 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
548 TEST_F(ProfileResetterTest, ResetExtensionsAndDefaultApps) {
549 service_->Init();
551 base::ScopedTempDir temp_dir;
552 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
554 scoped_refptr<Extension> ext1 =
555 CreateExtension(ASCIIToUTF16("example1"),
556 temp_dir.path(),
557 Manifest::INVALID_LOCATION,
558 extensions::Manifest::TYPE_THEME,
559 false);
560 service_->FinishInstallationForTest(ext1.get());
561 // Let ThemeService finish creating the theme pack.
562 base::MessageLoop::current()->RunUntilIdle();
564 ThemeService* theme_service =
565 ThemeServiceFactory::GetForProfile(profile());
566 EXPECT_FALSE(theme_service->UsingDefaultTheme());
568 scoped_refptr<Extension> ext2 =
569 CreateExtension(ASCIIToUTF16("example2"),
570 base::FilePath(FILE_PATH_LITERAL("//nonexistent2")),
571 Manifest::INVALID_LOCATION,
572 extensions::Manifest::TYPE_EXTENSION,
573 false);
574 service_->AddExtension(ext2.get());
576 scoped_refptr<Extension> ext3 =
577 CreateExtension(ASCIIToUTF16("example2"),
578 base::FilePath(FILE_PATH_LITERAL("//nonexistent3")),
579 Manifest::INVALID_LOCATION,
580 extensions::Manifest::TYPE_HOSTED_APP,
581 true);
582 service_->AddExtension(ext3.get());
583 EXPECT_EQ(3u, service_->extensions()->size());
585 ResetAndWait(ProfileResetter::EXTENSIONS);
587 EXPECT_EQ(1u, service_->extensions()->size());
588 EXPECT_FALSE(service_->extensions()->Contains(ext1->id()));
589 EXPECT_FALSE(service_->extensions()->Contains(ext2->id()));
590 EXPECT_TRUE(service_->extensions()->Contains(ext3->id()));
591 EXPECT_TRUE(theme_service->UsingDefaultTheme());
594 TEST_F(ProfileResetterTest, ResetStartPageNonOrganic) {
595 PrefService* prefs = profile()->GetPrefs();
596 DCHECK(prefs);
598 SessionStartupPref startup_pref(SessionStartupPref::LAST);
599 SessionStartupPref::SetStartupPref(prefs, startup_pref);
601 ResetAndWait(ProfileResetter::STARTUP_PAGES, kDistributionConfig);
603 startup_pref = SessionStartupPref::GetStartupPref(prefs);
604 EXPECT_EQ(SessionStartupPref::URLS, startup_pref.type);
605 const GURL urls[] = {GURL("http://goo.gl"), GURL("http://foo.de")};
606 EXPECT_EQ(std::vector<GURL>(urls, urls + arraysize(urls)), startup_pref.urls);
610 TEST_F(ProfileResetterTest, ResetStartPagePartially) {
611 PrefService* prefs = profile()->GetPrefs();
612 DCHECK(prefs);
614 const GURL urls[] = {GURL("http://foo"), GURL("http://bar")};
615 SessionStartupPref startup_pref(SessionStartupPref::URLS);
616 startup_pref.urls.assign(urls, urls + arraysize(urls));
617 SessionStartupPref::SetStartupPref(prefs, startup_pref);
619 ResetAndWait(ProfileResetter::STARTUP_PAGES, std::string());
621 startup_pref = SessionStartupPref::GetStartupPref(prefs);
622 EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(), startup_pref.type);
623 EXPECT_EQ(std::vector<GURL>(urls, urls + arraysize(urls)), startup_pref.urls);
626 TEST_F(PinnedTabsResetTest, ResetPinnedTabs) {
627 scoped_refptr<Extension> extension_app = CreateExtension(
628 ASCIIToUTF16("hello!"),
629 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
630 Manifest::INVALID_LOCATION,
631 extensions::Manifest::TYPE_HOSTED_APP,
632 false);
633 scoped_ptr<content::WebContents> contents1(CreateWebContents());
634 extensions::TabHelper::CreateForWebContents(contents1.get());
635 extensions::TabHelper::FromWebContents(contents1.get())->
636 SetExtensionApp(extension_app.get());
637 scoped_ptr<content::WebContents> contents2(CreateWebContents());
638 scoped_ptr<content::WebContents> contents3(CreateWebContents());
639 scoped_ptr<content::WebContents> contents4(CreateWebContents());
640 TabStripModel* tab_strip_model = browser()->tab_strip_model();
642 tab_strip_model->AppendWebContents(contents4.get(), true);
643 tab_strip_model->AppendWebContents(contents3.get(), true);
644 tab_strip_model->AppendWebContents(contents2.get(), true);
645 tab_strip_model->SetTabPinned(2, true);
646 tab_strip_model->AppendWebContents(contents1.get(), true);
647 tab_strip_model->SetTabPinned(3, true);
649 EXPECT_EQ(contents2, tab_strip_model->GetWebContentsAt(0));
650 EXPECT_EQ(contents1, tab_strip_model->GetWebContentsAt(1));
651 EXPECT_EQ(contents3, tab_strip_model->GetWebContentsAt(2));
652 EXPECT_EQ(contents4, tab_strip_model->GetWebContentsAt(3));
653 EXPECT_EQ(3, tab_strip_model->IndexOfFirstNonMiniTab());
655 ResetAndWait(ProfileResetter::PINNED_TABS);
657 EXPECT_EQ(contents1, tab_strip_model->GetWebContentsAt(0));
658 EXPECT_EQ(contents2, tab_strip_model->GetWebContentsAt(1));
659 EXPECT_EQ(contents3, tab_strip_model->GetWebContentsAt(2));
660 EXPECT_EQ(contents4, tab_strip_model->GetWebContentsAt(3));
661 EXPECT_EQ(1, tab_strip_model->IndexOfFirstNonMiniTab());
664 TEST_F(ProfileResetterTest, ResetFewFlags) {
665 // mock_object_ is a StrictMock, so we verify that it is called only once.
666 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
667 ProfileResetter::HOMEPAGE |
668 ProfileResetter::CONTENT_SETTINGS);
671 // Tries to load unavailable config file.
672 TEST_F(ConfigParserTest, NoConnectivity) {
673 const GURL url("http://test");
674 factory().SetFakeResponse(url, "", net::HTTP_INTERNAL_SERVER_ERROR,
675 net::URLRequestStatus::FAILED);
677 scoped_ptr<BrandcodeConfigFetcher> fetcher = WaitForRequest(GURL(url));
678 EXPECT_FALSE(fetcher->GetSettings());
681 // Tries to load available config file.
682 TEST_F(ConfigParserTest, ParseConfig) {
683 const GURL url("http://test");
684 std::string xml_config(kXmlConfig);
685 ReplaceString(&xml_config, "placeholder_for_data", kDistributionConfig);
686 ReplaceString(&xml_config,
687 "placeholder_for_id",
688 "abbaabbaabbaabbaabbaabbaabbaabba");
689 factory().SetFakeResponse(url, xml_config, net::HTTP_OK,
690 net::URLRequestStatus::SUCCESS);
692 scoped_ptr<BrandcodeConfigFetcher> fetcher = WaitForRequest(GURL(url));
693 scoped_ptr<BrandcodedDefaultSettings> settings = fetcher->GetSettings();
694 ASSERT_TRUE(settings);
696 std::vector<std::string> extension_ids;
697 EXPECT_TRUE(settings->GetExtensions(&extension_ids));
698 EXPECT_EQ(1u, extension_ids.size());
699 EXPECT_EQ("abbaabbaabbaabbaabbaabbaabbaabba", extension_ids[0]);
701 std::string homepage;
702 EXPECT_TRUE(settings->GetHomepage(&homepage));
703 EXPECT_EQ("http://www.foo.com", homepage);
705 scoped_ptr<base::ListValue> startup_list(
706 settings->GetUrlsToRestoreOnStartup());
707 EXPECT_TRUE(startup_list);
708 std::vector<std::string> startup_pages;
709 for (base::ListValue::iterator i = startup_list->begin();
710 i != startup_list->end(); ++i) {
711 std::string url;
712 EXPECT_TRUE((*i)->GetAsString(&url));
713 startup_pages.push_back(url);
715 ASSERT_EQ(2u, startup_pages.size());
716 EXPECT_EQ("http://goo.gl", startup_pages[0]);
717 EXPECT_EQ("http://foo.de", startup_pages[1]);
720 TEST_F(ProfileResetterTest, CheckSnapshots) {
721 ResettableSettingsSnapshot empty_snap(profile());
722 EXPECT_EQ(0, empty_snap.FindDifferentFields(empty_snap));
724 scoped_refptr<Extension> ext = CreateExtension(
725 ASCIIToUTF16("example"),
726 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
727 Manifest::INVALID_LOCATION,
728 extensions::Manifest::TYPE_EXTENSION,
729 false);
730 ASSERT_TRUE(ext);
731 service_->AddExtension(ext.get());
733 std::string master_prefs(kDistributionConfig);
734 std::string ext_id = ext->id();
735 ReplaceString(&master_prefs, "placeholder_for_id", ext_id);
737 // Reset to non organic defaults.
738 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
739 ProfileResetter::HOMEPAGE |
740 ProfileResetter::STARTUP_PAGES,
741 master_prefs);
743 ResettableSettingsSnapshot nonorganic_snap(profile());
744 EXPECT_EQ(ResettableSettingsSnapshot::ALL_FIELDS,
745 empty_snap.FindDifferentFields(nonorganic_snap));
746 empty_snap.Subtract(nonorganic_snap);
747 EXPECT_TRUE(empty_snap.startup_urls().empty());
748 EXPECT_EQ(SessionStartupPref::GetDefaultStartupType(),
749 empty_snap.startup_type());
750 EXPECT_TRUE(empty_snap.homepage().empty());
751 EXPECT_TRUE(empty_snap.homepage_is_ntp());
752 EXPECT_NE(std::string::npos, empty_snap.dse_url().find("{google:baseURL}"));
753 EXPECT_EQ(ResettableSettingsSnapshot::ExtensionList(),
754 empty_snap.enabled_extensions());
756 // Reset to organic defaults.
757 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
758 ProfileResetter::HOMEPAGE |
759 ProfileResetter::STARTUP_PAGES |
760 ProfileResetter::EXTENSIONS);
762 ResettableSettingsSnapshot organic_snap(profile());
763 EXPECT_EQ(ResettableSettingsSnapshot::ALL_FIELDS,
764 nonorganic_snap.FindDifferentFields(organic_snap));
765 nonorganic_snap.Subtract(organic_snap);
766 const GURL urls[] = {GURL("http://foo.de"), GURL("http://goo.gl")};
767 EXPECT_EQ(std::vector<GURL>(urls, urls + arraysize(urls)),
768 nonorganic_snap.startup_urls());
769 EXPECT_EQ(SessionStartupPref::URLS, nonorganic_snap.startup_type());
770 EXPECT_EQ("http://www.foo.com", nonorganic_snap.homepage());
771 EXPECT_FALSE(nonorganic_snap.homepage_is_ntp());
772 EXPECT_EQ("http://www.foo.com/s?q={searchTerms}", nonorganic_snap.dse_url());
773 EXPECT_EQ(ResettableSettingsSnapshot::ExtensionList(
774 1, std::make_pair(ext_id, "example")),
775 nonorganic_snap.enabled_extensions());
778 TEST_F(ProfileResetterTest, FeedbackSerializtionTest) {
779 // Reset to non organic defaults.
780 ResetAndWait(ProfileResetter::DEFAULT_SEARCH_ENGINE |
781 ProfileResetter::HOMEPAGE |
782 ProfileResetter::STARTUP_PAGES,
783 kDistributionConfig);
785 scoped_refptr<Extension> ext = CreateExtension(
786 ASCIIToUTF16("example"),
787 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
788 Manifest::INVALID_LOCATION,
789 extensions::Manifest::TYPE_EXTENSION,
790 false);
791 ASSERT_TRUE(ext);
792 service_->AddExtension(ext.get());
794 const ResettableSettingsSnapshot nonorganic_snap(profile());
796 COMPILE_ASSERT(ResettableSettingsSnapshot::ALL_FIELDS == 15,
797 expand_this_test);
798 for (int field_mask = 0; field_mask <= ResettableSettingsSnapshot::ALL_FIELDS;
799 ++field_mask) {
800 std::string report = SerializeSettingsReport(nonorganic_snap, field_mask);
801 JSONStringValueSerializer json(report);
802 std::string error;
803 scoped_ptr<base::Value> root(json.Deserialize(NULL, &error));
804 ASSERT_TRUE(root) << error;
805 ASSERT_TRUE(root->IsType(base::Value::TYPE_DICTIONARY)) << error;
807 base::DictionaryValue* dict =
808 static_cast<base::DictionaryValue*>(root.get());
810 base::ListValue* startup_urls = NULL;
811 int startup_type = 0;
812 std::string homepage;
813 bool homepage_is_ntp = true;
814 std::string default_search_engine;
815 base::ListValue* extensions;
817 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::STARTUP_MODE),
818 dict->GetList("startup_urls", &startup_urls));
819 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::STARTUP_MODE),
820 dict->GetInteger("startup_type", &startup_type));
821 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::HOMEPAGE),
822 dict->GetString("homepage", &homepage));
823 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::HOMEPAGE),
824 dict->GetBoolean("homepage_is_ntp", &homepage_is_ntp));
825 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::DSE_URL),
826 dict->GetString("default_search_engine", &default_search_engine));
827 EXPECT_EQ(!!(field_mask & ResettableSettingsSnapshot::EXTENSIONS),
828 dict->GetList("enabled_extensions", &extensions));
832 // Make sure GetReadableFeedback handles non-ascii letters.
833 TEST_F(ProfileResetterTest, GetReadableFeedback) {
834 scoped_refptr<Extension> ext = CreateExtension(
835 base::WideToUTF16(L"Tiësto"),
836 base::FilePath(FILE_PATH_LITERAL("//nonexistent")),
837 Manifest::INVALID_LOCATION,
838 extensions::Manifest::TYPE_EXTENSION,
839 false);
840 ASSERT_TRUE(ext);
841 service_->AddExtension(ext.get());
843 PrefService* prefs = profile()->GetPrefs();
844 DCHECK(prefs);
845 // The URL is "http://россия.рф".
846 std::wstring url(L"http://"
847 L"\u0440\u043e\u0441\u0441\u0438\u044f.\u0440\u0444");
848 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, false);
849 prefs->SetString(prefs::kHomePage, base::WideToUTF8(url));
851 SessionStartupPref startup_pref(SessionStartupPref::URLS);
852 startup_pref.urls.push_back(GURL(base::WideToUTF8(url)));
853 SessionStartupPref::SetStartupPref(prefs, startup_pref);
855 // The homepage and the startup page are in punycode. They are unreadable.
856 // Trying to find the extension name.
857 scoped_ptr<base::ListValue> list(GetReadableFeedback(profile()));
858 ASSERT_TRUE(list);
859 for (size_t i = 0; i < list->GetSize(); ++i) {
860 base::DictionaryValue* dict = NULL;
861 ASSERT_TRUE(list->GetDictionary(i, &dict));
862 std::string value;
863 ASSERT_TRUE(dict->GetString("key", &value));
864 if (value == "Extensions") {
865 base::string16 extensions;
866 EXPECT_TRUE(dict->GetString("value", &extensions));
867 EXPECT_EQ(base::WideToUTF16(L"Tiësto"), extensions);
872 } // namespace