| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/bookmarks/bookmark_index.h" | 5 #include "chrome/browser/bookmarks/bookmark_index.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model.h" | 15 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" | 17 #include "chrome/browser/bookmarks/bookmark_test_helpers.h" |
| 18 #include "chrome/browser/history/history_service.h" | 18 #include "chrome/browser/history/history_service.h" |
| 19 #include "chrome/browser/history/history_service_factory.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
| 20 #include "chrome/browser/history/url_database.h" | 20 #include "chrome/browser/history/url_database.h" |
| 21 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 22 #include "components/bookmarks/core/browser/bookmark_client.h" |
| 22 #include "components/bookmarks/core/browser/bookmark_title_match.h" | 23 #include "components/bookmarks/core/browser/bookmark_title_match.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 using base::ASCIIToUTF16; | 27 using base::ASCIIToUTF16; |
| 27 | 28 |
| 28 class BookmarkIndexTest : public testing::Test { | 29 class BookmarkIndexTest : public testing::Test { |
| 29 public: | 30 public: |
| 30 BookmarkIndexTest() : model_(new BookmarkModel(NULL)) {} | 31 BookmarkIndexTest() { ResetBookmarkMarkModel(); } |
| 31 | 32 |
| 32 void AddBookmarksWithTitles(const char** titles, size_t count) { | 33 void AddBookmarksWithTitles(const char** titles, size_t count) { |
| 33 std::vector<std::string> title_vector; | 34 std::vector<std::string> title_vector; |
| 34 for (size_t i = 0; i < count; ++i) | 35 for (size_t i = 0; i < count; ++i) |
| 35 title_vector.push_back(titles[i]); | 36 title_vector.push_back(titles[i]); |
| 36 AddBookmarksWithTitles(title_vector); | 37 AddBookmarksWithTitles(title_vector); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void AddBookmarksWithTitles(const std::vector<std::string>& titles) { | 40 void AddBookmarksWithTitles(const std::vector<std::string>& titles) { |
| 40 GURL url("about:blank"); | 41 GURL url("about:blank"); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 ASSERT_EQ(1U, matches.size()); | 96 ASSERT_EQ(1U, matches.size()); |
| 96 const BookmarkTitleMatch& match = matches[0]; | 97 const BookmarkTitleMatch& match = matches[0]; |
| 97 ASSERT_EQ(expected_positions.size(), match.match_positions.size()); | 98 ASSERT_EQ(expected_positions.size(), match.match_positions.size()); |
| 98 for (size_t i = 0; i < expected_positions.size(); ++i) { | 99 for (size_t i = 0; i < expected_positions.size(); ++i) { |
| 99 EXPECT_EQ(expected_positions[i].first, match.match_positions[i].first); | 100 EXPECT_EQ(expected_positions[i].first, match.match_positions[i].first); |
| 100 EXPECT_EQ(expected_positions[i].second, match.match_positions[i].second); | 101 EXPECT_EQ(expected_positions[i].second, match.match_positions[i].second); |
| 101 } | 102 } |
| 102 } | 103 } |
| 103 | 104 |
| 104 protected: | 105 protected: |
| 106 test::TestBookmarkClient client_; |
| 105 scoped_ptr<BookmarkModel> model_; | 107 scoped_ptr<BookmarkModel> model_; |
| 106 | 108 |
| 109 void ResetBookmarkMarkModel() { model_.reset(client_.CreateModel()); } |
| 110 |
| 107 private: | 111 private: |
| 108 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest); | 112 DISALLOW_COPY_AND_ASSIGN(BookmarkIndexTest); |
| 109 }; | 113 }; |
| 110 | 114 |
| 111 // Various permutations with differing input, queries and output that exercises | 115 // Various permutations with differing input, queries and output that exercises |
| 112 // all query paths. | 116 // all query paths. |
| 113 TEST_F(BookmarkIndexTest, Tests) { | 117 TEST_F(BookmarkIndexTest, Tests) { |
| 114 struct TestData { | 118 struct TestData { |
| 115 const std::string input; | 119 const std::string input; |
| 116 const std::string query; | 120 const std::string query; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 145 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 149 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 146 std::vector<std::string> titles; | 150 std::vector<std::string> titles; |
| 147 base::SplitString(data[i].input, ';', &titles); | 151 base::SplitString(data[i].input, ';', &titles); |
| 148 AddBookmarksWithTitles(titles); | 152 AddBookmarksWithTitles(titles); |
| 149 | 153 |
| 150 std::vector<std::string> expected; | 154 std::vector<std::string> expected; |
| 151 if (!data[i].expected.empty()) | 155 if (!data[i].expected.empty()) |
| 152 base::SplitString(data[i].expected, ';', &expected); | 156 base::SplitString(data[i].expected, ';', &expected); |
| 153 | 157 |
| 154 ExpectMatches(data[i].query, expected); | 158 ExpectMatches(data[i].query, expected); |
| 155 | 159 ResetBookmarkMarkModel(); |
| 156 model_.reset(new BookmarkModel(NULL)); | |
| 157 } | 160 } |
| 158 } | 161 } |
| 159 | 162 |
| 160 TEST_F(BookmarkIndexTest, TestNormalization) { | 163 TEST_F(BookmarkIndexTest, TestNormalization) { |
| 161 struct TestData { | 164 struct TestData { |
| 162 const char* title; | 165 const char* title; |
| 163 const char* query; | 166 const char* query; |
| 164 } data[] = { | 167 } data[] = { |
| 165 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, | 168 { "fooa\xcc\x88-test", "foo\xc3\xa4-test" }, |
| 166 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, | 169 { "fooa\xcc\x88-test", "fooa\xcc\x88-test" }, |
| 167 { "fooa\xcc\x88-test", "foo\xc3\xa4" }, | 170 { "fooa\xcc\x88-test", "foo\xc3\xa4" }, |
| 168 { "fooa\xcc\x88-test", "fooa\xcc\x88" }, | 171 { "fooa\xcc\x88-test", "fooa\xcc\x88" }, |
| 169 { "fooa\xcc\x88-test", "foo" }, | 172 { "fooa\xcc\x88-test", "foo" }, |
| 170 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" }, | 173 { "foo\xc3\xa4-test", "foo\xc3\xa4-test" }, |
| 171 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" }, | 174 { "foo\xc3\xa4-test", "fooa\xcc\x88-test" }, |
| 172 { "foo\xc3\xa4-test", "foo\xc3\xa4" }, | 175 { "foo\xc3\xa4-test", "foo\xc3\xa4" }, |
| 173 { "foo\xc3\xa4-test", "fooa\xcc\x88" }, | 176 { "foo\xc3\xa4-test", "fooa\xcc\x88" }, |
| 174 { "foo\xc3\xa4-test", "foo" }, | 177 { "foo\xc3\xa4-test", "foo" }, |
| 175 { "foo", "foo" } | 178 { "foo", "foo" } |
| 176 }; | 179 }; |
| 177 | 180 |
| 178 GURL url("about:blank"); | 181 GURL url("about:blank"); |
| 179 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 182 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 180 model_->AddURL(model_->other_node(), 0, base::UTF8ToUTF16(data[i].title), | 183 model_->AddURL(model_->other_node(), 0, base::UTF8ToUTF16(data[i].title), |
| 181 url); | 184 url); |
| 182 std::vector<BookmarkTitleMatch> matches; | 185 std::vector<BookmarkTitleMatch> matches; |
| 183 model_->GetBookmarksWithTitlesMatching( | 186 model_->GetBookmarksWithTitlesMatching( |
| 184 base::UTF8ToUTF16(data[i].query), 10, &matches); | 187 base::UTF8ToUTF16(data[i].query), 10, &matches); |
| 185 EXPECT_EQ(1u, matches.size()); | 188 EXPECT_EQ(1u, matches.size()); |
| 186 model_.reset(new BookmarkModel(NULL)); | 189 ResetBookmarkMarkModel(); |
| 187 } | 190 } |
| 188 } | 191 } |
| 189 | 192 |
| 190 // Makes sure match positions are updated appropriately. | 193 // Makes sure match positions are updated appropriately. |
| 191 TEST_F(BookmarkIndexTest, MatchPositions) { | 194 TEST_F(BookmarkIndexTest, MatchPositions) { |
| 192 struct TestData { | 195 struct TestData { |
| 193 const std::string title; | 196 const std::string title; |
| 194 const std::string query; | 197 const std::string query; |
| 195 const std::string expected; | 198 const std::string expected; |
| 196 } data[] = { | 199 } data[] = { |
| 197 // Trivial test case of only one term, exact match. | 200 // Trivial test case of only one term, exact match. |
| 198 { "a", "A", "0,1" }, | 201 { "a", "A", "0,1" }, |
| 199 { "foo bar", "bar", "4,7" }, | 202 { "foo bar", "bar", "4,7" }, |
| 200 { "fooey bark", "bar foo", "0,3:6,9"}, | 203 { "fooey bark", "bar foo", "0,3:6,9"}, |
| 201 // Non-trivial tests. | 204 // Non-trivial tests. |
| 202 { "foobar foo", "foobar foo", "0,6:7,10" }, | 205 { "foobar foo", "foobar foo", "0,6:7,10" }, |
| 203 { "foobar foo", "foo foobar", "0,6:7,10" }, | 206 { "foobar foo", "foo foobar", "0,6:7,10" }, |
| 204 { "foobar foobar", "foobar foo", "0,6:7,13" }, | 207 { "foobar foobar", "foobar foo", "0,6:7,13" }, |
| 205 { "foobar foobar", "foo foobar", "0,6:7,13" }, | 208 { "foobar foobar", "foo foobar", "0,6:7,13" }, |
| 206 }; | 209 }; |
| 207 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { | 210 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(data); ++i) { |
| 208 std::vector<std::string> titles; | 211 std::vector<std::string> titles; |
| 209 titles.push_back(data[i].title); | 212 titles.push_back(data[i].title); |
| 210 AddBookmarksWithTitles(titles); | 213 AddBookmarksWithTitles(titles); |
| 211 | 214 |
| 212 BookmarkTitleMatch::MatchPositions expected_matches; | 215 BookmarkTitleMatch::MatchPositions expected_matches; |
| 213 ExtractMatchPositions(data[i].expected, &expected_matches); | 216 ExtractMatchPositions(data[i].expected, &expected_matches); |
| 214 ExpectMatchPositions(data[i].query, expected_matches); | 217 ExpectMatchPositions(data[i].query, expected_matches); |
| 215 | 218 |
| 216 model_.reset(new BookmarkModel(NULL)); | 219 ResetBookmarkMarkModel(); |
| 217 } | 220 } |
| 218 } | 221 } |
| 219 | 222 |
| 220 // Makes sure index is updated when a node is removed. | 223 // Makes sure index is updated when a node is removed. |
| 221 TEST_F(BookmarkIndexTest, Remove) { | 224 TEST_F(BookmarkIndexTest, Remove) { |
| 222 const char* input[] = { "a", "b" }; | 225 const char* input[] = { "a", "b" }; |
| 223 AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input)); | 226 AddBookmarksWithTitles(input, ARRAYSIZE_UNSAFE(input)); |
| 224 | 227 |
| 225 // Remove the node and make sure we don't get back any results. | 228 // Remove the node and make sure we don't get back any results. |
| 226 model_->Remove(model_->other_node(), 0); | 229 model_->Remove(model_->other_node(), 0); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 EXPECT_EQ(data[1].url, matches[3].node->url()); | 338 EXPECT_EQ(data[1].url, matches[3].node->url()); |
| 336 | 339 |
| 337 matches.clear(); | 340 matches.clear(); |
| 338 // Select top two matches. | 341 // Select top two matches. |
| 339 model->GetBookmarksWithTitlesMatching(ASCIIToUTF16("google"), 2, &matches); | 342 model->GetBookmarksWithTitlesMatching(ASCIIToUTF16("google"), 2, &matches); |
| 340 | 343 |
| 341 EXPECT_EQ(2, static_cast<int>(matches.size())); | 344 EXPECT_EQ(2, static_cast<int>(matches.size())); |
| 342 EXPECT_EQ(data[0].url, matches[0].node->url()); | 345 EXPECT_EQ(data[0].url, matches[0].node->url()); |
| 343 EXPECT_EQ(data[3].url, matches[1].node->url()); | 346 EXPECT_EQ(data[3].url, matches[1].node->url()); |
| 344 } | 347 } |
| OLD | NEW |