Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/search_engines/template_url_fetcher_unittest.cc

Issue 513133002: Move TemplateURLService related tests to components/search_engines (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/callback_helpers.h" 5 #include "base/callback_helpers.h"
6 #include "base/files/file_util.h" 6 #include "base/files/file_util.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/search_engines/template_url_service_test_util.h" 11 #include "chrome/browser/search_engines/template_url_service_test_util.h"
12 #include "chrome/common/chrome_paths.h" 12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/test/base/testing_profile.h" 13 #include "chrome/test/base/testing_profile.h"
14 #include "components/search_engines/template_url.h" 14 #include "components/search_engines/template_url.h"
15 #include "components/search_engines/template_url_fetcher.h" 15 #include "components/search_engines/template_url_fetcher.h"
16 #include "components/search_engines/template_url_service.h" 16 #include "components/search_engines/template_url_service.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "net/test/embedded_test_server/embedded_test_server.h" 18 #include "net/test/embedded_test_server/embedded_test_server.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
21 using base::ASCIIToUTF16; 22 using base::ASCIIToUTF16;
22 23
23 // Basic set-up for TemplateURLFetcher tests. 24 // Basic set-up for TemplateURLFetcher tests.
24 class TemplateURLFetcherTest : public testing::Test { 25 class TemplateURLFetcherTest : public testing::Test {
25 public: 26 public:
26 TemplateURLFetcherTest(); 27 TemplateURLFetcherTest();
(...skipping 13 matching lines...) Expand all
40 41
41 // Called when the callback is destroyed. 42 // Called when the callback is destroyed.
42 void DestroyedCallback(); 43 void DestroyedCallback();
43 44
44 // TemplateURLFetcherCallbacks implementation. (Although not derived from 45 // TemplateURLFetcherCallbacks implementation. (Although not derived from
45 // this class, this method handles those calls for the test.) 46 // this class, this method handles those calls for the test.)
46 void ConfirmAddSearchProvider( 47 void ConfirmAddSearchProvider(
47 base::ScopedClosureRunner* callback_destruction_notifier, 48 base::ScopedClosureRunner* callback_destruction_notifier,
48 scoped_ptr<TemplateURL> template_url); 49 scoped_ptr<TemplateURL> template_url);
49 50
50 protected:
51 // Schedules the download of the url. 51 // Schedules the download of the url.
52 void StartDownload(const base::string16& keyword, 52 void StartDownload(const base::string16& keyword,
53 const std::string& osdd_file_name, 53 const std::string& osdd_file_name,
54 TemplateURLFetcher::ProviderType provider_type, 54 TemplateURLFetcher::ProviderType provider_type,
55 bool check_that_file_exists); 55 bool check_that_file_exists);
56 56
57 // Waits for any downloads to finish. 57 // Waits for any downloads to finish.
58 void WaitForDownloadToFinish(); 58 void WaitForDownloadToFinish();
59 59
60 TemplateURLServiceTestUtil* test_util() { return &test_util_; }
61 TemplateURLFetcher* template_url_fetcher() {
62 return template_url_fetcher_.get();
63 }
64 const TemplateURL* last_callback_template_url() const {
65 return last_callback_template_url_.get();
66 }
67 int callbacks_destroyed() const { return callbacks_destroyed_; }
68 int add_provider_called() const { return add_provider_called_; }
69
70 private:
71 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads.
60 TemplateURLServiceTestUtil test_util_; 72 TemplateURLServiceTestUtil test_util_;
61 scoped_ptr<TemplateURLFetcher> template_url_fetcher_; 73 scoped_ptr<TemplateURLFetcher> template_url_fetcher_;
62 net::test_server::EmbeddedTestServer test_server_; 74 net::test_server::EmbeddedTestServer test_server_;
63 75
64 // The last TemplateURL to come from a callback. 76 // The last TemplateURL to come from a callback.
65 scoped_ptr<TemplateURL> last_callback_template_url_; 77 scoped_ptr<TemplateURL> last_callback_template_url_;
66 78
67 // How many TemplateURLFetcherTestCallbacks have been destructed. 79 // How many TemplateURLFetcherTestCallbacks have been destructed.
68 int callbacks_destroyed_; 80 int callbacks_destroyed_;
69 81
70 // How many times ConfirmAddSearchProvider has been called. 82 // How many times ConfirmAddSearchProvider has been called.
71 int add_provider_called_; 83 int add_provider_called_;
72 84
73 // Is the code in WaitForDownloadToFinish in a message loop waiting for a 85 // Is the code in WaitForDownloadToFinish in a message loop waiting for a
74 // callback to finish? 86 // callback to finish?
75 bool waiting_for_download_; 87 bool waiting_for_download_;
76 88
77 private: 89 private:
78 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); 90 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest);
79 }; 91 };
80 92
81 TemplateURLFetcherTest::TemplateURLFetcherTest() 93 TemplateURLFetcherTest::TemplateURLFetcherTest()
82 : callbacks_destroyed_(0), 94 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
95 callbacks_destroyed_(0),
83 add_provider_called_(0), 96 add_provider_called_(0),
84 waiting_for_download_(false) { 97 waiting_for_download_(false) {
85 base::FilePath src_dir; 98 base::FilePath src_dir;
86 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); 99 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir));
87 test_server_.ServeFilesFromDirectory( 100 test_server_.ServeFilesFromDirectory(
88 src_dir.AppendASCII("chrome/test/data")); 101 src_dir.AppendASCII("chrome/test/data"));
89 } 102 }
90 103
91 void TemplateURLFetcherTest::DestroyedCallback() { 104 void TemplateURLFetcherTest::DestroyedCallback() {
92 callbacks_destroyed_++; 105 callbacks_destroyed_++;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 void TemplateURLFetcherTest::WaitForDownloadToFinish() { 148 void TemplateURLFetcherTest::WaitForDownloadToFinish() {
136 ASSERT_FALSE(waiting_for_download_); 149 ASSERT_FALSE(waiting_for_download_);
137 waiting_for_download_ = true; 150 waiting_for_download_ = true;
138 base::MessageLoop::current()->Run(); 151 base::MessageLoop::current()->Run();
139 waiting_for_download_ = false; 152 waiting_for_download_ = false;
140 } 153 }
141 154
142 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { 155 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) {
143 base::string16 keyword(ASCIIToUTF16("test")); 156 base::string16 keyword(ASCIIToUTF16("test"));
144 157
145 test_util_.ChangeModelToLoadState(); 158 test_util()->ChangeModelToLoadState();
146 ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword)); 159 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
147 160
148 std::string osdd_file_name("simple_open_search.xml"); 161 std::string osdd_file_name("simple_open_search.xml");
149 StartDownload(keyword, osdd_file_name, 162 StartDownload(keyword, osdd_file_name,
150 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); 163 TemplateURLFetcher::AUTODETECTED_PROVIDER, true);
151 ASSERT_EQ(0, add_provider_called_); 164 ASSERT_EQ(0, add_provider_called());
152 ASSERT_EQ(0, callbacks_destroyed_); 165 ASSERT_EQ(0, callbacks_destroyed());
153 166
154 WaitForDownloadToFinish(); 167 WaitForDownloadToFinish();
155 ASSERT_EQ(0, add_provider_called_); 168 ASSERT_EQ(0, add_provider_called());
156 ASSERT_EQ(1, callbacks_destroyed_); 169 ASSERT_EQ(1, callbacks_destroyed());
157 170
158 const TemplateURL* t_url = test_util_.model()->GetTemplateURLForKeyword( 171 const TemplateURL* t_url = test_util()->model()->GetTemplateURLForKeyword(
159 keyword); 172 keyword);
160 ASSERT_TRUE(t_url); 173 ASSERT_TRUE(t_url);
161 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), 174 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"),
162 t_url->url_ref().DisplayURL( 175 t_url->url_ref().DisplayURL(
163 test_util_.model()->search_terms_data())); 176 test_util()->model()->search_terms_data()));
164 EXPECT_TRUE(t_url->safe_for_autoreplace()); 177 EXPECT_TRUE(t_url->safe_for_autoreplace());
165 } 178 }
166 179
167 TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) { 180 TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) {
168 base::string16 keyword(ASCIIToUTF16("test")); 181 base::string16 keyword(ASCIIToUTF16("test"));
169 182
170 test_util_.ChangeModelToLoadState(); 183 test_util()->ChangeModelToLoadState();
171 ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword)); 184 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
172 185
173 std::string osdd_file_name("simple_open_search.xml"); 186 std::string osdd_file_name("simple_open_search.xml");
174 StartDownload(keyword, osdd_file_name, 187 StartDownload(keyword, osdd_file_name,
175 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); 188 TemplateURLFetcher::AUTODETECTED_PROVIDER, true);
176 ASSERT_EQ(0, add_provider_called_); 189 ASSERT_EQ(0, add_provider_called());
177 ASSERT_EQ(0, callbacks_destroyed_); 190 ASSERT_EQ(0, callbacks_destroyed());
178 191
179 struct { 192 struct {
180 std::string description; 193 std::string description;
181 std::string osdd_file_name; 194 std::string osdd_file_name;
182 base::string16 keyword; 195 base::string16 keyword;
183 TemplateURLFetcher::ProviderType provider_type; 196 TemplateURLFetcher::ProviderType provider_type;
184 } test_cases[] = { 197 } test_cases[] = {
185 { "Duplicate osdd url with autodetected provider.", osdd_file_name, 198 { "Duplicate osdd url with autodetected provider.", osdd_file_name,
186 keyword + ASCIIToUTF16("1"), 199 keyword + ASCIIToUTF16("1"),
187 TemplateURLFetcher::AUTODETECTED_PROVIDER }, 200 TemplateURLFetcher::AUTODETECTED_PROVIDER },
188 { "Duplicate keyword with autodetected provider.", osdd_file_name + "1", 201 { "Duplicate keyword with autodetected provider.", osdd_file_name + "1",
189 keyword, TemplateURLFetcher::AUTODETECTED_PROVIDER }, 202 keyword, TemplateURLFetcher::AUTODETECTED_PROVIDER },
190 { "Duplicate osdd url with explicit provider.", osdd_file_name, 203 { "Duplicate osdd url with explicit provider.", osdd_file_name,
191 base::string16(), TemplateURLFetcher::EXPLICIT_PROVIDER }, 204 base::string16(), TemplateURLFetcher::EXPLICIT_PROVIDER },
192 }; 205 };
193 206
194 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 207 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
195 StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, 208 StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name,
196 test_cases[i].provider_type, false); 209 test_cases[i].provider_type, false);
197 ASSERT_EQ(1, template_url_fetcher_->requests_count()) 210 ASSERT_EQ(1, template_url_fetcher()->requests_count())
198 << test_cases[i].description; 211 << test_cases[i].description;
199 ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_)); 212 ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed()));
200 } 213 }
201 214
202 WaitForDownloadToFinish(); 215 WaitForDownloadToFinish();
203 ASSERT_EQ(1 + ARRAYSIZE_UNSAFE(test_cases), 216 ASSERT_EQ(1 + ARRAYSIZE_UNSAFE(test_cases),
204 static_cast<size_t>(callbacks_destroyed_)); 217 static_cast<size_t>(callbacks_destroyed()));
205 ASSERT_EQ(0, add_provider_called_); 218 ASSERT_EQ(0, add_provider_called());
206 } 219 }
207 220
208 TEST_F(TemplateURLFetcherTest, BasicExplicitTest) { 221 TEST_F(TemplateURLFetcherTest, BasicExplicitTest) {
209 base::string16 keyword(ASCIIToUTF16("test")); 222 base::string16 keyword(ASCIIToUTF16("test"));
210 223
211 test_util_.ChangeModelToLoadState(); 224 test_util()->ChangeModelToLoadState();
212 ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword)); 225 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
213 226
214 std::string osdd_file_name("simple_open_search.xml"); 227 std::string osdd_file_name("simple_open_search.xml");
215 StartDownload(keyword, osdd_file_name, 228 StartDownload(keyword, osdd_file_name,
216 TemplateURLFetcher::EXPLICIT_PROVIDER, true); 229 TemplateURLFetcher::EXPLICIT_PROVIDER, true);
217 ASSERT_EQ(0, add_provider_called_); 230 ASSERT_EQ(0, add_provider_called());
218 ASSERT_EQ(0, callbacks_destroyed_); 231 ASSERT_EQ(0, callbacks_destroyed());
219 232
220 WaitForDownloadToFinish(); 233 WaitForDownloadToFinish();
221 ASSERT_EQ(1, add_provider_called_); 234 ASSERT_EQ(1, add_provider_called());
222 ASSERT_EQ(1, callbacks_destroyed_); 235 ASSERT_EQ(1, callbacks_destroyed());
223 236
224 ASSERT_TRUE(last_callback_template_url_.get()); 237 ASSERT_TRUE(last_callback_template_url());
225 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), 238 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"),
226 last_callback_template_url_->url_ref().DisplayURL( 239 last_callback_template_url()->url_ref().DisplayURL(
227 test_util_.model()->search_terms_data())); 240 test_util()->model()->search_terms_data()));
228 EXPECT_EQ(ASCIIToUTF16("example.com"), 241 EXPECT_EQ(ASCIIToUTF16("example.com"),
229 last_callback_template_url_->keyword()); 242 last_callback_template_url()->keyword());
230 EXPECT_FALSE(last_callback_template_url_->safe_for_autoreplace()); 243 EXPECT_FALSE(last_callback_template_url()->safe_for_autoreplace());
231 } 244 }
232 245
233 TEST_F(TemplateURLFetcherTest, AutodetectedBeforeLoadTest) { 246 TEST_F(TemplateURLFetcherTest, AutodetectedBeforeLoadTest) {
234 base::string16 keyword(ASCIIToUTF16("test")); 247 base::string16 keyword(ASCIIToUTF16("test"));
235 ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword)); 248 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
236 249
237 std::string osdd_file_name("simple_open_search.xml"); 250 std::string osdd_file_name("simple_open_search.xml");
238 StartDownload(keyword, osdd_file_name, 251 StartDownload(keyword, osdd_file_name,
239 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); 252 TemplateURLFetcher::AUTODETECTED_PROVIDER, true);
240 ASSERT_EQ(0, add_provider_called_); 253 ASSERT_EQ(0, add_provider_called());
241 ASSERT_EQ(1, callbacks_destroyed_); 254 ASSERT_EQ(1, callbacks_destroyed());
242 } 255 }
243 256
244 TEST_F(TemplateURLFetcherTest, ExplicitBeforeLoadTest) { 257 TEST_F(TemplateURLFetcherTest, ExplicitBeforeLoadTest) {
245 base::string16 keyword(ASCIIToUTF16("test")); 258 base::string16 keyword(ASCIIToUTF16("test"));
246 ASSERT_FALSE(test_util_.model()->GetTemplateURLForKeyword(keyword)); 259 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
247 260
248 std::string osdd_file_name("simple_open_search.xml"); 261 std::string osdd_file_name("simple_open_search.xml");
249 StartDownload(keyword, osdd_file_name, 262 StartDownload(keyword, osdd_file_name,
250 TemplateURLFetcher::EXPLICIT_PROVIDER, true); 263 TemplateURLFetcher::EXPLICIT_PROVIDER, true);
251 ASSERT_EQ(0, add_provider_called_); 264 ASSERT_EQ(0, add_provider_called());
252 ASSERT_EQ(0, callbacks_destroyed_); 265 ASSERT_EQ(0, callbacks_destroyed());
253 266
254 WaitForDownloadToFinish(); 267 WaitForDownloadToFinish();
255 ASSERT_EQ(1, add_provider_called_); 268 ASSERT_EQ(1, add_provider_called());
256 ASSERT_EQ(1, callbacks_destroyed_); 269 ASSERT_EQ(1, callbacks_destroyed());
257 270
258 ASSERT_TRUE(last_callback_template_url_.get()); 271 ASSERT_TRUE(last_callback_template_url());
259 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), 272 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"),
260 last_callback_template_url_->url_ref().DisplayURL( 273 last_callback_template_url()->url_ref().DisplayURL(
261 test_util_.model()->search_terms_data())); 274 test_util()->model()->search_terms_data()));
262 EXPECT_EQ(ASCIIToUTF16("example.com"), 275 EXPECT_EQ(ASCIIToUTF16("example.com"),
263 last_callback_template_url_->keyword()); 276 last_callback_template_url()->keyword());
264 EXPECT_FALSE(last_callback_template_url_->safe_for_autoreplace()); 277 EXPECT_FALSE(last_callback_template_url()->safe_for_autoreplace());
265 } 278 }
266 279
267 TEST_F(TemplateURLFetcherTest, DuplicateKeywordsTest) { 280 TEST_F(TemplateURLFetcherTest, DuplicateKeywordsTest) {
268 base::string16 keyword(ASCIIToUTF16("test")); 281 base::string16 keyword(ASCIIToUTF16("test"));
269 TemplateURLData data; 282 TemplateURLData data;
270 data.short_name = keyword; 283 data.short_name = keyword;
271 data.SetKeyword(keyword); 284 data.SetKeyword(keyword);
272 data.SetURL("http://example.com/"); 285 data.SetURL("http://example.com/");
273 test_util_.model()->Add(new TemplateURL(data)); 286 test_util()->model()->Add(new TemplateURL(data));
274 test_util_.ChangeModelToLoadState(); 287 test_util()->ChangeModelToLoadState();
275 288
276 ASSERT_TRUE(test_util_.model()->GetTemplateURLForKeyword(keyword)); 289 ASSERT_TRUE(test_util()->model()->GetTemplateURLForKeyword(keyword));
277 290
278 // This should bail because the keyword already exists. 291 // This should bail because the keyword already exists.
279 std::string osdd_file_name("simple_open_search.xml"); 292 std::string osdd_file_name("simple_open_search.xml");
280 StartDownload(keyword, osdd_file_name, 293 StartDownload(keyword, osdd_file_name,
281 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); 294 TemplateURLFetcher::AUTODETECTED_PROVIDER, true);
282 ASSERT_EQ(0, add_provider_called_); 295 ASSERT_EQ(0, add_provider_called());
283 ASSERT_EQ(1, callbacks_destroyed_); 296 ASSERT_EQ(1, callbacks_destroyed());
284 ASSERT_FALSE(last_callback_template_url_.get()); 297 ASSERT_FALSE(last_callback_template_url());
285 } 298 }
286 299
287 TEST_F(TemplateURLFetcherTest, DuplicateDownloadTest) { 300 TEST_F(TemplateURLFetcherTest, DuplicateDownloadTest) {
288 base::string16 keyword(ASCIIToUTF16("test")); 301 base::string16 keyword(ASCIIToUTF16("test"));
289 std::string osdd_file_name("simple_open_search.xml"); 302 std::string osdd_file_name("simple_open_search.xml");
290 StartDownload(keyword, osdd_file_name, 303 StartDownload(keyword, osdd_file_name,
291 TemplateURLFetcher::EXPLICIT_PROVIDER, true); 304 TemplateURLFetcher::EXPLICIT_PROVIDER, true);
292 ASSERT_EQ(0, add_provider_called_); 305 ASSERT_EQ(0, add_provider_called());
293 ASSERT_EQ(0, callbacks_destroyed_); 306 ASSERT_EQ(0, callbacks_destroyed());
294 307
295 // This should bail because the keyword already has a pending download. 308 // This should bail because the keyword already has a pending download.
296 StartDownload(keyword, osdd_file_name, 309 StartDownload(keyword, osdd_file_name,
297 TemplateURLFetcher::EXPLICIT_PROVIDER, true); 310 TemplateURLFetcher::EXPLICIT_PROVIDER, true);
298 ASSERT_EQ(0, add_provider_called_); 311 ASSERT_EQ(0, add_provider_called());
299 ASSERT_EQ(1, callbacks_destroyed_); 312 ASSERT_EQ(1, callbacks_destroyed());
300 313
301 WaitForDownloadToFinish(); 314 WaitForDownloadToFinish();
302 ASSERT_EQ(1, add_provider_called_); 315 ASSERT_EQ(1, add_provider_called());
303 ASSERT_EQ(2, callbacks_destroyed_); 316 ASSERT_EQ(2, callbacks_destroyed());
304 ASSERT_TRUE(last_callback_template_url_.get()); 317 ASSERT_TRUE(last_callback_template_url());
305 } 318 }
OLDNEW
« no previous file with comments | « components/search_engines/DEPS ('k') | components/search_engines/template_url_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698