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
|
// Copyright 2014 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/search_host_to_urls_map.h"
#include <stddef.h>
#include <memory>
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "testing/gtest/include/gtest/gtest.h"
typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet;
// Basic functionality for the SearchHostToURLsMap tests.
class SearchHostToURLsMapTest : public testing::Test {
public:
SearchHostToURLsMapTest() = default;
SearchHostToURLsMapTest(const SearchHostToURLsMapTest&) = delete;
SearchHostToURLsMapTest& operator=(const SearchHostToURLsMapTest&) = delete;
void SetUp() override;
protected:
std::unique_ptr<SearchHostToURLsMap> provider_map_;
TemplateURLService::OwnedTemplateURLVector template_urls_;
std::string host_;
};
void SearchHostToURLsMapTest::SetUp() {
// Add some entries to the search host map.
host_ = "www.unittest.com";
TemplateURLData data;
data.SetURL("http://" + host_ + "/path1");
data.last_modified = base::Time() + base::Microseconds(15);
template_urls_.push_back(std::make_unique<TemplateURL>(data));
// The second one should be slightly newer.
data.SetURL("http://" + host_ + "/path2");
data.last_modified = base::Time() + base::Microseconds(25);
template_urls_.push_back(std::make_unique<TemplateURL>(data));
provider_map_ = std::make_unique<SearchHostToURLsMap>();
provider_map_->Init(template_urls_, SearchTermsData());
}
TEST_F(SearchHostToURLsMapTest, Add) {
std::string new_host = "example.com";
TemplateURLData data;
data.SetURL("http://" + new_host + "/");
TemplateURL new_t_url(data);
provider_map_->Add(&new_t_url, SearchTermsData());
ASSERT_EQ(&new_t_url, provider_map_->GetTemplateURLForHost(new_host));
}
TEST_F(SearchHostToURLsMapTest, Remove) {
provider_map_->Remove(template_urls_[0].get());
const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
ASSERT_EQ(template_urls_[1].get(), found_url);
const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
ASSERT_TRUE(urls != nullptr);
int url_count = 0;
for (auto i(urls->begin()); i != urls->end(); ++i) {
url_count++;
ASSERT_EQ(template_urls_[1].get(), *i);
}
ASSERT_EQ(1, url_count);
}
TEST_F(SearchHostToURLsMapTest, GetsBestTemplateURLForKnownHost) {
// The second one should be slightly newer.
const TemplateURL* found_url = provider_map_->GetTemplateURLForHost(host_);
ASSERT_TRUE(found_url == template_urls_[1].get());
TemplateURLData data;
data.SetURL("http://" + host_ + "/path1");
// Make the new TemplateURL "better" by having it created by policy.
data.policy_origin = TemplateURLData::PolicyOrigin::kDefaultSearchProvider;
TemplateURL new_t_url(data);
provider_map_->Add(&new_t_url, SearchTermsData());
found_url = provider_map_->GetTemplateURLForHost(host_);
EXPECT_EQ(found_url, &new_t_url) << "We should have found the new better "
"TemplateURL that was created by policy.";
}
TEST_F(SearchHostToURLsMapTest, GetTemplateURLForUnknownHost) {
const TemplateURL* found_url =
provider_map_->GetTemplateURLForHost("a" + host_);
ASSERT_TRUE(found_url == nullptr);
}
TEST_F(SearchHostToURLsMapTest, GetURLsForKnownHost) {
const TemplateURLSet* urls = provider_map_->GetURLsForHost(host_);
ASSERT_TRUE(urls != nullptr);
for (const auto& url : template_urls_)
EXPECT_NE(urls->end(), urls->find(url.get()));
}
TEST_F(SearchHostToURLsMapTest, GetURLsForUnknownHost) {
const SearchHostToURLsMap::TemplateURLSet* urls =
provider_map_->GetURLsForHost("a" + host_);
ASSERT_TRUE(urls == nullptr);
}
|