| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/search_engines/search_provider_install_data.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "chrome/browser/search_engines/template_url_service_test_util.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 "components/syncable_prefs/testing_pref_service_syncable.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" | |
| 26 | |
| 27 using content::BrowserThread; | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 // TestGetInstallState -------------------------------------------------------- | |
| 32 | |
| 33 // Test the SearchProviderInstallData::GetInstallState. | |
| 34 class TestGetInstallState { | |
| 35 public: | |
| 36 explicit TestGetInstallState(SearchProviderInstallData* install_data); | |
| 37 | |
| 38 // Runs all of the test cases. | |
| 39 void RunTests(const std::string& search_provider_host, | |
| 40 const std::string& default_search_provider_host); | |
| 41 | |
| 42 private: | |
| 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); | |
| 47 | |
| 48 // Does a verification for one url and its expected state. | |
| 49 void VerifyInstallState(SearchProviderInstallData::State expected_state, | |
| 50 const std::string& url); | |
| 51 | |
| 52 SearchProviderInstallData* install_data_; | |
| 53 | |
| 54 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); | |
| 55 }; | |
| 56 | |
| 57 TestGetInstallState::TestGetInstallState( | |
| 58 SearchProviderInstallData* install_data) | |
| 59 : install_data_(install_data) { | |
| 60 } | |
| 61 | |
| 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(); | |
| 70 } | |
| 71 | |
| 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/"); | |
| 82 | |
| 83 // Not installed. | |
| 84 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | |
| 85 "http://" + search_provider_host + ":96/"); | |
| 86 | |
| 87 // Not installed due to different scheme. | |
| 88 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | |
| 89 "https://" + search_provider_host + "/"); | |
| 90 | |
| 91 // Not installed. | |
| 92 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | |
| 93 "http://a" + search_provider_host + "/"); | |
| 94 | |
| 95 // Installed as default. | |
| 96 if (!default_search_provider_host.empty()) { | |
| 97 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, | |
| 98 "http://" + default_search_provider_host + "/"); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void TestGetInstallState::VerifyInstallState( | |
| 103 SearchProviderInstallData::State expected_state, | |
| 104 const std::string& url) { | |
| 105 | |
| 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 << "."; | |
| 111 } | |
| 112 | |
| 113 } // namespace | |
| 114 | |
| 115 // SearchProviderInstallDataTest ---------------------------------------------- | |
| 116 | |
| 117 // Provides basic test set-up/tear-down functionality needed by all tests | |
| 118 // that use TemplateURLServiceTestUtil. | |
| 119 class SearchProviderInstallDataTest : public testing::Test { | |
| 120 public: | |
| 121 SearchProviderInstallDataTest(); | |
| 122 | |
| 123 void SetUp() override; | |
| 124 void TearDown() override; | |
| 125 | |
| 126 TemplateURL* AddNewTemplateURL(const std::string& url, | |
| 127 const base::string16& keyword); | |
| 128 | |
| 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); | |
| 132 | |
| 133 TemplateURLServiceTestUtil* util() { return &util_; } | |
| 134 SearchProviderInstallData* install_data() { return install_data_; } | |
| 135 | |
| 136 private: | |
| 137 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads. | |
| 138 TemplateURLServiceTestUtil util_; | |
| 139 | |
| 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_; | |
| 143 | |
| 144 // A mock RenderProcessHost that the SearchProviderInstallData will scope its | |
| 145 // lifetime to. | |
| 146 std::unique_ptr<content::MockRenderProcessHost> process_; | |
| 147 | |
| 148 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest); | |
| 149 }; | |
| 150 | |
| 151 SearchProviderInstallDataTest::SearchProviderInstallDataTest() | |
| 152 : install_data_(NULL) { | |
| 153 } | |
| 154 | |
| 155 void SearchProviderInstallDataTest::SetUp() { | |
| 156 testing::Test::SetUp(); | |
| 157 process_.reset(new content::MockRenderProcessHost(util_.profile())); | |
| 158 install_data_ = new SearchProviderInstallData( | |
| 159 util_.model(), SearchTermsData().GoogleBaseURLValue(), NULL, | |
| 160 process_.get()); | |
| 161 } | |
| 162 | |
| 163 void SearchProviderInstallDataTest::TearDown() { | |
| 164 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); | |
| 165 install_data_ = NULL; | |
| 166 | |
| 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. | |
| 169 process_.reset(); | |
| 170 | |
| 171 testing::Test::TearDown(); | |
| 172 } | |
| 173 | |
| 174 TemplateURL* SearchProviderInstallDataTest::AddNewTemplateURL( | |
| 175 const std::string& url, | |
| 176 const base::string16& keyword) { | |
| 177 TemplateURLData data; | |
| 178 data.SetShortName(keyword); | |
| 179 data.SetKeyword(keyword); | |
| 180 data.SetURL(url); | |
| 181 TemplateURL* t_url = new TemplateURL(data); | |
| 182 util_.model()->Add(t_url); | |
| 183 return t_url; | |
| 184 } | |
| 185 | |
| 186 void SearchProviderInstallDataTest::SetGoogleBaseURLAndProcessOnIOThread( | |
| 187 GURL base_url) { | |
| 188 util_.SetGoogleBaseURL(base_url); | |
| 189 BrowserThread::PostTask( | |
| 190 BrowserThread::IO, | |
| 191 FROM_HERE, | |
| 192 base::Bind(&SearchProviderInstallData::OnGoogleURLChange, | |
| 193 base::Unretained(install_data_), | |
| 194 base_url.spec())); | |
| 195 | |
| 196 // Wait for the I/O thread to process the update notification. | |
| 197 base::RunLoop().RunUntilIdle(); | |
| 198 } | |
| 199 | |
| 200 // Actual tests --------------------------------------------------------------- | |
| 201 | |
| 202 TEST_F(SearchProviderInstallDataTest, GetInstallState) { | |
| 203 // Set up the database. | |
| 204 util()->ChangeModelToLoadState(); | |
| 205 std::string host = "www.unittest.com"; | |
| 206 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); | |
| 207 | |
| 208 // Wait for the changes to be saved. | |
| 209 base::RunLoop().RunUntilIdle(); | |
| 210 | |
| 211 // Verify the search providers install state (with no default set). | |
| 212 TestGetInstallState test_get_install_state(install_data()); | |
| 213 test_get_install_state.RunTests(host, std::string()); | |
| 214 | |
| 215 // Set-up a default and try it all one more time. | |
| 216 std::string default_host = "www.mmm.com"; | |
| 217 TemplateURL* default_url = | |
| 218 AddNewTemplateURL("http://" + default_host + "/", | |
| 219 base::ASCIIToUTF16("mmm")); | |
| 220 util()->model()->SetUserSelectedDefaultSearchProvider(default_url); | |
| 221 test_get_install_state.RunTests(host, default_host); | |
| 222 } | |
| 223 | |
| 224 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { | |
| 225 // Set up the database. | |
| 226 util()->ChangeModelToLoadState(); | |
| 227 std::string host = "www.unittest.com"; | |
| 228 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); | |
| 229 | |
| 230 // Set a managed preference that establishes a default search provider. | |
| 231 std::string host2 = "www.managedtest.com"; | |
| 232 util()->SetManagedDefaultSearchPreferences( | |
| 233 true, | |
| 234 "managed", | |
| 235 "managed", | |
| 236 "http://" + host2 + "/p{searchTerms}", | |
| 237 std::string(), | |
| 238 std::string(), | |
| 239 std::string(), | |
| 240 std::string(), | |
| 241 std::string()); | |
| 242 | |
| 243 EXPECT_TRUE(util()->model()->is_default_search_managed()); | |
| 244 | |
| 245 // Wait for the changes to be saved. | |
| 246 base::RunLoop().RunUntilIdle(); | |
| 247 | |
| 248 // Verify the search providers install state. The default search should be | |
| 249 // the managed one we previously set. | |
| 250 TestGetInstallState test_get_install_state(install_data()); | |
| 251 test_get_install_state.RunTests(host, host2); | |
| 252 } | |
| 253 | |
| 254 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { | |
| 255 TestGetInstallState test_get_install_state(install_data()); | |
| 256 | |
| 257 // Set up the database. | |
| 258 util()->ChangeModelToLoadState(); | |
| 259 std::string google_host = "w.com"; | |
| 260 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); | |
| 261 | |
| 262 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", | |
| 263 base::ASCIIToUTF16("t")); | |
| 264 TemplateURL* default_url = | |
| 265 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d")); | |
| 266 util()->model()->SetUserSelectedDefaultSearchProvider(default_url); | |
| 267 | |
| 268 // Wait for the changes to be saved. | |
| 269 base::RunLoop().RunUntilIdle(); | |
| 270 | |
| 271 // Verify the search providers install state (with no default set). | |
| 272 test_get_install_state.RunTests(google_host, std::string()); | |
| 273 | |
| 274 // Change the Google base url. | |
| 275 google_host = "foo.com"; | |
| 276 SetGoogleBaseURLAndProcessOnIOThread(GURL("http://" + google_host + "/")); | |
| 277 | |
| 278 // Verify that the change got picked up. | |
| 279 test_get_install_state.RunTests(google_host, std::string()); | |
| 280 } | |
| OLD | NEW |