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

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

Issue 2266083002: Avoid DCHECK failure for chrome:// URLs in autocomplete suggestions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: changes for pkasting@ Created 4 years, 3 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') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/omnibox/browser/physical_web_provider.h" 5 #include "components/omnibox/browser/physical_web_provider.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 TestSchemeClassifier scheme_classifier_; 84 TestSchemeClassifier scheme_classifier_;
85 }; 85 };
86 86
87 class PhysicalWebProviderTest : public testing::Test { 87 class PhysicalWebProviderTest : public testing::Test {
88 protected: 88 protected:
89 PhysicalWebProviderTest() : provider_(NULL) {} 89 PhysicalWebProviderTest() : provider_(NULL) {}
90 ~PhysicalWebProviderTest() override {} 90 ~PhysicalWebProviderTest() override {}
91 91
92 void SetUp() override { 92 void SetUp() override {
93 client_.reset(new FakeAutocompleteProviderClient()); 93 client_.reset(new FakeAutocompleteProviderClient());
94 provider_ = PhysicalWebProvider::Create(client_.get()); 94 provider_ = PhysicalWebProvider::Create(client_.get(), nullptr);
95 } 95 }
96 96
97 void TearDown() override { 97 void TearDown() override {
98 provider_ = NULL; 98 provider_ = NULL;
99 } 99 }
100 100
101 // Create a dummy metadata list with |metadata_count| items. Each item is 101 // Create a dummy metadata list with |metadata_count| items. Each item is
102 // populated with a unique scanned URL and page metadata. 102 // populated with a unique scanned URL and page metadata.
103 static std::unique_ptr<base::ListValue> CreateMetadata( 103 static std::unique_ptr<base::ListValue> CreateMetadata(
104 size_t metadata_count) { 104 size_t metadata_count) {
105 auto metadata_list = base::MakeUnique<base::ListValue>(); 105 auto metadata_list = base::MakeUnique<base::ListValue>();
106 for (size_t i = 0; i < metadata_count; ++i) { 106 for (size_t i = 0; i < metadata_count; ++i) {
107 std::string item_id = base::SizeTToString(i); 107 std::string item_id = base::SizeTToString(i);
108 std::string url = "https://example.com/" + item_id; 108 std::string url = "https://example.com/" + item_id;
109 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); 109 auto metadata_item = base::MakeUnique<base::DictionaryValue>();
110 metadata_item->SetString("scannedUrl", url); 110 metadata_item->SetString("scannedUrl", url);
111 metadata_item->SetString("resolvedUrl", url); 111 metadata_item->SetString("resolvedUrl", url);
112 metadata_item->SetString("icon", url); 112 metadata_item->SetString("icon", url);
113 metadata_item->SetString("title", "Example title " + item_id); 113 metadata_item->SetString("title", "Example title " + item_id);
114 metadata_item->SetString("description", "Example description " + item_id); 114 metadata_item->SetString("description", "Example description " + item_id);
115 metadata_list->Append(std::move(metadata_item)); 115 metadata_list->Append(std::move(metadata_item));
116 } 116 }
117 return metadata_list; 117 return metadata_list;
118 } 118 }
119 119
120 // Construct an AutocompleteInput to represent tapping the omnibox from the
121 // new tab page.
122 static AutocompleteInput CreateInputForNTP() {
123 return AutocompleteInput(
124 base::string16(), base::string16::npos, std::string(), GURL(),
125 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
126 false, false, true, true, true, TestSchemeClassifier());
127 }
128
129 // Construct an AutocompleteInput to represent tapping the omnibox with |url|
130 // as the current web page.
131 static AutocompleteInput CreateInputWithCurrentUrl(const std::string& url) {
132 return AutocompleteInput(base::ASCIIToUTF16(url), base::string16::npos,
133 std::string(), GURL(url),
134 metrics::OmniboxEventProto::OTHER, false, false,
135 true, true, true, TestSchemeClassifier());
136 }
137
120 std::unique_ptr<FakeAutocompleteProviderClient> client_; 138 std::unique_ptr<FakeAutocompleteProviderClient> client_;
121 scoped_refptr<PhysicalWebProvider> provider_; 139 scoped_refptr<PhysicalWebProvider> provider_;
122 140
123 private: 141 private:
124 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); 142 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest);
125 }; 143 };
126 144
127 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { 145 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) {
128 MockPhysicalWebDataSource* data_source = 146 MockPhysicalWebDataSource* data_source =
129 client_->GetMockPhysicalWebDataSource(); 147 client_->GetMockPhysicalWebDataSource();
130 EXPECT_TRUE(data_source); 148 EXPECT_TRUE(data_source);
131 149
132 data_source->SetMetadata(CreateMetadata(0)); 150 data_source->SetMetadata(CreateMetadata(0));
133 151
134 // Construct an AutocompleteInput representing a typical "zero-suggest" 152 // Run the test with no text in the omnibox input to simulate NTP.
135 // case. The provider will verify that the content of the omnibox input field 153 provider_->Start(CreateInputForNTP(), false);
136 // was not entered by the user. 154 EXPECT_TRUE(provider_->matches().empty());
137 std::string url("http://www.cnn.com/");
138 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
139 std::string(), GURL(url), metrics::OmniboxEventProto::OTHER,
140 true, false, true, true, true, TestSchemeClassifier());
141 provider_->Start(input, false);
142 155
156 // Run the test again with a URL in the omnibox input.
157 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false);
143 EXPECT_TRUE(provider_->matches().empty()); 158 EXPECT_TRUE(provider_->matches().empty());
144 } 159 }
145 160
146 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { 161 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) {
147 MockPhysicalWebDataSource* data_source = 162 MockPhysicalWebDataSource* data_source =
148 client_->GetMockPhysicalWebDataSource(); 163 client_->GetMockPhysicalWebDataSource();
149 EXPECT_TRUE(data_source); 164 EXPECT_TRUE(data_source);
150 165
151 // Extract the URL and title before inserting the metadata into the data 166 // Extract the URL and title before inserting the metadata into the data
152 // source. 167 // source.
153 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1); 168 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1);
154 base::DictionaryValue* metadata_item; 169 base::DictionaryValue* metadata_item;
155 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); 170 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item));
156 std::string resolved_url; 171 std::string resolved_url;
157 EXPECT_TRUE(metadata_item->GetString("resolvedUrl", &resolved_url)); 172 EXPECT_TRUE(metadata_item->GetString("resolvedUrl", &resolved_url));
158 std::string title; 173 std::string title;
159 EXPECT_TRUE(metadata_item->GetString("title", &title)); 174 EXPECT_TRUE(metadata_item->GetString("title", &title));
160 175
161 data_source->SetMetadata(std::move(metadata_list)); 176 data_source->SetMetadata(std::move(metadata_list));
162 177
163 // Construct an AutocompleteInput representing a typical "zero-suggest" 178 // Run the test with no text in the omnibox input to simulate NTP.
164 // case. The provider will verify that the content of the omnibox input field 179 provider_->Start(CreateInputForNTP(), false);
165 // was not entered by the user.
166 std::string url("http://www.cnn.com/");
167 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
168 std::string(), GURL(url), metrics::OmniboxEventProto::OTHER,
169 true, false, true, true, true, TestSchemeClassifier());
170 provider_->Start(input, false);
171 180
172 // Check that there is only one match item and its fields are correct. 181 // Check that there is only one match item and its fields are correct.
173 EXPECT_EQ(1U, provider_->matches().size()); 182 EXPECT_EQ(1U, provider_->matches().size());
174 const AutocompleteMatch& metadata_match = provider_->matches().front(); 183 const AutocompleteMatch& metadata_match = provider_->matches().front();
175 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); 184 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type);
176 EXPECT_EQ(resolved_url, metadata_match.destination_url.spec()); 185 EXPECT_EQ(resolved_url, metadata_match.destination_url.spec());
177 EXPECT_EQ(resolved_url, base::UTF16ToASCII(metadata_match.contents)); 186 EXPECT_EQ(resolved_url, base::UTF16ToASCII(metadata_match.contents));
178 EXPECT_EQ(title, base::UTF16ToASCII(metadata_match.description)); 187 EXPECT_EQ(title, base::UTF16ToASCII(metadata_match.description));
188 EXPECT_FALSE(metadata_match.allowed_to_be_default_match);
189
190 // Run the test again with a URL in the omnibox input. An additional match
191 // should be added as a default match.
192 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false);
193
194 size_t metadata_match_count = 0;
195 size_t default_match_count = 0;
196 for (const auto& match : provider_->matches()) {
197 if (match.type == AutocompleteMatchType::PHYSICAL_WEB) {
198 EXPECT_EQ(resolved_url, match.destination_url.spec());
199 EXPECT_EQ(resolved_url, base::UTF16ToASCII(match.contents));
200 EXPECT_EQ(title, base::UTF16ToASCII(match.description));
201 EXPECT_FALSE(match.allowed_to_be_default_match);
202 ++metadata_match_count;
203 } else {
204 EXPECT_TRUE(match.allowed_to_be_default_match);
205 ++default_match_count;
206 }
207 }
208 EXPECT_EQ(2U, provider_->matches().size());
209 EXPECT_EQ(1U, metadata_match_count);
210 EXPECT_EQ(1U, default_match_count);
179 } 211 }
180 212
181 TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) { 213 TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) {
182 // This test is intended to verify that an overflow item is created when the 214 // This test is intended to verify that an overflow item is created when the
183 // number of nearby Physical Web URLs exceeds the maximum allowable matches 215 // number of nearby Physical Web URLs exceeds the maximum allowable matches
184 // for this provider. The actual limit for the PhysicalWebProvider may be 216 // for this provider. The actual limit for the PhysicalWebProvider may be
185 // changed in the future, so create enough metadata to exceed the 217 // changed in the future, so create enough metadata to exceed the
186 // AutocompleteProvider's limit. 218 // AutocompleteProvider's limit.
187 const size_t metadata_count = AutocompleteProvider::kMaxMatches + 1; 219 const size_t metadata_count = AutocompleteProvider::kMaxMatches + 1;
188 220
189 MockPhysicalWebDataSource* data_source = 221 MockPhysicalWebDataSource* data_source =
190 client_->GetMockPhysicalWebDataSource(); 222 client_->GetMockPhysicalWebDataSource();
191 EXPECT_TRUE(data_source); 223 EXPECT_TRUE(data_source);
192 224
193 data_source->SetMetadata(CreateMetadata(metadata_count)); 225 data_source->SetMetadata(CreateMetadata(metadata_count));
194 226
195 // Construct an AutocompleteInput representing a typical "zero-suggest" 227 {
196 // case. The provider will verify that the content of the omnibox input field 228 // Run the test with no text in the omnibox input to simulate NTP.
197 // was not entered by the user. 229 provider_->Start(CreateInputForNTP(), false);
198 std::string url("http://www.cnn.com/");
199 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
200 std::string(), GURL(url), metrics::OmniboxEventProto::OTHER,
201 true, false, true, true, true, TestSchemeClassifier());
202 provider_->Start(input, false);
203 230
204 const size_t match_count = provider_->matches().size(); 231 const size_t match_count = provider_->matches().size();
205 EXPECT_LT(match_count, metadata_count); 232 EXPECT_LT(match_count, metadata_count);
206 233
207 // Check that the overflow item is at the end of the match list and its fields 234 // Check that the overflow item is present and its fields are correct. There
208 // are correct. 235 // may be additional match items, but none should be marked as default.
209 const AutocompleteMatch& overflow_match = provider_->matches().back(); 236 size_t overflow_match_count = 0;
210 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW, overflow_match.type); 237 for (const auto& match : provider_->matches()) {
211 EXPECT_EQ("chrome://physical-web/", overflow_match.destination_url.spec()); 238 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) {
212 EXPECT_EQ("chrome://physical-web/", 239 EXPECT_EQ("chrome://physical-web/", match.destination_url.spec());
213 base::UTF16ToASCII(overflow_match.contents)); 240 EXPECT_EQ("chrome://physical-web/", base::UTF16ToASCII(match.contents));
214 std::string description = l10n_util::GetPluralStringFUTF8( 241 const size_t metadata_matches = match_count - 1;
215 IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - match_count + 1); 242 std::string description = l10n_util::GetPluralStringFUTF8(
216 EXPECT_EQ(description, base::UTF16ToASCII(overflow_match.description)); 243 IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - metadata_matches);
244 EXPECT_EQ(description, base::UTF16ToASCII(match.description));
245 ++overflow_match_count;
246 }
247 EXPECT_FALSE(match.allowed_to_be_default_match);
248 }
249 EXPECT_EQ(1U, overflow_match_count);
250 }
251
252 {
253 // Run the test again with a URL in the omnibox input. An additional match
254 // should be added as a default match.
255 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false);
256
257 const size_t match_count = provider_->matches().size();
258 EXPECT_LT(match_count - 1, metadata_count);
259
260 // Check that the overflow item and default match are present and their
261 // fields are correct.
262 size_t overflow_match_count = 0;
263 size_t default_match_count = 0;
264 for (const auto& match : provider_->matches()) {
265 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) {
266 EXPECT_EQ("chrome://physical-web/", match.destination_url.spec());
267 EXPECT_EQ("chrome://physical-web/", base::UTF16ToASCII(match.contents));
268 const size_t metadata_matches = match_count - 2;
269 std::string description = l10n_util::GetPluralStringFUTF8(
270 IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - metadata_matches);
271 EXPECT_EQ(description, base::UTF16ToASCII(match.description));
272 EXPECT_FALSE(match.allowed_to_be_default_match);
273 ++overflow_match_count;
274 } else if (match.allowed_to_be_default_match) {
275 ++default_match_count;
276 }
277 }
278 EXPECT_EQ(1U, overflow_match_count);
279 EXPECT_EQ(1U, default_match_count);
280 }
217 } 281 }
218 282
219 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { 283 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) {
220 MockPhysicalWebDataSource* data_source = 284 MockPhysicalWebDataSource* data_source =
221 client_->GetMockPhysicalWebDataSource(); 285 client_->GetMockPhysicalWebDataSource();
222 EXPECT_TRUE(data_source); 286 EXPECT_TRUE(data_source);
223 287
224 data_source->SetMetadata(CreateMetadata(1)); 288 data_source->SetMetadata(CreateMetadata(1));
225 289
226 // Construct an AutocompleteInput to simulate user input in the omnibox input 290 // Construct an AutocompleteInput to simulate user input in the omnibox input
227 // field. The provider should not generate any matches. 291 // field. The provider should not generate any matches.
228 std::string text("user input"); 292 std::string text("user input");
229 const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(), 293 const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(),
230 std::string(), GURL(), 294 std::string(), GURL(),
231 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, 295 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
232 true, false, true, true, false, TestSchemeClassifier()); 296 true, false, true, true, false, TestSchemeClassifier());
233 provider_->Start(input, false); 297 provider_->Start(input, false);
234 298
235 EXPECT_TRUE(provider_->matches().empty()); 299 EXPECT_TRUE(provider_->matches().empty());
236 } 300 }
237 301
238 } 302 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/physical_web_provider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698