OLD | NEW |
---|---|
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" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "components/metrics/proto/omnibox_event.pb.h" | 14 #include "components/metrics/proto/omnibox_event.pb.h" |
15 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" | 15 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" |
16 #include "components/omnibox/browser/test_scheme_classifier.h" | 16 #include "components/omnibox/browser/test_scheme_classifier.h" |
17 #include "components/physical_web/data_source/physical_web_data_source.h" | 17 #include "components/physical_web/data_source/physical_web_data_source.h" |
18 #include "components/physical_web/data_source/physical_web_listener.h" | 18 #include "components/physical_web/data_source/physical_web_listener.h" |
19 #include "grit/components_strings.h" | 19 #include "grit/components_strings.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
24 | 24 |
25 namespace { | 25 namespace { |
Mark P
2016/09/08 18:49:50
didn't notice this the first time: should everythi
mattreynolds
2016/09/09 21:24:55
Fixed so only the mocks are in the anonymous names
| |
26 | 26 |
27 // A mock implementation of the Physical Web data source that allows setting | 27 // A mock implementation of the Physical Web data source that allows setting |
28 // metadata for nearby URLs directly. | 28 // metadata for nearby URLs directly. |
29 class MockPhysicalWebDataSource : public PhysicalWebDataSource { | 29 class MockPhysicalWebDataSource : public PhysicalWebDataSource { |
30 public: | 30 public: |
31 MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {} | 31 MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {} |
32 ~MockPhysicalWebDataSource() override {} | 32 ~MockPhysicalWebDataSource() override {} |
33 | 33 |
34 void StartDiscovery(bool network_request_enabled) override {} | 34 void StartDiscovery(bool network_request_enabled) override {} |
35 void StopDiscovery() override {} | 35 void StopDiscovery() override {} |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 | 128 |
129 // Construct an AutocompleteInput to represent tapping the omnibox with |url| | 129 // Construct an AutocompleteInput to represent tapping the omnibox with |url| |
130 // as the current web page. | 130 // as the current web page. |
131 static AutocompleteInput CreateInputWithCurrentUrl(const std::string& url) { | 131 static AutocompleteInput CreateInputWithCurrentUrl(const std::string& url) { |
132 return AutocompleteInput(base::ASCIIToUTF16(url), base::string16::npos, | 132 return AutocompleteInput(base::ASCIIToUTF16(url), base::string16::npos, |
133 std::string(), GURL(url), | 133 std::string(), GURL(url), |
134 metrics::OmniboxEventProto::OTHER, false, false, | 134 metrics::OmniboxEventProto::OTHER, false, false, |
135 true, true, true, TestSchemeClassifier()); | 135 true, true, true, TestSchemeClassifier()); |
136 } | 136 } |
137 | 137 |
138 static void ValidateMatch(const AutocompleteMatch& match, | |
Mark P
2016/09/08 18:49:50
nit: comment.
mattreynolds
2016/09/09 21:24:55
Done.
| |
139 const std::string& url, | |
140 const std::string& contents, | |
141 const std::string& description, | |
142 bool allowed_to_be_default_match) { | |
143 EXPECT_EQ(url, match.destination_url.spec()); | |
144 EXPECT_EQ(contents, base::UTF16ToASCII(match.contents)); | |
145 EXPECT_EQ(description, base::UTF16ToASCII(match.description)); | |
146 EXPECT_EQ(allowed_to_be_default_match, match.allowed_to_be_default_match); | |
147 } | |
148 | |
138 std::unique_ptr<FakeAutocompleteProviderClient> client_; | 149 std::unique_ptr<FakeAutocompleteProviderClient> client_; |
139 scoped_refptr<PhysicalWebProvider> provider_; | 150 scoped_refptr<PhysicalWebProvider> provider_; |
140 | 151 |
141 private: | 152 private: |
142 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); | 153 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); |
143 }; | 154 }; |
144 | 155 |
145 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { | 156 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { |
146 MockPhysicalWebDataSource* data_source = | 157 MockPhysicalWebDataSource* data_source = |
147 client_->GetMockPhysicalWebDataSource(); | 158 client_->GetMockPhysicalWebDataSource(); |
(...skipping 27 matching lines...) Expand all Loading... | |
175 | 186 |
176 data_source->SetMetadata(std::move(metadata_list)); | 187 data_source->SetMetadata(std::move(metadata_list)); |
177 | 188 |
178 // Run the test with no text in the omnibox input to simulate NTP. | 189 // Run the test with no text in the omnibox input to simulate NTP. |
179 provider_->Start(CreateInputForNTP(), false); | 190 provider_->Start(CreateInputForNTP(), false); |
180 | 191 |
181 // Check that there is only one match item and its fields are correct. | 192 // Check that there is only one match item and its fields are correct. |
182 EXPECT_EQ(1U, provider_->matches().size()); | 193 EXPECT_EQ(1U, provider_->matches().size()); |
183 const AutocompleteMatch& metadata_match = provider_->matches().front(); | 194 const AutocompleteMatch& metadata_match = provider_->matches().front(); |
184 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); | 195 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); |
185 EXPECT_EQ(resolved_url, metadata_match.destination_url.spec()); | 196 ValidateMatch(metadata_match, resolved_url, resolved_url, title, false); |
186 EXPECT_EQ(resolved_url, base::UTF16ToASCII(metadata_match.contents)); | |
187 EXPECT_EQ(title, base::UTF16ToASCII(metadata_match.description)); | |
188 EXPECT_FALSE(metadata_match.allowed_to_be_default_match); | |
189 | 197 |
190 // Run the test again with a URL in the omnibox input. An additional match | 198 // Run the test again with a URL in the omnibox input. An additional match |
191 // should be added as a default match. | 199 // should be added as a default match. |
192 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); | 200 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
193 | 201 |
194 size_t metadata_match_count = 0; | 202 size_t metadata_match_count = 0; |
195 size_t default_match_count = 0; | 203 size_t default_match_count = 0; |
196 for (const auto& match : provider_->matches()) { | 204 for (const auto& match : provider_->matches()) { |
197 if (match.type == AutocompleteMatchType::PHYSICAL_WEB) { | 205 if (match.type == AutocompleteMatchType::PHYSICAL_WEB) { |
198 EXPECT_EQ(resolved_url, match.destination_url.spec()); | 206 ValidateMatch(match, resolved_url, resolved_url, title, false); |
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; | 207 ++metadata_match_count; |
203 } else { | 208 } else { |
204 EXPECT_TRUE(match.allowed_to_be_default_match); | 209 EXPECT_TRUE(match.allowed_to_be_default_match); |
205 ++default_match_count; | 210 ++default_match_count; |
206 } | 211 } |
207 } | 212 } |
208 EXPECT_EQ(2U, provider_->matches().size()); | 213 EXPECT_EQ(2U, provider_->matches().size()); |
209 EXPECT_EQ(1U, metadata_match_count); | 214 EXPECT_EQ(1U, metadata_match_count); |
210 EXPECT_EQ(1U, default_match_count); | 215 EXPECT_EQ(1U, default_match_count); |
211 } | 216 } |
(...skipping 12 matching lines...) Expand all Loading... | |
224 | 229 |
225 data_source->SetMetadata(CreateMetadata(metadata_count)); | 230 data_source->SetMetadata(CreateMetadata(metadata_count)); |
226 | 231 |
227 { | 232 { |
228 // Run the test with no text in the omnibox input to simulate NTP. | 233 // Run the test with no text in the omnibox input to simulate NTP. |
229 provider_->Start(CreateInputForNTP(), false); | 234 provider_->Start(CreateInputForNTP(), false); |
230 | 235 |
231 const size_t match_count = provider_->matches().size(); | 236 const size_t match_count = provider_->matches().size(); |
232 EXPECT_LT(match_count, metadata_count); | 237 EXPECT_LT(match_count, metadata_count); |
233 | 238 |
239 const size_t additional_url_count = metadata_count - match_count + 1; | |
Mark P
2016/09/08 18:49:50
This is an unusual placement. Either comment thes
mattreynolds
2016/09/09 21:24:55
Done.
| |
240 std::string content = "Example title 0 " + | |
241 l10n_util::GetPluralStringFUTF8(IDS_PHYSICAL_WEB_OVERFLOW, | |
242 additional_url_count - 1); | |
Mark P
2016/09/08 18:49:50
Also, I'm confused by all these +1 -1 here. Is th
mattreynolds
2016/09/09 21:24:55
Yeah, this was pretty confusing. I added some comm
| |
243 | |
234 // Check that the overflow item is present and its fields are correct. There | 244 // Check that the overflow item is present and its fields are correct. There |
235 // may be additional match items, but none should be marked as default. | 245 // may be additional match items, but none should be marked as default. |
236 size_t overflow_match_count = 0; | 246 size_t overflow_match_count = 0; |
237 for (const auto& match : provider_->matches()) { | 247 for (const auto& match : provider_->matches()) { |
238 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { | 248 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { |
239 EXPECT_EQ("chrome://physical-web/", match.destination_url.spec()); | 249 ValidateMatch( |
240 EXPECT_EQ("chrome://physical-web/", base::UTF16ToASCII(match.contents)); | 250 match, "chrome://physical-web/", content, |
241 const size_t metadata_matches = match_count - 1; | 251 l10n_util::GetStringUTF8(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION), |
242 std::string description = l10n_util::GetPluralStringFUTF8( | 252 false); |
243 IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - metadata_matches); | |
244 EXPECT_EQ(description, base::UTF16ToASCII(match.description)); | |
245 ++overflow_match_count; | 253 ++overflow_match_count; |
246 } | 254 } |
247 EXPECT_FALSE(match.allowed_to_be_default_match); | 255 EXPECT_FALSE(match.allowed_to_be_default_match); |
248 } | 256 } |
249 EXPECT_EQ(1U, overflow_match_count); | 257 EXPECT_EQ(1U, overflow_match_count); |
250 } | 258 } |
251 | 259 |
252 { | 260 { |
253 // Run the test again with a URL in the omnibox input. An additional match | 261 // Run the test again with a URL in the omnibox input. An additional match |
254 // should be added as a default match. | 262 // should be added as a default match. |
255 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); | 263 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
256 | 264 |
257 const size_t match_count = provider_->matches().size(); | 265 const size_t match_count = provider_->matches().size(); |
258 EXPECT_LT(match_count - 1, metadata_count); | 266 EXPECT_LT(match_count - 1, metadata_count); |
259 | 267 |
268 const size_t additional_url_count = metadata_count - match_count + 2; | |
269 std::string content = "Example title 0 " + | |
270 l10n_util::GetPluralStringFUTF8(IDS_PHYSICAL_WEB_OVERFLOW, | |
271 additional_url_count - 1); | |
272 | |
260 // Check that the overflow item and default match are present and their | 273 // Check that the overflow item and default match are present and their |
261 // fields are correct. | 274 // fields are correct. |
262 size_t overflow_match_count = 0; | 275 size_t overflow_match_count = 0; |
263 size_t default_match_count = 0; | 276 size_t default_match_count = 0; |
264 for (const auto& match : provider_->matches()) { | 277 for (const auto& match : provider_->matches()) { |
265 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { | 278 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { |
266 EXPECT_EQ("chrome://physical-web/", match.destination_url.spec()); | 279 ValidateMatch( |
267 EXPECT_EQ("chrome://physical-web/", base::UTF16ToASCII(match.contents)); | 280 match, "chrome://physical-web/", content, |
268 const size_t metadata_matches = match_count - 2; | 281 l10n_util::GetStringUTF8(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION), |
269 std::string description = l10n_util::GetPluralStringFUTF8( | 282 false); |
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; | 283 ++overflow_match_count; |
274 } else if (match.allowed_to_be_default_match) { | 284 } else if (match.allowed_to_be_default_match) { |
275 ++default_match_count; | 285 ++default_match_count; |
276 } | 286 } |
277 } | 287 } |
278 EXPECT_EQ(1U, overflow_match_count); | 288 EXPECT_EQ(1U, overflow_match_count); |
279 EXPECT_EQ(1U, default_match_count); | 289 EXPECT_EQ(1U, default_match_count); |
280 } | 290 } |
281 } | 291 } |
282 | 292 |
283 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { | 293 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { |
284 MockPhysicalWebDataSource* data_source = | 294 MockPhysicalWebDataSource* data_source = |
285 client_->GetMockPhysicalWebDataSource(); | 295 client_->GetMockPhysicalWebDataSource(); |
286 EXPECT_TRUE(data_source); | 296 EXPECT_TRUE(data_source); |
287 | 297 |
288 data_source->SetMetadata(CreateMetadata(1)); | 298 data_source->SetMetadata(CreateMetadata(1)); |
289 | 299 |
290 // Construct an AutocompleteInput to simulate user input in the omnibox input | 300 // Construct an AutocompleteInput to simulate user input in the omnibox input |
291 // field. The provider should not generate any matches. | 301 // field. The provider should not generate any matches. |
292 std::string text("user input"); | 302 std::string text("user input"); |
293 const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(), | 303 const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(), |
294 std::string(), GURL(), | 304 std::string(), GURL(), |
295 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 305 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
296 true, false, true, true, false, TestSchemeClassifier()); | 306 true, false, true, true, false, TestSchemeClassifier()); |
297 provider_->Start(input, false); | 307 provider_->Start(input, false); |
298 | 308 |
299 EXPECT_TRUE(provider_->matches().empty()); | 309 EXPECT_TRUE(provider_->matches().empty()); |
300 } | 310 } |
301 | 311 |
312 TEST_F(PhysicalWebProviderTest, TestLongPageTitleIsTruncated) { | |
Mark P
2016/09/08 18:49:50
This test is only applicable for whether there's a
mattreynolds
2016/09/09 21:24:55
Done. I added a note about truncation behavior in
| |
313 const size_t metadata_count = AutocompleteProvider::kMaxMatches + 1; | |
314 | |
315 MockPhysicalWebDataSource* data_source = | |
316 client_->GetMockPhysicalWebDataSource(); | |
317 EXPECT_TRUE(data_source); | |
318 | |
319 auto metadata_list = CreateMetadata(metadata_count); | |
320 | |
321 // Set a long title for the first item. The page title for this item will | |
322 // appear in the overflow item's content string. | |
323 base::DictionaryValue* metadata_item; | |
324 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | |
325 metadata_item->SetString("title", "Extra long example title 0"); | |
326 | |
327 data_source->SetMetadata(std::move(metadata_list)); | |
328 | |
329 // Run the test with no text in the omnibox input to simulate NTP. | |
330 provider_->Start(CreateInputForNTP(), false); | |
331 | |
332 const size_t match_count = provider_->matches().size(); | |
333 EXPECT_LT(match_count, metadata_count); | |
334 | |
335 const size_t additional_url_count = metadata_count - match_count + 1; | |
336 std::string content = "Extra long exam... " + | |
337 l10n_util::GetPluralStringFUTF8(IDS_PHYSICAL_WEB_OVERFLOW, | |
338 additional_url_count - 1); | |
339 | |
340 // Check that the overflow item is present and its fields are correct. There | |
341 // may be additional match items, but none should be marked as default. | |
342 size_t overflow_match_count = 0; | |
343 for (const auto& match : provider_->matches()) { | |
344 if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { | |
345 ValidateMatch( | |
346 match, "chrome://physical-web/", content, | |
347 l10n_util::GetStringUTF8(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION), | |
348 false); | |
349 ++overflow_match_count; | |
350 } | |
351 EXPECT_FALSE(match.allowed_to_be_default_match); | |
352 } | |
353 EXPECT_EQ(1U, overflow_match_count); | |
302 } | 354 } |
355 | |
356 } | |
OLD | NEW |