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
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include "base/string_util.h"
#include "chrome/browser/bookmarks/bookmark_index.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#include "chrome/browser/history/query_parser.h"
#include "testing/gtest/include/gtest/gtest.h"
class BookmarkIndexTest : public testing::Test {
public:
BookmarkIndexTest() : model_(new BookmarkModel(NULL)) {}
void AddBookmarksWithTitles(const wchar_t** titles, size_t count) {
std::vector<std::wstring> title_vector;
for (size_t i = 0; i < count; ++i)
title_vector.push_back(titles[i]);
AddBookmarksWithTitles(title_vector);
}
void AddBookmarksWithTitles(const std::vector<std::wstring>& titles) {
GURL url("about:blank");
for (size_t i = 0; i < titles.size(); ++i)
model_->AddURL(model_->other_node(), static_cast<int>(i), titles[i], url);
}
void ExpectMatches(const std::wstring& query,
const wchar_t** expected_titles,
size_t expected_count) {
std::vector<std::wstring> title_vector;
for (size_t i = 0; i < expected_count; ++i)
title_vector.push_back(expected_titles[i]);
ExpectMatches(query, title_vector);
}
void ExpectMatches(const std::wstring& query,
const std::vector<std::wstring> expected_titles) {
std::vector<bookmark_utils::TitleMatch> matches;
model_->GetBookmarksWithTitlesMatching(query, 1000, &matches);
ASSERT_EQ(expected_titles.size(), matches.size());
for (size_t i = 0; i < expected_titles.size(); ++i) {
bool found = false;
for (size_t j = 0; j < matches.size(); ++j) {
if (std::wstring(expected_titles[i]) == matches[j].node->GetTitle()) {
matches.erase(matches.begin() + j);
found = true;
break;
}
}
ASSERT_TRUE(found);
}
}
void ExtractMatchPositions(const std::string& string,
Snippet::MatchPositions* matches) {
std::vector<std::string> match_strings;
SplitString(string, L':', &match_strings);
for (size_t i = 0; i < match_strings.size(); ++i) {
std::vector<std::string> chunks;
SplitString(match_strings[i], ',', &chunks);
ASSERT_EQ(2U, chunks.size());
matches->push_back(Snippet::MatchPosition());
matches->back().first = StringToInt(chunks[0]);
matches->back().second = StringToInt(chunks[1]);
}
}
void ExpectMatchPositions(const std::wstring& query,
const Snippet::MatchPositions& expected_positions) {
std::vector<bookmark_utils::TitleMatch> matches;
model_->GetBookmarksWithTitlesMatching(query, 1000, &matches);
ASSERT_EQ(1U, matches.size());
const bookmark_utils::TitleMatch& match = matches[0];
ASSERT_EQ(expected_positions.size(), match.match_positions.size());
for (size_t i = 0; i < expected_positions.size(); ++i) {
EXPECT_EQ(expected_positions[i].first, match.match_positions[i].first);
EXPECT_EQ(expected_positions[i].second, match.match_positions[i].second);
}
}
protected:
scoped_ptr<BookmarkModel> model_;
private:
DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest);
};
// Various permutations with differing input, queries and output that exercises
// all query paths.
TEST_F(BookmarkIndexTest, Tests) {
struct TestData {
const std::wstring input;
const std::wstring query;
const std::wstring expected;
} data[] = {
// Trivial test case of only one term, exact match.
{ L"a;b", L"A", L"a" },
// Prefix match, one term.
{ L"abcd;abc;b", L"abc", L"abcd;abc" },
// Prefix match, multiple terms.
{ L"abcd cdef;abcd;abcd cdefg", L"abc cde", L"abcd cdef;abcd cdefg"},
// Exact and prefix match.
{ L"ab cdef;abcd;abcd cdefg", L"ab cdef", L"ab cdef"},
// Exact and prefix match.
{ L"ab cdef ghij;ab;cde;cdef;ghi;cdef ab;ghij ab",
L"ab cde ghi",
L"ab cdef ghij"},
// Title with term multiple times.
{ L"ab ab", L"ab", L"ab ab"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
std::vector<std::wstring> titles;
SplitString(data[i].input, L';', &titles);
AddBookmarksWithTitles(titles);
std::vector<std::wstring> expected;
SplitString(data[i].expected, L';', &expected);
ExpectMatches(data[i].query, expected);
model_.reset(new BookmarkModel(NULL));
}
}
// Makes sure match positions are updated appropriately.
TEST_F(BookmarkIndexTest, MatchPositions) {
struct TestData {
const std::wstring title;
const std::wstring query;
const std::string expected;
} data[] = {
// Trivial test case of only one term, exact match.
{ L"a", L"A", "0,1" },
{ L"foo bar", L"bar", "4,7" },
{ L"fooey bark", L"bar foo", "0,3:6,9"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) {
std::vector<std::wstring> titles;
titles.push_back(data[i].title);
AddBookmarksWithTitles(titles);
Snippet::MatchPositions expected_matches;
ExtractMatchPositions(data[i].expected, &expected_matches);
ExpectMatchPositions(data[i].query, expected_matches);
model_.reset(new BookmarkModel(NULL));
}
}
// Makes sure index is updated when a node is removed.
TEST_F(BookmarkIndexTest, Remove) {
const wchar_t* input[] = { L"a", L"b" };
AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input));
// Remove the node and make sure we don't get back any results.
model_->Remove(model_->other_node(), 0);
ExpectMatches(L"A", NULL, 0U);
}
// Makes sure index is updated when a node's title is changed.
TEST_F(BookmarkIndexTest, ChangeTitle) {
const wchar_t* input[] = { L"a", L"b" };
AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input));
// Remove the node and make sure we don't get back any results.
const wchar_t* expected[] = { L"blah" };
model_->SetTitle(model_->other_node()->GetChild(0), L"blah");
ExpectMatches(L"BlAh", expected, ARRAYSIZE_UNSAFE(expected));
}
// Makes sure no more than max queries is returned.
TEST_F(BookmarkIndexTest, HonorMax) {
const wchar_t* input[] = { L"abcd", L"abcde" };
AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input));
std::vector<bookmark_utils::TitleMatch> matches;
model_->GetBookmarksWithTitlesMatching(L"ABc", 1, &matches);
EXPECT_EQ(1U, matches.size());
}
// Makes sure if the lower case string of a bookmark title is more characters
// than the upper case string no match positions are returned.
TEST_F(BookmarkIndexTest, EmptyMatchOnMultiwideLowercaseString) {
BookmarkNode* n1 = model_->AddURL(model_->other_node(), 0, L"\u0130 i",
GURL("http://www.google.com"));
std::vector<bookmark_utils::TitleMatch> matches;
model_->GetBookmarksWithTitlesMatching(L"i", 100, &matches);
ASSERT_EQ(1U, matches.size());
EXPECT_TRUE(matches[0].node == n1);
EXPECT_TRUE(matches[0].match_positions.empty());
}
|