Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1608)

Side by Side Diff: components/omnibox/browser/shortcuts_provider_test_util.cc

Issue 1655933004: Refactor to share code between shortcuts tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shortcuts
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 "components/omnibox/browser/shortcuts_provider_test_util.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/browser/autocomplete_match.h"
10 #include "components/omnibox/browser/shortcuts_backend.h"
11 #include "components/omnibox/browser/shortcuts_provider.h"
12 #include "components/omnibox/browser/test_scheme_classifier.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 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.
16
17 namespace {
18
19 class SetShouldContain : public std::unary_function<
20 const ShortcutExpectedURLAndAllowedToBeDefault&,
21 std::set<std::string>> {
22 public:
23 explicit SetShouldContain(const ACMatches& matched_urls);
24
25 void operator()(const ShortcutExpectedURLAndAllowedToBeDefault& expected);
26 std::set<ShortcutExpectedURLAndAllowedToBeDefault> Leftovers() const {
27 return matches_;
28 }
29
30 private:
31 std::set<ShortcutExpectedURLAndAllowedToBeDefault> matches_;
32 };
33
34 SetShouldContain::SetShouldContain(const ACMatches& matched_urls) {
35 for (ACMatches::const_iterator iter = matched_urls.begin();
36 iter != matched_urls.end(); ++iter)
37 matches_.insert(ShortcutExpectedURLAndAllowedToBeDefault(
38 iter->destination_url.spec(), iter->allowed_to_be_default_match));
39 }
40
41 void SetShouldContain::operator()(
42 const ShortcutExpectedURLAndAllowedToBeDefault& expected) {
43 EXPECT_EQ(1U, matches_.erase(expected));
44 }
45
46 } // namespace
47
48 TestShortcutInfo::TestShortcutInfo(std::string guid,
49 std::string text,
50 std::string fill_into_edit,
51 std::string destination_url,
52 std::string contents,
53 std::string contents_class,
54 std::string description,
55 std::string description_class,
56 ui::PageTransition transition,
57 AutocompleteMatch::Type type,
58 std::string keyword,
59 int days_from_now,
60 int number_of_hits) {
61 this->guid = guid;
62 this->text = text;
63 this->fill_into_edit = fill_into_edit;
64 this->destination_url = destination_url;
65 this->contents = contents;
66 this->contents_class = contents_class;
67 this->description = description;
68 this->description_class = description_class;
69 this->transition = transition;
70 this->type = type;
71 this->keyword = keyword;
72 this->days_from_now = days_from_now;
73 this->number_of_hits = number_of_hits;
74 }
75
76 TestShortcutInfo::~TestShortcutInfo() {}
77
78 void PopulateShortcutsBackendWithTestShortcutData(
79 scoped_refptr<ShortcutsBackend> backend,
80 TestShortcutInfo* db,
81 size_t db_size) {
82 size_t expected_size = backend->shortcuts_map().size() + db_size;
83 for (size_t i = 0; i < db_size; ++i) {
84 const TestShortcutInfo& cur = db[i];
85 ShortcutsDatabase::Shortcut shortcut(
86 cur.guid, ASCIIToUTF16(cur.text),
87 ShortcutsDatabase::Shortcut::MatchCore(
88 ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url),
89 ASCIIToUTF16(cur.contents), cur.contents_class,
90 ASCIIToUTF16(cur.description), cur.description_class,
91 cur.transition, cur.type, ASCIIToUTF16(cur.keyword)),
92 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now),
93 cur.number_of_hits);
94 backend->AddShortcut(shortcut);
95 }
96 EXPECT_EQ(expected_size, backend->shortcuts_map().size());
97 }
98
99 void RunShortcutsProviderTest(scoped_refptr<ShortcutsProvider> provider,
100 const base::string16 text,
101 bool prevent_inline_autocomplete,
102 const ShortcutExpectedURLs& expected_urls,
103 std::string expected_top_result,
104 base::string16 top_result_inline_autocompletion) {
105 base::MessageLoop::current()->RunUntilIdle();
106 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(),
107 metrics::OmniboxEventProto::INVALID_SPEC,
108 prevent_inline_autocomplete, false, true, true, false,
109 TestSchemeClassifier());
110 provider->Start(input, false);
111 EXPECT_TRUE(provider->done());
112
113 ACMatches ac_matches_ = provider->matches();
114
115 // We should have gotten back at most AutocompleteProvider::kMaxMatches.
116 EXPECT_LE(ac_matches_.size(), AutocompleteProvider::kMaxMatches);
117
118 // If the number of expected and actual matches aren't equal then we need
119 // test no further, but let's do anyway so that we know which URLs failed.
120 EXPECT_EQ(expected_urls.size(), ac_matches_.size());
121
122 // Verify that all expected URLs were found and that all found URLs
123 // were expected.
124 std::set<ShortcutExpectedURLAndAllowedToBeDefault> Leftovers =
125 for_each(expected_urls.begin(), expected_urls.end(),
126 SetShouldContain(ac_matches_))
127 .Leftovers();
128 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.
129
130 // See if we got the expected top scorer.
131 if (!ac_matches_.empty()) {
132 std::partial_sort(ac_matches_.begin(), ac_matches_.begin() + 1,
133 ac_matches_.end(), AutocompleteMatch::MoreRelevant);
134 EXPECT_EQ(expected_top_result, ac_matches_[0].destination_url.spec());
135 EXPECT_EQ(top_result_inline_autocompletion,
136 ac_matches_[0].inline_autocompletion);
137 }
138 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698