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/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/search_engines/search_provider_install_data.h"
15 #include "chrome/browser/search_engines/template_url.h"
16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_test_util.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_pref_service_syncable.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_source.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using content::BrowserThread
;
31 // TestGetInstallState --------------------------------------------------------
33 // Test the SearchProviderInstallData::GetInstallState.
34 class TestGetInstallState
{
36 explicit TestGetInstallState(SearchProviderInstallData
* install_data
);
38 // Runs all of the test cases.
39 void RunTests(const std::string
& search_provider_host
,
40 const std::string
& default_search_provider_host
);
43 // Callback for when SearchProviderInstallData is ready to have
44 // GetInstallState called. Runs all of the test cases.
45 void DoInstallStateTests(const std::string
& search_provider_host
,
46 const std::string
& default_search_provider_host
);
48 // Does a verification for one url and its expected state.
49 void VerifyInstallState(SearchProviderInstallData::State expected_state
,
50 const std::string
& url
);
52 SearchProviderInstallData
* install_data_
;
54 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState
);
57 TestGetInstallState::TestGetInstallState(
58 SearchProviderInstallData
* install_data
)
59 : install_data_(install_data
) {
62 void TestGetInstallState::RunTests(
63 const std::string
& search_provider_host
,
64 const std::string
& default_search_provider_host
) {
65 install_data_
->CallWhenLoaded(
66 base::Bind(&TestGetInstallState::DoInstallStateTests
,
67 base::Unretained(this),
68 search_provider_host
, default_search_provider_host
));
69 base::RunLoop().RunUntilIdle();
72 void TestGetInstallState::DoInstallStateTests(
73 const std::string
& search_provider_host
,
74 const std::string
& default_search_provider_host
) {
75 SCOPED_TRACE("search provider: " + search_provider_host
+
76 ", default search provider: " + default_search_provider_host
);
77 // Installed but not default.
78 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT
,
79 "http://" + search_provider_host
+ "/");
80 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT
,
81 "http://" + search_provider_host
+ ":80/");
84 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
85 "http://" + search_provider_host
+ ":96/");
87 // Not installed due to different scheme.
88 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
89 "https://" + search_provider_host
+ "/");
92 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
93 "http://a" + search_provider_host
+ "/");
95 // Installed as default.
96 if (!default_search_provider_host
.empty()) {
97 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT
,
98 "http://" + default_search_provider_host
+ "/");
102 void TestGetInstallState::VerifyInstallState(
103 SearchProviderInstallData::State expected_state
,
104 const std::string
& url
) {
106 SearchProviderInstallData::State actual_state
=
107 install_data_
->GetInstallState(GURL(url
));
108 EXPECT_EQ(expected_state
, actual_state
)
109 << "GetInstallState for " << url
<< " failed. Expected "
110 << expected_state
<< ". Actual " << actual_state
<< ".";
115 // SearchProviderInstallDataTest ----------------------------------------------
117 // Provides basic test set-up/tear-down functionality needed by all tests
118 // that use TemplateURLServiceTestUtil.
119 class SearchProviderInstallDataTest
: public testing::Test
{
121 SearchProviderInstallDataTest();
123 virtual void SetUp() OVERRIDE
;
124 virtual void TearDown() OVERRIDE
;
126 void SimulateDefaultSearchIsManaged(const std::string
& url
);
129 TemplateURL
* AddNewTemplateURL(const std::string
& url
,
130 const base::string16
& keyword
);
132 TemplateURLServiceTestUtil util_
;
134 // Provides the search provider install state on the I/O thread. It must be
135 // deleted on the I/O thread, which is why it isn't a scoped_ptr.
136 SearchProviderInstallData
* install_data_
;
138 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest
);
141 SearchProviderInstallDataTest::SearchProviderInstallDataTest()
142 : install_data_(NULL
) {
145 void SearchProviderInstallDataTest::SetUp() {
146 testing::Test::SetUp();
147 #if defined(OS_ANDROID)
148 TemplateURLPrepopulateData::InitCountryCode(
149 std::string() /* unknown country code */);
152 install_data_
= new SearchProviderInstallData(util_
.profile(),
153 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
154 content::Source
<SearchProviderInstallDataTest
>(this));
157 void SearchProviderInstallDataTest::TearDown() {
158 BrowserThread::DeleteSoon(BrowserThread::IO
, FROM_HERE
, install_data_
);
159 install_data_
= NULL
;
161 // Make sure that the install data class on the UI thread gets cleaned up.
162 // It doesn't matter that this happens after install_data_ is deleted.
163 content::NotificationService::current()->Notify(
164 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED
,
165 content::Source
<SearchProviderInstallDataTest
>(this),
166 content::NotificationService::NoDetails());
169 testing::Test::TearDown();
172 void SearchProviderInstallDataTest::SimulateDefaultSearchIsManaged(
173 const std::string
& url
) {
174 ASSERT_FALSE(url
.empty());
175 TestingPrefServiceSyncable
* service
=
176 util_
.profile()->GetTestingPrefService();
177 service
->SetManagedPref(prefs::kDefaultSearchProviderEnabled
,
178 base::Value::CreateBooleanValue(true));
179 service
->SetManagedPref(prefs::kDefaultSearchProviderSearchURL
,
180 base::Value::CreateStringValue(url
));
181 service
->SetManagedPref(prefs::kDefaultSearchProviderName
,
182 base::Value::CreateStringValue("managed"));
183 // Clear the IDs that are not specified via policy.
184 service
->SetManagedPref(prefs::kDefaultSearchProviderID
,
185 new base::StringValue(std::string()));
186 service
->SetManagedPref(prefs::kDefaultSearchProviderPrepopulateID
,
187 new base::StringValue(std::string()));
188 util_
.model()->Observe(chrome::NOTIFICATION_DEFAULT_SEARCH_POLICY_CHANGED
,
189 content::NotificationService::AllSources(),
190 content::NotificationService::NoDetails());
193 TemplateURL
* SearchProviderInstallDataTest::AddNewTemplateURL(
194 const std::string
& url
,
195 const base::string16
& keyword
) {
196 TemplateURLData data
;
197 data
.short_name
= keyword
;
198 data
.SetKeyword(keyword
);
200 TemplateURL
* t_url
= new TemplateURL(util_
.profile(), data
);
201 util_
.model()->Add(t_url
);
205 // Actual tests ---------------------------------------------------------------
207 TEST_F(SearchProviderInstallDataTest
, GetInstallState
) {
208 // Set up the database.
209 util_
.ChangeModelToLoadState();
210 std::string host
= "www.unittest.com";
211 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
213 // Wait for the changes to be saved.
214 base::RunLoop().RunUntilIdle();
216 // Verify the search providers install state (with no default set).
217 TestGetInstallState
test_get_install_state(install_data_
);
218 test_get_install_state
.RunTests(host
, std::string());
220 // Set-up a default and try it all one more time.
221 std::string default_host
= "www.mmm.com";
222 TemplateURL
* default_url
=
223 AddNewTemplateURL("http://" + default_host
+ "/",
224 base::ASCIIToUTF16("mmm"));
225 util_
.model()->SetDefaultSearchProvider(default_url
);
226 test_get_install_state
.RunTests(host
, default_host
);
229 TEST_F(SearchProviderInstallDataTest
, ManagedDefaultSearch
) {
230 // Set up the database.
231 util_
.ChangeModelToLoadState();
232 std::string host
= "www.unittest.com";
233 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
235 // Set a managed preference that establishes a default search provider.
236 std::string host2
= "www.managedtest.com";
237 std::string url2
= "http://" + host2
+ "/p{searchTerms}";
238 SimulateDefaultSearchIsManaged(url2
);
239 EXPECT_TRUE(util_
.model()->is_default_search_managed());
241 // Wait for the changes to be saved.
242 base::RunLoop().RunUntilIdle();
244 // Verify the search providers install state. The default search should be
245 // the managed one we previously set.
246 TestGetInstallState
test_get_install_state(install_data_
);
247 test_get_install_state
.RunTests(host
, host2
);
250 TEST_F(SearchProviderInstallDataTest
, GoogleBaseUrlChange
) {
251 TestGetInstallState
test_get_install_state(install_data_
);
253 // Set up the database.
254 util_
.ChangeModelToLoadState();
255 std::string google_host
= "w.com";
256 util_
.SetGoogleBaseURL(GURL("http://" + google_host
+ "/"));
257 // Wait for the I/O thread to process the update notification.
258 base::RunLoop().RunUntilIdle();
260 AddNewTemplateURL("{google:baseURL}?q={searchTerms}",
261 base::ASCIIToUTF16("t"));
262 TemplateURL
* default_url
=
263 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d"));
264 util_
.model()->SetDefaultSearchProvider(default_url
);
266 // Wait for the changes to be saved.
267 base::RunLoop().RunUntilIdle();
269 // Verify the search providers install state (with no default set).
270 test_get_install_state
.RunTests(google_host
, std::string());
272 // Change the Google base url.
273 google_host
= "foo.com";
274 util_
.SetGoogleBaseURL(GURL("http://" + google_host
+ "/"));
275 // Wait for the I/O thread to process the update notification.
276 base::RunLoop().RunUntilIdle();
278 // Verify that the change got picked up.
279 test_get_install_state
.RunTests(google_host
, std::string());