1 // Copyright (c) 2012 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.
7 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/search_engines/search_provider_install_data.h"
16 #include "chrome/browser/search_engines/template_url.h"
17 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
18 #include "chrome/browser/search_engines/template_url_service.h"
19 #include "chrome/browser/search_engines/template_url_service_test_util.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/notification_source.h"
25 #include "content/public/test/mock_render_process_host.h"
26 #include "content/public/test/test_browser_thread.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using content::BrowserThread
;
33 // TestGetInstallState --------------------------------------------------------
35 // Test the SearchProviderInstallData::GetInstallState.
36 class TestGetInstallState
{
38 explicit TestGetInstallState(SearchProviderInstallData
* install_data
);
40 // Runs all of the test cases.
41 void RunTests(const std::string
& search_provider_host
,
42 const std::string
& default_search_provider_host
);
45 // Callback for when SearchProviderInstallData is ready to have
46 // GetInstallState called. Runs all of the test cases.
47 void DoInstallStateTests(const std::string
& search_provider_host
,
48 const std::string
& default_search_provider_host
);
50 // Does a verification for one url and its expected state.
51 void VerifyInstallState(SearchProviderInstallData::State expected_state
,
52 const std::string
& url
);
54 SearchProviderInstallData
* install_data_
;
56 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState
);
59 TestGetInstallState::TestGetInstallState(
60 SearchProviderInstallData
* install_data
)
61 : install_data_(install_data
) {
64 void TestGetInstallState::RunTests(
65 const std::string
& search_provider_host
,
66 const std::string
& default_search_provider_host
) {
67 install_data_
->CallWhenLoaded(
68 base::Bind(&TestGetInstallState::DoInstallStateTests
,
69 base::Unretained(this),
70 search_provider_host
, default_search_provider_host
));
71 base::RunLoop().RunUntilIdle();
74 void TestGetInstallState::DoInstallStateTests(
75 const std::string
& search_provider_host
,
76 const std::string
& default_search_provider_host
) {
77 SCOPED_TRACE("search provider: " + search_provider_host
+
78 ", default search provider: " + default_search_provider_host
);
79 // Installed but not default.
80 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT
,
81 "http://" + search_provider_host
+ "/");
82 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT
,
83 "http://" + search_provider_host
+ ":80/");
86 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
87 "http://" + search_provider_host
+ ":96/");
89 // Not installed due to different scheme.
90 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
91 "https://" + search_provider_host
+ "/");
94 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
95 "http://a" + search_provider_host
+ "/");
97 // Installed as default.
98 if (!default_search_provider_host
.empty()) {
99 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT
,
100 "http://" + default_search_provider_host
+ "/");
104 void TestGetInstallState::VerifyInstallState(
105 SearchProviderInstallData::State expected_state
,
106 const std::string
& url
) {
108 SearchProviderInstallData::State actual_state
=
109 install_data_
->GetInstallState(GURL(url
));
110 EXPECT_EQ(expected_state
, actual_state
)
111 << "GetInstallState for " << url
<< " failed. Expected "
112 << expected_state
<< ". Actual " << actual_state
<< ".";
117 // SearchProviderInstallDataTest ----------------------------------------------
119 // Provides basic test set-up/tear-down functionality needed by all tests
120 // that use TemplateURLServiceTestUtil.
121 class SearchProviderInstallDataTest
: public testing::Test
{
123 SearchProviderInstallDataTest();
125 virtual void SetUp() OVERRIDE
;
126 virtual void TearDown() OVERRIDE
;
128 void SimulateDefaultSearchIsManaged(const std::string
& url
);
131 TemplateURL
* AddNewTemplateURL(const std::string
& url
,
132 const base::string16
& keyword
);
134 TemplateURLServiceTestUtil util_
;
136 // Provides the search provider install state on the I/O thread. It must be
137 // deleted on the I/O thread, which is why it isn't a scoped_ptr.
138 SearchProviderInstallData
* install_data_
;
140 // A mock RenderProcessHost that the SearchProviderInstallData will scope its
142 scoped_ptr
<content::MockRenderProcessHost
> process_
;
144 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest
);
147 SearchProviderInstallDataTest::SearchProviderInstallDataTest()
148 : install_data_(NULL
) {
151 void SearchProviderInstallDataTest::SetUp() {
152 testing::Test::SetUp();
153 #if defined(OS_ANDROID)
154 TemplateURLPrepopulateData::InitCountryCode(
155 std::string() /* unknown country code */);
158 process_
.reset(new content::MockRenderProcessHost(util_
.profile()));
160 new SearchProviderInstallData(util_
.profile(), process_
.get());
163 void SearchProviderInstallDataTest::TearDown() {
164 BrowserThread::DeleteSoon(BrowserThread::IO
, FROM_HERE
, install_data_
);
165 install_data_
= NULL
;
167 // Make sure that the install data class on the UI thread gets cleaned up.
168 // It doesn't matter that this happens after install_data_ is deleted.
172 testing::Test::TearDown();
175 void SearchProviderInstallDataTest::SimulateDefaultSearchIsManaged(
176 const std::string
& url
) {
177 ASSERT_FALSE(url
.empty());
178 TestingPrefServiceSyncable
* service
=
179 util_
.profile()->GetTestingPrefService();
180 service
->SetManagedPref(prefs::kDefaultSearchProviderEnabled
,
181 base::Value::CreateBooleanValue(true));
182 service
->SetManagedPref(prefs::kDefaultSearchProviderSearchURL
,
183 base::Value::CreateStringValue(url
));
184 service
->SetManagedPref(prefs::kDefaultSearchProviderName
,
185 base::Value::CreateStringValue("managed"));
186 service
->SetManagedPref(prefs::kDefaultSearchProviderKeyword
,
187 new base::StringValue("managed"));
188 // Clear the IDs that are not specified via policy.
189 service
->SetManagedPref(prefs::kDefaultSearchProviderID
,
190 new base::StringValue(std::string()));
191 service
->SetManagedPref(prefs::kDefaultSearchProviderPrepopulateID
,
192 new base::StringValue(std::string()));
193 util_
.model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED
,
194 content::NotificationService::AllSources(),
195 content::NotificationService::NoDetails());
198 TemplateURL
* SearchProviderInstallDataTest::AddNewTemplateURL(
199 const std::string
& url
,
200 const base::string16
& keyword
) {
201 TemplateURLData data
;
202 data
.short_name
= keyword
;
203 data
.SetKeyword(keyword
);
205 TemplateURL
* t_url
= new TemplateURL(util_
.profile(), data
);
206 util_
.model()->Add(t_url
);
210 // Actual tests ---------------------------------------------------------------
212 TEST_F(SearchProviderInstallDataTest
, GetInstallState
) {
213 // Set up the database.
214 util_
.ChangeModelToLoadState();
215 std::string host
= "www.unittest.com";
216 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
218 // Wait for the changes to be saved.
219 base::RunLoop().RunUntilIdle();
221 // Verify the search providers install state (with no default set).
222 TestGetInstallState
test_get_install_state(install_data_
);
223 test_get_install_state
.RunTests(host
, std::string());
225 // Set-up a default and try it all one more time.
226 std::string default_host
= "www.mmm.com";
227 TemplateURL
* default_url
=
228 AddNewTemplateURL("http://" + default_host
+ "/",
229 base::ASCIIToUTF16("mmm"));
230 util_
.model()->SetUserSelectedDefaultSearchProvider(default_url
);
231 test_get_install_state
.RunTests(host
, default_host
);
234 TEST_F(SearchProviderInstallDataTest
, ManagedDefaultSearch
) {
235 // Set up the database.
236 util_
.ChangeModelToLoadState();
237 std::string host
= "www.unittest.com";
238 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
240 // Set a managed preference that establishes a default search provider.
241 std::string host2
= "www.managedtest.com";
242 std::string url2
= "http://" + host2
+ "/p{searchTerms}";
243 SimulateDefaultSearchIsManaged(url2
);
244 EXPECT_TRUE(util_
.model()->is_default_search_managed());
246 // Wait for the changes to be saved.
247 base::RunLoop().RunUntilIdle();
249 // Verify the search providers install state. The default search should be
250 // the managed one we previously set.
251 TestGetInstallState
test_get_install_state(install_data_
);
252 test_get_install_state
.RunTests(host
, host2
);
255 TEST_F(SearchProviderInstallDataTest
, GoogleBaseUrlChange
) {
256 TestGetInstallState
test_get_install_state(install_data_
);
258 // Set up the database.
259 util_
.ChangeModelToLoadState();
260 std::string google_host
= "w.com";
261 util_
.SetGoogleBaseURL(GURL("http://" + google_host
+ "/"));
262 // Wait for the I/O thread to process the update notification.
263 base::RunLoop().RunUntilIdle();
265 AddNewTemplateURL("{google:baseURL}?q={searchTerms}",
266 base::ASCIIToUTF16("t"));
267 TemplateURL
* default_url
=
268 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d"));
269 util_
.model()->SetUserSelectedDefaultSearchProvider(default_url
);
271 // Wait for the changes to be saved.
272 base::RunLoop().RunUntilIdle();
274 // Verify the search providers install state (with no default set).
275 test_get_install_state
.RunTests(google_host
, std::string());
277 // Change the Google base url.
278 google_host
= "foo.com";
279 util_
.SetGoogleBaseURL(GURL("http://" + google_host
+ "/"));
280 // Wait for the I/O thread to process the update notification.
281 base::RunLoop().RunUntilIdle();
283 // Verify that the change got picked up.
284 test_get_install_state
.RunTests(google_host
, std::string());