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