OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
13 #include "chrome/browser/search_engines/search_provider_install_data.h" | 14 #include "chrome/browser/search_engines/search_provider_install_data.h" |
14 #include "chrome/browser/search_engines/template_url.h" | 15 #include "chrome/browser/search_engines/template_url.h" |
15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
16 #include "chrome/browser/search_engines/template_url_service.h" | 17 #include "chrome/browser/search_engines/template_url_service.h" |
17 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 18 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
19 #include "chrome/test/base/testing_pref_service_syncable.h" | 20 #include "chrome/test/base/testing_pref_service_syncable.h" |
20 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
21 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
23 #include "content/public/test/test_browser_thread.h" | 24 #include "content/public/test/test_browser_thread.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 using content::BrowserThread; | 27 using content::BrowserThread; |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // TestGetInstallState -------------------------------------------------------- | 31 // TestGetInstallState -------------------------------------------------------- |
31 | 32 |
32 // Test the SearchProviderInstallData::GetInstallState. | 33 // Test the SearchProviderInstallData::GetInstallState. |
33 class TestGetInstallState : | 34 class TestGetInstallState { |
34 public base::RefCountedThreadSafe<TestGetInstallState> { | |
35 public: | 35 public: |
36 explicit TestGetInstallState(SearchProviderInstallData* install_data); | 36 explicit TestGetInstallState(SearchProviderInstallData* install_data); |
37 | 37 |
38 void set_search_provider_host( | 38 // Runs all of the test cases. |
39 const std::string& search_provider_host) { | 39 void RunTests(const std::string& search_provider_host, |
40 search_provider_host_ = search_provider_host; | 40 const std::string& default_search_provider_host); |
41 } | |
42 | |
43 void set_default_search_provider_host( | |
44 const std::string& default_search_provider_host) { | |
45 default_search_provider_host_ = default_search_provider_host; | |
46 } | |
47 | |
48 // Runs the test. Returns true if all passed. False if any failed. | |
49 bool RunTests(); | |
50 | 41 |
51 private: | 42 private: |
52 friend class base::RefCountedThreadSafe<TestGetInstallState>; | |
53 ~TestGetInstallState(); | |
54 | |
55 // Starts the test run on the IO thread. | |
56 void StartTestOnIOThread(); | |
57 | |
58 // Callback for when SearchProviderInstallData is ready to have | 43 // Callback for when SearchProviderInstallData is ready to have |
59 // GetInstallState called. Runs all of the test cases. | 44 // GetInstallState called. Runs all of the test cases. |
60 void DoInstallStateTests(); | 45 void DoInstallStateTests(const std::string& search_provider_host, |
| 46 const std::string& default_search_provider_host); |
61 | 47 |
62 // Does a verification for one url and its expected state. | 48 // Does a verification for one url and its expected state. |
63 void VerifyInstallState(SearchProviderInstallData::State expected_state, | 49 void VerifyInstallState(SearchProviderInstallData::State expected_state, |
64 const std::string& url); | 50 const std::string& url); |
65 | 51 |
66 SearchProviderInstallData* install_data_; | 52 SearchProviderInstallData* install_data_; |
67 base::MessageLoop* main_loop_; | |
68 | |
69 // A host which should be a search provider but not the default. | |
70 std::string search_provider_host_; | |
71 | |
72 // A host which should be a search provider but not the default. | |
73 std::string default_search_provider_host_; | |
74 | |
75 // Used to indicate if DoInstallStateTests passed all test. | |
76 bool passed_; | |
77 | 53 |
78 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); | 54 DISALLOW_COPY_AND_ASSIGN(TestGetInstallState); |
79 }; | 55 }; |
80 | 56 |
81 TestGetInstallState::TestGetInstallState( | 57 TestGetInstallState::TestGetInstallState( |
82 SearchProviderInstallData* install_data) | 58 SearchProviderInstallData* install_data) |
83 : install_data_(install_data), | 59 : install_data_(install_data) { |
84 main_loop_(NULL), | |
85 passed_(false) { | |
86 } | 60 } |
87 | 61 |
88 bool TestGetInstallState::RunTests() { | 62 void TestGetInstallState::RunTests( |
89 passed_ = true; | 63 const std::string& search_provider_host, |
90 | 64 const std::string& default_search_provider_host) { |
91 main_loop_ = base::MessageLoop::current(); | 65 install_data_->CallWhenLoaded( |
92 | 66 base::Bind(&TestGetInstallState::DoInstallStateTests, |
93 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)->PostTask( | 67 base::Unretained(this), |
94 FROM_HERE, | 68 search_provider_host, default_search_provider_host)); |
95 base::Bind(&TestGetInstallState::StartTestOnIOThread, this)); | 69 base::RunLoop().RunUntilIdle(); |
96 // Run the current message loop. When the test is finished on the I/O thread, | |
97 // it invokes Quit, which unblocks this. | |
98 base::MessageLoop::current()->Run(); | |
99 main_loop_ = NULL; | |
100 | |
101 // Let the testing code know what the result is. | |
102 return passed_; | |
103 } | 70 } |
104 | 71 |
105 TestGetInstallState::~TestGetInstallState() { | 72 void TestGetInstallState::DoInstallStateTests( |
106 } | 73 const std::string& search_provider_host, |
107 | 74 const std::string& default_search_provider_host) { |
108 void TestGetInstallState::StartTestOnIOThread() { | 75 SCOPED_TRACE("search provider: " + search_provider_host + |
109 install_data_->CallWhenLoaded( | 76 ", default search provider: " + default_search_provider_host); |
110 base::Bind(&TestGetInstallState::DoInstallStateTests, this)); | |
111 } | |
112 | |
113 void TestGetInstallState::DoInstallStateTests() { | |
114 // Installed but not default. | 77 // Installed but not default. |
115 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, | 78 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, |
116 "http://" + search_provider_host_ + "/"); | 79 "http://" + search_provider_host + "/"); |
117 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, | 80 VerifyInstallState(SearchProviderInstallData::INSTALLED_BUT_NOT_DEFAULT, |
118 "http://" + search_provider_host_ + ":80/"); | 81 "http://" + search_provider_host + ":80/"); |
119 | 82 |
120 // Not installed. | 83 // Not installed. |
121 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | 84 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, |
122 "http://" + search_provider_host_ + ":96/"); | 85 "http://" + search_provider_host + ":96/"); |
123 | 86 |
124 // Not installed due to different scheme. | 87 // Not installed due to different scheme. |
125 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | 88 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, |
126 "https://" + search_provider_host_ + "/"); | 89 "https://" + search_provider_host + "/"); |
127 | 90 |
128 // Not installed. | 91 // Not installed. |
129 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, | 92 VerifyInstallState(SearchProviderInstallData::NOT_INSTALLED, |
130 "http://a" + search_provider_host_ + "/"); | 93 "http://a" + search_provider_host + "/"); |
131 | 94 |
132 // Installed as default. | 95 // Installed as default. |
133 if (!default_search_provider_host_.empty()) { | 96 if (!default_search_provider_host.empty()) { |
134 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, | 97 VerifyInstallState(SearchProviderInstallData::INSTALLED_AS_DEFAULT, |
135 "http://" + default_search_provider_host_ + "/"); | 98 "http://" + default_search_provider_host + "/"); |
136 } | 99 } |
137 | |
138 // All done. | |
139 main_loop_->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); | |
140 } | 100 } |
141 | 101 |
142 void TestGetInstallState::VerifyInstallState( | 102 void TestGetInstallState::VerifyInstallState( |
143 SearchProviderInstallData::State expected_state, | 103 SearchProviderInstallData::State expected_state, |
144 const std::string& url) { | 104 const std::string& url) { |
145 | 105 |
146 SearchProviderInstallData::State actual_state = | 106 SearchProviderInstallData::State actual_state = |
147 install_data_->GetInstallState(GURL(url)); | 107 install_data_->GetInstallState(GURL(url)); |
148 if (expected_state == actual_state) | 108 EXPECT_EQ(expected_state, actual_state) |
149 return; | 109 << "GetInstallState for " << url << " failed. Expected " |
150 | 110 << expected_state << ". Actual " << actual_state << "."; |
151 passed_ = false; | |
152 LOG(ERROR) << "GetInstallState for " << url << " failed. Expected " << | |
153 expected_state << ". Actual " << actual_state << "."; | |
154 } | 111 } |
155 | 112 |
156 }; // namespace | 113 } // namespace |
157 | |
158 | 114 |
159 // SearchProviderInstallDataTest ---------------------------------------------- | 115 // SearchProviderInstallDataTest ---------------------------------------------- |
160 | 116 |
161 // Provides basic test set-up/tear-down functionality needed by all tests | 117 // Provides basic test set-up/tear-down functionality needed by all tests |
162 // that use TemplateURLServiceTestUtil. | 118 // that use TemplateURLServiceTestUtil. |
163 class SearchProviderInstallDataTest : public testing::Test { | 119 class SearchProviderInstallDataTest : public testing::Test { |
164 public: | 120 public: |
165 SearchProviderInstallDataTest(); | 121 SearchProviderInstallDataTest(); |
166 | 122 |
167 virtual void SetUp() OVERRIDE; | 123 virtual void SetUp() OVERRIDE; |
(...skipping 18 matching lines...) Expand all Loading... |
186 : install_data_(NULL) { | 142 : install_data_(NULL) { |
187 } | 143 } |
188 | 144 |
189 void SearchProviderInstallDataTest::SetUp() { | 145 void SearchProviderInstallDataTest::SetUp() { |
190 testing::Test::SetUp(); | 146 testing::Test::SetUp(); |
191 #if defined(OS_ANDROID) | 147 #if defined(OS_ANDROID) |
192 TemplateURLPrepopulateData::InitCountryCode( | 148 TemplateURLPrepopulateData::InitCountryCode( |
193 std::string() /* unknown country code */); | 149 std::string() /* unknown country code */); |
194 #endif | 150 #endif |
195 util_.SetUp(); | 151 util_.SetUp(); |
196 util_.StartIOThread(); | |
197 install_data_ = new SearchProviderInstallData(util_.profile(), | 152 install_data_ = new SearchProviderInstallData(util_.profile(), |
198 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 153 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
199 content::Source<SearchProviderInstallDataTest>(this)); | 154 content::Source<SearchProviderInstallDataTest>(this)); |
200 } | 155 } |
201 | 156 |
202 void SearchProviderInstallDataTest::TearDown() { | 157 void SearchProviderInstallDataTest::TearDown() { |
203 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); | 158 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, install_data_); |
204 install_data_ = NULL; | 159 install_data_ = NULL; |
205 | 160 |
206 // Make sure that the install data class on the UI thread gets cleaned up. | 161 // Make sure that the install data class on the UI thread gets cleaned up. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 const string16& keyword) { | 195 const string16& keyword) { |
241 TemplateURLData data; | 196 TemplateURLData data; |
242 data.short_name = keyword; | 197 data.short_name = keyword; |
243 data.SetKeyword(keyword); | 198 data.SetKeyword(keyword); |
244 data.SetURL(url); | 199 data.SetURL(url); |
245 TemplateURL* t_url = new TemplateURL(util_.profile(), data); | 200 TemplateURL* t_url = new TemplateURL(util_.profile(), data); |
246 util_.model()->Add(t_url); | 201 util_.model()->Add(t_url); |
247 return t_url; | 202 return t_url; |
248 } | 203 } |
249 | 204 |
250 | |
251 // Actual tests --------------------------------------------------------------- | 205 // Actual tests --------------------------------------------------------------- |
252 | 206 |
253 TEST_F(SearchProviderInstallDataTest, GetInstallState) { | 207 TEST_F(SearchProviderInstallDataTest, GetInstallState) { |
254 // Set up the database. | 208 // Set up the database. |
255 util_.ChangeModelToLoadState(); | 209 util_.ChangeModelToLoadState(); |
256 std::string host = "www.unittest.com"; | 210 std::string host = "www.unittest.com"; |
257 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); | 211 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); |
258 | 212 |
259 // Wait for the changes to be saved. | 213 // Wait for the changes to be saved. |
260 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); | 214 base::RunLoop().RunUntilIdle(); |
261 | 215 |
262 // Verify the search providers install state (with no default set). | 216 // Verify the search providers install state (with no default set). |
263 scoped_refptr<TestGetInstallState> test_get_install_state( | 217 TestGetInstallState test_get_install_state(install_data_); |
264 new TestGetInstallState(install_data_)); | 218 test_get_install_state.RunTests(host, std::string()); |
265 test_get_install_state->set_search_provider_host(host); | |
266 EXPECT_TRUE(test_get_install_state->RunTests()); | |
267 | 219 |
268 // Set-up a default and try it all one more time. | 220 // Set-up a default and try it all one more time. |
269 std::string default_host = "www.mmm.com"; | 221 std::string default_host = "www.mmm.com"; |
270 TemplateURL* default_url = | 222 TemplateURL* default_url = |
271 AddNewTemplateURL("http://" + default_host + "/", ASCIIToUTF16("mmm")); | 223 AddNewTemplateURL("http://" + default_host + "/", ASCIIToUTF16("mmm")); |
272 util_.model()->SetDefaultSearchProvider(default_url); | 224 util_.model()->SetDefaultSearchProvider(default_url); |
273 test_get_install_state->set_default_search_provider_host(default_host); | 225 test_get_install_state.RunTests(host, default_host); |
274 EXPECT_TRUE(test_get_install_state->RunTests()); | |
275 } | 226 } |
276 | 227 |
277 | |
278 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { | 228 TEST_F(SearchProviderInstallDataTest, ManagedDefaultSearch) { |
279 // Set up the database. | 229 // Set up the database. |
280 util_.ChangeModelToLoadState(); | 230 util_.ChangeModelToLoadState(); |
281 std::string host = "www.unittest.com"; | 231 std::string host = "www.unittest.com"; |
282 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); | 232 AddNewTemplateURL("http://" + host + "/path", ASCIIToUTF16("unittest")); |
283 | 233 |
284 // Set a managed preference that establishes a default search provider. | 234 // Set a managed preference that establishes a default search provider. |
285 std::string host2 = "www.managedtest.com"; | 235 std::string host2 = "www.managedtest.com"; |
286 std::string url2 = "http://" + host2 + "/p{searchTerms}"; | 236 std::string url2 = "http://" + host2 + "/p{searchTerms}"; |
287 SimulateDefaultSearchIsManaged(url2); | 237 SimulateDefaultSearchIsManaged(url2); |
288 EXPECT_TRUE(util_.model()->is_default_search_managed()); | 238 EXPECT_TRUE(util_.model()->is_default_search_managed()); |
289 | 239 |
290 // Wait for the changes to be saved. | 240 // Wait for the changes to be saved. |
291 util_.BlockTillServiceProcessesRequests(); | 241 base::RunLoop().RunUntilIdle(); |
292 | 242 |
293 // Verify the search providers install state. The default search should be | 243 // Verify the search providers install state. The default search should be |
294 // the managed one we previously set. | 244 // the managed one we previously set. |
295 scoped_refptr<TestGetInstallState> test_get_install_state( | 245 TestGetInstallState test_get_install_state(install_data_); |
296 new TestGetInstallState(install_data_)); | 246 test_get_install_state.RunTests(host, host2); |
297 test_get_install_state->set_search_provider_host(host); | |
298 test_get_install_state->set_default_search_provider_host(host2); | |
299 EXPECT_TRUE(test_get_install_state->RunTests()); | |
300 } | 247 } |
301 | 248 |
302 | |
303 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { | 249 TEST_F(SearchProviderInstallDataTest, GoogleBaseUrlChange) { |
304 scoped_refptr<TestGetInstallState> test_get_install_state( | 250 TestGetInstallState test_get_install_state(install_data_); |
305 new TestGetInstallState(install_data_)); | |
306 | 251 |
307 // Set up the database. | 252 // Set up the database. |
308 util_.ChangeModelToLoadState(); | 253 util_.ChangeModelToLoadState(); |
309 std::string google_host = "w.com"; | 254 std::string google_host = "w.com"; |
310 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); | 255 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); |
311 // Wait for the I/O thread to process the update notification. | 256 // Wait for the I/O thread to process the update notification. |
312 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); | 257 base::RunLoop().RunUntilIdle(); |
313 | 258 |
314 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", ASCIIToUTF16("t")); | 259 AddNewTemplateURL("{google:baseURL}?q={searchTerms}", ASCIIToUTF16("t")); |
315 TemplateURL* default_url = | 260 TemplateURL* default_url = |
316 AddNewTemplateURL("http://d.com/", ASCIIToUTF16("d")); | 261 AddNewTemplateURL("http://d.com/", ASCIIToUTF16("d")); |
317 util_.model()->SetDefaultSearchProvider(default_url); | 262 util_.model()->SetDefaultSearchProvider(default_url); |
318 | 263 |
319 // Wait for the changes to be saved. | 264 // Wait for the changes to be saved. |
320 TemplateURLServiceTestUtil::BlockTillServiceProcessesRequests(); | 265 base::RunLoop().RunUntilIdle(); |
321 | 266 |
322 // Verify the search providers install state (with no default set). | 267 // Verify the search providers install state (with no default set). |
323 test_get_install_state->set_search_provider_host(google_host); | 268 test_get_install_state.RunTests(google_host, std::string()); |
324 EXPECT_TRUE(test_get_install_state->RunTests()); | |
325 | 269 |
326 // Change the Google base url. | 270 // Change the Google base url. |
327 google_host = "foo.com"; | 271 google_host = "foo.com"; |
328 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); | 272 util_.SetGoogleBaseURL(GURL("http://" + google_host + "/")); |
329 // Wait for the I/O thread to process the update notification. | 273 // Wait for the I/O thread to process the update notification. |
330 TemplateURLServiceTestUtil::BlockTillIOThreadProcessesRequests(); | 274 base::RunLoop().RunUntilIdle(); |
331 | 275 |
332 // Verify that the change got picked up. | 276 // Verify that the change got picked up. |
333 test_get_install_state->set_search_provider_host(google_host); | 277 test_get_install_state.RunTests(google_host, std::string()); |
334 EXPECT_TRUE(test_get_install_state->RunTests()); | |
335 } | 278 } |
OLD | NEW |