Chromium Code Reviews| Index: components/omnibox/browser/physical_web_provider_unittest.cc |
| diff --git a/components/omnibox/browser/physical_web_provider_unittest.cc b/components/omnibox/browser/physical_web_provider_unittest.cc |
| index 6890785719a6a51ba22f57a1f43646d84a9c4379..e045c1b80c0ff2cbcb2c41b10ac96707a983521c 100644 |
| --- a/components/omnibox/browser/physical_web_provider_unittest.cc |
| +++ b/components/omnibox/browser/physical_web_provider_unittest.cc |
| @@ -91,7 +91,7 @@ class PhysicalWebProviderTest : public testing::Test { |
| void SetUp() override { |
| client_.reset(new FakeAutocompleteProviderClient()); |
| - provider_ = PhysicalWebProvider::Create(client_.get()); |
| + provider_ = PhysicalWebProvider::Create(client_.get(), NULL); |
|
Peter Kasting
2016/09/01 22:06:36
Nit: nullptr
mattreynolds
2016/09/02 00:29:08
Done.
|
| } |
| void TearDown() override { |
| @@ -117,6 +117,23 @@ class PhysicalWebProviderTest : public testing::Test { |
| return metadata_list; |
| } |
| + // Construct an AutocompleteInput to represent tapping the omnibox from the |
| + // new tab page. |
| + static AutocompleteInput CreateInputForNTP() { |
| + return AutocompleteInput(base::string16(), base::string16::npos, |
| + std::string(), GURL(), |
|
Peter Kasting
2016/09/01 22:06:36
Nit: All lines of args must be aligned (2 places)
mattreynolds
2016/09/02 00:29:08
Done.
|
| + metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| + false, false, true, true, true, TestSchemeClassifier()); |
| + } |
| + |
| + // Construct an AutocompleteInput to represent tapping the omnibox with |url| |
| + // as the current web page. |
| + static AutocompleteInput CreateInputWithCurrentUrl(const std::string& url) { |
| + return AutocompleteInput(base::ASCIIToUTF16(url), base::string16::npos, |
| + std::string(), GURL(url), metrics::OmniboxEventProto::OTHER, |
| + false, false, true, true, true, TestSchemeClassifier()); |
| + } |
| + |
| std::unique_ptr<FakeAutocompleteProviderClient> client_; |
| scoped_refptr<PhysicalWebProvider> provider_; |
| @@ -131,15 +148,12 @@ TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { |
| data_source->SetMetadata(CreateMetadata(0)); |
| - // Construct an AutocompleteInput representing a typical "zero-suggest" |
| - // case. The provider will verify that the content of the omnibox input field |
| - // was not entered by the user. |
| - std::string url("http://www.cnn.com/"); |
| - const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
| - std::string(), GURL(url), metrics::OmniboxEventProto::OTHER, |
| - true, false, true, true, true, TestSchemeClassifier()); |
| - provider_->Start(input, false); |
| + // Run the test with no text in the omnibox input to simulate NTP. |
| + provider_->Start(CreateInputForNTP(), false); |
| + EXPECT_TRUE(provider_->matches().empty()); |
| + // Run the test again with a URL in the omnibox input. |
| + provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
| EXPECT_TRUE(provider_->matches().empty()); |
| } |
| @@ -160,14 +174,8 @@ TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { |
| data_source->SetMetadata(std::move(metadata_list)); |
| - // Construct an AutocompleteInput representing a typical "zero-suggest" |
| - // case. The provider will verify that the content of the omnibox input field |
| - // was not entered by the user. |
| - std::string url("http://www.cnn.com/"); |
| - const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
| - std::string(), GURL(url), metrics::OmniboxEventProto::OTHER, |
| - true, false, true, true, true, TestSchemeClassifier()); |
| - provider_->Start(input, false); |
| + // Run the test with no text in the omnibox input to simulate NTP. |
| + provider_->Start(CreateInputForNTP(), false); |
| // Check that there is only one match item and its fields are correct. |
| EXPECT_EQ(1U, provider_->matches().size()); |
| @@ -176,6 +184,31 @@ TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { |
| EXPECT_EQ(resolved_url, metadata_match.destination_url.spec()); |
| EXPECT_EQ(resolved_url, base::UTF16ToASCII(metadata_match.contents)); |
| EXPECT_EQ(title, base::UTF16ToASCII(metadata_match.description)); |
| + EXPECT_FALSE(metadata_match.allowed_to_be_default_match); |
| + |
| + // Run the test again with a URL in the omnibox input. An additional match |
| + // should be added as a default match. |
| + provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
| + |
| + size_t metadata_match_count = 0; |
| + size_t default_match_count = 0; |
| + for (size_t i = 0; i < provider_->matches().size(); i++) { |
| + const AutocompleteMatch& match = provider_->matches()[i]; |
|
Peter Kasting
2016/09/01 22:06:36
Nit: Use range-based for:
for (const auto& matc
mattreynolds
2016/09/02 00:29:08
Done.
|
| + if (match.type == AutocompleteMatchType::PHYSICAL_WEB) { |
| + EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, match.type); |
|
Peter Kasting
2016/09/01 22:06:36
Nit: This EXPECT_EQ can never fail because you jus
mattreynolds
2016/09/02 00:29:08
Removed.
|
| + EXPECT_EQ(resolved_url, match.destination_url.spec()); |
| + EXPECT_EQ(resolved_url, base::UTF16ToASCII(match.contents)); |
| + EXPECT_EQ(title, base::UTF16ToASCII(match.description)); |
| + EXPECT_FALSE(match.allowed_to_be_default_match); |
| + ++metadata_match_count; |
| + } else { |
| + EXPECT_TRUE(match.allowed_to_be_default_match); |
| + ++default_match_count; |
| + } |
| + } |
| + EXPECT_EQ(2U, provider_->matches().size()); |
| + EXPECT_EQ(1U, metadata_match_count); |
| + EXPECT_EQ(1U, default_match_count); |
| } |
| TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) { |
| @@ -192,28 +225,64 @@ TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) { |
| data_source->SetMetadata(CreateMetadata(metadata_count)); |
| - // Construct an AutocompleteInput representing a typical "zero-suggest" |
| - // case. The provider will verify that the content of the omnibox input field |
| - // was not entered by the user. |
| - std::string url("http://www.cnn.com/"); |
| - const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos, |
| - std::string(), GURL(url), metrics::OmniboxEventProto::OTHER, |
| - true, false, true, true, true, TestSchemeClassifier()); |
| - provider_->Start(input, false); |
| + { |
| + // Run the test with no text in the omnibox input to simulate NTP. |
| + provider_->Start(CreateInputForNTP(), false); |
| + |
| + const size_t match_count = provider_->matches().size(); |
| + EXPECT_LT(match_count, metadata_count); |
| + |
| + // Check that the overflow item is present and its fields are correct. There |
| + // may be additional match items, but none should be marked as default. |
| + size_t overflow_match_count = 0; |
| + for (size_t i = 0; i < match_count; i++) { |
| + const AutocompleteMatch& match = provider_->matches()[i]; |
| + if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { |
| + EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW, match.type); |
| + EXPECT_EQ("chrome://physical-web/", match.destination_url.spec()); |
| + EXPECT_EQ("chrome://physical-web/", base::UTF16ToASCII(match.contents)); |
| + const size_t metadata_matches = match_count - 1; |
| + std::string description = l10n_util::GetPluralStringFUTF8( |
| + IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - metadata_matches); |
| + EXPECT_EQ(description, base::UTF16ToASCII(match.description)); |
| + ++overflow_match_count; |
| + } |
| + EXPECT_FALSE(match.allowed_to_be_default_match); |
| + } |
| + EXPECT_EQ(1U, overflow_match_count); |
| + } |
| - const size_t match_count = provider_->matches().size(); |
| - EXPECT_LT(match_count, metadata_count); |
| - |
| - // Check that the overflow item is at the end of the match list and its fields |
| - // are correct. |
| - const AutocompleteMatch& overflow_match = provider_->matches().back(); |
| - EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW, overflow_match.type); |
| - EXPECT_EQ("chrome://physical-web/", overflow_match.destination_url.spec()); |
| - EXPECT_EQ("chrome://physical-web/", |
| - base::UTF16ToASCII(overflow_match.contents)); |
| - std::string description = l10n_util::GetPluralStringFUTF8( |
| - IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - match_count + 1); |
| - EXPECT_EQ(description, base::UTF16ToASCII(overflow_match.description)); |
| + { |
| + // Run the test again with a URL in the omnibox input. An additional match |
| + // should be added as a default match. |
| + provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
| + |
| + const size_t match_count = provider_->matches().size(); |
| + EXPECT_LT(match_count - 1, metadata_count); |
| + |
| + // Check that the overflow item and default match are present and their |
| + // fields are correct. |
| + size_t overflow_match_count = 0; |
| + size_t default_match_count = 0; |
| + for (size_t i = 0; i < match_count; i++) { |
| + const AutocompleteMatch& match = provider_->matches()[i]; |
| + if (match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW) { |
| + EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW, match.type); |
| + EXPECT_EQ("chrome://physical-web/", match.destination_url.spec()); |
| + EXPECT_EQ("chrome://physical-web/", base::UTF16ToASCII(match.contents)); |
| + const size_t metadata_matches = match_count - 2; |
| + std::string description = l10n_util::GetPluralStringFUTF8( |
| + IDS_PHYSICAL_WEB_OVERFLOW, metadata_count - metadata_matches); |
| + EXPECT_EQ(description, base::UTF16ToASCII(match.description)); |
| + EXPECT_FALSE(match.allowed_to_be_default_match); |
| + ++overflow_match_count; |
| + } else if (match.allowed_to_be_default_match) { |
| + ++default_match_count; |
| + } |
| + } |
| + EXPECT_EQ(1U, overflow_match_count); |
| + EXPECT_EQ(1U, default_match_count); |
| + } |
| } |
| TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { |