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

Side by Side 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: add unit tests 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "components/omnibox/browser/physical_web_provider.h"
6
7 #include <memory>
8 #include <string>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "components/metrics/proto/omnibox_event.pb.h"
14 #include "components/omnibox/browser/mock_autocomplete_provider_client.h"
15 #include "components/omnibox/browser/test_scheme_classifier.h"
16 #include "components/physical_web/data_source/physical_web_data_source.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "url/gurl.h"
20
21 namespace {
22
23 // A mock implementation of the Physical Web data source that allows setting
24 // metadata for nearby URLs directly.
25 class MockPhysicalWebDataSource : public PhysicalWebDataSource {
26 public:
27 MockPhysicalWebDataSource()
28 : metadata_(new base::ListValue())
29 {}
Mark P 2016/08/16 22:49:08 nit: prefer { at end of previous line ditto on the
mattreynolds 2016/08/17 01:07:22 Done.
30
31 ~MockPhysicalWebDataSource() override {}
32
33 // PhysicalWebDataSource implementation
Mark P 2016/08/16 22:49:09 This comment makes it seem like it applies to only
mattreynolds 2016/08/17 01:07:22 Removed
34 void StartDiscovery(bool network_request_enabled) override {}
35 void StopDiscovery() override {}
36
37 std::unique_ptr<base::ListValue> GetMetadata() override {
38 return metadata_->CreateDeepCopy();
39 }
40
41 bool HasUnresolvedDiscoveries() override {
42 return false;
43 }
44
45 // for testing
46 void SetMetadata(std::unique_ptr<base::ListValue> metadata) {
47 metadata_ = std::move(metadata);
48 }
49
50 private:
51 std::unique_ptr<base::ListValue> metadata_;
52 };
53
54 // An autocomplete provider client that embeds the mock Physical Web data
55 // source.
56 class FakeAutocompleteProviderClient
57 : public testing::NiceMock<MockAutocompleteProviderClient> {
58 public:
59 FakeAutocompleteProviderClient()
60 : physical_web_data_source_(base::MakeUnique<MockPhysicalWebDataSource>())
61 {}
62
63 const AutocompleteSchemeClassifier& GetSchemeClassifier() const override {
64 return scheme_classifier_;
65 }
66
67 PhysicalWebDataSource* GetPhysicalWebDataSource() override {
68 return physical_web_data_source_.get();
69 }
70
71 // Convenience method for accessing the mock data source.
Mark P 2016/08/16 22:49:08 This seems unnecessary.
mattreynolds 2016/08/17 01:07:22 Removed.
Mark P 2016/08/18 19:52:26 Oops, I see why you had this--so you don't have to
mattreynolds 2016/08/18 22:33:32 Done.
72 MockPhysicalWebDataSource* GetMockPhysicalWebDataSource() {
73 return physical_web_data_source_.get();
74 }
75
76 private:
77 std::unique_ptr<MockPhysicalWebDataSource> physical_web_data_source_;
78 TestSchemeClassifier scheme_classifier_;
79 };
80
81 class PhysicalWebProviderTest : public testing::Test {
82 protected:
83 PhysicalWebProviderTest() : provider_(NULL) {}
84 ~PhysicalWebProviderTest() override {}
85
86 void SetUp() override {
87 client_.reset(new FakeAutocompleteProviderClient());
88 provider_ = PhysicalWebProvider::Create(client_.get());
89 }
90
91 void TearDown() override {
92 provider_ = NULL;
93 }
94
95 static std::unique_ptr<base::ListValue> CreateMetadata(
Mark P 2016/08/16 22:49:08 Comment.
mattreynolds 2016/08/17 01:07:22 Done.
96 size_t metadata_count) {
97 auto metadata_list = base::MakeUnique<base::ListValue>();
98 for (size_t i = 0; i < metadata_count; ++i) {
99 std::ostringstream url;
Mark P 2016/08/16 22:49:08 cleanup
mattreynolds 2016/08/17 01:07:22 Not sure what this comment was referring to, the o
100 url << "https://example.com/" << i;
101 auto metadata_item = base::MakeUnique<base::DictionaryValue>();
102 metadata_item->SetString("scannedUrl", url.str());
103 metadata_item->SetString("resolvedUrl", url.str());
104 metadata_item->SetString("icon", url.str());
105 metadata_item->SetString("title", "Example title");
Mark P 2016/08/16 22:49:08 consider putting "i" in the title and description
mattreynolds 2016/08/17 01:07:22 Done.
106 metadata_item->SetString("description", "Example description");
107 metadata_list->Append(std::move(metadata_item));
108 }
109 return metadata_list;
110 }
111
112 std::unique_ptr<FakeAutocompleteProviderClient> client_;
113 scoped_refptr<PhysicalWebProvider> provider_;
114
115 private:
116 DISALLOW_COPY_AND_ASSIGN(PhysicalWebProviderTest);
117 };
118
119 TEST_F(PhysicalWebProviderTest, TestEmptyMetadataListCreatesNoMatches) {
120 MockPhysicalWebDataSource* data_source =
121 client_->GetMockPhysicalWebDataSource();
122 EXPECT_TRUE(data_source);
123
124 data_source->SetMetadata(CreateMetadata(0));
125
126 // Construct an AutocompleteInput representing a typical "zero-suggest"
127 // case. The provider will verify that the content of the omnibox input field
128 // was not entered by the user.
129 std::string url("http://www.cnn.com/");
130 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
131 std::string(), GURL(url), metrics::OmniboxEventProto::INVALID_SPEC,
Mark P 2016/08/16 22:49:08 Prefer OTHER to be correspond with the http://www.
mattreynolds 2016/08/17 01:07:23 Done.
132 true, false, true, true, true, TestSchemeClassifier());
133 provider_->Start(input, false);
134
135 EXPECT_TRUE(provider_->matches().empty());
136 }
137
138 TEST_F(PhysicalWebProviderTest, TestSingleMetadataItemCreatesOneMatch) {
139 MockPhysicalWebDataSource* data_source =
140 client_->GetMockPhysicalWebDataSource();
141 EXPECT_TRUE(data_source);
142
143 data_source->SetMetadata(CreateMetadata(1));
144
145 // Construct an AutocompleteInput representing a typical "zero-suggest"
146 // case. The provider will verify that the content of the omnibox input field
147 // was not entered by the user.
148 std::string url("http://www.cnn.com/");
149 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
150 std::string(), GURL(url), metrics::OmniboxEventProto::INVALID_SPEC,
151 true, false, true, true, true, TestSchemeClassifier());
152 provider_->Start(input, false);
153
154 // Check that there is only one match item and it has the correct type.
155 EXPECT_TRUE(provider_->matches().size() == 1);
Mark P 2016/08/16 22:49:08 Prefer EXPECT_EQ(1, provider_->matches().size());
mattreynolds 2016/08/17 01:07:22 Done.
156 const AutocompleteMatch& metadata_match = provider_->matches().front();
157 EXPECT_TRUE(metadata_match.type == AutocompleteMatchType::PHYSICAL_WEB);
Mark P 2016/08/16 22:49:09 Consider checking some of the other fields (i.e.,
mattreynolds 2016/08/17 01:07:22 Added checks for destination_url, contents, and de
158 }
159
160 TEST_F(PhysicalWebProviderTest, TestManyMetadataItemsCreatesOverflowItem) {
161 // This test is intended to verify that an overflow item is created when the
162 // number of nearby Physical Web URLs exceeds the maximum allowable matches
163 // for this provider. The actual limit for the PhysicalWebProvider may be
164 // changed in the future, so create enough metadata to exceed the
165 // AutocompleteProvider's limit.
Mark P 2016/08/16 22:49:08 Good comment. :-)
166 const size_t metadata_count = AutocompleteProvider::kMaxMatches + 1;
167
168 MockPhysicalWebDataSource* data_source =
169 client_->GetMockPhysicalWebDataSource();
170 EXPECT_TRUE(data_source);
171
172 data_source->SetMetadata(CreateMetadata(metadata_count));
173
174 // Construct an AutocompleteInput representing a typical "zero-suggest"
175 // case. The provider will verify that the content of the omnibox input field
176 // was not entered by the user.
177 std::string url("http://www.cnn.com/");
178 const AutocompleteInput input(base::ASCIIToUTF16(url), base::string16::npos,
179 std::string(), GURL(url), metrics::OmniboxEventProto::INVALID_SPEC,
180 true, false, true, true, true, TestSchemeClassifier());
181 provider_->Start(input, false);
182
183 EXPECT_TRUE(provider_->matches().size() < metadata_count);
184
185 // Check that the overflow item is at the end of the match list and has the
186 // correct type.
187 const AutocompleteMatch& overflow_match = provider_->matches().back();
188 EXPECT_TRUE(
189 overflow_match.type == AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW);
190 }
191
192 TEST_F(PhysicalWebProviderTest, TestNoMatchesWithUserInput) {
193 MockPhysicalWebDataSource* data_source =
194 client_->GetMockPhysicalWebDataSource();
195 EXPECT_TRUE(data_source);
196
197 data_source->SetMetadata(CreateMetadata(1));
198
199 // Construct an AutocompleteInput to simulate user input in the omnibox input
200 // field. The provider should not generate any matches.
201 std::string text("user input");
202 const AutocompleteInput input(base::ASCIIToUTF16(text), text.length(),
203 std::string(), GURL(), metrics::OmniboxEventProto::INVALID_SPEC,
Mark P 2016/08/16 22:49:09 consider using something like INSTANT_NTP_WITH_OMN
mattreynolds 2016/08/17 01:07:22 Done.
204 true, false, true, true, false, TestSchemeClassifier());
205 provider_->Start(input, false);
206
207 EXPECT_TRUE(provider_->matches().empty());
208 }
209
210 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698