1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/prefs/pref_service.h"
6 #include "base/run_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_browsertest.h"
9 #include "chrome/browser/prefs/session_startup_pref.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/search_engines/template_url.h"
14 #include "components/search_engines/template_url_service.h"
18 class TemplateURLServiceObserver
{
20 TemplateURLServiceObserver(TemplateURLService
* service
,
24 template_url_sub_
= service
->RegisterOnLoadedCallback(
25 base::Bind(&TemplateURLServiceObserver::StopLoop
,
26 base::Unretained(this)));
29 ~TemplateURLServiceObserver() {}
35 base::RunLoop
* runner_
;
36 scoped_ptr
<TemplateURLService::Subscription
> template_url_sub_
;
38 DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceObserver
);
41 testing::AssertionResult
VerifyTemplateURLServiceLoad(
42 TemplateURLService
* service
) {
43 if (service
->loaded())
44 return testing::AssertionSuccess();
46 TemplateURLServiceObserver
observer(service
, &runner
);
48 if (service
->loaded())
49 return testing::AssertionSuccess();
50 return testing::AssertionFailure() << "TemplateURLService isn't loaded";
53 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest
, OverrideSettings
) {
54 PrefService
* prefs
= profile()->GetPrefs();
56 prefs
->SetString(prefs::kHomePage
, "http://google.com/");
57 prefs
->SetBoolean(prefs::kHomePageIsNewTabPage
, true);
58 const GURL urls
[] = {GURL("http://foo"), GURL("http://bar")};
59 SessionStartupPref
startup_pref(SessionStartupPref::LAST
);
60 startup_pref
.urls
.assign(urls
, urls
+ arraysize(urls
));
61 SessionStartupPref::SetStartupPref(prefs
, startup_pref
);
62 TemplateURLService
* url_service
=
63 TemplateURLServiceFactory::GetForProfile(profile());
64 ASSERT_TRUE(url_service
);
65 EXPECT_TRUE(VerifyTemplateURLServiceLoad(url_service
));
66 TemplateURL
* default_provider
= url_service
->GetDefaultSearchProvider();
67 ASSERT_TRUE(default_provider
);
68 EXPECT_EQ(TemplateURL::NORMAL
, default_provider
->GetType());
71 const extensions::Extension
* extension
= LoadExtensionWithInstallParam(
72 test_data_dir_
.AppendASCII("settings_override"),
73 kFlagEnableFileAccess
,
75 ASSERT_TRUE(extension
);
76 EXPECT_EQ("http://www.homepage.de/?param=10",
77 prefs
->GetString(prefs::kHomePage
));
78 EXPECT_FALSE(prefs
->GetBoolean(prefs::kHomePageIsNewTabPage
));
79 startup_pref
= SessionStartupPref::GetStartupPref(prefs
);
80 EXPECT_EQ(SessionStartupPref::URLS
, startup_pref
.type
);
81 EXPECT_EQ(std::vector
<GURL
>(1, GURL("http://www.startup.de/?param=10")),
83 TemplateURL
* extension_provider
= url_service
->GetDefaultSearchProvider();
84 EXPECT_EQ(TemplateURL::NORMAL_CONTROLLED_BY_EXTENSION
,
85 extension_provider
->GetType());
86 EXPECT_EQ(base::ASCIIToUTF16("name.de"), extension_provider
->short_name());
87 EXPECT_EQ(base::ASCIIToUTF16("keyword.de"), extension_provider
->keyword());
88 EXPECT_EQ("http://www.foo.de/s?q={searchTerms}&id=10",
89 extension_provider
->url());
90 EXPECT_EQ(GURL("http://www.foo.de/favicon.ico?id=10"),
91 extension_provider
->favicon_url());
92 EXPECT_EQ("http://www.foo.de/suggest?q={searchTerms}&id=10",
93 extension_provider
->suggestions_url());
94 EXPECT_EQ("http://www.foo.de/instant?q={searchTerms}&id=10",
95 extension_provider
->instant_url());
96 EXPECT_EQ("http://www.foo.de/image?q={searchTerms}&id=10",
97 extension_provider
->image_url());
98 EXPECT_EQ("search_lang=de", extension_provider
->search_url_post_params());
99 EXPECT_EQ("suggest_lang=de",
100 extension_provider
->suggestions_url_post_params());
101 EXPECT_EQ("instant_lang=de", extension_provider
->instant_url_post_params());
102 EXPECT_EQ("image_lang=de", extension_provider
->image_url_post_params());
103 const std::string alternate_urls
[] = {
104 "http://www.moo.de/s?q={searchTerms}&id=10",
105 "http://www.noo.de/s?q={searchTerms}&id=10"};
106 EXPECT_EQ(std::vector
<std::string
>(
107 alternate_urls
, alternate_urls
+ arraysize(alternate_urls
)),
108 extension_provider
->alternate_urls());
109 EXPECT_EQ(std::vector
<std::string
>(1, "UTF-8"),
110 extension_provider
->input_encodings());
111 EXPECT_EQ(default_provider
->new_tab_url(), extension_provider
->new_tab_url());
113 UnloadExtension(extension
->id());
114 EXPECT_EQ("http://google.com/", prefs
->GetString(prefs::kHomePage
));
115 EXPECT_TRUE(prefs
->GetBoolean(prefs::kHomePageIsNewTabPage
));
116 startup_pref
= SessionStartupPref::GetStartupPref(prefs
);
117 EXPECT_EQ(SessionStartupPref::LAST
, startup_pref
.type
);
118 EXPECT_EQ(std::vector
<GURL
>(urls
, urls
+ arraysize(urls
)), startup_pref
.urls
);
119 EXPECT_EQ(default_provider
, url_service
->GetDefaultSearchProvider());
121 const extensions::Extension
* extension
=
122 LoadExtensionWithFlags(test_data_dir_
.AppendASCII("settings_override"),
123 kFlagIgnoreManifestWarnings
);
124 ASSERT_TRUE(extension
);
125 ASSERT_EQ(1u, extension
->install_warnings().size());
126 EXPECT_EQ(std::string(
127 "'chrome_settings_overrides' "
128 "is not allowed for specified platform."),
129 extension
->install_warnings().front().message
);