| 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/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/values.h" | |
| 15 #include "components/metrics/proto/omnibox_event.pb.h" | 14 #include "components/metrics/proto/omnibox_event.pb.h" |
| 16 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" | 15 #include "components/omnibox/browser/mock_autocomplete_provider_client.h" |
| 17 #include "components/omnibox/browser/omnibox_field_trial.h" | 16 #include "components/omnibox/browser/omnibox_field_trial.h" |
| 18 #include "components/omnibox/browser/test_scheme_classifier.h" | 17 #include "components/omnibox/browser/test_scheme_classifier.h" |
| 19 #include "components/physical_web/data_source/fake_physical_web_data_source.h" | 18 #include "components/physical_web/data_source/fake_physical_web_data_source.h" |
| 20 #include "components/physical_web/data_source/physical_web_data_source.h" | 19 #include "components/physical_web/data_source/physical_web_data_source.h" |
| 21 #include "components/physical_web/data_source/physical_web_listener.h" | 20 #include "components/physical_web/data_source/physical_web_listener.h" |
| 22 #include "components/variations/entropy_provider.h" | 21 #include "components/variations/entropy_provider.h" |
| 23 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
| 24 #include "grit/components_strings.h" | 23 #include "grit/components_strings.h" |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 params[OmniboxFieldTrial::kPhysicalWebZeroSuggestRule] = "true"; | 106 params[OmniboxFieldTrial::kPhysicalWebZeroSuggestRule] = "true"; |
| 108 params[OmniboxFieldTrial::kPhysicalWebAfterTypingRule] = "true"; | 107 params[OmniboxFieldTrial::kPhysicalWebAfterTypingRule] = "true"; |
| 109 variations::AssociateVariationParams( | 108 variations::AssociateVariationParams( |
| 110 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); | 109 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A", params); |
| 111 return base::FieldTrialList::CreateFieldTrial( | 110 return base::FieldTrialList::CreateFieldTrial( |
| 112 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); | 111 OmniboxFieldTrial::kBundledExperimentFieldTrialName, "A"); |
| 113 } | 112 } |
| 114 | 113 |
| 115 // Create a dummy metadata list with |metadata_count| items. Each item is | 114 // Create a dummy metadata list with |metadata_count| items. Each item is |
| 116 // populated with a unique scanned URL and page metadata. | 115 // populated with a unique scanned URL and page metadata. |
| 117 static std::unique_ptr<base::ListValue> CreateMetadata( | 116 static std::unique_ptr<physical_web::MetadataList> CreateMetadata( |
| 118 size_t metadata_count) { | 117 size_t metadata_count) { |
| 119 auto metadata_list = base::MakeUnique<base::ListValue>(); | 118 auto metadata_list = base::MakeUnique<physical_web::MetadataList>(); |
| 120 for (size_t i = 0; i < metadata_count; ++i) { | 119 for (size_t i = 0; i < metadata_count; ++i) { |
| 121 std::string item_id = base::SizeTToString(i); | 120 std::string item_id = base::SizeTToString(i); |
| 122 std::string url = "https://example.com/" + item_id; | 121 std::string url = "https://example.com/" + item_id; |
| 123 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); | 122 metadata_list->emplace_back(); |
| 124 metadata_item->SetString(physical_web::kScannedUrlKey, url); | 123 auto& metadata_item = metadata_list->back(); |
| 125 metadata_item->SetString(physical_web::kResolvedUrlKey, url); | 124 metadata_item.scanned_url = GURL(url); |
| 126 metadata_item->SetString(physical_web::kIconUrlKey, url); | 125 metadata_item.resolved_url = GURL(url); |
| 127 metadata_item->SetString(physical_web::kTitleKey, | 126 metadata_item.icon_url = GURL(url); |
| 128 "Example title " + item_id); | 127 metadata_item.title = "Example title " + item_id; |
| 129 metadata_item->SetString(physical_web::kDescriptionKey, | 128 metadata_item.description = "Example description " + item_id; |
| 130 "Example description " + item_id); | |
| 131 metadata_list->Append(std::move(metadata_item)); | |
| 132 } | 129 } |
| 133 return metadata_list; | 130 return metadata_list; |
| 134 } | 131 } |
| 135 | 132 |
| 136 // Construct an AutocompleteInput to represent tapping the omnibox from the | 133 // Construct an AutocompleteInput to represent tapping the omnibox from the |
| 137 // new tab page. | 134 // new tab page. |
| 138 static AutocompleteInput CreateInputForNTP() { | 135 static AutocompleteInput CreateInputForNTP() { |
| 139 return AutocompleteInput( | 136 return AutocompleteInput( |
| 140 base::string16(), base::string16::npos, std::string(), GURL(), | 137 base::string16(), base::string16::npos, std::string(), GURL(), |
| 141 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 138 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 } | 190 } |
| 194 | 191 |
| 195 // Run a test case using |input| as the simulated state of the omnibox input | 192 // Run a test case using |input| as the simulated state of the omnibox input |
| 196 // field, |metadata_list| as the list of simulated Physical Web metadata, | 193 // field, |metadata_list| as the list of simulated Physical Web metadata, |
| 197 // and |title_truncated| as the truncated title of the first match. In | 194 // and |title_truncated| as the truncated title of the first match. In |
| 198 // addition to checking the fields of the overflow item, this will also check | 195 // addition to checking the fields of the overflow item, this will also check |
| 199 // that the total number of matches is equal to |expected_match_count| and | 196 // that the total number of matches is equal to |expected_match_count| and |
| 200 // that a default match and overflow item are only present when | 197 // that a default match and overflow item are only present when |
| 201 // |should_expect_default_match| or |should_expect_overflow_item| are true. | 198 // |should_expect_default_match| or |should_expect_overflow_item| are true. |
| 202 // Metadata matches are not checked. | 199 // Metadata matches are not checked. |
| 203 void OverflowItemTestCase(const AutocompleteInput& input, | 200 void OverflowItemTestCase( |
| 204 std::unique_ptr<base::ListValue> metadata_list, | 201 const AutocompleteInput& input, |
| 205 const std::string& title_truncated, | 202 std::unique_ptr<physical_web::MetadataList> metadata_list, |
| 206 size_t expected_match_count, | 203 const std::string& title_truncated, |
| 207 bool should_expect_default_match, | 204 size_t expected_match_count, |
| 208 bool should_expect_overflow_item) { | 205 bool should_expect_default_match, |
| 209 const size_t metadata_count = metadata_list->GetSize(); | 206 bool should_expect_overflow_item) { |
| 207 const size_t metadata_count = metadata_list->size(); |
| 210 | 208 |
| 211 FakePhysicalWebDataSource* data_source = | 209 FakePhysicalWebDataSource* data_source = |
| 212 client_->GetFakePhysicalWebDataSource(); | 210 client_->GetFakePhysicalWebDataSource(); |
| 213 EXPECT_TRUE(data_source); | 211 EXPECT_TRUE(data_source); |
| 214 | 212 |
| 215 data_source->SetMetadata(std::move(metadata_list)); | 213 data_source->SetMetadataList(std::move(metadata_list)); |
| 216 | 214 |
| 217 provider_->Start(input, false); | 215 provider_->Start(input, false); |
| 218 | 216 |
| 219 const size_t match_count = provider_->matches().size(); | 217 const size_t match_count = provider_->matches().size(); |
| 220 EXPECT_EQ(expected_match_count, match_count); | 218 EXPECT_EQ(expected_match_count, match_count); |
| 221 | 219 |
| 222 const size_t match_count_without_default = | 220 const size_t match_count_without_default = |
| 223 should_expect_default_match ? match_count - 1 : match_count; | 221 should_expect_default_match ? match_count - 1 : match_count; |
| 224 | 222 |
| 225 if (should_expect_overflow_item) { | 223 if (should_expect_overflow_item) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 253 | 251 |
| 254 private: | 252 private: |
| 255 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); | 253 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); |
| 256 }; | 254 }; |
| 257 | 255 |
| 258 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { | 256 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { |
| 259 FakePhysicalWebDataSource* data_source = | 257 FakePhysicalWebDataSource* data_source = |
| 260 client_->GetFakePhysicalWebDataSource(); | 258 client_->GetFakePhysicalWebDataSource(); |
| 261 EXPECT_TRUE(data_source); | 259 EXPECT_TRUE(data_source); |
| 262 | 260 |
| 263 data_source->SetMetadata(CreateMetadata(0)); | 261 data_source->SetMetadataList(CreateMetadata(0)); |
| 264 | 262 |
| 265 // Run the test with no text in the omnibox input to simulate NTP. | 263 // Run the test with no text in the omnibox input to simulate NTP. |
| 266 provider_->Start(CreateInputForNTP(), false); | 264 provider_->Start(CreateInputForNTP(), false); |
| 267 EXPECT_TRUE(provider_->matches().empty()); | 265 EXPECT_TRUE(provider_->matches().empty()); |
| 268 | 266 |
| 269 // Run the test again with a URL in the omnibox input. | 267 // Run the test again with a URL in the omnibox input. |
| 270 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); | 268 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
| 271 EXPECT_TRUE(provider_->matches().empty()); | 269 EXPECT_TRUE(provider_->matches().empty()); |
| 272 } | 270 } |
| 273 | 271 |
| 274 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { | 272 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { |
| 275 FakePhysicalWebDataSource* data_source = | 273 FakePhysicalWebDataSource* data_source = |
| 276 client_->GetFakePhysicalWebDataSource(); | 274 client_->GetFakePhysicalWebDataSource(); |
| 277 EXPECT_TRUE(data_source); | 275 EXPECT_TRUE(data_source); |
| 278 | 276 |
| 279 // Extract the URL and title before inserting the metadata into the data | 277 // Extract the URL and title before inserting the metadata into the data |
| 280 // source. | 278 // source. |
| 281 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1); | 279 auto metadata_list = CreateMetadata(1); |
| 282 base::DictionaryValue* metadata_item; | 280 const auto& metadata_item = metadata_list->front(); |
| 283 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 281 std::string resolved_url = metadata_item.resolved_url.spec(); |
| 284 std::string resolved_url; | 282 std::string title = metadata_item.title; |
| 285 EXPECT_TRUE( | |
| 286 metadata_item->GetString(physical_web::kResolvedUrlKey, &resolved_url)); | |
| 287 std::string title; | |
| 288 EXPECT_TRUE(metadata_item->GetString(physical_web::kTitleKey, &title)); | |
| 289 | 283 |
| 290 data_source->SetMetadata(std::move(metadata_list)); | 284 data_source->SetMetadataList(std::move(metadata_list)); |
| 291 | 285 |
| 292 // Run the test with no text in the omnibox input to simulate NTP. | 286 // Run the test with no text in the omnibox input to simulate NTP. |
| 293 provider_->Start(CreateInputForNTP(), false); | 287 provider_->Start(CreateInputForNTP(), false); |
| 294 | 288 |
| 295 // Check that there is only one match item and its fields are correct. | 289 // Check that there is only one match item and its fields are correct. |
| 296 EXPECT_EQ(1U, provider_->matches().size()); | 290 EXPECT_EQ(1U, provider_->matches().size()); |
| 297 const AutocompleteMatch& metadata_match = provider_->matches().front(); | 291 const AutocompleteMatch& metadata_match = provider_->matches().front(); |
| 298 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); | 292 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); |
| 299 ValidateMatch(metadata_match, resolved_url, resolved_url, title, false); | 293 ValidateMatch(metadata_match, resolved_url, resolved_url, title, false); |
| 300 | 294 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 316 EXPECT_EQ(2U, provider_->matches().size()); | 310 EXPECT_EQ(2U, provider_->matches().size()); |
| 317 EXPECT_EQ(1U, metadata_match_count); | 311 EXPECT_EQ(1U, metadata_match_count); |
| 318 EXPECT_EQ(1U, default_match_count); | 312 EXPECT_EQ(1U, default_match_count); |
| 319 } | 313 } |
| 320 | 314 |
| 321 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { | 315 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { |
| 322 FakePhysicalWebDataSource* data_source = | 316 FakePhysicalWebDataSource* data_source = |
| 323 client_->GetFakePhysicalWebDataSource(); | 317 client_->GetFakePhysicalWebDataSource(); |
| 324 EXPECT_TRUE(data_source); | 318 EXPECT_TRUE(data_source); |
| 325 | 319 |
| 326 data_source->SetMetadata(CreateMetadata(1)); | 320 data_source->SetMetadataList(CreateMetadata(1)); |
| 327 | 321 |
| 328 // Construct an AutocompleteInput to simulate user input in the omnibox input | 322 // Construct an AutocompleteInput to simulate user input in the omnibox input |
| 329 // field. The provider should not generate any matches. | 323 // field. The provider should not generate any matches. |
| 330 std::string text("user input"); | 324 std::string text("user input"); |
| 331 const AutocompleteInput input( | 325 const AutocompleteInput input( |
| 332 base::UTF8ToUTF16(text), text.length(), std::string(), GURL(), | 326 base::UTF8ToUTF16(text), text.length(), std::string(), GURL(), |
| 333 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 327 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| 334 true, false, true, true, false, TestSchemeClassifier()); | 328 true, false, true, true, false, TestSchemeClassifier()); |
| 335 provider_->Start(input, false); | 329 provider_->Start(input, false); |
| 336 | 330 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 351 OverflowItemTestCase(CreateInputWithCurrentUrl("http://www.cnn.com"), | 345 OverflowItemTestCase(CreateInputWithCurrentUrl("http://www.cnn.com"), |
| 352 CreateMetadata(metadata_count), "Example title 0", | 346 CreateMetadata(metadata_count), "Example title 0", |
| 353 PhysicalWebProvider::kPhysicalWebMaxMatches + 1, true, | 347 PhysicalWebProvider::kPhysicalWebMaxMatches + 1, true, |
| 354 true); | 348 true); |
| 355 } | 349 } |
| 356 | 350 |
| 357 TEST_F(PhysicalWebProviderTest, TestLongPageTitleIsTruncatedInOverflowItem) { | 351 TEST_F(PhysicalWebProviderTest, TestLongPageTitleIsTruncatedInOverflowItem) { |
| 358 // Set a long title for the first item. The page title for this item will | 352 // Set a long title for the first item. The page title for this item will |
| 359 // appear in the overflow item's content string. | 353 // appear in the overflow item's content string. |
| 360 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); | 354 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); |
| 361 base::DictionaryValue* metadata_item; | 355 auto& metadata_item = metadata_list->front(); |
| 362 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 356 metadata_item.title = "Extra long example title 0"; |
| 363 metadata_item->SetString(physical_web::kTitleKey, | |
| 364 "Extra long example title 0"); | |
| 365 | 357 |
| 366 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), | 358 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), |
| 367 "Extra long exa" + std::string(gfx::kEllipsis), | 359 "Extra long exa" + std::string(gfx::kEllipsis), |
| 368 PhysicalWebProvider::kPhysicalWebMaxMatches, false, | 360 PhysicalWebProvider::kPhysicalWebMaxMatches, false, |
| 369 true); | 361 true); |
| 370 } | 362 } |
| 371 | 363 |
| 372 TEST_F(PhysicalWebProviderTest, TestEmptyPageTitleInOverflowItem) { | 364 TEST_F(PhysicalWebProviderTest, TestEmptyPageTitleInOverflowItem) { |
| 373 // Set an empty title for the first item. Because the title is empty, we will | 365 // Set an empty title for the first item. Because the title is empty, we will |
| 374 // display an alternate string in the overflow item's contents. | 366 // display an alternate string in the overflow item's contents. |
| 375 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); | 367 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); |
| 376 base::DictionaryValue* metadata_item; | 368 auto& metadata_item = metadata_list->front(); |
| 377 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 369 metadata_item.title = ""; |
| 378 metadata_item->SetString(physical_web::kTitleKey, ""); | |
| 379 | 370 |
| 380 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), "", | 371 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), "", |
| 381 PhysicalWebProvider::kPhysicalWebMaxMatches, false, | 372 PhysicalWebProvider::kPhysicalWebMaxMatches, false, |
| 382 true); | 373 true); |
| 383 } | 374 } |
| 384 | 375 |
| 385 TEST_F(PhysicalWebProviderTest, TestRTLPageTitleInOverflowItem) { | 376 TEST_F(PhysicalWebProviderTest, TestRTLPageTitleInOverflowItem) { |
| 386 // Set a Hebrew title for the first item. | 377 // Set a Hebrew title for the first item. |
| 387 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); | 378 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); |
| 388 base::DictionaryValue* metadata_item; | 379 auto& metadata_item = metadata_list->front(); |
| 389 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 380 metadata_item.title = "ויקיפדיה"; |
| 390 metadata_item->SetString(physical_web::kTitleKey, "ויקיפדיה"); | |
| 391 | 381 |
| 392 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), | 382 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), |
| 393 "ויקיפדיה", PhysicalWebProvider::kPhysicalWebMaxMatches, | 383 "ויקיפדיה", PhysicalWebProvider::kPhysicalWebMaxMatches, |
| 394 false, true); | 384 false, true); |
| 395 } | 385 } |
| 396 | 386 |
| 397 TEST_F(PhysicalWebProviderTest, TestNoMatchesInIncognito) { | 387 TEST_F(PhysicalWebProviderTest, TestNoMatchesInIncognito) { |
| 398 // Enable incognito mode | 388 // Enable incognito mode |
| 399 client_->SetOffTheRecord(true); | 389 client_->SetOffTheRecord(true); |
| 400 | 390 |
| 401 FakePhysicalWebDataSource* data_source = | 391 FakePhysicalWebDataSource* data_source = |
| 402 client_->GetFakePhysicalWebDataSource(); | 392 client_->GetFakePhysicalWebDataSource(); |
| 403 EXPECT_TRUE(data_source); | 393 EXPECT_TRUE(data_source); |
| 404 | 394 |
| 405 data_source->SetMetadata(CreateMetadata(1)); | 395 data_source->SetMetadataList(CreateMetadata(1)); |
| 406 provider_->Start(CreateInputForNTP(), false); | 396 provider_->Start(CreateInputForNTP(), false); |
| 407 | 397 |
| 408 EXPECT_TRUE(provider_->matches().empty()); | 398 EXPECT_TRUE(provider_->matches().empty()); |
| 409 } | 399 } |
| OLD | NEW |