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/search_engines/search_provider_install_data.h"
15 #include "chrome/browser/search_engines/template_url_service_test_util.h"
16 #include "chrome/test/base/testing_pref_service_syncable.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/search_engines/search_terms_data.h"
19 #include "components/search_engines/template_url.h"
20 #include "components/search_engines/template_url_prepopulate_data.h"
21 #include "components/search_engines/template_url_service.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/public/test/test_browser_thread.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using content::BrowserThread
;
30 // TestGetInstallState --------------------------------------------------------
32 // Test the SearchProviderInstallData::GetInstallState.
33 class TestGetInstallState
{
35 explicit TestGetInstallState(SearchProviderInstallData
* install_data
);
37 // Runs all of the test cases.
38 void RunTests(const std::string
& search_provider_host
,
39 const std::string
& default_search_provider_host
);
42 // Callback for when SearchProviderInstallData is ready to have
43 // GetInstallState called. Runs all of the test cases.
44 void DoInstallStateTests(const std::string
& search_provider_host
,
45 const std::string
& default_search_provider_host
);
47 // Does a verification for one url and its expected state.
48 void VerifyInstallState(SearchProviderInstallData::State expected_state
,
49 const std::string
& url
);
51 SearchProviderInstallData
* install_data_
;
53 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState
);
56 TestGetInstallState::TestGetInstallState(
57 SearchProviderInstallData
* install_data
)
58 : install_data_(install_data
) {
61 void TestGetInstallState::RunTests(
62 const std::string
& search_provider_host
,
63 const std::string
& default_search_provider_host
) {
64 install_data_
->CallWhenLoaded(
65 base::Bind(&TestGetInstallState::DoInstallStateTests
,
66 base::Unretained(this),
67 search_provider_host
, default_search_provider_host
));
68 base::RunLoop().RunUntilIdle();
71 void TestGetInstallState::DoInstallStateTests(
72 const std::string
& search_provider_host
,
73 const std::string
& default_search_provider_host
) {
74 SCOPED_TRACE("search provider: " + search_provider_host
+
75 ", default search provider: " + default_search_provider_host
);
76 // Installed but not default.
77 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT
,
78 "http://" + search_provider_host
+ "/");
79 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT
,
80 "http://" + search_provider_host
+ ":80/");
83 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
84 "http://" + search_provider_host
+ ":96/");
86 // Not installed due to different scheme.
87 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
88 "https://" + search_provider_host
+ "/");
91 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED
,
92 "http://a" + search_provider_host
+ "/");
94 // Installed as default.
95 if (!default_search_provider_host
.empty()) {
96 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT
,
97 "http://" + default_search_provider_host
+ "/");
101 void TestGetInstallState::VerifyInstallState(
102 SearchProviderInstallData::State expected_state
,
103 const std::string
& url
) {
105 SearchProviderInstallData::State actual_state
=
106 install_data_
->GetInstallState(GURL(url
));
107 EXPECT_EQ(expected_state
, actual_state
)
108 << "GetInstallState for " << url
<< " failed. Expected "
109 << expected_state
<< ". Actual " << actual_state
<< ".";
114 // SearchProviderInstallDataTest ----------------------------------------------
116 // Provides basic test set-up/tear-down functionality needed by all tests
117 // that use TemplateURLServiceTestUtil.
118 class SearchProviderInstallDataTest
: public testing::Test
{
120 SearchProviderInstallDataTest();
122 virtual void SetUp() OVERRIDE
;
123 virtual void TearDown() OVERRIDE
;
126 TemplateURL
* AddNewTemplateURL(const std::string
& url
,
127 const base::string16
& keyword
);
129 // Sets the Google base URL to |base_url| and runs the IO thread for
130 // |SearchProviderInstallData| to process the update.
131 void SetGoogleBaseURLAndProcessOnIOThread(GURL base_url
);
133 TemplateURLServiceTestUtil util_
;
135 // Provides the search provider install state on the I/O thread. It must be
136 // deleted on the I/O thread, which is why it isn't a scoped_ptr.
137 SearchProviderInstallData
* install_data_
;
139 // A mock RenderProcessHost that the SearchProviderInstallData will scope its
141 scoped_ptr
<content::MockRenderProcessHost
> process_
;
143 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest
);
146 SearchProviderInstallDataTest::SearchProviderInstallDataTest()
147 : install_data_(NULL
) {
150 void SearchProviderInstallDataTest::SetUp() {
151 testing::Test::SetUp();
152 #if defined(OS_ANDROID)
153 TemplateURLPrepopulateData::InitCountryCode(
154 std::string() /* unknown country code */);
156 process_
.reset(new content::MockRenderProcessHost(util_
.profile()));
157 install_data_
= new SearchProviderInstallData(
158 util_
.model(), SearchTermsData().GoogleBaseURLValue(), NULL
,
162 void SearchProviderInstallDataTest::TearDown() {
163 BrowserThread::DeleteSoon(BrowserThread::IO
, FROM_HERE
, install_data_
);
164 install_data_
= NULL
;
166 // Make sure that the install data class on the UI thread gets cleaned up.
167 // It doesn't matter that this happens after install_data_ is deleted.
170 testing::Test::TearDown();
173 TemplateURL
* SearchProviderInstallDataTest::AddNewTemplateURL(
174 const std::string
& url
,
175 const base::string16
& keyword
) {
176 TemplateURLData data
;
177 data
.short_name
= keyword
;
178 data
.SetKeyword(keyword
);
180 TemplateURL
* t_url
= new TemplateURL(data
);
181 util_
.model()->Add(t_url
);
185 void SearchProviderInstallDataTest::SetGoogleBaseURLAndProcessOnIOThread(
187 util_
.SetGoogleBaseURL(base_url
);
188 BrowserThread::PostTask(
191 base::Bind(&SearchProviderInstallData::OnGoogleURLChange
,
192 base::Unretained(install_data_
),
195 // Wait for the I/O thread to process the update notification.
196 base::RunLoop().RunUntilIdle();
199 // Actual tests ---------------------------------------------------------------
201 TEST_F(SearchProviderInstallDataTest
, GetInstallState
) {
202 // Set up the database.
203 util_
.ChangeModelToLoadState();
204 std::string host
= "www.unittest.com";
205 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
207 // Wait for the changes to be saved.
208 base::RunLoop().RunUntilIdle();
210 // Verify the search providers install state (with no default set).
211 TestGetInstallState
test_get_install_state(install_data_
);
212 test_get_install_state
.RunTests(host
, std::string());
214 // Set-up a default and try it all one more time.
215 std::string default_host
= "www.mmm.com";
216 TemplateURL
* default_url
=
217 AddNewTemplateURL("http://" + default_host
+ "/",
218 base::ASCIIToUTF16("mmm"));
219 util_
.model()->SetUserSelectedDefaultSearchProvider(default_url
);
220 test_get_install_state
.RunTests(host
, default_host
);
223 TEST_F(SearchProviderInstallDataTest
, ManagedDefaultSearch
) {
224 // Set up the database.
225 util_
.ChangeModelToLoadState();
226 std::string host
= "www.unittest.com";
227 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
229 // Set a managed preference that establishes a default search provider.
230 std::string host2
= "www.managedtest.com";
231 util_
.SetManagedDefaultSearchPreferences(
235 "http://" + host2
+ "/p{searchTerms}",
242 EXPECT_TRUE(util_
.model()->is_default_search_managed());
244 // Wait for the changes to be saved.
245 base::RunLoop().RunUntilIdle();
247 // Verify the search providers install state. The default search should be
248 // the managed one we previously set.
249 TestGetInstallState
test_get_install_state(install_data_
);
250 test_get_install_state
.RunTests(host
, host2
);
253 TEST_F(SearchProviderInstallDataTest
, GoogleBaseUrlChange
) {
254 TestGetInstallState
test_get_install_state(install_data_
);
256 // Set up the database.
257 util_
.ChangeModelToLoadState();
258 std::string google_host
= "w.com";
259 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host
+ "/"));
261 AddNewTemplateURL("{google:baseURL}?q={searchTerms}",
262 base::ASCIIToUTF16("t"));
263 TemplateURL
* default_url
=
264 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d"));
265 util_
.model()->SetUserSelectedDefaultSearchProvider(default_url
);
267 // Wait for the changes to be saved.
268 base::RunLoop().RunUntilIdle();
270 // Verify the search providers install state (with no default set).
271 test_get_install_state
.RunTests(google_host
, std::string());
273 // Change the Google base url.
274 google_host
= "foo.com";
275 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host
+ "/"));
277 // Verify that the change got picked up.
278 test_get_install_state
.RunTests(google_host
, std::string());