1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/search_engines/template_url_fetcher.h"
#include <stddef.h>
#include <array>
#include <memory>
#include <string>
#include <utility>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/search_engines/template_url_service_test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/url_loader_interceptor.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace {
constexpr int32_t kRequestID = 10;
bool GetTestFilePath(const std::string& file_name, base::FilePath* path) {
if (!base::PathService::Get(base::DIR_SRC_TEST_DATA_ROOT, path)) {
return false;
}
*path = path->AppendASCII("components")
.AppendASCII("test")
.AppendASCII("data")
.AppendASCII("search_engines")
.AppendASCII(file_name);
return true;
}
class TestTemplateUrlFetcher : public TemplateURLFetcher {
public:
TestTemplateUrlFetcher(
TemplateURLService* template_url_service,
const base::RepeatingClosure& request_completed_callback)
: TemplateURLFetcher(template_url_service),
callback_(request_completed_callback) {}
TestTemplateUrlFetcher(const TestTemplateUrlFetcher&) = delete;
TestTemplateUrlFetcher& operator=(const TestTemplateUrlFetcher&) = delete;
~TestTemplateUrlFetcher() override = default;
protected:
void RequestCompleted(RequestDelegate* request) override {
callback_.Run();
TemplateURLFetcher::RequestCompleted(request);
}
private:
// Callback to be run when a request completes.
base::RepeatingClosure callback_;
};
// Basic set-up for TemplateURLFetcher tests.
class TemplateURLFetcherTest : public testing::Test {
public:
TemplateURLFetcherTest();
TemplateURLFetcherTest(const TemplateURLFetcherTest&) = delete;
TemplateURLFetcherTest& operator=(const TemplateURLFetcherTest&) = delete;
void SetUp() override {
template_url_fetcher_ = std::make_unique<TestTemplateUrlFetcher>(
test_util_.model(),
base::BindRepeating(&TemplateURLFetcherTest::RequestCompletedCallback,
base::Unretained(this)));
}
// Called when a request completes.
void RequestCompletedCallback();
// Schedules the download of the url.
void StartDownload(const std::u16string& keyword,
const std::string& osdd_file_name,
bool check_that_file_exists);
// Handles an incoming request.
bool HandleRequest(content::URLLoaderInterceptor::RequestParams* params) {
base::FilePath path;
CHECK(GetTestFilePath(params->url_request.url.ExtractFileName(), &path));
content::URLLoaderInterceptor::WriteResponse(path, params->client.get());
EXPECT_EQ(params->request_id, kRequestID);
return true;
}
// Waits for any downloads to finish.
void WaitForDownloadToFinish();
TemplateURLServiceTestUtil* test_util() { return &test_util_; }
TemplateURLFetcher* template_url_fetcher() {
return template_url_fetcher_.get();
}
int requests_completed() const { return requests_completed_; }
private:
content::BrowserTaskEnvironment
task_environment_; // To set up BrowserThreads.
TemplateURLServiceTestUtil test_util_;
std::unique_ptr<TemplateURLFetcher> template_url_fetcher_;
content::URLLoaderInterceptor url_loader_interceptor_;
// How many TemplateURKFetcher::RequestDelegate requests have completed.
int requests_completed_;
// Is the code in WaitForDownloadToFinish in a message loop waiting for a
// callback to finish?
bool waiting_for_download_;
base::RunLoop loop_;
};
TemplateURLFetcherTest::TemplateURLFetcherTest()
: task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP),
url_loader_interceptor_(
base::BindRepeating(&TemplateURLFetcherTest::HandleRequest,
base::Unretained(this))),
requests_completed_(0),
waiting_for_download_(false) {}
void TemplateURLFetcherTest::RequestCompletedCallback() {
requests_completed_++;
if (waiting_for_download_)
loop_.QuitWhenIdle();
}
void TemplateURLFetcherTest::StartDownload(const std::u16string& keyword,
const std::string& osdd_file_name,
bool check_that_file_exists) {
if (check_that_file_exists) {
base::FilePath osdd_full_path;
ASSERT_TRUE(GetTestFilePath(osdd_file_name, &osdd_full_path));
ASSERT_TRUE(base::PathExists(osdd_full_path));
ASSERT_FALSE(base::DirectoryExists(osdd_full_path));
}
// Start the fetch.
GURL osdd_url("http://some.url/" + osdd_file_name);
GURL favicon_url;
TestingProfile* profile = test_util_.profile();
template_url_fetcher_->ScheduleDownload(
keyword, osdd_url, favicon_url, url::Origin::Create(GURL()),
profile->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess()
.get(),
0 /* render_frame_id */, kRequestID);
}
void TemplateURLFetcherTest::WaitForDownloadToFinish() {
ASSERT_FALSE(waiting_for_download_);
waiting_for_download_ = true;
loop_.Run();
waiting_for_download_ = false;
}
TEST_F(TemplateURLFetcherTest, BasicAutodetectedTest) {
std::u16string keyword(u"test");
test_util()->ChangeModelToLoadState();
ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
std::string osdd_file_name("simple_open_search.xml");
StartDownload(keyword, osdd_file_name, true);
EXPECT_EQ(0, requests_completed());
WaitForDownloadToFinish();
EXPECT_EQ(1, requests_completed());
const TemplateURL* t_url = test_util()->model()->GetTemplateURLForKeyword(
keyword);
ASSERT_TRUE(t_url);
EXPECT_EQ(
u"http://example.com/%s/other_stuff",
t_url->url_ref().DisplayURL(test_util()->model()->search_terms_data()));
EXPECT_EQ(u"Simple Search", t_url->short_name());
EXPECT_TRUE(t_url->safe_for_autoreplace());
}
// This test is similar to the BasicAutodetectedTest except the xml file
// provided doesn't include a short name for the search engine. We should
// fall back to the hostname.
TEST_F(TemplateURLFetcherTest, InvalidShortName) {
std::u16string keyword(u"test");
test_util()->ChangeModelToLoadState();
ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
std::string osdd_file_name("simple_open_search_no_name.xml");
StartDownload(keyword, osdd_file_name, true);
WaitForDownloadToFinish();
const TemplateURL* t_url =
test_util()->model()->GetTemplateURLForKeyword(keyword);
ASSERT_TRUE(t_url);
EXPECT_EQ(u"example.com", t_url->short_name());
}
TEST_F(TemplateURLFetcherTest, DuplicatesThrownAway) {
std::u16string keyword(u"test");
test_util()->ChangeModelToLoadState();
ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
std::string osdd_file_name("simple_open_search.xml");
StartDownload(keyword, osdd_file_name, true);
EXPECT_EQ(0, requests_completed());
struct TestCases {
std::string description;
std::string osdd_file_name;
std::u16string keyword;
};
auto test_cases = std::to_array<TestCases>({
{"Duplicate osdd url with autodetected provider.", osdd_file_name,
keyword + u"1"},
{"Duplicate keyword with autodetected provider.", osdd_file_name + "1",
keyword},
});
for (size_t i = 0; i < std::size(test_cases); ++i) {
StartDownload(test_cases[i].keyword, test_cases[i].osdd_file_name, false);
EXPECT_EQ(1, template_url_fetcher()->requests_count())
<< test_cases[i].description;
}
WaitForDownloadToFinish();
EXPECT_EQ(1, requests_completed());
}
TEST_F(TemplateURLFetcherTest, AutodetectedBeforeLoadTest) {
std::u16string keyword(u"test");
EXPECT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
// This should bail because the model isn't loaded yet.
std::string osdd_file_name("simple_open_search.xml");
StartDownload(keyword, osdd_file_name, true);
EXPECT_EQ(0, template_url_fetcher()->requests_count());
EXPECT_EQ(0, requests_completed());
}
TEST_F(TemplateURLFetcherTest, DuplicateKeywordsTest) {
std::u16string keyword(u"test");
TemplateURLData data;
data.SetShortName(keyword);
data.SetKeyword(keyword);
data.SetURL("http://example.com/");
test_util()->model()->Add(std::make_unique<TemplateURL>(data));
test_util()->ChangeModelToLoadState();
EXPECT_TRUE(test_util()->model()->GetTemplateURLForKeyword(keyword));
// This should bail because the keyword already exists.
std::string osdd_file_name("simple_open_search.xml");
StartDownload(keyword, osdd_file_name, true);
EXPECT_EQ(0, template_url_fetcher()->requests_count());
EXPECT_EQ(0, requests_completed());
}
TEST_F(TemplateURLFetcherTest, DuplicateDownloadTest) {
test_util()->ChangeModelToLoadState();
std::u16string keyword(u"test");
std::string osdd_file_name("simple_open_search.xml");
StartDownload(keyword, osdd_file_name, true);
EXPECT_EQ(1, template_url_fetcher()->requests_count());
EXPECT_EQ(0, requests_completed());
// This should bail because the keyword already has a pending download.
StartDownload(keyword, osdd_file_name, true);
EXPECT_EQ(1, template_url_fetcher()->requests_count());
EXPECT_EQ(0, requests_completed());
WaitForDownloadToFinish();
EXPECT_EQ(1, requests_completed());
}
TEST_F(TemplateURLFetcherTest, UnicodeTest) {
std::u16string keyword(u"test");
test_util()->ChangeModelToLoadState();
ASSERT_FALSE(test_util()->model()->GetTemplateURLForKeyword(keyword));
std::string osdd_file_name("unicode_open_search.xml");
StartDownload(keyword, osdd_file_name, true);
WaitForDownloadToFinish();
const TemplateURL* t_url =
test_util()->model()->GetTemplateURLForKeyword(keyword);
EXPECT_EQ(u"ัะตัั", t_url->short_name());
}
} // namespace
|