Chromium Code Reviews| 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 "components/search_engines/template_url_fetcher.h" | 5 #include "components/search_engines/template_url_fetcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "chrome/browser/search_engines/template_url_service_test_util.h" | 19 #include "chrome/browser/search_engines/template_url_service_test_util.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
| 22 #include "components/search_engines/template_url_service.h" | 22 #include "components/search_engines/template_url_service.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "net/test/embedded_test_server/embedded_test_server.h" | 24 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 namespace { | |
| 29 | |
| 28 using base::ASCIIToUTF16; | 30 using base::ASCIIToUTF16; |
| 29 | 31 |
| 32 class TestTemplateUrlFetcher : public TemplateURLFetcher { | |
| 33 public: | |
| 34 TestTemplateUrlFetcher(TemplateURLService* template_url_service, | |
| 35 net::URLRequestContextGetter* request_context, | |
| 36 const base::Closure& request_completed_callback) | |
| 37 : TemplateURLFetcher(template_url_service, request_context), | |
| 38 callback_(request_completed_callback) {} | |
| 39 ~TestTemplateUrlFetcher() override {} | |
| 40 | |
| 41 protected: | |
| 42 void RequestCompleted(RequestDelegate* request) override { | |
| 43 callback_.Run(); | |
| 44 TemplateURLFetcher::RequestCompleted(request); | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 // Callback to be run when a request completes. | |
| 49 base::Closure callback_; | |
| 50 | |
| 51 DISALLOW_COPY_AND_ASSIGN(TestTemplateUrlFetcher); | |
| 52 }; | |
| 53 | |
| 30 // Basic set-up for TemplateURLFetcher tests. | 54 // Basic set-up for TemplateURLFetcher tests. |
| 31 class TemplateURLFetcherTest : public testing::Test { | 55 class TemplateURLFetcherTest : public testing::Test { |
| 32 public: | 56 public: |
| 33 TemplateURLFetcherTest(); | 57 TemplateURLFetcherTest(); |
| 34 | 58 |
| 35 void SetUp() override { | 59 void SetUp() override { |
| 36 TestingProfile* profile = test_util_.profile(); | 60 TestingProfile* profile = test_util_.profile(); |
| 37 ASSERT_TRUE(profile->GetRequestContext()); | 61 ASSERT_TRUE(profile->GetRequestContext()); |
| 38 template_url_fetcher_.reset(new TemplateURLFetcher( | 62 template_url_fetcher_.reset(new TestTemplateUrlFetcher( |
| 39 test_util_.model(), profile->GetRequestContext())); | 63 test_util_.model(), profile->GetRequestContext(), |
| 64 base::Bind(&TemplateURLFetcherTest::RequestCompletedCallback, | |
| 65 base::Unretained(this)))); | |
| 40 | 66 |
| 41 ASSERT_TRUE(test_server_.Start()); | 67 ASSERT_TRUE(test_server_.Start()); |
| 42 } | 68 } |
| 43 | 69 |
| 44 void TearDown() override { | 70 void TearDown() override { |
| 45 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 71 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 46 } | 72 } |
| 47 | 73 |
| 48 // Called when the callback is destroyed. | 74 // Called when a request completes. |
| 49 void DestroyedCallback(); | 75 void RequestCompletedCallback(); |
| 50 | |
| 51 // TemplateURLFetcherCallbacks implementation. (Although not derived from | |
| 52 // this class, this method handles those calls for the test.) | |
| 53 void ConfirmAddSearchProvider( | |
| 54 base::ScopedClosureRunner* callback_destruction_notifier, | |
| 55 std::unique_ptr<TemplateURL> template_url); | |
| 56 | 76 |
| 57 // Schedules the download of the url. | 77 // Schedules the download of the url. |
| 58 void StartDownload(const base::string16& keyword, | 78 void StartDownload(const base::string16& keyword, |
| 59 const std::string& osdd_file_name, | 79 const std::string& osdd_file_name, |
| 60 TemplateURLFetcher::ProviderType provider_type, | |
| 61 bool check_that_file_exists); | 80 bool check_that_file_exists); |
| 62 | 81 |
| 63 // Waits for any downloads to finish. | 82 // Waits for any downloads to finish. |
| 64 void WaitForDownloadToFinish(); | 83 void WaitForDownloadToFinish(); |
| 65 | 84 |
| 66 TemplateURLServiceTestUtil* test_util() { return &test_util_; } | 85 TemplateURLServiceTestUtil* test_util() { return &test_util_; } |
| 67 TemplateURLFetcher* template_url_fetcher() { | 86 TemplateURLFetcher* template_url_fetcher() { |
| 68 return template_url_fetcher_.get(); | 87 return template_url_fetcher_.get(); |
| 69 } | 88 } |
| 70 const TemplateURL* last_callback_template_url() const { | 89 int requests_completed() const { return requests_completed_; } |
| 71 return last_callback_template_url_.get(); | |
| 72 } | |
| 73 int callbacks_destroyed() const { return callbacks_destroyed_; } | |
| 74 int add_provider_called() const { return add_provider_called_; } | |
| 75 | 90 |
| 76 private: | 91 private: |
| 77 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads. | 92 content::TestBrowserThreadBundle thread_bundle_; // To set up BrowserThreads. |
| 78 TemplateURLServiceTestUtil test_util_; | 93 TemplateURLServiceTestUtil test_util_; |
| 79 std::unique_ptr<TemplateURLFetcher> template_url_fetcher_; | 94 std::unique_ptr<TemplateURLFetcher> template_url_fetcher_; |
| 80 net::EmbeddedTestServer test_server_; | 95 net::EmbeddedTestServer test_server_; |
| 81 | 96 |
| 82 // The last TemplateURL to come from a callback. | 97 // How many TemplateURKFetcher::RequestDelegate requests have completed. |
| 83 std::unique_ptr<TemplateURL> last_callback_template_url_; | 98 int requests_completed_; |
| 84 | |
| 85 // How many TemplateURLFetcherTestCallbacks have been destructed. | |
| 86 int callbacks_destroyed_; | |
| 87 | |
| 88 // How many times ConfirmAddSearchProvider has been called. | |
| 89 int add_provider_called_; | |
| 90 | 99 |
| 91 // Is the code in WaitForDownloadToFinish in a message loop waiting for a | 100 // Is the code in WaitForDownloadToFinish in a message loop waiting for a |
| 92 // callback to finish? | 101 // callback to finish? |
| 93 bool waiting_for_download_; | 102 bool waiting_for_download_; |
| 94 | 103 |
| 95 private: | 104 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); | 105 DISALLOW_COPY_AND_ASSIGN(TemplateURLFetcherTest); |
| 97 }; | 106 }; |
| 98 | 107 |
| 99 bool GetTestDataDir(base::FilePath* dir) { | 108 bool GetTestDataDir(base::FilePath* dir) { |
| 100 if (!PathService::Get(base::DIR_SOURCE_ROOT, dir)) | 109 if (!PathService::Get(base::DIR_SOURCE_ROOT, dir)) |
| 101 return false; | 110 return false; |
| 102 *dir = dir->AppendASCII("components") | 111 *dir = dir->AppendASCII("components") |
| 103 .AppendASCII("test") | 112 .AppendASCII("test") |
| 104 .AppendASCII("data") | 113 .AppendASCII("data") |
| 105 .AppendASCII("search_engines"); | 114 .AppendASCII("search_engines"); |
| 106 return true; | 115 return true; |
| 107 } | 116 } |
| 108 | 117 |
| 109 TemplateURLFetcherTest::TemplateURLFetcherTest() | 118 TemplateURLFetcherTest::TemplateURLFetcherTest() |
| 110 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 119 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 111 callbacks_destroyed_(0), | 120 requests_completed_(0), |
| 112 add_provider_called_(0), | |
| 113 waiting_for_download_(false) { | 121 waiting_for_download_(false) { |
| 114 base::FilePath test_data_dir; | 122 base::FilePath test_data_dir; |
| 115 CHECK(GetTestDataDir(&test_data_dir)); | 123 CHECK(GetTestDataDir(&test_data_dir)); |
| 116 test_server_.ServeFilesFromDirectory(test_data_dir); | 124 test_server_.ServeFilesFromDirectory(test_data_dir); |
| 117 } | 125 } |
| 118 | 126 |
| 119 void TemplateURLFetcherTest::DestroyedCallback() { | 127 void TemplateURLFetcherTest::RequestCompletedCallback() { |
| 120 callbacks_destroyed_++; | 128 requests_completed_++; |
| 121 if (waiting_for_download_) | 129 if (waiting_for_download_) |
| 122 base::MessageLoop::current()->QuitWhenIdle(); | 130 base::MessageLoop::current()->QuitWhenIdle(); |
| 123 } | 131 } |
| 124 | 132 |
| 125 void TemplateURLFetcherTest::ConfirmAddSearchProvider( | |
| 126 base::ScopedClosureRunner* callback_destruction_notifier, | |
| 127 std::unique_ptr<TemplateURL> template_url) { | |
| 128 last_callback_template_url_ = std::move(template_url); | |
| 129 add_provider_called_++; | |
| 130 } | |
| 131 | |
| 132 void TemplateURLFetcherTest::StartDownload( | 133 void TemplateURLFetcherTest::StartDownload( |
| 133 const base::string16& keyword, | 134 const base::string16& keyword, |
| 134 const std::string& osdd_file_name, | 135 const std::string& osdd_file_name, |
| 135 TemplateURLFetcher::ProviderType provider_type, | |
| 136 bool check_that_file_exists) { | 136 bool check_that_file_exists) { |
| 137 if (check_that_file_exists) { | 137 if (check_that_file_exists) { |
| 138 base::FilePath osdd_full_path; | 138 base::FilePath osdd_full_path; |
| 139 ASSERT_TRUE(GetTestDataDir(&osdd_full_path)); | 139 ASSERT_TRUE(GetTestDataDir(&osdd_full_path)); |
| 140 osdd_full_path = osdd_full_path.AppendASCII(osdd_file_name); | 140 osdd_full_path = osdd_full_path.AppendASCII(osdd_file_name); |
| 141 ASSERT_TRUE(base::PathExists(osdd_full_path)); | 141 ASSERT_TRUE(base::PathExists(osdd_full_path)); |
| 142 ASSERT_FALSE(base::DirectoryExists(osdd_full_path)); | 142 ASSERT_FALSE(base::DirectoryExists(osdd_full_path)); |
| 143 } | 143 } |
| 144 | 144 |
| 145 // Start the fetch. | 145 // Start the fetch. |
| 146 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); | 146 GURL osdd_url = test_server_.GetURL("/" + osdd_file_name); |
| 147 GURL favicon_url; | 147 GURL favicon_url; |
| 148 base::ScopedClosureRunner* callback_destruction_notifier = | |
| 149 new base::ScopedClosureRunner( | |
| 150 base::Bind(&TemplateURLFetcherTest::DestroyedCallback, | |
| 151 base::Unretained(this))); | |
| 152 | |
| 153 template_url_fetcher_->ScheduleDownload( | 148 template_url_fetcher_->ScheduleDownload( |
| 154 keyword, osdd_url, favicon_url, | 149 keyword, osdd_url, favicon_url, |
| 155 TemplateURLFetcher::URLFetcherCustomizeCallback(), | 150 TemplateURLFetcher::URLFetcherCustomizeCallback()); |
| 156 base::Bind(&TemplateURLFetcherTest::ConfirmAddSearchProvider, | |
| 157 base::Unretained(this), | |
| 158 base::Owned(callback_destruction_notifier)), | |
| 159 provider_type); | |
| 160 } | 151 } |
| 161 | 152 |
| 162 void TemplateURLFetcherTest::WaitForDownloadToFinish() { | 153 void TemplateURLFetcherTest::WaitForDownloadToFinish() { |
| 163 ASSERT_FALSE(waiting_for_download_); | 154 ASSERT_FALSE(waiting_for_download_); |
| 164 waiting_for_download_ = true; | 155 waiting_for_download_ = true; |
| 165 base::MessageLoop::current()->Run(); | 156 base::MessageLoop::current()->Run(); |
| 166 waiting_for_download_ = false; | 157 waiting_for_download_ = false; |
| 167 } | 158 } |
| 168 | 159 |
| 169 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { | 160 TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) { |
| 170 base::string16 keyword(ASCIIToUTF16("test")); | 161 base::string16 keyword(ASCIIToUTF16("test")); |
| 171 | 162 |
| 172 test_util()->ChangeModelToLoadState(); | 163 test_util()->ChangeModelToLoadState(); |
| 173 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | 164 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); |
| 174 | 165 |
| 175 std::string osdd_file_name("simple_open_search.xml"); | 166 std::string osdd_file_name("simple_open_search.xml"); |
| 176 StartDownload(keyword, osdd_file_name, | 167 StartDownload(keyword, osdd_file_name, true); |
| 177 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); | 168 ASSERT_EQ(0, requests_completed()); |
|
Peter Kasting
2016/05/09 22:23:59
How come here and the next two modified lines foll
Evan Stade
2016/05/09 22:54:53
no reason, should be expect.
| |
| 178 ASSERT_EQ(0, add_provider_called()); | |
| 179 ASSERT_EQ(0, callbacks_destroyed()); | |
| 180 | 169 |
| 181 WaitForDownloadToFinish(); | 170 WaitForDownloadToFinish(); |
| 182 ASSERT_EQ(0, add_provider_called()); | 171 ASSERT_EQ(1, requests_completed()); |
| 183 ASSERT_EQ(1, callbacks_destroyed()); | |
| 184 | 172 |
| 185 const TemplateURL* t_url = test_util()->model()->GetTemplateURLForKeyword( | 173 const TemplateURL* t_url = test_util()->model()->GetTemplateURLForKeyword( |
| 186 keyword); | 174 keyword); |
| 187 ASSERT_TRUE(t_url); | 175 ASSERT_TRUE(t_url); |
| 188 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), | 176 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), |
| 189 t_url->url_ref().DisplayURL( | 177 t_url->url_ref().DisplayURL( |
| 190 test_util()->model()->search_terms_data())); | 178 test_util()->model()->search_terms_data())); |
| 191 EXPECT_EQ(ASCIIToUTF16("Simple Search"), t_url->short_name()); | 179 EXPECT_EQ(ASCIIToUTF16("Simple Search"), t_url->short_name()); |
| 192 EXPECT_TRUE(t_url->safe_for_autoreplace()); | 180 EXPECT_TRUE(t_url->safe_for_autoreplace()); |
| 193 } | 181 } |
| 194 | 182 |
| 195 TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) { | 183 TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) { |
| 196 base::string16 keyword(ASCIIToUTF16("test")); | 184 base::string16 keyword(ASCIIToUTF16("test")); |
| 197 | 185 |
| 198 test_util()->ChangeModelToLoadState(); | 186 test_util()->ChangeModelToLoadState(); |
| 199 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | 187 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); |
| 200 | 188 |
| 201 std::string osdd_file_name("simple_open_search.xml"); | 189 std::string osdd_file_name("simple_open_search.xml"); |
| 202 StartDownload(keyword, osdd_file_name, | 190 StartDownload(keyword, osdd_file_name, true); |
| 203 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); | 191 ASSERT_EQ(0, requests_completed()); |
| 204 ASSERT_EQ(0, add_provider_called()); | |
| 205 ASSERT_EQ(0, callbacks_destroyed()); | |
| 206 | 192 |
| 207 struct { | 193 struct { |
| 208 std::string description; | 194 std::string description; |
| 209 std::string osdd_file_name; | 195 std::string osdd_file_name; |
| 210 base::string16 keyword; | 196 base::string16 keyword; |
| 211 TemplateURLFetcher::ProviderType provider_type; | |
| 212 } test_cases[] = { | 197 } test_cases[] = { |
| 213 { "Duplicate osdd url with autodetected provider.", osdd_file_name, | 198 {"Duplicate osdd url with autodetected provider.", osdd_file_name, |
| 214 keyword + ASCIIToUTF16("1"), | 199 keyword + ASCIIToUTF16("1")}, |
| 215 TemplateURLFetcher::AUTODETECTED_PROVIDER }, | 200 {"Duplicate keyword with autodetected provider.", osdd_file_name + "1", |
| 216 { "Duplicate keyword with autodetected provider.", osdd_file_name + "1", | 201 keyword}, |
| 217 keyword, TemplateURLFetcher::AUTODETECTED_PROVIDER }, | |
| 218 { "Duplicate osdd url with explicit provider.", osdd_file_name, | |
| 219 base::string16(), TemplateURLFetcher::EXPLICIT_PROVIDER }, | |
| 220 }; | 202 }; |
| 221 | 203 |
| 222 for (size_t i = 0; i < arraysize(test_cases); ++i) { | 204 for (size_t i = 0; i < arraysize(test_cases); ++i) { |
| 223 StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, | 205 StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, false); |
| 224 test_cases[i].provider_type, false); | 206 EXPECT_EQ(1, template_url_fetcher()->requests_count()) |
| 225 ASSERT_EQ(1, template_url_fetcher()->requests_count()) | |
| 226 << test_cases[i].description; | 207 << test_cases[i].description; |
| 227 ASSERT_EQ(i + 1, static_cast<size_t>(callbacks_destroyed())); | |
| 228 } | 208 } |
| 229 | 209 |
| 230 WaitForDownloadToFinish(); | 210 WaitForDownloadToFinish(); |
| 231 ASSERT_EQ(1 + arraysize(test_cases), | 211 EXPECT_EQ(1, requests_completed()); |
| 232 static_cast<size_t>(callbacks_destroyed())); | |
| 233 ASSERT_EQ(0, add_provider_called()); | |
| 234 } | |
| 235 | |
| 236 TEST_F(TemplateURLFetcherTest, BasicExplicitTest) { | |
| 237 base::string16 keyword(ASCIIToUTF16("test")); | |
| 238 | |
| 239 test_util()->ChangeModelToLoadState(); | |
| 240 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | |
| 241 | |
| 242 std::string osdd_file_name("simple_open_search.xml"); | |
| 243 StartDownload(keyword, osdd_file_name, | |
| 244 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | |
| 245 ASSERT_EQ(0, add_provider_called()); | |
| 246 ASSERT_EQ(0, callbacks_destroyed()); | |
| 247 | |
| 248 WaitForDownloadToFinish(); | |
| 249 ASSERT_EQ(1, add_provider_called()); | |
| 250 ASSERT_EQ(1, callbacks_destroyed()); | |
| 251 | |
| 252 ASSERT_TRUE(last_callback_template_url()); | |
| 253 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), | |
| 254 last_callback_template_url()->url_ref().DisplayURL( | |
| 255 test_util()->model()->search_terms_data())); | |
| 256 EXPECT_EQ(ASCIIToUTF16("example.com"), | |
| 257 last_callback_template_url()->keyword()); | |
| 258 EXPECT_FALSE(last_callback_template_url()->safe_for_autoreplace()); | |
| 259 } | 212 } |
| 260 | 213 |
| 261 TEST_F(TemplateURLFetcherTest, AutodetectedBeforeLoadTest) { | 214 TEST_F(TemplateURLFetcherTest, AutodetectedBeforeLoadTest) { |
| 262 base::string16 keyword(ASCIIToUTF16("test")); | 215 base::string16 keyword(ASCIIToUTF16("test")); |
| 263 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | 216 EXPECT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); |
| 264 | 217 |
| 265 std::string osdd_file_name("simple_open_search.xml"); | 218 std::string osdd_file_name("simple_open_search.xml"); |
| 266 StartDownload(keyword, osdd_file_name, | 219 StartDownload(keyword, osdd_file_name, true); |
| 267 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); | 220 EXPECT_EQ(0, template_url_fetcher()->requests_count()); |
| 268 ASSERT_EQ(0, add_provider_called()); | 221 EXPECT_EQ(0, requests_completed()); |
|
Peter Kasting
2016/05/09 22:23:59
It seems like this test gets the same results as t
Evan Stade
2016/05/09 22:54:53
I think the name of the test explains why the requ
Peter Kasting
2016/05/09 22:57:32
Maybe just add a comment like "This should bail be
Evan Stade
2016/05/10 16:51:13
Done.
| |
| 269 ASSERT_EQ(1, callbacks_destroyed()); | |
| 270 } | |
| 271 | |
| 272 TEST_F(TemplateURLFetcherTest, ExplicitBeforeLoadTest) { | |
| 273 base::string16 keyword(ASCIIToUTF16("test")); | |
| 274 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | |
| 275 | |
| 276 std::string osdd_file_name("simple_open_search.xml"); | |
| 277 StartDownload(keyword, osdd_file_name, | |
| 278 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | |
| 279 ASSERT_EQ(0, add_provider_called()); | |
| 280 ASSERT_EQ(0, callbacks_destroyed()); | |
| 281 | |
| 282 WaitForDownloadToFinish(); | |
| 283 ASSERT_EQ(1, add_provider_called()); | |
| 284 ASSERT_EQ(1, callbacks_destroyed()); | |
| 285 | |
| 286 ASSERT_TRUE(last_callback_template_url()); | |
| 287 EXPECT_EQ(ASCIIToUTF16("http://example.com/%s/other_stuff"), | |
| 288 last_callback_template_url()->url_ref().DisplayURL( | |
| 289 test_util()->model()->search_terms_data())); | |
| 290 EXPECT_EQ(ASCIIToUTF16("example.com"), | |
| 291 last_callback_template_url()->keyword()); | |
| 292 EXPECT_FALSE(last_callback_template_url()->safe_for_autoreplace()); | |
| 293 } | 222 } |
| 294 | 223 |
| 295 TEST_F(TemplateURLFetcherTest, DuplicateKeywordsTest) { | 224 TEST_F(TemplateURLFetcherTest, DuplicateKeywordsTest) { |
| 296 base::string16 keyword(ASCIIToUTF16("test")); | 225 base::string16 keyword(ASCIIToUTF16("test")); |
| 297 TemplateURLData data; | 226 TemplateURLData data; |
| 298 data.SetShortName(keyword); | 227 data.SetShortName(keyword); |
| 299 data.SetKeyword(keyword); | 228 data.SetKeyword(keyword); |
| 300 data.SetURL("http://example.com/"); | 229 data.SetURL("http://example.com/"); |
| 301 test_util()->model()->Add(new TemplateURL(data)); | 230 test_util()->model()->Add(new TemplateURL(data)); |
| 302 test_util()->ChangeModelToLoadState(); | 231 test_util()->ChangeModelToLoadState(); |
| 303 | 232 |
| 304 ASSERT_TRUE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | 233 EXPECT_TRUE(test_util()->model()->GetTemplateURLForKeyword(keyword)); |
| 305 | 234 |
| 306 // This should bail because the keyword already exists. | 235 // This should bail because the keyword already exists. |
| 307 std::string osdd_file_name("simple_open_search.xml"); | 236 std::string osdd_file_name("simple_open_search.xml"); |
| 308 StartDownload(keyword, osdd_file_name, | 237 StartDownload(keyword, osdd_file_name, true); |
| 309 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); | 238 EXPECT_EQ(0, template_url_fetcher()->requests_count()); |
| 310 ASSERT_EQ(0, add_provider_called()); | 239 EXPECT_EQ(0, requests_completed()); |
| 311 ASSERT_EQ(1, callbacks_destroyed()); | |
| 312 ASSERT_FALSE(last_callback_template_url()); | |
| 313 } | 240 } |
| 314 | 241 |
| 315 TEST_F(TemplateURLFetcherTest, DuplicateDownloadTest) { | 242 TEST_F(TemplateURLFetcherTest, DuplicateDownloadTest) { |
| 243 test_util()->ChangeModelToLoadState(); | |
| 244 | |
| 316 base::string16 keyword(ASCIIToUTF16("test")); | 245 base::string16 keyword(ASCIIToUTF16("test")); |
| 317 std::string osdd_file_name("simple_open_search.xml"); | 246 std::string osdd_file_name("simple_open_search.xml"); |
| 318 StartDownload(keyword, osdd_file_name, | 247 StartDownload(keyword, osdd_file_name, true); |
| 319 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | 248 EXPECT_EQ(1, template_url_fetcher()->requests_count()); |
| 320 ASSERT_EQ(0, add_provider_called()); | 249 EXPECT_EQ(0, requests_completed()); |
| 321 ASSERT_EQ(0, callbacks_destroyed()); | |
| 322 | 250 |
| 323 // This should bail because the keyword already has a pending download. | 251 // This should bail because the keyword already has a pending download. |
| 324 StartDownload(keyword, osdd_file_name, | 252 StartDownload(keyword, osdd_file_name, true); |
| 325 TemplateURLFetcher::EXPLICIT_PROVIDER, true); | 253 EXPECT_EQ(1, template_url_fetcher()->requests_count()); |
| 326 ASSERT_EQ(0, add_provider_called()); | 254 EXPECT_EQ(0, requests_completed()); |
| 327 ASSERT_EQ(1, callbacks_destroyed()); | |
| 328 | 255 |
| 329 WaitForDownloadToFinish(); | 256 WaitForDownloadToFinish(); |
| 330 ASSERT_EQ(1, add_provider_called()); | 257 EXPECT_EQ(1, requests_completed()); |
| 331 ASSERT_EQ(2, callbacks_destroyed()); | |
| 332 ASSERT_TRUE(last_callback_template_url()); | |
| 333 } | 258 } |
| 334 | 259 |
| 335 TEST_F(TemplateURLFetcherTest, UnicodeTest) { | 260 TEST_F(TemplateURLFetcherTest, UnicodeTest) { |
| 336 base::string16 keyword(ASCIIToUTF16("test")); | 261 base::string16 keyword(ASCIIToUTF16("test")); |
| 337 | 262 |
| 338 test_util()->ChangeModelToLoadState(); | 263 test_util()->ChangeModelToLoadState(); |
| 339 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); | 264 ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword)); |
| 340 | 265 |
| 341 std::string osdd_file_name("unicode_open_search.xml"); | 266 std::string osdd_file_name("unicode_open_search.xml"); |
| 342 StartDownload(keyword, osdd_file_name, | 267 StartDownload(keyword, osdd_file_name, true); |
| 343 TemplateURLFetcher::AUTODETECTED_PROVIDER, true); | |
| 344 WaitForDownloadToFinish(); | 268 WaitForDownloadToFinish(); |
| 345 const TemplateURL* t_url = | 269 const TemplateURL* t_url = |
| 346 test_util()->model()->GetTemplateURLForKeyword(keyword); | 270 test_util()->model()->GetTemplateURLForKeyword(keyword); |
| 347 EXPECT_EQ(base::UTF8ToUTF16("\xd1\x82\xd0\xb5\xd1\x81\xd1\x82"), | 271 EXPECT_EQ(base::UTF8ToUTF16("\xd1\x82\xd0\xb5\xd1\x81\xd1\x82"), |
| 348 t_url->short_name()); | 272 t_url->short_name()); |
| 349 } | 273 } |
| 274 | |
| 275 } // namespace | |
| OLD | NEW |