Index: components/omnibox/browser/shortcuts_provider_test_util.cc |
diff --git a/components/omnibox/browser/shortcuts_provider_test_util.cc b/components/omnibox/browser/shortcuts_provider_test_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..79840dfcaf0d72db1ca086083dc0c4c6f2c29575 |
--- /dev/null |
+++ b/components/omnibox/browser/shortcuts_provider_test_util.cc |
@@ -0,0 +1,138 @@ |
+// Copyright 2016 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 "components/omnibox/browser/shortcuts_provider_test_util.h" |
+ |
+#include "base/message_loop/message_loop.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "components/omnibox/browser/autocomplete_match.h" |
+#include "components/omnibox/browser/shortcuts_backend.h" |
+#include "components/omnibox/browser/shortcuts_provider.h" |
+#include "components/omnibox/browser/test_scheme_classifier.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using base::ASCIIToUTF16; |
Peter Kasting
2016/02/03 00:31:47
Nit: Just omit this and qualify the names below
rohitrao (ping after 24h)
2016/02/03 14:58:08
Done.
|
+ |
+namespace { |
+ |
+class SetShouldContain : public std::unary_function< |
+ const ShortcutExpectedURLAndAllowedToBeDefault&, |
+ std::set<std::string>> { |
+ public: |
+ explicit SetShouldContain(const ACMatches& matched_urls); |
+ |
+ void operator()(const ShortcutExpectedURLAndAllowedToBeDefault& expected); |
+ std::set<ShortcutExpectedURLAndAllowedToBeDefault> Leftovers() const { |
+ return matches_; |
+ } |
+ |
+ private: |
+ std::set<ShortcutExpectedURLAndAllowedToBeDefault> matches_; |
+}; |
+ |
+SetShouldContain::SetShouldContain(const ACMatches& matched_urls) { |
+ for (ACMatches::const_iterator iter = matched_urls.begin(); |
+ iter != matched_urls.end(); ++iter) |
+ matches_.insert(ShortcutExpectedURLAndAllowedToBeDefault( |
+ iter->destination_url.spec(), iter->allowed_to_be_default_match)); |
+} |
+ |
+void SetShouldContain::operator()( |
+ const ShortcutExpectedURLAndAllowedToBeDefault& expected) { |
+ EXPECT_EQ(1U, matches_.erase(expected)); |
+} |
+ |
+} // namespace |
+ |
+TestShortcutInfo::TestShortcutInfo(std::string guid, |
+ std::string text, |
+ std::string fill_into_edit, |
+ std::string destination_url, |
+ std::string contents, |
+ std::string contents_class, |
+ std::string description, |
+ std::string description_class, |
+ ui::PageTransition transition, |
+ AutocompleteMatch::Type type, |
+ std::string keyword, |
+ int days_from_now, |
+ int number_of_hits) { |
+ this->guid = guid; |
+ this->text = text; |
+ this->fill_into_edit = fill_into_edit; |
+ this->destination_url = destination_url; |
+ this->contents = contents; |
+ this->contents_class = contents_class; |
+ this->description = description; |
+ this->description_class = description_class; |
+ this->transition = transition; |
+ this->type = type; |
+ this->keyword = keyword; |
+ this->days_from_now = days_from_now; |
+ this->number_of_hits = number_of_hits; |
+} |
+ |
+TestShortcutInfo::~TestShortcutInfo() {} |
+ |
+void PopulateShortcutsBackendWithTestShortcutData( |
+ scoped_refptr<ShortcutsBackend> backend, |
+ TestShortcutInfo* db, |
+ size_t db_size) { |
+ size_t expected_size = backend->shortcuts_map().size() + db_size; |
+ for (size_t i = 0; i < db_size; ++i) { |
+ const TestShortcutInfo& cur = db[i]; |
+ ShortcutsDatabase::Shortcut shortcut( |
+ cur.guid, ASCIIToUTF16(cur.text), |
+ ShortcutsDatabase::Shortcut::MatchCore( |
+ ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url), |
+ ASCIIToUTF16(cur.contents), cur.contents_class, |
+ ASCIIToUTF16(cur.description), cur.description_class, |
+ cur.transition, cur.type, ASCIIToUTF16(cur.keyword)), |
+ base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now), |
+ cur.number_of_hits); |
+ backend->AddShortcut(shortcut); |
+ } |
+ EXPECT_EQ(expected_size, backend->shortcuts_map().size()); |
+} |
+ |
+void RunShortcutsProviderTest(scoped_refptr<ShortcutsProvider> provider, |
+ const base::string16 text, |
+ bool prevent_inline_autocomplete, |
+ const ShortcutExpectedURLs& expected_urls, |
+ std::string expected_top_result, |
+ base::string16 top_result_inline_autocompletion) { |
+ base::MessageLoop::current()->RunUntilIdle(); |
+ AutocompleteInput input(text, base::string16::npos, std::string(), GURL(), |
+ metrics::OmniboxEventProto::INVALID_SPEC, |
+ prevent_inline_autocomplete, false, true, true, false, |
+ TestSchemeClassifier()); |
+ provider->Start(input, false); |
+ EXPECT_TRUE(provider->done()); |
+ |
+ ACMatches ac_matches_ = provider->matches(); |
+ |
+ // We should have gotten back at most AutocompleteProvider::kMaxMatches. |
+ EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches); |
+ |
+ // If the number of expected and actual matches aren't equal then we need |
+ // test no further, but let's do anyway so that we know which URLs failed. |
+ EXPECT_EQ(expected_urls.size(), ac_matches_.size()); |
+ |
+ // Verify that all expected URLs were found and that all found URLs |
+ // were expected. |
+ std::set<ShortcutExpectedURLAndAllowedToBeDefault> Leftovers = |
+ for_each(expected_urls.begin(), expected_urls.end(), |
+ SetShouldContain(ac_matches_)) |
+ .Leftovers(); |
+ EXPECT_EQ(0U, Leftovers.size()); |
Peter Kasting
2016/02/03 00:31:47
I think we can nuke all the SetShouldContain stuff
rohitrao (ping after 24h)
2016/02/03 14:58:08
Done.
|
+ |
+ // See if we got the expected top scorer. |
+ if (!ac_matches_.empty()) { |
+ std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1, |
+ ac_matches_.end(), AutocompleteMatch::MoreRelevant); |
+ EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec()); |
+ EXPECT_EQ(top_result_inline_autocompletion, |
+ ac_matches_[0].inline_autocompletion); |
+ } |
+} |