Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(644)

Unified Diff: components/omnibox/browser/physical_web_provider_unittest.cc

Issue 2203993002: Add a Physical Web omnibox autocomplete provider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unit test fixes Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/omnibox/browser/physical_web_provider.cc ('k') | components/omnibox_strings.grdp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
new file mode 100644
index 0000000000000000000000000000000000000000..606f92955a02fe0a9b404a4cc49e0969ff2faa59
--- /dev/null
+++ b/components/omnibox/browser/physical_web_provider_unittest.cc
@@ -0,0 +1,219 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/omnibox/browser/physical_web_provider.h"
+
+#include <memory>
+#include <string>
+
+#include "base/memory/ptr_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "components/metrics/proto/omnibox_event.pb.h"
+#include "components/omnibox/browser/mock_autocomplete_provider_client.h"
+#include "components/omnibox/browser/test_scheme_classifier.h"
+#include "components/physical_web/data_source/physical_web_data_source.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "url/gurl.h"
+
+namespace {
+
+// A mock implementation of the Physical Web data source that allows setting
+// metadata for nearby URLs directly.
+class MockPhysicalWebDataSource : public PhysicalWebDataSource {
+ public:
+ MockPhysicalWebDataSource() : metadata_(new base::ListValue()) {}
+ ~MockPhysicalWebDataSource() override {}
+
+ void StartDiscovery(bool network_request_enabled) override {}
+ void StopDiscovery() override {}
+
+ std::unique_ptr<base::ListValue> GetMetadata() override {
+ return metadata_->CreateDeepCopy();
+ }
+
+ bool HasUnresolvedDiscoveries() override {
+ return false;
+ }
+
+ // for testing
+ void SetMetadata(std::unique_ptr<base::ListValue> metadata) {
+ metadata_ = std::move(metadata);
+ }
+
+ private:
+ std::unique_ptr<base::ListValue> metadata_;
+};
+
+// An autocomplete provider client that embeds the mock Physical Web data
+// source.
+class FakeAutocompleteProviderClient
+ : public testing::NiceMock<MockAutocompleteProviderClient> {
+ public:
+ FakeAutocompleteProviderClient()
+ : physical_web_data_source_(base::MakeUnique<MockPhysicalWebDataSource>())
+ {
+ }
+
+ const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
+ return scheme_classifier_;
+ }
+
+ PhysicalWebDataSource* GetPhysicalWebDataSource() override {
+ return physical_web_data_source_.get();
+ }
+
+ private:
+ std::unique_ptr<MockPhysicalWebDataSource> physical_web_data_source_;
+ TestSchemeClassifier scheme_classifier_;
+};
+
+class PhysicalWebProviderTest : public testing::Test {
+ protected:
+ PhysicalWebProviderTest() : provider_(NULL) {}
+ ~PhysicalWebProviderTest() override {}
+
+ void SetUp() override {
+ client_.reset(new FakeAutocompleteProviderClient());
+ provider_ = PhysicalWebProvider::Create(client_.get());
+ }
+
+ void TearDown() override {
+ provider_ = NULL;
+ }
+
+ // Create a dummy metadata list with |metadata_count| items. Each item is
+ // populated with a unique scanned URL and page metadata.
+ static std::unique_ptr<base::ListValue> CreateMetadata(
+ size_t metadata_count) {
+ auto metadata_list = base::MakeUnique<base::ListValue>();
+ for (size_t i = 0; i < metadata_count; ++i) {
+ std::ostringstream buf;
+ buf << i;
+ std::string item_id = buf.str();
Mark P 2016/08/18 19:52:26 What's wrong with base::SizeTToString()?
mattreynolds 2016/08/18 22:33:32 Nothing, just never encountered it before. Thanks!
+ std::string url = "https://example.com/" + item_id;
+ auto metadata_item = base::MakeUnique<base::DictionaryValue>();
+ metadata_item->SetString("scannedUrl", url);
+ metadata_item->SetString("resolvedUrl", url);
+ metadata_item->SetString("icon", url);
+ metadata_item->SetString("title", "Example title " + item_id);
+ metadata_item->SetString("description", "Example description " + item_id);
+ metadata_list->Append(std::move(metadata_item));
+ }
+ return metadata_list;
+ }
+
+ std::unique_ptr<FakeAutocompleteProviderClient> client_;
+ scoped_refptr<PhysicalWebProvider> provider_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest);
+};
+
+TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) {
+ MockPhysicalWebDataSource* data_source =
+ (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
+ EXPECT_TRUE(data_source);
+
+ 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);
+
+ EXPECT_TRUE(provider_->matches().empty());
+}
+
+TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) {
+ MockPhysicalWebDataSource* data_source =
+ (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
+ EXPECT_TRUE(data_source);
+
+ // Extract the URL and title before inserting the metadata into the data
+ // source.
+ std::unique_ptr<base::ListValue> metadata_list = CreateMetadata(1);
+ base::DictionaryValue* metadata_item;
+ EXPECT_TRUE(metadata_list->GetDictionary(0, &metadata_item));
+ std::string resolved_url;
+ EXPECT_TRUE(metadata_item->GetString("resolvedUrl", &resolved_url));
+ std::string title;
+ EXPECT_TRUE(metadata_item->GetString("title", &title));
+
+ 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);
+
+ // Check that there is only one match item and its fields are correct.
+ EXPECT_EQ(1U, provider_->matches().size());
+ const AutocompleteMatch& metadata_match = provider_->matches().front();
+ EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB, metadata_match.type);
+ 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));
+}
+
+TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) {
+ // This test is intended to verify that an overflow item is created when the
+ // number of nearby Physical Web URLs exceeds the maximum allowable matches
+ // for this provider. The actual limit for the PhysicalWebProvider may be
+ // changed in the future, so create enough metadata to exceed the
+ // AutocompleteProvider's limit.
+ const size_t metadata_count = AutocompleteProvider::kMaxMatches + 1;
+
+ MockPhysicalWebDataSource* data_source =
+ (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
+ EXPECT_TRUE(data_source);
+
+ 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);
+
+ EXPECT_LT(provider_->matches().size(), metadata_count);
+
+ // Check that the overflow item is at the end of the match list and has the
+ // correct type.
+ const AutocompleteMatch& overflow_match = provider_->matches().back();
+ EXPECT_EQ(AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW, overflow_match.type);
Mark P 2016/08/18 19:52:26 Does it makes sense to check other fields here? P
mattreynolds 2016/08/18 22:33:32 Sure, I added checks for the destination URL, desc
+}
+
+TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) {
+ MockPhysicalWebDataSource* data_source =
+ (MockPhysicalWebDataSource*)client_->GetPhysicalWebDataSource();
+ EXPECT_TRUE(data_source);
+
+ data_source->SetMetadata(CreateMetadata(1));
+
+ // Construct an AutocompleteInput to simulate user input in the omnibox input
+ // field. The provider should not generate any matches.
+ std::string text("user input");
+ const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(),
+ std::string(), GURL(),
+ metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
+ true, false, true, true, false, TestSchemeClassifier());
+ provider_->Start(input, false);
+
+ EXPECT_TRUE(provider_->matches().empty());
+}
+
+}
« no previous file with comments | « components/omnibox/browser/physical_web_provider.cc ('k') | components/omnibox_strings.grdp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698