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/browser/browser_thread.h"
23 #include "content/public/test/mock_render_process_host.h"
24 #include "content/public/test/test_browser_thread_bundle.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 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() { return &util_
; }
134 SearchProviderInstallData
* install_data() { return install_data_
; }
137 content::TestBrowserThreadBundle thread_bundle_
; // To set up BrowserThreads.
138 TemplateURLServiceTestUtil util_
;
140 // Provides the search provider install state on the I/O thread. It must be
141 // deleted on the I/O thread, which is why it isn't a scoped_ptr.
142 SearchProviderInstallData
* install_data_
;
144 // A mock RenderProcessHost that the SearchProviderInstallData will scope its
146 scoped_ptr
<content::MockRenderProcessHost
> process_
;
148 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest
);
151 SearchProviderInstallDataTest::SearchProviderInstallDataTest()
152 : install_data_(NULL
) {
155 void SearchProviderInstallDataTest::SetUp() {
156 testing::Test::SetUp();
157 #if defined(OS_ANDROID)
158 TemplateURLPrepopulateData::InitCountryCode(
159 std::string() /* unknown country code */);
161 process_
.reset(new content::MockRenderProcessHost(util_
.profile()));
162 install_data_
= new SearchProviderInstallData(
163 util_
.model(), SearchTermsData().GoogleBaseURLValue(), NULL
,
167 void SearchProviderInstallDataTest::TearDown() {
168 BrowserThread::DeleteSoon(BrowserThread::IO
, FROM_HERE
, install_data_
);
169 install_data_
= NULL
;
171 // Make sure that the install data class on the UI thread gets cleaned up.
172 // It doesn't matter that this happens after install_data_ is deleted.
175 testing::Test::TearDown();
178 TemplateURL
* SearchProviderInstallDataTest::AddNewTemplateURL(
179 const std::string
& url
,
180 const base::string16
& keyword
) {
181 TemplateURLData data
;
182 data
.short_name
= keyword
;
183 data
.SetKeyword(keyword
);
185 TemplateURL
* t_url
= new TemplateURL(data
);
186 util_
.model()->Add(t_url
);
190 void SearchProviderInstallDataTest::SetGoogleBaseURLAndProcessOnIOThread(
192 util_
.SetGoogleBaseURL(base_url
);
193 BrowserThread::PostTask(
196 base::Bind(&SearchProviderInstallData::OnGoogleURLChange
,
197 base::Unretained(install_data_
),
200 // Wait for the I/O thread to process the update notification.
201 base::RunLoop().RunUntilIdle();
204 // Actual tests ---------------------------------------------------------------
206 TEST_F(SearchProviderInstallDataTest
, GetInstallState
) {
207 // Set up the database.
208 util()->ChangeModelToLoadState();
209 std::string host
= "www.unittest.com";
210 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
212 // Wait for the changes to be saved.
213 base::RunLoop().RunUntilIdle();
215 // Verify the search providers install state (with no default set).
216 TestGetInstallState
test_get_install_state(install_data());
217 test_get_install_state
.RunTests(host
, std::string());
219 // Set-up a default and try it all one more time.
220 std::string default_host
= "www.mmm.com";
221 TemplateURL
* default_url
=
222 AddNewTemplateURL("http://" + default_host
+ "/",
223 base::ASCIIToUTF16("mmm"));
224 util()->model()->SetUserSelectedDefaultSearchProvider(default_url
);
225 test_get_install_state
.RunTests(host
, default_host
);
228 TEST_F(SearchProviderInstallDataTest
, ManagedDefaultSearch
) {
229 // Set up the database.
230 util()->ChangeModelToLoadState();
231 std::string host
= "www.unittest.com";
232 AddNewTemplateURL("http://" + host
+ "/path", base::ASCIIToUTF16("unittest"));
234 // Set a managed preference that establishes a default search provider.
235 std::string host2
= "www.managedtest.com";
236 util()->SetManagedDefaultSearchPreferences(
240 "http://" + host2
+ "/p{searchTerms}",
247 EXPECT_TRUE(util()->model()->is_default_search_managed());
249 // Wait for the changes to be saved.
250 base::RunLoop().RunUntilIdle();
252 // Verify the search providers install state. The default search should be
253 // the managed one we previously set.
254 TestGetInstallState
test_get_install_state(install_data());
255 test_get_install_state
.RunTests(host
, host2
);
258 TEST_F(SearchProviderInstallDataTest
, GoogleBaseUrlChange
) {
259 TestGetInstallState
test_get_install_state(install_data());
261 // Set up the database.
262 util()->ChangeModelToLoadState();
263 std::string google_host
= "w.com";
264 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host
+ "/"));
266 AddNewTemplateURL("{google:baseURL}?q={searchTerms}",
267 base::ASCIIToUTF16("t"));
268 TemplateURL
* default_url
=
269 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d"));
270 util()->model()->SetUserSelectedDefaultSearchProvider(default_url
);
272 // Wait for the changes to be saved.
273 base::RunLoop().RunUntilIdle();
275 // Verify the search providers install state (with no default set).
276 test_get_install_state
.RunTests(google_host
, std::string());
278 // Change the Google base url.
279 google_host
= "foo.com";
280 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host
+ "/"));
282 // Verify that the change got picked up.
283 test_get_install_state
.RunTests(google_host
, std::string());