Chromium Code Reviews| Index: chrome/browser/autocomplete/bookmark_provider_unittest.cc |
| =================================================================== |
| --- chrome/browser/autocomplete/bookmark_provider_unittest.cc (revision 0) |
| +++ chrome/browser/autocomplete/bookmark_provider_unittest.cc (revision 0) |
| @@ -0,0 +1,219 @@ |
| +// Copyright (c) 2012 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 "chrome/browser/autocomplete/bookmark_provider.h" |
| + |
| +#include <algorithm> |
| +#include <vector> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/autocomplete/autocomplete_provider.h" |
| +#include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| +#include "chrome/browser/bookmarks/bookmark_model.h" |
| +#include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +class BookmarkProviderTest : public testing::Test, |
| + public AutocompleteProviderListener { |
| + public: |
| + BookmarkProviderTest() : model_(new BookmarkModel(NULL)) {} |
| + |
| + // AutocompleteProviderListener: Not called. |
| + virtual void OnProviderUpdate(bool updated_matches) OVERRIDE {} |
| + |
| + protected: |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + // Initializes test data. |
| + void AddBookmarksWithTitles(const char** titles, size_t count); |
| + void AddBookmarksWithTitles(const std::vector<std::string>& titles); |
| + |
| + scoped_ptr<TestingProfile> profile_; |
| + scoped_ptr<BookmarkModel> model_; |
| + scoped_refptr<BookmarkProvider> provider_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(BookmarkProviderTest); |
| +}; |
| + |
| +void BookmarkProviderTest::SetUp() { |
| + profile_.reset(new TestingProfile()); |
| + DCHECK(profile_.get()); |
| + provider_ = new BookmarkProvider(this, profile_.get()); |
| + DCHECK(provider_); |
| + provider_->set_bookmark_model_for_testing(model_.get()); |
| +} |
| + |
| +void BookmarkProviderTest::TearDown() { |
| +} |
| + |
| +void BookmarkProviderTest::AddBookmarksWithTitles(const char** titles, |
| + size_t count) { |
| + std::vector<std::string> title_vector; |
| + for (size_t i = 0; i < count; ++i) |
| + title_vector.push_back(titles[i]); |
| + AddBookmarksWithTitles(title_vector); |
| +} |
| + |
| +void BookmarkProviderTest::AddBookmarksWithTitles( |
| + const std::vector<std::string>& titles) { |
| + GURL url("about:blank"); |
| + for (size_t i = 0; i < titles.size(); ++i) { |
| + model_->AddURL(model_->other_node(), static_cast<int>(i), |
| + ASCIIToUTF16(titles[i]), url); |
| + } |
| +} |
| + |
| +// Structures and functions supporting the BookmarkProviderTest.Positions |
| +// unit test. |
| + |
| +struct TestBookmarkPosition { |
| + TestBookmarkPosition(size_t from, size_t to) |
|
Peter Kasting
2012/10/04 20:40:31
Nit: Use the same arg names as your member names (
mrossetti
2012/10/05 22:10:04
Done.
|
| + : begin(from), end(to) {} |
| + size_t begin; |
| + size_t end; |
| +}; |
| +typedef std::vector<TestBookmarkPosition> TestBookmarkPositions; |
| + |
| +// Comparison function for sorting search terms by descending length. |
| +bool TestBookmarkPositionsEqual(const TestBookmarkPosition& pos_a, |
| + const TestBookmarkPosition& pos_b) { |
| + return pos_a.begin == pos_b.begin && pos_a.end == pos_b.end; |
| +} |
| + |
| +TestBookmarkPositions PositionsFromAutocompleteMatch( |
| + const AutocompleteMatch& match) { |
| + TestBookmarkPositions positions; |
| + bool started = false; |
| + size_t start = 0; |
| + for (AutocompleteMatch::ACMatchClassifications::const_iterator |
| + i = match.description_class.begin(); |
| + i != match.description_class.end(); ++i) { |
| + if (i->style & AutocompleteMatch::ACMatchClassification::MATCH) { |
| + // We have found the start of a match. |
| + EXPECT_FALSE(started); |
| + started = true; |
| + start = i->offset; |
| + } else if (started) { |
| + // We have found the end of a match. |
| + started = false; |
| + positions.push_back(TestBookmarkPosition(start, i->offset)); |
| + start = 0; |
| + } |
| + } |
| + // Record the final position if the last match goes to the end of the |
| + // candidate string. |
| + if (started) |
| + positions.push_back(TestBookmarkPosition(start, match.description.size())); |
| + return positions; |
| +} |
| + |
| +TestBookmarkPositions PositionsFromExpectations( |
| + const size_t expectations[9][2]) { |
| + TestBookmarkPositions positions; |
| + size_t i = 0; |
| + // The array is zero-terminated in the [1]th element. |
| + while (expectations[i][1]) { |
| + positions.push_back( |
| + TestBookmarkPosition(expectations[i][0], expectations[i][1])); |
| + ++i; |
| + } |
| + return positions; |
| +} |
| + |
| +TEST_F(BookmarkProviderTest, Positions) { |
| + // Create the bookmark corpus against which we will simulate searches. |
| + const char* test_data[] = { |
| + "abc def", |
| + "abcde", |
| + "abcdef", |
| + "a definition", |
| + "carry carbon", |
| + }; |
| + AddBookmarksWithTitles(test_data, ARRAYSIZE_UNSAFE(test_data)); |
| + |
| + // Simulate searches. |
| + // Description of |positions|: |
| + // The first index represents the collection of positions for each expected |
| + // match. The count of the actual subarrays in each instance of |query_data| |
| + // must equal |match_count|. The second index represents each expected |
| + // match position. This array must be terminated by an entry with a value |
| + // of '0' for |end|. The third index represents the |start| and |end| of |
| + // the expected match's position within the |test_data|. |
| + // Example: |
| + // Consider the line for 'def' below: |
| + // {"def", 2, {{{4, 7}, {0, 0}}, {{2, 5}, {11, 14}, {0, 0}}}}, |
| + // There are two expected matches: |
| + // 0. {{4, 7}, {0, 0}} |
| + // 1. {{2, 5}, {11 ,14}, {0, 0}} |
| + // For the first match, [0], there is one match within the bookmark's title |
| + // expected, {4,7}, which maps to the 'def' within "abc def". |
| + // The second match, [1], indicates that two matches are expected within |
| + // the bookmark title "a definite definition". In each case, the {0,0} |
| + // indicates the end of the subarray. Or: |
| + // Match #1 Match #2 |
| + // ---------------- -------------------------- |
| + // Pos1 Term Pos1 Pos2 Term |
| + // ------ ------ ------ -------- ------ |
| + // {"def", 2, {{{4, 7}, {0, 0}}, {{2, 5}, {11, 14}, {0, 0}}}}, |
| + // |
| + struct QueryData { |
| + const std::string query; |
| + const size_t match_count; |
| + const size_t positions[99][9][2]; |
| + } query_data[] = { |
| + {"abc", 3, {{{0, 3}, {0, 0}}, |
| + {{0, 3}, {0, 0}}, |
| + {{0, 3}, {0, 0}}}}, |
| + {"foo bar", 0, {{{0, 0}}}}, |
| + {"fooey bark", 0, {{{0, 0}}}}, |
| + {"def", 2, {{{4, 7}, {0, 0}}, |
| + {{2, 5}, {0, 0}}}}, |
| + // NB: GetBookmarksWithTitlesMatching(...) uses exact match for "a". |
| + {"a", 1, {{{0, 1}, {0, 0}}}}, |
| + {"a d", 0, {{{0, 0}}}}, |
| + {"carry carbon", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, |
| + // NB: GetBookmarksWithTitlesMatching(...) sorts the match positions. |
| + {"carbon carry", 1, {{{0, 5}, {6, 12}, {0, 0}}}}, |
| + {"arbon", 0, {{{0, 0}}}}, |
| + {"ar", 0, {{{0, 0}}}}, |
| + {"arry", 0, {{{0, 0}}}}, |
| + }; |
| + |
| + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(query_data); ++i) { |
| + // WHAT??? |
|
Peter Kasting
2012/10/04 20:40:31
??
mrossetti
2012/10/05 22:10:04
Leftovers removed.
|
| + AutocompleteInput input(ASCIIToUTF16(query_data[i].query), |
| + ASCIIToUTF16(""), false, false, false, |
|
Peter Kasting
2012/10/04 20:40:31
Nit: string16()
mrossetti
2012/10/05 22:10:04
Done.
|
| + AutocompleteInput::ALL_MATCHES); |
| + provider_->Start(input, false); |
| + const ACMatches& matches(provider_->matches()); |
| + // Validate number of results is as expected. |
| + EXPECT_EQ(query_data[i].match_count, matches.size()); |
| + if (query_data[i].match_count != matches.size()) { |
| + // Log the actual matches to aid in diagnosis. |
| + LOG(ERROR) << "One or more of the following were unexpected:"; |
| + for (ACMatches::const_iterator j = matches.begin(); j != matches.end(); |
| + ++j) |
| + LOG(ERROR) << " '" << j->description << "'"; |
| + LOG(ERROR) << "For the search term: '" << query_data[i].query << "'"; |
| + } |
| + // Validate positions within each match is as expected. |
| + for (size_t j = 0; j <matches.size(); ++j) { |
| + // Collect the expected positions as a vector, collect the match's |
| + // classifications for match positions as a vector, then compare. |
| + TestBookmarkPositions expected_positions( |
| + PositionsFromExpectations(query_data[i].positions[j])); |
| + TestBookmarkPositions actual_positions( |
| + PositionsFromAutocompleteMatch(matches[j])); |
| + EXPECT_TRUE(std::equal(expected_positions.begin(), |
| + expected_positions.end(), |
| + actual_positions.begin(), |
| + TestBookmarkPositionsEqual)); |
| + } |
| + } |
| +} |
| Property changes on: chrome/browser/autocomplete/bookmark_provider_unittest.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |