| 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/file_util.h" | 6 #include "base/file_util.h" |
| 6 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 8 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/search_engines/template_url_fetcher.h" | 11 #include "chrome/browser/search_engines/template_url_fetcher.h" |
| 11 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" | |
| 12 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" | 12 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
| 13 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 13 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 14 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/test/base/testing_profile.h" | 15 #include "chrome/test/base/testing_profile.h" |
| 16 #include "components/search_engines/template_url.h" | 16 #include "components/search_engines/template_url.h" |
| 17 #include "components/search_engines/template_url_service.h" | 17 #include "components/search_engines/template_url_service.h" |
| 18 #include "net/test/embedded_test_server/embedded_test_server.h" | 18 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 21 | 21 |
| 22 using base::ASCIIToUTF16; | 22 using base::ASCIIToUTF16; |
| 23 | 23 |
| 24 class TemplateURLFetcherTest; | |
| 25 | |
| 26 // Handles callbacks from TemplateURLFetcher. | |
| 27 class TemplateURLFetcherTestCallbacks : public TemplateURLFetcherCallbacks { | |
| 28 public: | |
| 29 explicit TemplateURLFetcherTestCallbacks(TemplateURLFetcherTest* test) | |
| 30 : test_(test) { | |
| 31 } | |
| 32 virtual ~TemplateURLFetcherTestCallbacks(); | |
| 33 | |
| 34 // TemplateURLFetcherCallbacks implementation. | |
| 35 virtual void ConfirmAddSearchProvider(TemplateURL* template_url, | |
| 36 Profile* profile) OVERRIDE; | |
| 37 | |
| 38 private: | |
| 39 TemplateURLFetcherTest* test_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTestCallbacks); | |
| 42 }; | |
| 43 | |
| 44 // Basic set-up for TemplateURLFetcher tests. | 24 // Basic set-up for TemplateURLFetcher tests. |
| 45 class TemplateURLFetcherTest : public testing::Test { | 25 class TemplateURLFetcherTest : public testing::Test { |
| 46 public: | 26 public: |
| 47 TemplateURLFetcherTest(); | 27 TemplateURLFetcherTest(); |
| 48 | 28 |
| 49 virtual void SetUp() OVERRIDE { | 29 virtual void SetUp() OVERRIDE { |
| 50 test_util_.SetUp(); | 30 test_util_.SetUp(); |
| 51 TestingProfile* profile = test_util_.profile(); | 31 TestingProfile* profile = test_util_.profile(); |
| 52 ASSERT_TRUE(profile); | 32 ASSERT_TRUE(profile); |
| 53 ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile)); | 33 ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile)); |
| 54 | 34 |
| 55 ASSERT_TRUE(profile->GetRequestContext()); | 35 ASSERT_TRUE(profile->GetRequestContext()); |
| 56 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); | 36 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
| 57 } | 37 } |
| 58 | 38 |
| 59 virtual void TearDown() OVERRIDE { | 39 virtual void TearDown() OVERRIDE { |
| 60 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 40 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 61 test_util_.TearDown(); | 41 test_util_.TearDown(); |
| 62 } | 42 } |
| 63 | 43 |
| 64 // Called by ~TemplateURLFetcherTestCallbacks. | 44 // Called when the callback is destroyed. |
| 65 void DestroyedCallback(TemplateURLFetcherTestCallbacks* callbacks); | 45 void DestroyedCallback(); |
| 66 | 46 |
| 67 // TemplateURLFetcherCallbacks implementation. (Although not derived from | 47 // TemplateURLFetcherCallbacks implementation. (Although not derived from |
| 68 // this class, this method handles those calls for the test.) | 48 // this class, this method handles those calls for the test.) |
| 69 void ConfirmAddSearchProvider(TemplateURL* template_url, Profile* profile); | 49 void ConfirmAddSearchProvider( |
| 50 base::ScopedClosureRunner* callback_destruction_notifier, |
| 51 scoped_ptr<TemplateURL> template_url); |
| 70 | 52 |
| 71 protected: | 53 protected: |
| 72 // Schedules the download of the url. | 54 // Schedules the download of the url. |
| 73 void StartDownload(const base::string16& keyword, | 55 void StartDownload(const base::string16& keyword, |
| 74 const std::string& osdd_file_name, | 56 const std::string& osdd_file_name, |
| 75 TemplateURLFetcher::ProviderType provider_type, | 57 TemplateURLFetcher::ProviderType provider_type, |
| 76 bool check_that_file_exists); | 58 bool check_that_file_exists); |
| 77 | 59 |
| 78 // Waits for any downloads to finish. | 60 // Waits for any downloads to finish. |
| 79 void WaitForDownloadToFinish(); | 61 void WaitForDownloadToFinish(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 91 int add_provider_called_; | 73 int add_provider_called_; |
| 92 | 74 |
| 93 // Is the code in WaitForDownloadToFinish in a message loop waiting for a | 75 // Is the code in WaitForDownloadToFinish in a message loop waiting for a |
| 94 // callback to finish? | 76 // callback to finish? |
| 95 bool waiting_for_download_; | 77 bool waiting_for_download_; |
| 96 | 78 |
| 97 private: | 79 private: |
| 98 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); | 80 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); |
| 99 }; | 81 }; |
| 100 | 82 |
| 101 TemplateURLFetcherTestCallbacks::~TemplateURLFetcherTestCallbacks() { | |
| 102 test_->DestroyedCallback(this); | |
| 103 } | |
| 104 | |
| 105 void TemplateURLFetcherTestCallbacks::ConfirmAddSearchProvider( | |
| 106 TemplateURL* template_url, | |
| 107 Profile* profile) { | |
| 108 test_->ConfirmAddSearchProvider(template_url, profile); | |
| 109 } | |
| 110 | |
| 111 TemplateURLFetcherTest::TemplateURLFetcherTest() | 83 TemplateURLFetcherTest::TemplateURLFetcherTest() |
| 112 : callbacks_destroyed_(0), | 84 : callbacks_destroyed_(0), |
| 113 add_provider_called_(0), | 85 add_provider_called_(0), |
| 114 waiting_for_download_(false) { | 86 waiting_for_download_(false) { |
| 115 base::FilePath src_dir; | 87 base::FilePath src_dir; |
| 116 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); | 88 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); |
| 117 test_server_.ServeFilesFromDirectory( | 89 test_server_.ServeFilesFromDirectory( |
| 118 src_dir.AppendASCII("chrome/test/data")); | 90 src_dir.AppendASCII("chrome/test/data")); |
| 119 } | 91 } |
| 120 | 92 |
| 121 void TemplateURLFetcherTest::DestroyedCallback( | 93 void TemplateURLFetcherTest::DestroyedCallback() { |
| 122 TemplateURLFetcherTestCallbacks* callbacks) { | |
| 123 callbacks_destroyed_++; | 94 callbacks_destroyed_++; |
| 124 if (waiting_for_download_) | 95 if (waiting_for_download_) |
| 125 base::MessageLoop::current()->Quit(); | 96 base::MessageLoop::current()->Quit(); |
| 126 } | 97 } |
| 127 | 98 |
| 128 void TemplateURLFetcherTest::ConfirmAddSearchProvider( | 99 void TemplateURLFetcherTest::ConfirmAddSearchProvider( |
| 129 TemplateURL* template_url, | 100 base::ScopedClosureRunner* callback_destruction_notifier, |
| 130 Profile* profile) { | 101 scoped_ptr<TemplateURL> template_url) { |
| 131 last_callback_template_url_.reset(template_url); | 102 last_callback_template_url_ = template_url.Pass(); |
| 132 add_provider_called_++; | 103 add_provider_called_++; |
| 133 } | 104 } |
| 134 | 105 |
| 135 void TemplateURLFetcherTest::StartDownload( | 106 void TemplateURLFetcherTest::StartDownload( |
| 136 const base::string16& keyword, | 107 const base::string16& keyword, |
| 137 const std::string& osdd_file_name, | 108 const std::string& osdd_file_name, |
| 138 TemplateURLFetcher::ProviderType provider_type, | 109 TemplateURLFetcher::ProviderType provider_type, |
| 139 bool check_that_file_exists) { | 110 bool check_that_file_exists) { |
| 140 | 111 |
| 141 if (check_that_file_exists) { | 112 if (check_that_file_exists) { |
| 142 base::FilePath osdd_full_path; | 113 base::FilePath osdd_full_path; |
| 143 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &osdd_full_path)); | 114 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &osdd_full_path)); |
| 144 osdd_full_path = osdd_full_path.AppendASCII(osdd_file_name); | 115 osdd_full_path = osdd_full_path.AppendASCII(osdd_file_name); |
| 145 ASSERT_TRUE(base::PathExists(osdd_full_path)); | 116 ASSERT_TRUE(base::PathExists(osdd_full_path)); |
| 146 ASSERT_FALSE(base::DirectoryExists(osdd_full_path)); | 117 ASSERT_FALSE(base::DirectoryExists(osdd_full_path)); |
| 147 } | 118 } |
| 148 | 119 |
| 149 // Start the fetch. | 120 // Start the fetch. |
| 150 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); | 121 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); |
| 151 GURL favicon_url; | 122 GURL favicon_url; |
| 123 base::ScopedClosureRunner* callback_destruction_notifier = |
| 124 new base::ScopedClosureRunner( |
| 125 base::Bind(&TemplateURLFetcherTest::DestroyedCallback, |
| 126 base::Unretained(this))); |
| 127 |
| 152 TemplateURLFetcherFactory::GetForProfile( | 128 TemplateURLFetcherFactory::GetForProfile( |
| 153 test_util_.profile())->ScheduleDownload( | 129 test_util_.profile())->ScheduleDownload( |
| 154 keyword, osdd_url, favicon_url, NULL, | 130 keyword, osdd_url, favicon_url, NULL, |
| 155 new TemplateURLFetcherTestCallbacks(this), provider_type); | 131 base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, |
| 132 base::Unretained(this), |
| 133 base::Owned(callback_destruction_notifier)), |
| 134 provider_type); |
| 156 } | 135 } |
| 157 | 136 |
| 158 void TemplateURLFetcherTest::WaitForDownloadToFinish() { | 137 void TemplateURLFetcherTest::WaitForDownloadToFinish() { |
| 159 ASSERT_FALSE(waiting_for_download_); | 138 ASSERT_FALSE(waiting_for_download_); |
| 160 waiting_for_download_ = true; | 139 waiting_for_download_ = true; |
| 161 base::MessageLoop::current()->Run(); | 140 base::MessageLoop::current()->Run(); |
| 162 waiting_for_download_ = false; | 141 waiting_for_download_ = false; |
| 163 } | 142 } |
| 164 | 143 |
| 165 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { | 144 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 StartDownload(keyword, osdd_file_name, | 301 StartDownload(keyword, osdd_file_name, |
| 323 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | 302 TemplateURLFetcher::EXPLICIT_PROVIDER, true); |
| 324 ASSERT_EQ(0, add_provider_called_); | 303 ASSERT_EQ(0, add_provider_called_); |
| 325 ASSERT_EQ(1, callbacks_destroyed_); | 304 ASSERT_EQ(1, callbacks_destroyed_); |
| 326 | 305 |
| 327 WaitForDownloadToFinish(); | 306 WaitForDownloadToFinish(); |
| 328 ASSERT_EQ(1, add_provider_called_); | 307 ASSERT_EQ(1, add_provider_called_); |
| 329 ASSERT_EQ(2, callbacks_destroyed_); | 308 ASSERT_EQ(2, callbacks_destroyed_); |
| 330 ASSERT_TRUE(last_callback_template_url_.get()); | 309 ASSERT_TRUE(last_callback_template_url_.get()); |
| 331 } | 310 } |
| OLD | NEW |