search_provider_install_data_unittest.cc revision 010d83a9304c5a91596085d917d248abff47903a
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 <string> 6 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/chrome_notification_types.h" 15#include "chrome/browser/search_engines/search_provider_install_data.h" 16#include "chrome/browser/search_engines/template_url.h" 17#include "chrome/browser/search_engines/template_url_prepopulate_data.h" 18#include "chrome/browser/search_engines/template_url_service.h" 19#include "chrome/browser/search_engines/template_url_service_test_util.h" 20#include "chrome/common/pref_names.h" 21#include "chrome/test/base/testing_pref_service_syncable.h" 22#include "chrome/test/base/testing_profile.h" 23#include "content/public/browser/notification_service.h" 24#include "content/public/browser/notification_source.h" 25#include "content/public/test/mock_render_process_host.h" 26#include "content/public/test/test_browser_thread.h" 27#include "testing/gtest/include/gtest/gtest.h" 28 29using content::BrowserThread; 30 31namespace { 32 33// TestGetInstallState -------------------------------------------------------- 34 35// Test the SearchProviderInstallData::GetInstallState. 36class TestGetInstallState { 37 public: 38 explicit TestGetInstallState(SearchProviderInstallData* install_data); 39 40 // Runs all of the test cases. 41 void RunTests(const std::string& search_provider_host, 42 const std::string& default_search_provider_host); 43 44 private: 45 // Callback for when SearchProviderInstallData is ready to have 46 // GetInstallState called. Runs all of the test cases. 47 void DoInstallStateTests(const std::string& search_provider_host, 48 const std::string& default_search_provider_host); 49 50 // Does a verification for one url and its expected state. 51 void VerifyInstallState(SearchProviderInstallData::State expected_state, 52 const std::string& url); 53 54 SearchProviderInstallData* install_data_; 55 56 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); 57}; 58 59TestGetInstallState::TestGetInstallState( 60 SearchProviderInstallData* install_data) 61 : install_data_(install_data) { 62} 63 64void TestGetInstallState::RunTests( 65 const std::string& search_provider_host, 66 const std::string& default_search_provider_host) { 67 install_data_->CallWhenLoaded( 68 base::Bind(&TestGetInstallState::DoInstallStateTests, 69 base::Unretained(this), 70 search_provider_host, default_search_provider_host)); 71 base::RunLoop().RunUntilIdle(); 72} 73 74void TestGetInstallState::DoInstallStateTests( 75 const std::string& search_provider_host, 76 const std::string& default_search_provider_host) { 77 SCOPED_TRACE("search provider: " + search_provider_host + 78 ", default search provider: " + default_search_provider_host); 79 // Installed but not default. 80 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, 81 "http://" + search_provider_host + "/"); 82 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, 83 "http://" + search_provider_host + ":80/"); 84 85 // Not installed. 86 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, 87 "http://" + search_provider_host + ":96/"); 88 89 // Not installed due to different scheme. 90 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, 91 "https://" + search_provider_host + "/"); 92 93 // Not installed. 94 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, 95 "http://a" + search_provider_host + "/"); 96 97 // Installed as default. 98 if (!default_search_provider_host.empty()) { 99 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, 100 "http://" + default_search_provider_host + "/"); 101 } 102} 103 104void TestGetInstallState::VerifyInstallState( 105 SearchProviderInstallData::State expected_state, 106 const std::string& url) { 107 108 SearchProviderInstallData::State actual_state = 109 install_data_->GetInstallState(GURL(url)); 110 EXPECT_EQ(expected_state, actual_state) 111 << "GetInstallState for " << url << " failed. Expected " 112 << expected_state << ". Actual " << actual_state << "."; 113} 114 115} // namespace 116 117// SearchProviderInstallDataTest ---------------------------------------------- 118 119// Provides basic test set-up/tear-down functionality needed by all tests 120// that use TemplateURLServiceTestUtil. 121class SearchProviderInstallDataTest : public testing::Test { 122 public: 123 SearchProviderInstallDataTest(); 124 125 virtual void SetUp() OVERRIDE; 126 virtual void TearDown() OVERRIDE; 127 128 protected: 129 TemplateURL* AddNewTemplateURL(const std::string& url, 130 const base::string16& keyword); 131 132 TemplateURLServiceTestUtil util_; 133 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_; 137 138 // A mock RenderProcessHost that the SearchProviderInstallData will scope its 139 // lifetime to. 140 scoped_ptr<content::MockRenderProcessHost> process_; 141 142 DISALLOW_COPY_AND_ASSIGN(SearchProviderInstallDataTest); 143}; 144 145SearchProviderInstallDataTest::SearchProviderInstallDataTest() 146 : install_data_(NULL) { 147} 148 149void SearchProviderInstallDataTest::SetUp() { 150 testing::Test::SetUp(); 151#if defined(OS_ANDROID) 152 TemplateURLPrepopulateData::InitCountryCode( 153 std::string() /* unknown country code */); 154#endif 155 util_.SetUp(); 156 process_.reset(new content::MockRenderProcessHost(util_.profile())); 157 install_data_ = 158 new SearchProviderInstallData(util_.profile(), process_.get()); 159} 160 161void SearchProviderInstallDataTest::TearDown() { 162 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); 163 install_data_ = NULL; 164 165 // Make sure that the install data class on the UI thread gets cleaned up. 166 // It doesn't matter that this happens after install_data_ is deleted. 167 process_.reset(); 168 169 util_.TearDown(); 170 testing::Test::TearDown(); 171} 172 173TemplateURL* SearchProviderInstallDataTest::AddNewTemplateURL( 174 const std::string& url, 175 const base::string16& keyword) { 176 TemplateURLData data; 177 data.short_name = keyword; 178 data.SetKeyword(keyword); 179 data.SetURL(url); 180 TemplateURL* t_url = new TemplateURL(util_.profile(), data); 181 util_.model()->Add(t_url); 182 return t_url; 183} 184 185// Actual tests --------------------------------------------------------------- 186 187TEST_F(SearchProviderInstallDataTest, GetInstallState) { 188 // Set up the database. 189 util_.ChangeModelToLoadState(); 190 std::string host = "www.unittest.com"; 191 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); 192 193 // Wait for the changes to be saved. 194 base::RunLoop().RunUntilIdle(); 195 196 // Verify the search providers install state (with no default set). 197 TestGetInstallState test_get_install_state(install_data_); 198 test_get_install_state.RunTests(host, std::string()); 199 200 // Set-up a default and try it all one more time. 201 std::string default_host = "www.mmm.com"; 202 TemplateURL* default_url = 203 AddNewTemplateURL("http://" + default_host + "/", 204 base::ASCIIToUTF16("mmm")); 205 util_.model()->SetUserSelectedDefaultSearchProvider(default_url); 206 test_get_install_state.RunTests(host, default_host); 207} 208 209TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { 210 // Set up the database. 211 util_.ChangeModelToLoadState(); 212 std::string host = "www.unittest.com"; 213 AddNewTemplateURL("http://" + host + "/path", base::ASCIIToUTF16("unittest")); 214 215 // Set a managed preference that establishes a default search provider. 216 std::string host2 = "www.managedtest.com"; 217 util_.SetManagedDefaultSearchPreferences( 218 true, 219 "managed", 220 "managed", 221 "http://" + host2 + "/p{searchTerms}", 222 std::string(), 223 std::string(), 224 std::string(), 225 std::string(), 226 std::string()); 227 228 EXPECT_TRUE(util_.model()->is_default_search_managed()); 229 230 // Wait for the changes to be saved. 231 base::RunLoop().RunUntilIdle(); 232 233 // Verify the search providers install state. The default search should be 234 // the managed one we previously set. 235 TestGetInstallState test_get_install_state(install_data_); 236 test_get_install_state.RunTests(host, host2); 237} 238 239TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { 240 TestGetInstallState test_get_install_state(install_data_); 241 242 // Set up the database. 243 util_.ChangeModelToLoadState(); 244 std::string google_host = "w.com"; 245 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); 246 // Wait for the I/O thread to process the update notification. 247 base::RunLoop().RunUntilIdle(); 248 249 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", 250 base::ASCIIToUTF16("t")); 251 TemplateURL* default_url = 252 AddNewTemplateURL("http://d.com/", base::ASCIIToUTF16("d")); 253 util_.model()->SetUserSelectedDefaultSearchProvider(default_url); 254 255 // Wait for the changes to be saved. 256 base::RunLoop().RunUntilIdle(); 257 258 // Verify the search providers install state (with no default set). 259 test_get_install_state.RunTests(google_host, std::string()); 260 261 // Change the Google base url. 262 google_host = "foo.com"; 263 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); 264 // Wait for the I/O thread to process the update notification. 265 base::RunLoop().RunUntilIdle(); 266 267 // Verify that the change got picked up. 268 test_get_install_state.RunTests(google_host, std::string()); 269} 270