| 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/fake_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_data_source.h" |
| 18 #include "components/physical_web/data_source/physical_web_listener.h" | 19 #include "components/physical_web/data_source/physical_web_listener.h" |
| 19 #include "grit/components_strings.h" | 20 #include "grit/components_strings.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/gfx/text_elider.h" | 24 #include "ui/gfx/text_elider.h" |
| 24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 25 | 26 |
| 27 using physical_web::FakePhysicalWebDataSource; |
| 26 using physical_web::PhysicalWebDataSource; | 28 using physical_web::PhysicalWebDataSource; |
| 27 using physical_web::PhysicalWebListener; | 29 using physical_web::PhysicalWebListener; |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 // A mock implementation of the Physical Web data source that allows setting | 33 // An autocomplete provider client that embeds the fake Physical Web data |
| 32 // metadata for nearby URLs directly. | |
| 33 class MockPhysicalWebDataSource : public PhysicalWebDataSource { | |
| 34 public: | |
| 35 MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {} | |
| 36 ~MockPhysicalWebDataSource() override {} | |
| 37 | |
| 38 void StartDiscovery(bool network_request_enabled) override {} | |
| 39 void StopDiscovery() override {} | |
| 40 | |
| 41 std::unique_ptr<base::ListValue> GetMetadata() override { | |
| 42 return metadata_->CreateDeepCopy(); | |
| 43 } | |
| 44 | |
| 45 bool HasUnresolvedDiscoveries() override { | |
| 46 return false; | |
| 47 } | |
| 48 | |
| 49 void RegisterListener(PhysicalWebListener* physical_web_listener) override {} | |
| 50 | |
| 51 void UnregisterListener( | |
| 52 PhysicalWebListener* physical_web_listener) override {} | |
| 53 | |
| 54 // for testing | |
| 55 void SetMetadata(std::unique_ptr<base::ListValue> metadata) { | |
| 56 metadata_ = std::move(metadata); | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 std::unique_ptr<base::ListValue> metadata_; | |
| 61 }; | |
| 62 | |
| 63 // An autocomplete provider client that embeds the mock Physical Web data | |
| 64 // source. | 34 // source. |
| 65 class FakeAutocompleteProviderClient | 35 class FakeAutocompleteProviderClient |
| 66 : public testing::NiceMock<MockAutocompleteProviderClient> { | 36 : public testing::NiceMock<MockAutocompleteProviderClient> { |
| 67 public: | 37 public: |
| 68 FakeAutocompleteProviderClient() | 38 FakeAutocompleteProviderClient() |
| 69 : physical_web_data_source_( | 39 : physical_web_data_source_( |
| 70 base::MakeUnique<MockPhysicalWebDataSource>()), | 40 base::MakeUnique<FakePhysicalWebDataSource>()), |
| 71 is_off_the_record_(false) | 41 is_off_the_record_(false) {} |
| 72 { | |
| 73 } | |
| 74 | 42 |
| 75 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override { | 43 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override { |
| 76 return scheme_classifier_; | 44 return scheme_classifier_; |
| 77 } | 45 } |
| 78 | 46 |
| 79 PhysicalWebDataSource* GetPhysicalWebDataSource() override { | 47 PhysicalWebDataSource* GetPhysicalWebDataSource() override { |
| 80 return physical_web_data_source_.get(); | 48 return physical_web_data_source_.get(); |
| 81 } | 49 } |
| 82 | 50 |
| 83 bool IsOffTheRecord() const override { | 51 bool IsOffTheRecord() const override { |
| 84 return is_off_the_record_; | 52 return is_off_the_record_; |
| 85 } | 53 } |
| 86 | 54 |
| 87 // Convenience method to avoid downcasts when accessing the mock data source. | 55 // Convenience method to avoid downcasts when accessing the fake data source. |
| 88 MockPhysicalWebDataSource* GetMockPhysicalWebDataSource() { | 56 FakePhysicalWebDataSource* GetFakePhysicalWebDataSource() { |
| 89 return physical_web_data_source_.get(); | 57 return physical_web_data_source_.get(); |
| 90 } | 58 } |
| 91 | 59 |
| 92 // Allow tests to enable incognito mode. | 60 // Allow tests to enable incognito mode. |
| 93 void SetOffTheRecord(bool is_off_the_record) { | 61 void SetOffTheRecord(bool is_off_the_record) { |
| 94 is_off_the_record_ = is_off_the_record; | 62 is_off_the_record_ = is_off_the_record; |
| 95 } | 63 } |
| 96 | 64 |
| 97 private: | 65 private: |
| 98 std::unique_ptr<MockPhysicalWebDataSource> physical_web_data_source_; | 66 std::unique_ptr<FakePhysicalWebDataSource> physical_web_data_source_; |
| 99 TestSchemeClassifier scheme_classifier_; | 67 TestSchemeClassifier scheme_classifier_; |
| 100 bool is_off_the_record_; | 68 bool is_off_the_record_; |
| 101 }; | 69 }; |
| 102 | 70 |
| 103 } // namespace | 71 } // namespace |
| 104 | 72 |
| 105 class PhysicalWebProviderTest : public testing::Test { | 73 class PhysicalWebProviderTest : public testing::Test { |
| 106 protected: | 74 protected: |
| 107 PhysicalWebProviderTest() : provider_(NULL) {} | 75 PhysicalWebProviderTest() : provider_(NULL) {} |
| 108 ~PhysicalWebProviderTest() override {} | 76 ~PhysicalWebProviderTest() override {} |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 // |should_expect_default_match| or |should_expect_overflow_item| are true. | 173 // |should_expect_default_match| or |should_expect_overflow_item| are true. |
| 206 // Metadata matches are not checked. | 174 // Metadata matches are not checked. |
| 207 void OverflowItemTestCase(const AutocompleteInput& input, | 175 void OverflowItemTestCase(const AutocompleteInput& input, |
| 208 std::unique_ptr<base::ListValue> metadata_list, | 176 std::unique_ptr<base::ListValue> metadata_list, |
| 209 const std::string& title_truncated, | 177 const std::string& title_truncated, |
| 210 size_t expected_match_count, | 178 size_t expected_match_count, |
| 211 bool should_expect_default_match, | 179 bool should_expect_default_match, |
| 212 bool should_expect_overflow_item) { | 180 bool should_expect_overflow_item) { |
| 213 const size_t metadata_count = metadata_list->GetSize(); | 181 const size_t metadata_count = metadata_list->GetSize(); |
| 214 | 182 |
| 215 MockPhysicalWebDataSource* data_source = | 183 FakePhysicalWebDataSource* data_source = |
| 216 client_->GetMockPhysicalWebDataSource(); | 184 client_->GetFakePhysicalWebDataSource(); |
| 217 EXPECT_TRUE(data_source); | 185 EXPECT_TRUE(data_source); |
| 218 | 186 |
| 219 data_source->SetMetadata(std::move(metadata_list)); | 187 data_source->SetMetadata(std::move(metadata_list)); |
| 220 | 188 |
| 221 provider_->Start(input, false); | 189 provider_->Start(input, false); |
| 222 | 190 |
| 223 const size_t match_count = provider_->matches().size(); | 191 const size_t match_count = provider_->matches().size(); |
| 224 EXPECT_EQ(expected_match_count, match_count); | 192 EXPECT_EQ(expected_match_count, match_count); |
| 225 | 193 |
| 226 const size_t match_count_without_default = | 194 const size_t match_count_without_default = |
| (...skipping 25 matching lines...) Expand all Loading... |
| 252 } | 220 } |
| 253 | 221 |
| 254 std::unique_ptr<FakeAutocompleteProviderClient> client_; | 222 std::unique_ptr<FakeAutocompleteProviderClient> client_; |
| 255 scoped_refptr<PhysicalWebProvider> provider_; | 223 scoped_refptr<PhysicalWebProvider> provider_; |
| 256 | 224 |
| 257 private: | 225 private: |
| 258 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); | 226 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest); |
| 259 }; | 227 }; |
| 260 | 228 |
| 261 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { | 229 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) { |
| 262 MockPhysicalWebDataSource* data_source = | 230 FakePhysicalWebDataSource* data_source = |
| 263 client_->GetMockPhysicalWebDataSource(); | 231 client_->GetFakePhysicalWebDataSource(); |
| 264 EXPECT_TRUE(data_source); | 232 EXPECT_TRUE(data_source); |
| 265 | 233 |
| 266 data_source->SetMetadata(CreateMetadata(0)); | 234 data_source->SetMetadata(CreateMetadata(0)); |
| 267 | 235 |
| 268 // Run the test with no text in the omnibox input to simulate NTP. | 236 // Run the test with no text in the omnibox input to simulate NTP. |
| 269 provider_->Start(CreateInputForNTP(), false); | 237 provider_->Start(CreateInputForNTP(), false); |
| 270 EXPECT_TRUE(provider_->matches().empty()); | 238 EXPECT_TRUE(provider_->matches().empty()); |
| 271 | 239 |
| 272 // Run the test again with a URL in the omnibox input. | 240 // Run the test again with a URL in the omnibox input. |
| 273 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); | 241 provider_->Start(CreateInputWithCurrentUrl("http://www.cnn.com"), false); |
| 274 EXPECT_TRUE(provider_->matches().empty()); | 242 EXPECT_TRUE(provider_->matches().empty()); |
| 275 } | 243 } |
| 276 | 244 |
| 277 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { | 245 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) { |
| 278 MockPhysicalWebDataSource* data_source = | 246 FakePhysicalWebDataSource* data_source = |
| 279 client_->GetMockPhysicalWebDataSource(); | 247 client_->GetFakePhysicalWebDataSource(); |
| 280 EXPECT_TRUE(data_source); | 248 EXPECT_TRUE(data_source); |
| 281 | 249 |
| 282 // Extract the URL and title before inserting the metadata into the data | 250 // Extract the URL and title before inserting the metadata into the data |
| 283 // source. | 251 // source. |
| 284 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1); | 252 std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1); |
| 285 base::DictionaryValue* metadata_item; | 253 base::DictionaryValue* metadata_item; |
| 286 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); | 254 EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item)); |
| 287 std::string resolved_url; | 255 std::string resolved_url; |
| 288 EXPECT_TRUE( | 256 EXPECT_TRUE( |
| 289 metadata_item->GetString(physical_web::kResolvedUrlKey, &resolved_url)); | 257 metadata_item->GetString(physical_web::kResolvedUrlKey, &resolved_url)); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 315 EXPECT_TRUE(match.allowed_to_be_default_match); | 283 EXPECT_TRUE(match.allowed_to_be_default_match); |
| 316 ++default_match_count; | 284 ++default_match_count; |
| 317 } | 285 } |
| 318 } | 286 } |
| 319 EXPECT_EQ(2U, provider_->matches().size()); | 287 EXPECT_EQ(2U, provider_->matches().size()); |
| 320 EXPECT_EQ(1U, metadata_match_count); | 288 EXPECT_EQ(1U, metadata_match_count); |
| 321 EXPECT_EQ(1U, default_match_count); | 289 EXPECT_EQ(1U, default_match_count); |
| 322 } | 290 } |
| 323 | 291 |
| 324 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { | 292 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) { |
| 325 MockPhysicalWebDataSource* data_source = | 293 FakePhysicalWebDataSource* data_source = |
| 326 client_->GetMockPhysicalWebDataSource(); | 294 client_->GetFakePhysicalWebDataSource(); |
| 327 EXPECT_TRUE(data_source); | 295 EXPECT_TRUE(data_source); |
| 328 | 296 |
| 329 data_source->SetMetadata(CreateMetadata(1)); | 297 data_source->SetMetadata(CreateMetadata(1)); |
| 330 | 298 |
| 331 // Construct an AutocompleteInput to simulate user input in the omnibox input | 299 // Construct an AutocompleteInput to simulate user input in the omnibox input |
| 332 // field. The provider should not generate any matches. | 300 // field. The provider should not generate any matches. |
| 333 std::string text("user input"); | 301 std::string text("user input"); |
| 334 const AutocompleteInput input( | 302 const AutocompleteInput input( |
| 335 base::UTF8ToUTF16(text), text.length(), std::string(), GURL(), | 303 base::UTF8ToUTF16(text), text.length(), std::string(), GURL(), |
| 336 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 304 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 | 362 |
| 395 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), | 363 OverflowItemTestCase(CreateInputForNTP(), std::move(metadata_list), |
| 396 "ויקיפדיה", PhysicalWebProvider::kPhysicalWebMaxMatches, | 364 "ויקיפדיה", PhysicalWebProvider::kPhysicalWebMaxMatches, |
| 397 false, true); | 365 false, true); |
| 398 } | 366 } |
| 399 | 367 |
| 400 TEST_F(PhysicalWebProviderTest, TestNoMatchesInIncognito) { | 368 TEST_F(PhysicalWebProviderTest, TestNoMatchesInIncognito) { |
| 401 // Enable incognito mode | 369 // Enable incognito mode |
| 402 client_->SetOffTheRecord(true); | 370 client_->SetOffTheRecord(true); |
| 403 | 371 |
| 404 MockPhysicalWebDataSource* data_source = | 372 FakePhysicalWebDataSource* data_source = |
| 405 client_->GetMockPhysicalWebDataSource(); | 373 client_->GetFakePhysicalWebDataSource(); |
| 406 EXPECT_TRUE(data_source); | 374 EXPECT_TRUE(data_source); |
| 407 | 375 |
| 408 data_source->SetMetadata(CreateMetadata(1)); | 376 data_source->SetMetadata(CreateMetadata(1)); |
| 409 provider_->Start(CreateInputForNTP(), false); | 377 provider_->Start(CreateInputForNTP(), false); |
| 410 | 378 |
| 411 EXPECT_TRUE(provider_->matches().empty()); | 379 EXPECT_TRUE(provider_->matches().empty()); |
| 412 } | 380 } |
| OLD | NEW |