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 "base/callback_helpers.h" | 5 #include "base/callback_helpers.h" |
6 #include "base/file_util.h" | 6 #include "base/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_fetcher_factory.h" | |
12 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 11 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
13 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
14 #include "chrome/test/base/testing_profile.h" | 13 #include "chrome/test/base/testing_profile.h" |
15 #include "components/search_engines/template_url.h" | 14 #include "components/search_engines/template_url.h" |
16 #include "components/search_engines/template_url_fetcher.h" | 15 #include "components/search_engines/template_url_fetcher.h" |
17 #include "components/search_engines/template_url_service.h" | 16 #include "components/search_engines/template_url_service.h" |
18 #include "net/test/embedded_test_server/embedded_test_server.h" | 17 #include "net/test/embedded_test_server/embedded_test_server.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
20 #include "url/gurl.h" | 19 #include "url/gurl.h" |
21 | 20 |
22 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
23 | 22 |
24 // Basic set-up for TemplateURLFetcher tests. | 23 // Basic set-up for TemplateURLFetcher tests. |
25 class TemplateURLFetcherTest : public testing::Test { | 24 class TemplateURLFetcherTest : public testing::Test { |
26 public: | 25 public: |
27 TemplateURLFetcherTest(); | 26 TemplateURLFetcherTest(); |
28 | 27 |
29 virtual void SetUp() OVERRIDE { | 28 virtual void SetUp() OVERRIDE { |
30 test_util_.SetUp(); | |
31 TestingProfile* profile = test_util_.profile(); | 29 TestingProfile* profile = test_util_.profile(); |
32 ASSERT_TRUE(profile); | 30 ASSERT_TRUE(profile->GetRequestContext()); |
33 ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile)); | 31 template_url_fetcher_.reset(new TemplateURLFetcher( |
| 32 test_util_.model(), profile->GetRequestContext())); |
34 | 33 |
35 ASSERT_TRUE(profile->GetRequestContext()); | |
36 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 34 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
37 } | 35 } |
38 | 36 |
39 virtual void TearDown() OVERRIDE { | 37 virtual void TearDown() OVERRIDE { |
40 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 38 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
41 test_util_.TearDown(); | |
42 } | 39 } |
43 | 40 |
44 // Called when the callback is destroyed. | 41 // Called when the callback is destroyed. |
45 void DestroyedCallback(); | 42 void DestroyedCallback(); |
46 | 43 |
47 // TemplateURLFetcherCallbacks implementation. (Although not derived from | 44 // TemplateURLFetcherCallbacks implementation. (Although not derived from |
48 // this class, this method handles those calls for the test.) | 45 // this class, this method handles those calls for the test.) |
49 void ConfirmAddSearchProvider( | 46 void ConfirmAddSearchProvider( |
50 base::ScopedClosureRunner* callback_destruction_notifier, | 47 base::ScopedClosureRunner* callback_destruction_notifier, |
51 scoped_ptr<TemplateURL> template_url); | 48 scoped_ptr<TemplateURL> template_url); |
52 | 49 |
53 protected: | 50 protected: |
54 // Schedules the download of the url. | 51 // Schedules the download of the url. |
55 void StartDownload(const base::string16& keyword, | 52 void StartDownload(const base::string16& keyword, |
56 const std::string& osdd_file_name, | 53 const std::string& osdd_file_name, |
57 TemplateURLFetcher::ProviderType provider_type, | 54 TemplateURLFetcher::ProviderType provider_type, |
58 bool check_that_file_exists); | 55 bool check_that_file_exists); |
59 | 56 |
60 // Waits for any downloads to finish. | 57 // Waits for any downloads to finish. |
61 void WaitForDownloadToFinish(); | 58 void WaitForDownloadToFinish(); |
62 | 59 |
63 TemplateURLServiceTestUtil test_util_; | 60 TemplateURLServiceTestUtil test_util_; |
| 61 scoped_ptr<TemplateURLFetcher> template_url_fetcher_; |
64 net::test_server::EmbeddedTestServer test_server_; | 62 net::test_server::EmbeddedTestServer test_server_; |
65 | 63 |
66 // The last TemplateURL to come from a callback. | 64 // The last TemplateURL to come from a callback. |
67 scoped_ptr<TemplateURL> last_callback_template_url_; | 65 scoped_ptr<TemplateURL> last_callback_template_url_; |
68 | 66 |
69 // How many TemplateURLFetcherTestCallbacks have been destructed. | 67 // How many TemplateURLFetcherTestCallbacks have been destructed. |
70 int callbacks_destroyed_; | 68 int callbacks_destroyed_; |
71 | 69 |
72 // How many times ConfirmAddSearchProvider has been called. | 70 // How many times ConfirmAddSearchProvider has been called. |
73 int add_provider_called_; | 71 int add_provider_called_; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 } | 116 } |
119 | 117 |
120 // Start the fetch. | 118 // Start the fetch. |
121 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); | 119 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); |
122 GURL favicon_url; | 120 GURL favicon_url; |
123 base::ScopedClosureRunner* callback_destruction_notifier = | 121 base::ScopedClosureRunner* callback_destruction_notifier = |
124 new base::ScopedClosureRunner( | 122 new base::ScopedClosureRunner( |
125 base::Bind(&TemplateURLFetcherTest::DestroyedCallback, | 123 base::Bind(&TemplateURLFetcherTest::DestroyedCallback, |
126 base::Unretained(this))); | 124 base::Unretained(this))); |
127 | 125 |
128 TemplateURLFetcherFactory::GetForProfile( | 126 template_url_fetcher_->ScheduleDownload( |
129 test_util_.profile())->ScheduleDownload( | 127 keyword, osdd_url, favicon_url, |
130 keyword, osdd_url, favicon_url, | 128 TemplateURLFetcher::URLFetcherCustomizeCallback(), |
131 TemplateURLFetcher::URLFetcherCustomizeCallback(), | 129 base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, |
132 base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, | 130 base::Unretained(this), |
133 base::Unretained(this), | 131 base::Owned(callback_destruction_notifier)), |
134 base::Owned(callback_destruction_notifier)), | 132 provider_type); |
135 provider_type); | |
136 } | 133 } |
137 | 134 |
138 void TemplateURLFetcherTest::WaitForDownloadToFinish() { | 135 void TemplateURLFetcherTest::WaitForDownloadToFinish() { |
139 ASSERT_FALSE(waiting_for_download_); | 136 ASSERT_FALSE(waiting_for_download_); |
140 waiting_for_download_ = true; | 137 waiting_for_download_ = true; |
141 base::MessageLoop::current()->Run(); | 138 base::MessageLoop::current()->Run(); |
142 waiting_for_download_ = false; | 139 waiting_for_download_ = false; |
143 } | 140 } |
144 | 141 |
145 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { | 142 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 TemplateURLFetcher::AUTODETECTED_PROVIDER }, | 187 TemplateURLFetcher::AUTODETECTED_PROVIDER }, |
191 { "Duplicate keyword with autodetected provider.", osdd_file_name + "1", | 188 { "Duplicate keyword with autodetected provider.", osdd_file_name + "1", |
192 keyword, TemplateURLFetcher::AUTODETECTED_PROVIDER }, | 189 keyword, TemplateURLFetcher::AUTODETECTED_PROVIDER }, |
193 { "Duplicate osdd url with explicit provider.", osdd_file_name, | 190 { "Duplicate osdd url with explicit provider.", osdd_file_name, |
194 base::string16(), TemplateURLFetcher::EXPLICIT_PROVIDER }, | 191 base::string16(), TemplateURLFetcher::EXPLICIT_PROVIDER }, |
195 }; | 192 }; |
196 | 193 |
197 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 194 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
198 StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, | 195 StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, |
199 test_cases[i].provider_type, false); | 196 test_cases[i].provider_type, false); |
200 ASSERT_EQ( | 197 ASSERT_EQ(1, template_url_fetcher_->requests_count()) |
201 1, | 198 << test_cases[i].description; |
202 TemplateURLFetcherFactory::GetForProfile( | |
203 test_util_.profile())->requests_count()) << | |
204 test_cases[i].description; | |
205 ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_)); | 199 ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed_)); |
206 } | 200 } |
207 | 201 |
208 WaitForDownloadToFinish(); | 202 WaitForDownloadToFinish(); |
209 ASSERT_EQ(1 + ARRAYSIZE_UNSAFE(test_cases), | 203 ASSERT_EQ(1 + ARRAYSIZE_UNSAFE(test_cases), |
210 static_cast<size_t>(callbacks_destroyed_)); | 204 static_cast<size_t>(callbacks_destroyed_)); |
211 ASSERT_EQ(0, add_provider_called_); | 205 ASSERT_EQ(0, add_provider_called_); |
212 } | 206 } |
213 | 207 |
214 TEST_F(TemplateURLFetcherTest, BasicExplicitTest) { | 208 TEST_F(TemplateURLFetcherTest, BasicExplicitTest) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 StartDownload(keyword, osdd_file_name, | 296 StartDownload(keyword, osdd_file_name, |
303 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | 297 TemplateURLFetcher::EXPLICIT_PROVIDER, true); |
304 ASSERT_EQ(0, add_provider_called_); | 298 ASSERT_EQ(0, add_provider_called_); |
305 ASSERT_EQ(1, callbacks_destroyed_); | 299 ASSERT_EQ(1, callbacks_destroyed_); |
306 | 300 |
307 WaitForDownloadToFinish(); | 301 WaitForDownloadToFinish(); |
308 ASSERT_EQ(1, add_provider_called_); | 302 ASSERT_EQ(1, add_provider_called_); |
309 ASSERT_EQ(2, callbacks_destroyed_); | 303 ASSERT_EQ(2, callbacks_destroyed_); |
310 ASSERT_TRUE(last_callback_template_url_.get()); | 304 ASSERT_TRUE(last_callback_template_url_.get()); |
311 } | 305 } |
OLD | NEW |