Chromium Code Reviews| Index: chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider_unittest.cc |
| diff --git a/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider_unittest.cc b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fb0f7e2af778157fd625465df79ef3d47b2051db |
| --- /dev/null |
| +++ b/chrome/browser/ui/app_list/search/suggestions/suggestions_search_provider_unittest.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright 2014 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/ui/app_list/search/suggestions/suggestions_search_provider.h" |
| + |
| +#include <string> |
| + |
|
huangs
2014/12/03 20:36:12
#include <algorithm>
#include <vector>
Mathieu
2014/12/03 21:01:31
Done.
|
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/time/time.h" |
| +#include "chrome/browser/sync/profile_sync_service_mock.h" |
| +#include "chrome/browser/ui/app_list/app_list_test_util.h" |
| +#include "chrome/test/base/testing_pref_service_syncable.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "components/suggestions/proto/suggestions.pb.h" |
| +#include "components/suggestions/suggestions_store.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/app_list/search_result.h" |
| + |
| +using suggestions::ChromeSuggestion; |
| +using suggestions::SuggestionsProfile; |
| +using suggestions::SuggestionsStore; |
| + |
| +namespace app_list { |
| +namespace test { |
| + |
| +namespace { |
| + |
| +// Used to expire suggestions in the future. |
| +const int kTimeGapUsec = 100000; |
|
huangs
2014/12/03 20:36:11
// 0.1 s.
Mathieu
2014/12/03 21:01:30
Done.
|
| + |
| +void AddSuggestion(SuggestionsProfile* suggestions, const char *title, |
|
huangs
2014/12/03 20:36:12
Group "*" with "char".
Mathieu
2014/12/03 21:01:30
Done.
|
| + const char *url) { |
| + ChromeSuggestion* suggestion = suggestions->add_suggestions(); |
| + suggestion->set_url(title); |
| + suggestion->set_title(url); |
| + int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) |
| + .ToInternalValue(); |
| + suggestion->set_expiry_ts(now_usec + kTimeGapUsec); |
| +} |
| + |
| +bool MostRelevant(const SearchResult* result1, const SearchResult* result2) { |
| + return result1->relevance() > result2->relevance(); |
| +} |
| + |
| +} // namespace |
| + |
| +class SuggestionsSearchProviderTest : public AppListTestBase { |
| + public: |
| + SuggestionsSearchProviderTest() {} |
| + ~SuggestionsSearchProviderTest() override {} |
| + |
| + // AppListTestBase overrides: |
| + void SetUp() override { |
| + AppListTestBase::SetUp(); |
| + |
| + profile_.reset(ProfileSyncServiceMock::MakeSignedInTestingProfile()); |
| + TestingPrefServiceSyncable* pref_service = |
| + profile_->GetTestingPrefService(); |
| + SuggestionsStore::RegisterProfilePrefs(pref_service->registry()); |
| + suggestions_store_.reset(new SuggestionsStore(pref_service)); |
| + |
| + // Under test. |
| + suggestions_search_.reset( |
| + new SuggestionsSearchProvider(profile_.get(), NULL)); |
| + } |
| + |
| + void TearDown() override { |
| + // Making sure suggestions are cleared between tests. |
| + suggestions_store_->ClearSuggestions(); |
| + profile_.reset(); |
| + } |
| + |
| + std::string RunQuery(const std::string& query) { |
| + suggestions_search_->Start(base::UTF8ToUTF16(query)); |
| + |
| + // Sort results by relevance. |
|
huangs
2014/12/03 20:36:11
// Sort results from most to least relevant.
Mathieu
2014/12/03 21:01:30
Done.
|
| + std::vector<SearchResult*> sorted_results; |
|
huangs
2014/12/03 20:36:11
Can copy from constructor:
std::vector<SearchResu
Mathieu
2014/12/03 21:01:31
Done.
|
| + std::copy(suggestions_search_->results().begin(), |
| + suggestions_search_->results().end(), |
| + std::back_inserter(sorted_results)); |
| + std::sort(sorted_results.begin(), sorted_results.end(), &MostRelevant); |
| + |
| + std::string result_str; |
| + for (size_t i = 0; i < sorted_results.size(); ++i) { |
| + if (!result_str.empty()) |
| + result_str += ','; |
| + |
| + result_str += base::UTF16ToUTF8(sorted_results[i]->title()); |
| + } |
| + return result_str; |
| + } |
| + |
| + protected: |
| + // Used to store fake suggestions data. |
| + scoped_ptr<SuggestionsStore> suggestions_store_; |
| + |
| + private: |
| + // Under test. |
| + scoped_ptr<SuggestionsSearchProvider> suggestions_search_; |
| + |
| + // Helpers. |
| + ProfileSyncServiceMock* mock_pss_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SuggestionsSearchProviderTest); |
| +}; |
| + |
| +TEST_F(SuggestionsSearchProviderTest, Basic) { |
| + EXPECT_EQ("", RunQuery("anything other than empty string")); |
| + |
| + // Empty query with no suggestions in cache returns nothing. |
| + EXPECT_EQ("", RunQuery("")); |
| +} |
| + |
| +TEST_F(SuggestionsSearchProviderTest, OneSuggestion) { |
| + SuggestionsProfile profile; |
| + AddSuggestion(&profile, "http://foo.com", "title 1"); |
| + EXPECT_TRUE(suggestions_store_->StoreSuggestions(profile)); |
| + |
| + // Empty query with 1 suggestion in cache returns it. |
| + EXPECT_EQ("title 1", RunQuery("")); |
| + |
| + // Non-empty query returns nothing. |
| + EXPECT_EQ("", RunQuery("t")); |
| +} |
| + |
| +TEST_F(SuggestionsSearchProviderTest, ManySuggestions) { |
| + SuggestionsProfile profile; |
| + AddSuggestion(&profile, "http://foo.com", "foo"); |
| + AddSuggestion(&profile, "http://bar.com", "bar"); |
| + EXPECT_TRUE(suggestions_store_->StoreSuggestions(profile)); |
| + |
| + // Empty query with 2 suggestions in cache returns them, with "foo" being more |
|
huangs
2014/12/03 20:36:11
Weird sentence: the empty queue does the returning
Mathieu
2014/12/03 21:01:31
Done.
|
| + // relevant since it came first in the suggestions profile. |
| + EXPECT_EQ("foo,bar", RunQuery("")); |
| + |
| + // Non-empty query returns nothing. |
| + EXPECT_EQ("", RunQuery("t")); |
| +} |
| + |
| +} // namespace test |
| +} // namespace app_list |