Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / search_engines / search_provider_install_data_unittest.cc
blob8a72aee2c7339a7ffbf502f30bb91b2c9a731729
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.
5 #include <string>
7 #include "base/basictypes.h"
8 #include "base/bind.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_service.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/test/mock_render_process_host.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 using content::BrowserThread;
28 namespace {
30 // TestGetInstallState --------------------------------------------------------
32 // Test the SearchProviderInstallData::GetInstallState.
33 class TestGetInstallState {
34 public:
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);
41 private:
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/");
82 // Not installed.
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 + "/");
90 // Not installed.
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 << ".";
112 } // namespace
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 {
119 public:
120 SearchProviderInstallDataTest();
122 void SetUp() override;
123 void TearDown() override;
125 TemplateURL* AddNewTemplateURL(const std::string& url,
126 const base::string16& keyword);
128 // Sets the Google base URL to |base_url| and runs the IO thread for
129 // |SearchProviderInstallData| to process the update.
130 void SetGoogleBaseURLAndProcessOnIOThread(GURL base_url);
132 TemplateURLServiceTestUtil* util() { return &util_; }
133 SearchProviderInstallData* install_data() { return install_data_; }
135 private:
136 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads.
137 TemplateURLServiceTestUtil util_;
139 // Provides the search provider install state on the I/O thread. It must be
140 // deleted on the I/O thread, which is why it isn't a scoped_ptr.
141 SearchProviderInstallData* install_data_;
143 // A mock RenderProcessHost that the SearchProviderInstallData will scope its
144 // lifetime to.
145 scoped_ptr<content::MockRenderProcessHost> process_;
147 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest);
150 SearchProviderInstallDataTest::SearchProviderInstallDataTest()
151 : install_data_(NULL) {
154 void SearchProviderInstallDataTest::SetUp() {
155 testing::Test::SetUp();
156 process_.reset(new content::MockRenderProcessHost(util_.profile()));
157 install_data_ = new SearchProviderInstallData(
158 util_.model(), SearchTermsData().GoogleBaseURLValue(), NULL,
159 process_.get());
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.
168 process_.reset();
170 testing::Test::TearDown();
173 TemplateURL* SearchProviderInstallDataTest::AddNewTemplateURL(
174 const std::string& url,
175 const base::string16& keyword) {
176 TemplateURLData data;
177 data.SetShortName(keyword);
178 data.SetKeyword(keyword);
179 data.SetURL(url);
180 TemplateURL* t_url = new TemplateURL(data);
181 util_.model()->Add(t_url);
182 return t_url;
185 void SearchProviderInstallDataTest::SetGoogleBaseURLAndProcessOnIOThread(
186 GURL base_url) {
187 util_.SetGoogleBaseURL(base_url);
188 BrowserThread::PostTask(
189 BrowserThread::IO,
190 FROM_HERE,
191 base::Bind(&SearchProviderInstallData::OnGoogleURLChange,
192 base::Unretained(install_data_),
193 base_url.spec()));
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(
232 true,
233 "managed",
234 "managed",
235 "http://" + host2 + "/p{searchTerms}",
236 std::string(),
237 std::string(),
238 std::string(),
239 std::string(),
240 std::string());
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());