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

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

Issue 1654833005: Componentizes shortcuts unittests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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 TestShortcutData::TestShortcutData(std::string guid,
16 std::string text,
17 std::string fill_into_edit,
18 std::string destination_url,
19 std::string contents,
20 std::string contents_class,
21 std::string description,
22 std::string description_class,
23 ui::PageTransition transition,
24 AutocompleteMatch::Type type,
25 std::string keyword,
26 int days_from_now,
27 int number_of_hits) {
28 this->guid = guid;
29 this->text = text;
30 this->fill_into_edit = fill_into_edit;
31 this->destination_url = destination_url;
32 this->contents = contents;
33 this->contents_class = contents_class;
34 this->description = description;
35 this->description_class = description_class;
36 this->transition = transition;
37 this->type = type;
38 this->keyword = keyword;
39 this->days_from_now = days_from_now;
40 this->number_of_hits = number_of_hits;
41 }
42
43 TestShortcutData::~TestShortcutData() {}
44
45 void PopulateShortcutsBackendWithTestData(
46 scoped_refptr<ShortcutsBackend> backend,
47 TestShortcutData* db,
48 size_t db_size) {
49 size_t expected_size = backend->shortcuts_map().size() + db_size;
50 for (size_t i = 0; i < db_size; ++i) {
51 const TestShortcutData& cur = db[i];
52 ShortcutsDatabase::Shortcut shortcut(
53 cur.guid, base::ASCIIToUTF16(cur.text),
54 ShortcutsDatabase::Shortcut::MatchCore(
55 base::ASCIIToUTF16(cur.fill_into_edit), GURL(cur.destination_url),
56 base::ASCIIToUTF16(cur.contents), cur.contents_class,
57 base::ASCIIToUTF16(cur.description), cur.description_class,
58 cur.transition, cur.type, base::ASCIIToUTF16(cur.keyword)),
59 base::Time::Now() - base::TimeDelta::FromDays(cur.days_from_now),
60 cur.number_of_hits);
61 backend->AddShortcut(shortcut);
62 }
63 EXPECT_EQ(expected_size, backend->shortcuts_map().size());
64 }
65
66 void RunShortcutsProviderTest(
67 scoped_refptr<ShortcutsProvider> provider,
68 const base::string16 text,
69 bool prevent_inline_autocomplete,
70 const std::vector<ExpectedURLAndAllowedToBeDefault>& expected_urls,
71 std::string expected_top_result,
72 base::string16 top_result_inline_autocompletion) {
73 base::MessageLoop::current()->RunUntilIdle();
74 AutocompleteInput input(text, base::string16::npos, std::string(), GURL(),
75 metrics::OmniboxEventProto::INVALID_SPEC,
76 prevent_inline_autocomplete, false, true, true, false,
77 TestSchemeClassifier());
78 provider->Start(input, false);
79 EXPECT_TRUE(provider->done());
80
81 ACMatches ac_matches = provider->matches();
82
83 // We should have gotten back at most AutocompleteProvider::kMaxMatches.
84 EXPECT_LE(ac_matches.size(), AutocompleteProvider::kMaxMatches);
85
86 // If the number of expected and actual matches aren't equal then we need
87 // test no further, but let's do anyway so that we know which URLs failed.
88 EXPECT_EQ(expected_urls.size(), ac_matches.size());
89
90 for (const auto& expected_url : expected_urls) {
91 auto iter = std::find_if(
92 ac_matches.begin(), ac_matches.end(),
93 [&expected_url](const AutocompleteMatch& match) {
94 return expected_url.first == match.destination_url.spec() &&
95 expected_url.second == match.allowed_to_be_default_match;
96 });
97 EXPECT_TRUE(iter != ac_matches.end());
98 }
99
100 // See if we got the expected top scorer.
101 if (!ac_matches.empty()) {
102 std::partial_sort(ac_matches.begin(), ac_matches.begin() + 1,
103 ac_matches.end(), AutocompleteMatch::MoreRelevant);
104 EXPECT_EQ(expected_top_result, ac_matches[0].destination_url.spec());
105 EXPECT_EQ(top_result_inline_autocompletion,
106 ac_matches[0].inline_autocompletion);
107 }
108 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/shortcuts_provider_test_util.h ('k') | components/omnibox/browser/shortcuts_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698