| 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/search_engines/template_url.h" | 10 #include "chrome/browser/search_engines/template_url.h" |
| 11 #include "chrome/browser/search_engines/template_url_fetcher.h" | 11 #include "chrome/browser/search_engines/template_url_fetcher.h" |
| 12 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" | 12 #include "chrome/browser/search_engines/template_url_fetcher_callbacks.h" |
| 13 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" | 13 #include "chrome/browser/search_engines/template_url_fetcher_factory.h" |
| 14 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
| 15 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 15 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 16 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
| 19 #include "net/test/spawned_test_server/spawned_test_server.h" | 19 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 class TemplateURLFetcherTest; | 22 class TemplateURLFetcherTest; |
| 23 | 23 |
| 24 // Handles callbacks from TemplateURLFetcher. | 24 // Handles callbacks from TemplateURLFetcher. |
| 25 class TemplateURLFetcherTestCallbacks : public TemplateURLFetcherCallbacks { | 25 class TemplateURLFetcherTestCallbacks : public TemplateURLFetcherCallbacks { |
| 26 public: | 26 public: |
| 27 explicit TemplateURLFetcherTestCallbacks(TemplateURLFetcherTest* test) | 27 explicit TemplateURLFetcherTestCallbacks(TemplateURLFetcherTest* test) |
| 28 : test_(test) { | 28 : test_(test) { |
| 29 } | 29 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 46 | 46 |
| 47 virtual void SetUp() OVERRIDE { | 47 virtual void SetUp() OVERRIDE { |
| 48 test_util_.SetUp(); | 48 test_util_.SetUp(); |
| 49 test_util_.StartIOThread(); | 49 test_util_.StartIOThread(); |
| 50 TestingProfile* profile = test_util_.profile(); | 50 TestingProfile* profile = test_util_.profile(); |
| 51 ASSERT_TRUE(profile); | 51 ASSERT_TRUE(profile); |
| 52 ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile)); | 52 ASSERT_TRUE(TemplateURLFetcherFactory::GetForProfile(profile)); |
| 53 | 53 |
| 54 profile->CreateRequestContext(); | 54 profile->CreateRequestContext(); |
| 55 ASSERT_TRUE(profile->GetRequestContext()); | 55 ASSERT_TRUE(profile->GetRequestContext()); |
| 56 ASSERT_TRUE(test_server_.Start()); | 56 ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady()); |
| 57 } | 57 } |
| 58 | 58 |
| 59 virtual void TearDown() OVERRIDE { | 59 virtual void TearDown() OVERRIDE { |
| 60 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 60 test_util_.TearDown(); | 61 test_util_.TearDown(); |
| 61 } | 62 } |
| 62 | 63 |
| 63 // Called by ~TemplateURLFetcherTestCallbacks. | 64 // Called by ~TemplateURLFetcherTestCallbacks. |
| 64 void DestroyedCallback(TemplateURLFetcherTestCallbacks* callbacks); | 65 void DestroyedCallback(TemplateURLFetcherTestCallbacks* callbacks); |
| 65 | 66 |
| 66 // TemplateURLFetcherCallbacks implementation. (Although not derived from | 67 // TemplateURLFetcherCallbacks implementation. (Although not derived from |
| 67 // this class, this method handles those calls for the test.) | 68 // this class, this method handles those calls for the test.) |
| 68 void ConfirmAddSearchProvider(TemplateURL* template_url, Profile* profile); | 69 void ConfirmAddSearchProvider(TemplateURL* template_url, Profile* profile); |
| 69 | 70 |
| 70 protected: | 71 protected: |
| 71 // Schedules the download of the url. | 72 // Schedules the download of the url. |
| 72 void StartDownload(const string16& keyword, | 73 void StartDownload(const string16& keyword, |
| 73 const std::string& osdd_file_name, | 74 const std::string& osdd_file_name, |
| 74 TemplateURLFetcher::ProviderType provider_type, | 75 TemplateURLFetcher::ProviderType provider_type, |
| 75 bool check_that_file_exists); | 76 bool check_that_file_exists); |
| 76 | 77 |
| 77 // Waits for any downloads to finish. | 78 // Waits for any downloads to finish. |
| 78 void WaitForDownloadToFinish(); | 79 void WaitForDownloadToFinish(); |
| 79 | 80 |
| 80 TemplateURLServiceTestUtil test_util_; | 81 TemplateURLServiceTestUtil test_util_; |
| 81 net::SpawnedTestServer test_server_; | 82 net::test_server::EmbeddedTestServer test_server_; |
| 82 | 83 |
| 83 // The last TemplateURL to come from a callback. | 84 // The last TemplateURL to come from a callback. |
| 84 scoped_ptr<TemplateURL> last_callback_template_url_; | 85 scoped_ptr<TemplateURL> last_callback_template_url_; |
| 85 | 86 |
| 86 // How many TemplateURLFetcherTestCallbacks have been destructed. | 87 // How many TemplateURLFetcherTestCallbacks have been destructed. |
| 87 int callbacks_destroyed_; | 88 int callbacks_destroyed_; |
| 88 | 89 |
| 89 // How many times ConfirmAddSearchProvider has been called. | 90 // How many times ConfirmAddSearchProvider has been called. |
| 90 int add_provider_called_; | 91 int add_provider_called_; |
| 91 | 92 |
| 92 // Is the code in WaitForDownloadToFinish in a message loop waiting for a | 93 // Is the code in WaitForDownloadToFinish in a message loop waiting for a |
| 93 // callback to finish? | 94 // callback to finish? |
| 94 bool waiting_for_download_; | 95 bool waiting_for_download_; |
| 95 | 96 |
| 96 private: | 97 private: |
| 97 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); | 98 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); |
| 98 }; | 99 }; |
| 99 | 100 |
| 100 TemplateURLFetcherTestCallbacks::~TemplateURLFetcherTestCallbacks() { | 101 TemplateURLFetcherTestCallbacks::~TemplateURLFetcherTestCallbacks() { |
| 101 test_->DestroyedCallback(this); | 102 test_->DestroyedCallback(this); |
| 102 } | 103 } |
| 103 | 104 |
| 104 void TemplateURLFetcherTestCallbacks::ConfirmAddSearchProvider( | 105 void TemplateURLFetcherTestCallbacks::ConfirmAddSearchProvider( |
| 105 TemplateURL* template_url, | 106 TemplateURL* template_url, |
| 106 Profile* profile) { | 107 Profile* profile) { |
| 107 test_->ConfirmAddSearchProvider(template_url, profile); | 108 test_->ConfirmAddSearchProvider(template_url, profile); |
| 108 } | 109 } |
| 109 | 110 |
| 110 TemplateURLFetcherTest::TemplateURLFetcherTest() | 111 TemplateURLFetcherTest::TemplateURLFetcherTest() |
| 111 : test_server_(net::SpawnedTestServer::TYPE_HTTP, | 112 : test_server_( |
| 112 net::SpawnedTestServer::kLocalhost, | 113 content::BrowserThread::GetMessageLoopProxyForThread( |
| 113 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))), | 114 content::BrowserThread::IO)), |
| 114 callbacks_destroyed_(0), | 115 callbacks_destroyed_(0), |
| 115 add_provider_called_(0), | 116 add_provider_called_(0), |
| 116 waiting_for_download_(false) { | 117 waiting_for_download_(false) { |
| 118 base::FilePath src_dir; |
| 119 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &src_dir)); |
| 120 test_server_.ServeFilesFromDirectory( |
| 121 src_dir.AppendASCII("chrome/test/data")); |
| 117 } | 122 } |
| 118 | 123 |
| 119 void TemplateURLFetcherTest::DestroyedCallback( | 124 void TemplateURLFetcherTest::DestroyedCallback( |
| 120 TemplateURLFetcherTestCallbacks* callbacks) { | 125 TemplateURLFetcherTestCallbacks* callbacks) { |
| 121 callbacks_destroyed_++; | 126 callbacks_destroyed_++; |
| 122 if (waiting_for_download_) | 127 if (waiting_for_download_) |
| 123 base::MessageLoop::current()->Quit(); | 128 base::MessageLoop::current()->Quit(); |
| 124 } | 129 } |
| 125 | 130 |
| 126 void TemplateURLFetcherTest::ConfirmAddSearchProvider( | 131 void TemplateURLFetcherTest::ConfirmAddSearchProvider( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 138 | 143 |
| 139 if (check_that_file_exists) { | 144 if (check_that_file_exists) { |
| 140 base::FilePath osdd_full_path; | 145 base::FilePath osdd_full_path; |
| 141 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &osdd_full_path)); | 146 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &osdd_full_path)); |
| 142 osdd_full_path = osdd_full_path.AppendASCII(osdd_file_name); | 147 osdd_full_path = osdd_full_path.AppendASCII(osdd_file_name); |
| 143 ASSERT_TRUE(file_util::PathExists(osdd_full_path)); | 148 ASSERT_TRUE(file_util::PathExists(osdd_full_path)); |
| 144 ASSERT_FALSE(file_util::DirectoryExists(osdd_full_path)); | 149 ASSERT_FALSE(file_util::DirectoryExists(osdd_full_path)); |
| 145 } | 150 } |
| 146 | 151 |
| 147 // Start the fetch. | 152 // Start the fetch. |
| 148 GURL osdd_url = test_server_.GetURL("files/" + osdd_file_name); | 153 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); |
| 149 GURL favicon_url; | 154 GURL favicon_url; |
| 150 TemplateURLFetcherFactory::GetForProfile( | 155 TemplateURLFetcherFactory::GetForProfile( |
| 151 test_util_.profile())->ScheduleDownload( | 156 test_util_.profile())->ScheduleDownload( |
| 152 keyword, osdd_url, favicon_url, NULL, | 157 keyword, osdd_url, favicon_url, NULL, |
| 153 new TemplateURLFetcherTestCallbacks(this), provider_type); | 158 new TemplateURLFetcherTestCallbacks(this), provider_type); |
| 154 } | 159 } |
| 155 | 160 |
| 156 void TemplateURLFetcherTest::WaitForDownloadToFinish() { | 161 void TemplateURLFetcherTest::WaitForDownloadToFinish() { |
| 157 ASSERT_FALSE(waiting_for_download_); | 162 ASSERT_FALSE(waiting_for_download_); |
| 158 waiting_for_download_ = true; | 163 waiting_for_download_ = true; |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 StartDownload(keyword, osdd_file_name, | 322 StartDownload(keyword, osdd_file_name, |
| 318 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | 323 TemplateURLFetcher::EXPLICIT_PROVIDER, true); |
| 319 ASSERT_EQ(0, add_provider_called_); | 324 ASSERT_EQ(0, add_provider_called_); |
| 320 ASSERT_EQ(1, callbacks_destroyed_); | 325 ASSERT_EQ(1, callbacks_destroyed_); |
| 321 | 326 |
| 322 WaitForDownloadToFinish(); | 327 WaitForDownloadToFinish(); |
| 323 ASSERT_EQ(1, add_provider_called_); | 328 ASSERT_EQ(1, add_provider_called_); |
| 324 ASSERT_EQ(2, callbacks_destroyed_); | 329 ASSERT_EQ(2, callbacks_destroyed_); |
| 325 ASSERT_TRUE(last_callback_template_url_.get()); | 330 ASSERT_TRUE(last_callback_template_url_.get()); |
| 326 } | 331 } |
| OLD | NEW |