OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/autocomplete/bookmark_provider.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 14 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 16 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 class BookmarkProviderTest : public testing::Test, |
| 21 public AutocompleteProviderListener { |
| 22 public: |
| 23 BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {} |
| 24 |
| 25 // AutocompleteProviderListener: Not called. |
| 26 virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} |
| 27 |
| 28 protected: |
| 29 virtual void SetUp() OVERRIDE; |
| 30 virtual void TearDown() OVERRIDE; |
| 31 |
| 32 // Initializes test data. |
| 33 void AddBookmarksWithTitles(const char** titles, size_t count); |
| 34 void AddBookmarksWithTitles(const std::vector<std::string>& titles); |
| 35 |
| 36 scoped_ptr<TestingProfile> profile_; |
| 37 scoped_ptr<BookmarkModel> model_; |
| 38 scoped_refptr<BookmarkProvider> provider_; |
| 39 |
| 40 private: |
| 41 DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); |
| 42 }; |
| 43 |
| 44 void BookmarkProviderTest::SetUp() { |
| 45 profile_.reset(new TestingProfile()); |
| 46 DCHECK(profile_.get()); |
| 47 provider_ = new BookmarkProvider(this, profile_.get()); |
| 48 DCHECK(provider_); |
| 49 provider_->set_bookmark_model_for_testing(model_.get()); |
| 50 } |
| 51 |
| 52 void BookmarkProviderTest::TearDown() { |
| 53 } |
| 54 |
| 55 void BookmarkProviderTest::AddBookmarksWithTitles(const char** titles, |
| 56 size_t count) { |
| 57 std::vector<std::string> title_vector; |
| 58 for (size_t i = 0; i < count; ++i) |
| 59 title_vector.push_back(titles[i]); |
| 60 AddBookmarksWithTitles(title_vector); |
| 61 } |
| 62 |
| 63 void BookmarkProviderTest::AddBookmarksWithTitles( |
| 64 const std::vector<std::string>& titles) { |
| 65 GURL url("about:blank"); |
| 66 for (size_t i = 0; i < titles.size(); ++i) { |
| 67 model_->AddURL(model_->other_node(), static_cast<int>(i), |
| 68 ASCIIToUTF16(titles[i]), url); |
| 69 } |
| 70 } |
| 71 |
| 72 // Structures and functions supporting the BookmarkProviderTest.Positions |
| 73 // unit test. |
| 74 |
| 75 struct TestBookmarkPosition { |
| 76 TestBookmarkPosition(size_t from, size_t to) |
| 77 : begin(from), end(to) {} |
| 78 size_t begin; |
| 79 size_t end; |
| 80 }; |
| 81 typedef std::vector<TestBookmarkPosition> TestBookmarkPositions; |
| 82 |
| 83 // Comparison function for sorting search terms by descending length. |
| 84 bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a, |
| 85 const TestBookmarkPosition& pos_b) { |
| 86 return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end; |
| 87 } |
| 88 |
| 89 TestBookmarkPositions PositionsFromAutocompleteMatch( |
| 90 const AutocompleteMatch& match) { |
| 91 TestBookmarkPositions positions; |
| 92 bool started = false; |
| 93 size_t start = 0; |
| 94 for (AutocompleteMatch::ACMatchClassifications::const_iterator |
| 95 i = match.description_class.begin(); |
| 96 i != match.description_class.end(); ++i) { |
| 97 if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) { |
| 98 // We have found the start of a match. |
| 99 EXPECT_FALSE(started); |
| 100 started = true; |
| 101 start = i->offset; |
| 102 } else if (started) { |
| 103 // We have found the end of a match. |
| 104 started = false; |
| 105 positions.push_back(TestBookmarkPosition(start, i->offset)); |
| 106 start = 0; |
| 107 } |
| 108 } |
| 109 // Record the final position if the last match goes to the end of the |
| 110 // candidate string. |
| 111 if (started) |
| 112 positions.push_back(TestBookmarkPosition(start, match.description.size())); |
| 113 return positions; |
| 114 } |
| 115 |
| 116 TestBookmarkPositions PositionsFromExpectations( |
| 117 const size_t expectations[9][2]) { |
| 118 TestBookmarkPositions positions; |
| 119 size_t i = 0; |
| 120 // The array is zero-terminated in the [1]th element. |
| 121 while (expectations[i][1]) { |
| 122 positions.push_back( |
| 123 TestBookmarkPosition(expectations[i][0], expectations[i][1])); |
| 124 ++i; |
| 125 } |
| 126 return positions; |
| 127 } |
| 128 |
| 129 TEST_F(BookmarkProviderTest, Positions) { |
| 130 // Create the bookmark corpus against which we will simulate searches. |
| 131 const char* test_data[] = { |
| 132 "abc def", |
| 133 "abcde", |
| 134 "abcdef", |
| 135 "a definition", |
| 136 "carry carbon", |
| 137 }; |
| 138 AddBookmarksWithTitles(test_data, ARRAYSIZE_UNSAFE(test_data)); |
| 139 |
| 140 // Simulate searches. |
| 141 // Description of |positions|: |
| 142 // The first index represents the collection of positions for each expected |
| 143 // match. The count of the actual subarrays in each instance of |query_data| |
| 144 // must equal |match_count|. The second index represents each expected |
| 145 // match position. This array must be terminated by an entry with a value |
| 146 // of '0' for |end|. The third index represents the |start| and |end| of |
| 147 // the expected match's position within the |test_data|. |
| 148 // Example: |
| 149 // Consider the line for 'def' below: |
| 150 // { "def", 2, {{{4,7},{0,0}}, {{2,5},{11,14},{0,0}}} }, |
| 151 // There are two expected matches: |
| 152 // 0. {{4,7},{0,0}} |
| 153 // 1. {{2,5},{11,14},{0,0}} |
| 154 // For the first match, [0], there is one match within the bookmark's title |
| 155 // expected, {4,7}, which maps to the 'def' within "abc def". |
| 156 // The second match, [1], indicates that two matches are expected within |
| 157 // the bookmark title "a definite definition". |
| 158 // In each case, the {0,0} indicates the end of the subarray. |
| 159 struct QueryData { |
| 160 const std::string query; |
| 161 const size_t match_count; |
| 162 const size_t positions[99][9][2]; |
| 163 } query_data[] = { |
| 164 { "abc", 3, {{{0,3},{0,0}}, {{0,3},{0,0}}, {{0,3},{0,0}}} }, |
| 165 { "foo bar", 0, {{{0,0}}} }, |
| 166 { "fooey bark", 0, {{{0,0}}} }, |
| 167 { "def", 2, {{{4,7},{0,0}}, {{2,5},{0,0}}} }, |
| 168 // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a". |
| 169 { "a", 1, {{{0,1},{0,0}}} }, |
| 170 { "a d", 0, {{{0,0}}} }, |
| 171 { "carry carbon", 1, {{{0,5},{6,12},{0,0}}} }, |
| 172 // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions. |
| 173 { "carbon carry", 1, {{{0,5},{6,12},{0,0}}} }, |
| 174 { "arbon", 0, {{{0,0}}} }, |
| 175 { "ar", 0, {{{0,0}}} }, |
| 176 { "arry", 0, {{{0,0}}} }, |
| 177 }; |
| 178 |
| 179 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
| 180 // WHAT??? |
| 181 AutocompleteInput input(ASCIIToUTF16(query_data[i].query), |
| 182 ASCIIToUTF16(""), false, false, false, |
| 183 AutocompleteInput::ALL_MATCHES); |
| 184 provider_->Start(input, false); |
| 185 const ACMatches& matches(provider_->matches()); |
| 186 // Validate number of results is as expected. |
| 187 EXPECT_EQ(query_data[i].match_count, matches.size()); |
| 188 if (query_data[i].match_count != matches.size()) { |
| 189 // Log the actual matches to aid in diagnosis. |
| 190 LOG(ERROR) << "One or more of the following were unexpected:"; |
| 191 for (ACMatches::const_iterator j = matches.begin(); j != matches.end(); |
| 192 ++j) |
| 193 LOG(ERROR) << " '" << j->description << "'"; |
| 194 LOG(ERROR) << "For the search term: '" << query_data[i].query << "'"; |
| 195 } |
| 196 // Validate positions within each match is as expected. |
| 197 for (size_t j = 0; j <matches.size(); ++j) { |
| 198 // Collect the expected positions as a vector, collect the match's |
| 199 // classifications for match positions as a vector, then compare. |
| 200 TestBookmarkPositions expected_positions( |
| 201 PositionsFromExpectations(query_data[i].positions[j])); |
| 202 TestBookmarkPositions actual_positions( |
| 203 PositionsFromAutocompleteMatch(matches[j])); |
| 204 EXPECT_TRUE(std::equal(expected_positions.begin(), |
| 205 expected_positions.end(), |
| 206 actual_positions.begin(), |
| 207 TestBookmarkPositionsEqual)); |
| 208 } |
| 209 } |
| 210 } |
OLD | NEW |