This source file includes following definitions.
- BuildTopSites
- InitTopSiteCache
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/history/top_sites_cache.h"
#include <set>
#include "base/basictypes.h"
#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace history {
namespace {
class TopSitesCacheTest : public testing::Test {
public:
TopSitesCacheTest() {
}
protected:
void BuildTopSites(const char** spec, size_t size);
void InitTopSiteCache(const char** spec, size_t size);
MostVisitedURLList top_sites_;
TopSitesCache cache_;
private:
DISALLOW_COPY_AND_ASSIGN(TopSitesCacheTest);
};
void TopSitesCacheTest::BuildTopSites(const char** spec, size_t size) {
std::set<std::string> urls_seen;
for (size_t i = 0; i < size; ++i) {
const char* spec_item = spec[i];
while (*spec_item && *spec_item == ' ')
++spec_item;
if (urls_seen.find(spec_item) != urls_seen.end())
NOTREACHED() << "Duplicate URL found: " << spec_item;
urls_seen.insert(spec_item);
if (spec_item == spec[i]) {
base::string16 title(base::ASCIIToUTF16("Title ") +
base::Uint64ToString16(top_sites_.size() + 1));
top_sites_.push_back(MostVisitedURL(GURL(spec_item), title));
}
ASSERT_TRUE(!top_sites_.empty());
top_sites_.back().redirects.push_back(GURL(spec_item));
}
}
void TopSitesCacheTest::InitTopSiteCache(const char** spec, size_t size) {
BuildTopSites(spec, size);
cache_.SetTopSites(top_sites_);
}
const char* kTopSitesSpecBasic[] = {
"http://www.google.com",
" http://www.gogle.com",
" http://www.gooogle.com",
"http://www.youtube.com/a/b",
" http://www.youtube.com/a/b?test=1",
"https://www.google.com/",
" https://www.gogle.com",
"http://www.example.com:3141/",
};
TEST_F(TopSitesCacheTest, GetCanonicalURL) {
InitTopSiteCache(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic));
struct {
const char* expected;
const char* query;
} test_cases[] = {
{"http://www.google.com/", "http://www.google.com"},
{"http://www.google.com/", "http://www.gooogle.com"},
{"http://www.google.com/", "http://www.gooogle.com/"},
{"http://www.youtube.com/a/b", "http://www.youtube.com/a/b?test=1"},
{"http://www.youtube.com/a/b?test", "http://www.youtube.com/a/b?test"},
{"http://maps.google.com/", "http://maps.google.com/"},
{"http://www.example.com:1234/", "http://www.example.com:1234"},
{"http://www.google.com/", "http://www.gooogle.com:80"},
{"http://www.youtube.com/a", "http://www.youtube.com/a"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
std::string expected(test_cases[i].expected);
std::string query(test_cases[i].query);
EXPECT_EQ(expected, cache_.GetCanonicalURL(GURL(query)).spec())
<< " for test_case[" << i << "]";
}
}
TEST_F(TopSitesCacheTest, IsKnownUrl) {
InitTopSiteCache(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic));
EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.google.com")));
EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.gooogle.com")));
EXPECT_TRUE(cache_.IsKnownURL(GURL("http://www.google.com/")));
EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.com?")));
EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.net")));
EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.google.com/stuff")));
EXPECT_FALSE(cache_.IsKnownURL(GURL("https://www.gooogle.com")));
EXPECT_FALSE(cache_.IsKnownURL(GURL("http://www.youtube.com/a")));
}
const char* kTopSitesSpecPrefix[] = {
"http://www.google.com/",
" http://www.google.com/test?q=3",
" http://www.google.com/test/y?d",
" http://www.chromium.org/a/b",
"http://www.google.com/2",
" http://www.google.com/test/q",
" http://www.google.com/test/y?b",
"http://www.google.com/3",
" http://www.google.com/testing",
"http://www.google.com/test-hyphen",
"http://www.google.com/sh",
" http://www.google.com/sh/1/2/3",
"http://www.google.com/sh/1",
};
TEST_F(TopSitesCacheTest, GetCanonicalURLExactMatch) {
InitTopSiteCache(kTopSitesSpecPrefix, arraysize(kTopSitesSpecPrefix));
for (size_t i = 0; i < arraysize(kTopSitesSpecPrefix); ++i) {
const char* s = kTopSitesSpecPrefix[i];
while (*s && *s == ' ')
++s;
GURL stored_url(s);
GURL expected(cache_.GetCanonicalURL(stored_url));
GURL result(cache_.GetGeneralizedCanonicalURL(stored_url));
EXPECT_EQ(expected, result) << " for kTopSitesSpecPrefix[" << i << "]";
}
}
TEST_F(TopSitesCacheTest, GetGeneralizedCanonicalURL) {
InitTopSiteCache(kTopSitesSpecPrefix, arraysize(kTopSitesSpecPrefix));
struct {
const char* expected;
const char* query;
} test_cases[] = {
{"http://www.google.com/", "http://www.google.com/test"},
{"http://www.google.com/", "http://www.google.com/test/y?e"},
{"http://www.google.com/", "http://www.google.com/test/y?c"},
{"http://www.google.com/2", "http://www.google.com/test/y?a"},
{"http://www.google.com/3", "http://www.google.com/3/1/4/1/5/9"},
{"http://www.google.com/3", "http://www.google.com/3/1/4/1/5/9/"},
{"http://www.google.com/", "http://www.chromium.org/a/b/c"},
{"http://www.google.com/2", "http://www.google.com/test/y"},
{"http://www.google.com/sh", "http://www.google.com/sh/1/2/3/4/"},
{"http://www.google.com/sh", "http://www.google.com/sh/1/2/3/4/"},
{"http://www.google.com/2", "http://www.google.com/test/q"},
{"", "http://www.a.com/"},
{"", "http://www.e-is-between-chromium-and-google.com/"},
{"", "http://www.zzzzzzz.com/"},
{"", "http://www.chromium.org/a/beeswax"},
{"http://www.google.com/", "http://www.google.com/shhhhhh"},
{"", "https://www.google.com/test"},
{"http://www.google.com/", "http://www.google.com:80/test"},
{"", "http://www.chromium.org/a"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
std::string expected(test_cases[i].expected);
std::string query(test_cases[i].query);
GURL result(cache_.GetGeneralizedCanonicalURL(GURL(query)));
EXPECT_EQ(expected, result.spec()) << " for test_case[" << i << "]";
}
}
TEST_F(TopSitesCacheTest, GetPrefixCanonicalURLDiffByQuery) {
const char* top_sites_spec[] = {
"http://www.dest.com/1",
" http://www.source.com/a?m=5",
"http://www.dest.com/2",
" http://www.source.com/a/t?q=3",
};
InitTopSiteCache(top_sites_spec, arraysize(top_sites_spec));
struct {
const char* expected;
const char* query;
} test_cases[] = {
{"http://www.dest.com/1", "http://www.source.com/a?l=5"},
{"http://www.dest.com/1", "http://www.source.com/a?n=5"},
{"http://www.dest.com/2", "http://www.source.com/a/t?q=2"},
{"http://www.dest.com/2", "http://www.source.com/a/t?q=4"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) {
std::string expected(test_cases[i].expected);
std::string query(test_cases[i].query);
GURL result(cache_.GetGeneralizedCanonicalURL(GURL(query)));
EXPECT_EQ(expected, result.spec()) << " for test_case[" << i << "]";
}
}
TEST_F(TopSitesCacheTest, CacheForcedURLs) {
BuildTopSites(kTopSitesSpecBasic, arraysize(kTopSitesSpecBasic));
top_sites_[0].last_forced_time = base::Time::FromJsTime(1000);
top_sites_[1].last_forced_time = base::Time::FromJsTime(2000);
cache_.SetTopSites(top_sites_);
EXPECT_EQ(2u, cache_.GetNumForcedURLs());
EXPECT_EQ(2u, cache_.GetNumNonForcedURLs());
}
}
}