1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/android/history_report/delta_file_commons.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/history/core/browser/history_types.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using bookmarks::BookmarkModel
;
14 namespace history_report
{
16 class DeltaFileEntryWithDataTest
: public testing::Test
{
18 DeltaFileEntryWithDataTest()
21 ~DeltaFileEntryWithDataTest() override
{}
24 void SetUp() override
{}
26 DeltaFileEntry entry_
;
27 DeltaFileEntryWithData data_
;
30 DISALLOW_COPY_AND_ASSIGN(DeltaFileEntryWithDataTest
);
33 TEST_F(DeltaFileEntryWithDataTest
, NotValid
) {
34 EXPECT_FALSE(data_
.Valid());
37 TEST_F(DeltaFileEntryWithDataTest
, ValidDelEntry
) {
39 entry
.set_type("del");
40 DeltaFileEntryWithData
data(entry
);
41 EXPECT_TRUE(data
.Valid());
44 TEST_F(DeltaFileEntryWithDataTest
, DelEntryWithData
) {
46 entry
.set_type("del");
47 DeltaFileEntryWithData
data(entry
);
49 row
.set_hidden(false);
51 EXPECT_TRUE(data
.Valid());
52 // If deletion entry turns out to have data
53 // then it's an update entry instead.
54 EXPECT_EQ("add", data
.Type());
57 TEST_F(DeltaFileEntryWithDataTest
, Valid
) {
59 row
.set_hidden(false);
61 EXPECT_TRUE(data_
.Valid());
64 TEST_F(DeltaFileEntryWithDataTest
, Hidden
) {
68 EXPECT_FALSE(data_
.Valid());
71 TEST_F(DeltaFileEntryWithDataTest
, NoBookmarkScore
) {
73 row
.set_hidden(false);
74 row
.set_typed_count(2);
75 row
.set_visit_count(3);
77 EXPECT_TRUE(data_
.Valid());
78 EXPECT_EQ(5, data_
.Score());
81 TEST_F(DeltaFileEntryWithDataTest
, BookmarkScore
) {
82 BookmarkModel::URLAndTitle bookmark
;
84 row
.set_hidden(false);
85 row
.set_typed_count(2);
86 row
.set_visit_count(3);
88 data_
.MarkAsBookmark(bookmark
);
89 EXPECT_TRUE(data_
.Valid());
90 EXPECT_EQ(18, data_
.Score());
93 TEST_F(DeltaFileEntryWithDataTest
, NoBookmarkEmptyTitle
) {
94 history::URLRow
row(GURL("http://www.host.org/path?query=param"));
95 row
.set_title(base::UTF8ToUTF16(""));
96 row
.set_hidden(false);
98 EXPECT_TRUE(data_
.Valid());
99 EXPECT_EQ(base::UTF8ToUTF16("www.host.org"), data_
.Title());
102 TEST_F(DeltaFileEntryWithDataTest
, NoBookmarkNonEmptyTitle
) {
103 history::URLRow
row(GURL("http://host.org/path?query=param"));
104 row
.set_title(base::UTF8ToUTF16("title"));
105 row
.set_hidden(false);
107 EXPECT_TRUE(data_
.Valid());
108 EXPECT_EQ(base::UTF8ToUTF16("title"), data_
.Title());
111 TEST_F(DeltaFileEntryWithDataTest
, BookmarkTitle
) {
112 BookmarkModel::URLAndTitle bookmark
;
113 bookmark
.title
= base::UTF8ToUTF16("bookmark_title");
114 history::URLRow
row(GURL("http://host.org/path?query=param"));
115 row
.set_title(base::UTF8ToUTF16("title"));
116 row
.set_hidden(false);
118 data_
.MarkAsBookmark(bookmark
);
119 EXPECT_TRUE(data_
.Valid());
120 EXPECT_EQ(base::UTF8ToUTF16("bookmark_title"), data_
.Title());
123 TEST_F(DeltaFileEntryWithDataTest
, TrimWWWPrefix
) {
124 history::URLRow
row(GURL("http://www.host.org/path?query=param"));
125 row
.set_hidden(false);
127 EXPECT_TRUE(data_
.Valid());
128 EXPECT_EQ("host", data_
.IndexedUrl());
131 TEST_F(DeltaFileEntryWithDataTest
, TrimWW2Prefix
) {
132 history::URLRow
row(GURL("http://ww2.host.org/path?query=param"));
133 row
.set_hidden(false);
135 EXPECT_TRUE(data_
.Valid());
136 EXPECT_EQ("host", data_
.IndexedUrl());
139 TEST_F(DeltaFileEntryWithDataTest
, TrimComSuffix
) {
140 history::URLRow
row(GURL("http://host.com/path?query=param"));
141 row
.set_hidden(false);
143 EXPECT_TRUE(data_
.Valid());
144 EXPECT_EQ("host", data_
.IndexedUrl());
147 TEST_F(DeltaFileEntryWithDataTest
, TrimCoUKSuffix
) {
148 history::URLRow
row(GURL("http://host.co.uk/path?query=param"));
149 row
.set_hidden(false);
151 EXPECT_TRUE(data_
.Valid());
152 EXPECT_EQ("host", data_
.IndexedUrl());
155 TEST_F(DeltaFileEntryWithDataTest
, TrimOrgKSuffix
) {
156 history::URLRow
row(GURL("http://host.org/path?query=param"));
157 row
.set_hidden(false);
159 EXPECT_TRUE(data_
.Valid());
160 EXPECT_EQ("host", data_
.IndexedUrl());
163 TEST_F(DeltaFileEntryWithDataTest
, TrimRegionalSuffix
) {
164 history::URLRow
row(GURL("http://host.waw.pl/path?query=param"));
165 row
.set_hidden(false);
167 EXPECT_TRUE(data_
.Valid());
168 EXPECT_EQ("host", data_
.IndexedUrl());
171 TEST_F(DeltaFileEntryWithDataTest
, TrimPrivateDomainSuffix
) {
172 history::URLRow
row(GURL("http://host.appspot.com/path?query=param"));
173 row
.set_hidden(false);
175 EXPECT_TRUE(data_
.Valid());
176 EXPECT_EQ("host.appspot", data_
.IndexedUrl());
179 TEST_F(DeltaFileEntryWithDataTest
, IdForShortUrl
) {
180 std::string
short_url("http://this.is.a.short.url.dot.com");
182 EXPECT_TRUE(DeltaFileEntryWithData::IsValidId(short_url
));
184 DeltaFileEntry entry
;
185 entry
.set_url(short_url
);
186 DeltaFileEntryWithData
data(entry
);
188 EXPECT_EQ(short_url
, data
.Id());
191 TEST_F(DeltaFileEntryWithDataTest
, IdForLongUrl
) {
193 std::stringstream
url("http://domain.com/");
195 while (DeltaFileEntryWithData::IsValidId(url
.str())) {
199 EXPECT_FALSE(DeltaFileEntryWithData::IsValidId(url
.str()));
201 DeltaFileEntry entry
;
202 entry
.set_url(url
.str());
203 DeltaFileEntryWithData
data(entry
);
205 EXPECT_NE(url
.str(), data
.Id());
206 EXPECT_TRUE(DeltaFileEntryWithData::IsValidId(data
.Id()))
207 << data
.Id() << " is not a valid ID";
209 std::stringstream expected_length_stream
;
210 expected_length_stream
<< std::setfill('0') << std::setw(8)
211 << base::Uint64ToString(static_cast<uint64
>(url
.str().size()));
212 std::string length
= data
.Id().substr(0, 8);
213 EXPECT_EQ(expected_length_stream
.str(), length
);
215 std::string prefix
= data
.Id().substr(72);
216 std::string
expected_prefix(url
.str().substr(0, prefix
.size()));
217 EXPECT_EQ(expected_prefix
, prefix
);
220 } // namespace history_report