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

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

Issue 2203993002: Add a Physical Web omnibox autocomplete provider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unit test fixes Created 4 years, 4 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
« no previous file with comments | « components/omnibox/browser/physical_web_provider.cc ('k') | components/omnibox_strings.grdp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/physical_web_provider.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "components/metrics/proto/omnibox_event.pb.h"
14 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
15 #include "components/omnibox/browser/test_scheme_classifier.h"
16 #include "components/physical_web/data_source/physical_web_data_source.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h"
20
21 namespace {
22
23 // A mock implementation of the Physical Web data source that allows setting
24 // metadata for nearby URLs directly.
25 class MockPhysicalWebDataSource : public PhysicalWebDataSource {
26 public:
27 MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {}
28 ~MockPhysicalWebDataSource() override {}
29
30 void StartDiscovery(bool network_request_enabled) override {}
31 void StopDiscovery() override {}
32
33 std::unique_ptr<base::ListValue> GetMetadata() override {
34 return metadata_->CreateDeepCopy();
35 }
36
37 bool HasUnresolvedDiscoveries() override {
38 return false;
39 }
40
41 // for testing
42 void SetMetadata(std::unique_ptr<base::ListValue> metadata) {
43 metadata_ = std::move(metadata);
44 }
45
46 private:
47 std::unique_ptr<base::ListValue> metadata_;
48 };
49
50 // An autocomplete provider client that embeds the mock Physical Web data
51 // source.
52 class FakeAutocompleteProviderClient
53 : public testing::NiceMock<MockAutocompleteProviderClient> {
54 public:
55 FakeAutocompleteProviderClient()
56 : physical_web_data_source_(base::MakeUnique<MockPhysicalWebDataSource>())
57 {
58 }
59
60 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
61 return scheme_classifier_;
62 }
63
64 PhysicalWebDataSource* GetPhysicalWebDataSource() override {
65 return physical_web_data_source_.get();
66 }
67
68 private:
69 std::unique_ptr<MockPhysicalWebDataSource> physical_web_data_source_;
70 TestSchemeClassifier scheme_classifier_;
71 };
72
73 class PhysicalWebProviderTest : public testing::Test {
74 protected:
75 PhysicalWebProviderTest() : provider_(NULL) {}
76 ~PhysicalWebProviderTest() override {}
77
78 void SetUp() override {
79 client_.reset(new FakeAutocompleteProviderClient());
80 provider_ = PhysicalWebProvider::Create(client_.get());
81 }
82
83 void TearDown() override {
84 provider_ = NULL;
85 }
86
87 // Create a dummy metadata list with |metadata_count| items. Each item is
88 // populated with a unique scanned URL and page metadata.
89 static std::unique_ptr<base::ListValue> CreateMetadata(
90 size_t metadata_count) {
91 auto metadata_list = base::MakeUnique<base::ListValue>();
92 for (size_t i = 0; i < metadata_count; ++i) {
93 std::ostringstream buf;
94 buf << i;
95 std::string item_id = buf.str();
Mark P 2016/08/18 19:52:26 What's wrong with base::SizeTToString()?
mattreynolds 2016/08/18 22:33:32 Nothing, just never encountered it before. Thanks!
96 std::string url = "https://example.com/" + item_id;
97 auto metadata_item = base::MakeUnique<base::DictionaryValue>();
98 metadata_item->SetString("scannedUrl", url);
99 metadata_item->SetString("resolvedUrl", url);
100 metadata_item->SetString("icon", url);
101 metadata_item->SetString("title", "Example title " + item_id);
102 metadata_item->SetString("description", "Example description " + item_id);
103 metadata_list->Append(std::move(metadata_item));
104 }
105 return metadata_list;
106 }
107
108 std::unique_ptr<FakeAutocompleteProviderClient> client_;
109 scoped_refptr<PhysicalWebProvider> provider_;
110
111 private:
112 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest);
113 };
114
115 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) {
116 MockPhysicalWebDataSource* data_source =
117 (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
118 EXPECT_TRUE(data_source);
119
120 data_source->SetMetadata(CreateMetadata(0));
121
122 // Construct an AutocompleteInput representing a typical "zero-suggest"
123 // case. The provider will verify that the content of the omnibox input field
124 // was not entered by the user.
125 std::string url("http://www.cnn.com/");
126 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
127 std::string(), GURL(url), metrics::OmniboxEventProto::OTHER,
128 true, false, true, true, true, TestSchemeClassifier());
129 provider_->Start(input, false);
130
131 EXPECT_TRUE(provider_->matches().empty());
132 }
133
134 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) {
135 MockPhysicalWebDataSource* data_source =
136 (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
137 EXPECT_TRUE(data_source);
138
139 // Extract the URL and title before inserting the metadata into the data
140 // source.
141 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1);
142 base::DictionaryValue* metadata_item;
143 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item));
144 std::string resolved_url;
145 EXPECT_TRUE(metadata_item->GetString("resolvedUrl", &resolved_url));
146 std::string title;
147 EXPECT_TRUE(metadata_item->GetString("title", &title));
148
149 data_source->SetMetadata(std::move(metadata_list));
150
151 // Construct an AutocompleteInput representing a typical "zero-suggest"
152 // case. The provider will verify that the content of the omnibox input field
153 // was not entered by the user.
154 std::string url("http://www.cnn.com/");
155 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
156 std::string(), GURL(url), metrics::OmniboxEventProto::OTHER,
157 true, false, true, true, true, TestSchemeClassifier());
158 provider_->Start(input, false);
159
160 // Check that there is only one match item and its fields are correct.
161 EXPECT_EQ(1U, provider_->matches().size());
162 const AutocompleteMatch& metadata_match = provider_->matches().front();
163 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type);
164 EXPECT_EQ(resolved_url, metadata_match.destination_url.spec());
165 EXPECT_EQ(resolved_url, base::UTF16ToASCII(metadata_match.contents));
166 EXPECT_EQ(title, base::UTF16ToASCII(metadata_match.description));
167 }
168
169 TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) {
170 // This test is intended to verify that an overflow item is created when the
171 // number of nearby Physical Web URLs exceeds the maximum allowable matches
172 // for this provider. The actual limit for the PhysicalWebProvider may be
173 // changed in the future, so create enough metadata to exceed the
174 // AutocompleteProvider's limit.
175 const size_t metadata_count = AutocompleteProvider::kMaxMatches + 1;
176
177 MockPhysicalWebDataSource* data_source =
178 (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
179 EXPECT_TRUE(data_source);
180
181 data_source->SetMetadata(CreateMetadata(metadata_count));
182
183 // Construct an AutocompleteInput representing a typical "zero-suggest"
184 // case. The provider will verify that the content of the omnibox input field
185 // was not entered by the user.
186 std::string url("http://www.cnn.com/");
187 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
188 std::string(), GURL(url), metrics::OmniboxEventProto::OTHER,
189 true, false, true, true, true, TestSchemeClassifier());
190 provider_->Start(input, false);
191
192 EXPECT_LT(provider_->matches().size(), metadata_count);
193
194 // Check that the overflow item is at the end of the match list and has the
195 // correct type.
196 const AutocompleteMatch& overflow_match = provider_->matches().back();
197 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW, overflow_match.type);
Mark P 2016/08/18 19:52:26 Does it makes sense to check other fields here? P
mattreynolds 2016/08/18 22:33:32 Sure, I added checks for the destination URL, desc
198 }
199
200 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) {
201 MockPhysicalWebDataSource* data_source =
202 (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
203 EXPECT_TRUE(data_source);
204
205 data_source->SetMetadata(CreateMetadata(1));
206
207 // Construct an AutocompleteInput to simulate user input in the omnibox input
208 // field. The provider should not generate any matches.
209 std::string text("user input");
210 const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(),
211 std::string(), GURL(),
212 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
213 true, false, true, true, false, TestSchemeClassifier());
214 provider_->Start(input, false);
215
216 EXPECT_TRUE(provider_->matches().empty());
217 }
218
219 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/physical_web_provider.cc ('k') | components/omnibox_strings.grdp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698