Chromium Code Reviews| 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 "ui/gfx/text_elider.h" | 23 #include "ui/gfx/text_elider.h" |
| 24 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 25 | 25 |
| 26 using physical_web::PhysicalWebDataSource; | |
| 27 using physical_web::PhysicalWebListener; | |
| 28 | |
| 26 namespace { | 29 namespace { |
| 27 | 30 |
| 28 // A mock implementation of the Physical Web data source that allows setting | 31 // A mock implementation of the Physical Web data source that allows setting |
| 29 // metadata for nearby URLs directly. | 32 // metadata for nearby URLs directly. |
| 30 class MockPhysicalWebDataSource : public PhysicalWebDataSource { | 33 class MockPhysicalWebDataSource : public PhysicalWebDataSource { |
|
vitaliii
2016/11/25 11:57:02
I am going to move this to components/physical_web
| |
| 31 public: | 34 public: |
| 32 MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {} | 35 MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {} |
| 33 ~MockPhysicalWebDataSource() override {} | 36 ~MockPhysicalWebDataSource() override {} |
| 34 | 37 |
| 35 void StartDiscovery(bool network_request_enabled) override {} | 38 void StartDiscovery(bool network_request_enabled) override {} |
| 36 void StopDiscovery() override {} | 39 void StopDiscovery() override {} |
| 37 | 40 |
| 38 std::unique_ptr<base::ListValue> GetMetadata() override { | 41 std::unique_ptr<base::ListValue> GetMetadata() override { |
| 39 return metadata_->CreateDeepCopy(); | 42 return metadata_->CreateDeepCopy(); |
| 40 } | 43 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 | 118 |
| 116 // Create a dummy metadata list with |metadata_count| items. Each item is | 119 // Create a dummy metadata list with |metadata_count| items. Each item is |
| 117 // populated with a unique scanned URL and page metadata. | 120 // populated with a unique scanned URL and page metadata. |
| 118 static std::unique_ptr<base::ListValue> CreateMetadata( | 121 static std::unique_ptr<base::ListValue> CreateMetadata( |
| 119 size_t metadata_count) { | 122 size_t metadata_count) { |
| 120 auto metadata_list = base::MakeUnique<base::ListValue>(); | 123 auto metadata_list = base::MakeUnique<base::ListValue>(); |
| 121 for (size_t i = 0; i < metadata_count; ++i) { | 124 for (size_t i = 0; i < metadata_count; ++i) { |
| 122 std::string item_id = base::SizeTToString(i); | 125 std::string item_id = base::SizeTToString(i); |
| 123 std::string url = "https://example.com/" + item_id; | 126 std::string url = "https://example.com/" + item_id; |
| 124 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); | 127 auto metadata_item = base::MakeUnique<base::DictionaryValue>(); |
| 125 metadata_item->SetString(kPhysicalWebScannedUrlKey, url); | 128 metadata_item->SetString(physical_web::kScannedUrlKey, url); |
| 126 metadata_item->SetString(kPhysicalWebResolvedUrlKey, url); | 129 metadata_item->SetString(physical_web::kResolvedUrlKey, url); |
| 127 metadata_item->SetString(kPhysicalWebIconUrlKey, url); | 130 metadata_item->SetString(physical_web::kIconUrlKey, url); |
| 128 metadata_item->SetString(kPhysicalWebTitleKey, | 131 metadata_item->SetString(physical_web::kTitleKey, |
| 129 "Example title " + item_id); | 132 "Example title " + item_id); |
| 130 metadata_item->SetString(kPhysicalWebDescriptionKey, | 133 metadata_item->SetString(physical_web::kDescriptionKey, |
| 131 "Example description " + item_id); | 134 "Example description " + item_id); |
| 132 metadata_list->Append(std::move(metadata_item)); | 135 metadata_list->Append(std::move(metadata_item)); |
| 133 } | 136 } |
| 134 return metadata_list; | 137 return metadata_list; |
| 135 } | 138 } |
| 136 | 139 |
| 137 // Construct an AutocompleteInput to represent tapping the omnibox from the | 140 // Construct an AutocompleteInput to represent tapping the omnibox from the |
| 138 // new tab page. | 141 // new tab page. |
| 139 static AutocompleteInput CreateInputForNTP() { | 142 static AutocompleteInput CreateInputForNTP() { |
| 140 return AutocompleteInput( | 143 return AutocompleteInput( |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 MockPhysicalWebDataSource* data_source = | 278 MockPhysicalWebDataSource* data_source = |
| 276 client_->GetMockPhysicalWebDataSource(); | 279 client_->GetMockPhysicalWebDataSource(); |
| 277 EXPECT_TRUE(data_source); | 280 EXPECT_TRUE(data_source); |
| 278 | 281 |
| 279 // Extract the URL and title before inserting the metadata into the data | 282 // Extract the URL and title before inserting the metadata into the data |
| 280 // source. | 283 // source. |
| 281 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1); | 284 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1); |
| 282 base::DictionaryValue* metadata_item; | 285 base::DictionaryValue* metadata_item; |
| 283 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 286 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); |
| 284 std::string resolved_url; | 287 std::string resolved_url; |
| 285 EXPECT_TRUE(metadata_item->GetString(kPhysicalWebResolvedUrlKey, | 288 EXPECT_TRUE( |
| 286 &resolved_url)); | 289 metadata_item->GetString(physical_web::kResolvedUrlKey, &resolved_url)); |
| 287 std::string title; | 290 std::string title; |
| 288 EXPECT_TRUE(metadata_item->GetString(kPhysicalWebTitleKey, &title)); | 291 EXPECT_TRUE(metadata_item->GetString(physical_web::kTitleKey, &title)); |
| 289 | 292 |
| 290 data_source->SetMetadata(std::move(metadata_list)); | 293 data_source->SetMetadata(std::move(metadata_list)); |
| 291 | 294 |
| 292 // Run the test with no text in the omnibox input to simulate NTP. | 295 // Run the test with no text in the omnibox input to simulate NTP. |
| 293 provider_->Start(CreateInputForNTP(), false); | 296 provider_->Start(CreateInputForNTP(), false); |
| 294 | 297 |
| 295 // Check that there is only one match item and its fields are correct. | 298 // Check that there is only one match item and its fields are correct. |
| 296 EXPECT_EQ(1U, provider_->matches().size()); | 299 EXPECT_EQ(1U, provider_->matches().size()); |
| 297 const AutocompleteMatch& metadata_match = provider_->matches().front(); | 300 const AutocompleteMatch& metadata_match = provider_->matches().front(); |
| 298 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); | 301 EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 PhysicalWebProvider::kPhysicalWebMaxMatches + 1, true, | 356 PhysicalWebProvider::kPhysicalWebMaxMatches + 1, true, |
| 354 true); | 357 true); |
| 355 } | 358 } |
| 356 | 359 |
| 357 TEST_F(PhysicalWebProviderTest, TestLongPageTitleIsTruncatedInOverflowItem) { | 360 TEST_F(PhysicalWebProviderTest, TestLongPageTitleIsTruncatedInOverflowItem) { |
| 358 // Set a long title for the first item. The page title for this item will | 361 // Set a long title for the first item. The page title for this item will |
| 359 // appear in the overflow item's content string. | 362 // appear in the overflow item's content string. |
| 360 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); | 363 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); |
| 361 base::DictionaryValue* metadata_item; | 364 base::DictionaryValue* metadata_item; |
| 362 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 365 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); |
| 363 metadata_item->SetString(kPhysicalWebTitleKey, "Extra long example title 0"); | 366 metadata_item->SetString(physical_web::kTitleKey, |
| 367 "Extra long example title 0"); | |
| 364 | 368 |
| 365 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), | 369 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), |
| 366 "Extra long exa" + std::string(gfx::kEllipsis), | 370 "Extra long exa" + std::string(gfx::kEllipsis), |
| 367 PhysicalWebProvider::kPhysicalWebMaxMatches, false, | 371 PhysicalWebProvider::kPhysicalWebMaxMatches, false, |
| 368 true); | 372 true); |
| 369 } | 373 } |
| 370 | 374 |
| 371 TEST_F(PhysicalWebProviderTest, TestEmptyPageTitleInOverflowItem) { | 375 TEST_F(PhysicalWebProviderTest, TestEmptyPageTitleInOverflowItem) { |
| 372 // Set an empty title for the first item. Because the title is empty, we will | 376 // Set an empty title for the first item. Because the title is empty, we will |
| 373 // display an alternate string in the overflow item's contents. | 377 // display an alternate string in the overflow item's contents. |
| 374 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); | 378 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); |
| 375 base::DictionaryValue* metadata_item; | 379 base::DictionaryValue* metadata_item; |
| 376 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 380 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); |
| 377 metadata_item->SetString(kPhysicalWebTitleKey, ""); | 381 metadata_item->SetString(physical_web::kTitleKey, ""); |
| 378 | 382 |
| 379 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), "", | 383 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), "", |
| 380 PhysicalWebProvider::kPhysicalWebMaxMatches, false, | 384 PhysicalWebProvider::kPhysicalWebMaxMatches, false, |
| 381 true); | 385 true); |
| 382 } | 386 } |
| 383 | 387 |
| 384 TEST_F(PhysicalWebProviderTest, TestRTLPageTitleInOverflowItem) { | 388 TEST_F(PhysicalWebProviderTest, TestRTLPageTitleInOverflowItem) { |
| 385 // Set a Hebrew title for the first item. | 389 // Set a Hebrew title for the first item. |
| 386 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); | 390 auto metadata_list = CreateMetadata(AutocompleteProvider::kMaxMatches + 1); |
| 387 base::DictionaryValue* metadata_item; | 391 base::DictionaryValue* metadata_item; |
| 388 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 392 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); |
| 389 metadata_item->SetString(kPhysicalWebTitleKey, "ויקיפדיה"); | 393 metadata_item->SetString(physical_web::kTitleKey, "ויקיפדיה"); |
| 390 | 394 |
| 391 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), | 395 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), |
| 392 "ויקיפדיה", PhysicalWebProvider::kPhysicalWebMaxMatches, | 396 "ויקיפדיה", PhysicalWebProvider::kPhysicalWebMaxMatches, |
| 393 false, true); | 397 false, true); |
| 394 } | 398 } |
| 395 | 399 |
| 396 TEST_F(PhysicalWebProviderTest, TestNoMatchesInIncognito) { | 400 TEST_F(PhysicalWebProviderTest, TestNoMatchesInIncognito) { |
| 397 // Enable incognito mode | 401 // Enable incognito mode |
| 398 client_->SetOffTheRecord(true); | 402 client_->SetOffTheRecord(true); |
| 399 | 403 |
| 400 MockPhysicalWebDataSource* data_source = | 404 MockPhysicalWebDataSource* data_source = |
| 401 client_->GetMockPhysicalWebDataSource(); | 405 client_->GetMockPhysicalWebDataSource(); |
| 402 EXPECT_TRUE(data_source); | 406 EXPECT_TRUE(data_source); |
| 403 | 407 |
| 404 data_source->SetMetadata(CreateMetadata(1)); | 408 data_source->SetMetadata(CreateMetadata(1)); |
| 405 provider_->Start(CreateInputForNTP(), false); | 409 provider_->Start(CreateInputForNTP(), false); |
| 406 | 410 |
| 407 EXPECT_TRUE(provider_->matches().empty()); | 411 EXPECT_TRUE(provider_->matches().empty()); |
| 408 } | 412 } |
| OLD | NEW |