File: contextual_content_suggestions_service_unittest.cc

package info (click to toggle)
chromium-browser 70.0.3538.110-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,619,476 kB
  • sloc: cpp: 13,024,755; ansic: 1,349,823; python: 916,672; xml: 314,489; java: 280,047; asm: 276,936; perl: 75,771; objc: 66,634; sh: 45,860; cs: 28,354; php: 11,064; makefile: 10,911; yacc: 9,109; tcl: 8,403; ruby: 4,065; lex: 1,779; pascal: 1,411; lisp: 1,055; awk: 41; jsp: 39; sed: 17; sql: 3
file content (318 lines) | stat: -rw-r--r-- 12,708 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright 2017 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/ntp_snippets/contextual/contextual_content_suggestions_service.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/test/mock_callback.h"
#include "components/image_fetcher/core/image_fetcher_impl.h"
#include "components/ntp_snippets/category_info.h"
#include "components/ntp_snippets/content_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestion.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_fetcher.h"
#include "components/ntp_snippets/contextual/contextual_suggestions_test_utils.h"
#include "components/ntp_snippets/contextual/reporting/contextual_suggestions_debugging_reporter.h"
#include "components/ntp_snippets/contextual/reporting/contextual_suggestions_reporter.h"
#include "components/ntp_snippets/remote/cached_image_fetcher.h"
#include "components/ntp_snippets/remote/json_to_categories.h"
#include "components/ntp_snippets/remote/remote_suggestions_database.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_unittest_util.h"

using ntp_snippets::CachedImageFetcher;
using ntp_snippets::Category;
using ntp_snippets::ContentSuggestion;
using ntp_snippets::KnownCategories;
using ntp_snippets::ImageFetchedCallback;
using ntp_snippets::ImageDataFetchedCallback;
using ntp_snippets::RemoteSuggestionsDatabase;
using ntp_snippets::RequestThrottler;

using testing::_;
using testing::AllOf;
using testing::ElementsAre;
using testing::IsEmpty;
using testing::Mock;
using testing::Pointee;
using testing::Property;

namespace contextual_suggestions {

namespace {

// Always fetches the result that was set by SetFakeResponse.
class FakeContextualSuggestionsFetcher : public ContextualSuggestionsFetcher {
 public:
  void FetchContextualSuggestionsClusters(
      const GURL& url,
      FetchClustersCallback callback,
      ReportFetchMetricsCallback metrics_callback) override {
    ContextualSuggestionsResult result;
    result.peek_text = "peek text";
    result.clusters = std::move(fake_suggestions_);
    result.peek_conditions = peek_conditions_;
    std::move(callback).Run(std::move(result));
    fake_suggestions_.clear();
  }

  void SetFakeResponse(std::vector<Cluster> fake_suggestions,
                       PeekConditions peek_conditions = PeekConditions()) {
    fake_suggestions_ = std::move(fake_suggestions);
    peek_conditions_ = peek_conditions;
  }

 private:
  std::vector<Cluster> fake_suggestions_;
  PeekConditions peek_conditions_;
};

// Always fetches a fake image if the given URL is valid.
class FakeCachedImageFetcher : public CachedImageFetcher {
 public:
  explicit FakeCachedImageFetcher(PrefService* pref_service)
      : CachedImageFetcher(std::unique_ptr<image_fetcher::ImageFetcher>(),
                           pref_service,
                           nullptr) {}

  void FetchSuggestionImage(const ContentSuggestion::ID&,
                            const GURL& image_url,
                            ImageDataFetchedCallback image_data_callback,
                            ImageFetchedCallback callback) override {
    gfx::Image image;
    if (image_url.is_valid()) {
      image = gfx::test::CreateImage();
    }
    std::move(callback).Run(image);
  }
};

}  // namespace

class ContextualContentSuggestionsServiceTest : public testing::Test {
 public:
  ContextualContentSuggestionsServiceTest() {
    RequestThrottler::RegisterProfilePrefs(pref_service_.registry());
    std::unique_ptr<FakeContextualSuggestionsFetcher> fetcher =
        std::make_unique<FakeContextualSuggestionsFetcher>();
    fetcher_ = fetcher.get();
    auto debugging_reporter = std::make_unique<
        contextual_suggestions::ContextualSuggestionsDebuggingReporter>();
    auto reporter_provider = std::make_unique<
        contextual_suggestions::ContextualSuggestionsReporterProvider>(
        std::move(debugging_reporter));
    source_ = std::make_unique<ContextualContentSuggestionsService>(
        std::move(fetcher),
        std::make_unique<FakeCachedImageFetcher>(&pref_service_),
        std::unique_ptr<RemoteSuggestionsDatabase>(),
        std::move(reporter_provider));
  }

  FakeContextualSuggestionsFetcher* fetcher() { return fetcher_; }
  ContextualContentSuggestionsService* source() { return source_.get(); }

 private:
  FakeContextualSuggestionsFetcher* fetcher_;
  base::MessageLoop message_loop_;
  TestingPrefServiceSimple pref_service_;
  std::unique_ptr<ContextualContentSuggestionsService> source_;

  DISALLOW_COPY_AND_ASSIGN(ContextualContentSuggestionsServiceTest);
};

TEST_F(ContextualContentSuggestionsServiceTest,
       ShouldFetchContextualSuggestionsClusters) {
  MockClustersCallback mock_callback;
  std::vector<Cluster> clusters;
  GURL context_url("http://www.from.url");

  clusters.emplace_back(ClusterBuilder("Title")
                            .AddSuggestion(SuggestionBuilder(context_url)
                                               .Title("Title1")
                                               .PublisherName("from.url")
                                               .Snippet("Summary")
                                               .ImageId("abc")
                                               .Build())
                            .Build());

  fetcher()->SetFakeResponse(std::move(clusters));
  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback.ToOnceCallback(), base::DoNothing());
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(mock_callback.has_run);
}

TEST_F(ContextualContentSuggestionsServiceTest, ShouldRejectInvalidUrls) {
  std::vector<Cluster> clusters;
  for (GURL invalid_url :
       {GURL("htp:/"), GURL("www.foobar"), GURL("http://127.0.0.1/"),
        GURL("file://some.file"), GURL("chrome://settings"), GURL("")}) {
    MockClustersCallback mock_callback;
    source()->FetchContextualSuggestionClusters(
        invalid_url,
        base::BindOnce(&MockClustersCallback::Done,
                       base::Unretained(&mock_callback)),
        base::DoNothing());
    base::RunLoop().RunUntilIdle();
    EXPECT_TRUE(mock_callback.has_run);
    EXPECT_EQ(mock_callback.response_peek_text, "");
    EXPECT_EQ(mock_callback.response_clusters.size(), 0u);
  }
}

TEST_F(ContextualContentSuggestionsServiceTest,
       ShouldNotReportLowConfidenceResults) {
  MockClustersCallback mock_callback;
  std::vector<Cluster> clusters;
  GURL context_url("http://www.from.url");

  clusters.emplace_back(ClusterBuilder("Title")
                            .AddSuggestion(SuggestionBuilder(context_url)
                                               .Title("Title1")
                                               .PublisherName("from.url")
                                               .Snippet("Summary")
                                               .ImageId("abc")
                                               .Build())
                            .Build());

  PeekConditions peek_conditions;
  peek_conditions.confidence = 0.5;

  fetcher()->SetFakeResponse(std::move(clusters), peek_conditions);

  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback.ToOnceCallback(), base::DoNothing());
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(mock_callback.has_run);
  EXPECT_EQ(mock_callback.response_clusters.size(), 0u);
  EXPECT_EQ(mock_callback.response_peek_text, std::string());
}

TEST_F(ContextualContentSuggestionsServiceTest, ShouldCacheResults) {
  MockClustersCallback mock_callback;
  MockClustersCallback mock_callback2;
  std::vector<Cluster> clusters;
  GURL context_url("http://www.from.url");

  clusters.emplace_back(ClusterBuilder("Title")
                            .AddSuggestion(SuggestionBuilder(context_url)
                                               .Title("Title1")
                                               .PublisherName("from.url")
                                               .Snippet("Summary")
                                               .ImageId("abc")
                                               .Build())
                            .Build());
  fetcher()->SetFakeResponse(clusters);
  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback.ToOnceCallback(), base::DoNothing());
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(mock_callback.has_run);

  // The correct result should be present even though we haven't set the fake
  // response.
  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback2.ToOnceCallback(), base::DoNothing());
  EXPECT_TRUE(mock_callback2.has_run);
  ExpectResponsesMatch(
      mock_callback2,
      ContextualSuggestionsResult("peek text", clusters, PeekConditions(),
                                  ServerExperimentInfos()));
}

TEST_F(ContextualContentSuggestionsServiceTest, ShouldEvictOldCachedResults) {
  std::vector<Cluster> clusters;
  clusters.emplace_back(
      ClusterBuilder("Title")
          .AddSuggestion(SuggestionBuilder(GURL("http://foobar.com"))
                             .Title("Title1")
                             .PublisherName("from.url")
                             .Snippet("Summary")
                             .ImageId("abc")
                             .Build())
          .Build());

  for (int i = 0; i < kFetchCacheCapacity + 1; i++) {
    MockClustersCallback mock_callback;
    GURL context_url("http://www.from.url/" + base::NumberToString(i));

    fetcher()->SetFakeResponse(clusters);
    source()->FetchContextualSuggestionClusters(
        context_url, mock_callback.ToOnceCallback(), base::DoNothing());
    base::RunLoop().RunUntilIdle();

    ExpectResponsesMatch(
        mock_callback,
        ContextualSuggestionsResult("peek text", clusters, PeekConditions(),
                                    ServerExperimentInfos()));
  }

  // Urls numbered kFetchCacheCapacity through 1 should be cached still; 0
  // should have been evicted.
  for (int i = kFetchCacheCapacity; i > 0; i--) {
    GURL context_url("http://www.from.url/" + base::NumberToString(i));
    MockClustersCallback mock_callback;
    source()->FetchContextualSuggestionClusters(
        context_url, mock_callback.ToOnceCallback(), base::DoNothing());
    ExpectResponsesMatch(
        mock_callback,
        ContextualSuggestionsResult("peek text", clusters, PeekConditions(),
                                    ServerExperimentInfos()));
  }

  GURL context_url("http://www.from.url/0");
  MockClustersCallback mock_callback;
  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback.ToOnceCallback(), base::DoNothing());
  EXPECT_EQ(mock_callback.response_clusters.size(), 0u);
}

TEST_F(ContextualContentSuggestionsServiceTest,
       ShouldNotReturnCachedLowConfidenceResults) {
  MockClustersCallback mock_callback;
  MockClustersCallback mock_callback2;
  std::vector<Cluster> clusters;
  GURL context_url("http://www.from.url");

  clusters.emplace_back(ClusterBuilder("Title")
                            .AddSuggestion(SuggestionBuilder(context_url)
                                               .Title("Title1")
                                               .PublisherName("from.url")
                                               .Snippet("Summary")
                                               .ImageId("abc")
                                               .Build())
                            .Build());
  PeekConditions peek_conditions;
  peek_conditions.confidence = 0;
  fetcher()->SetFakeResponse(clusters, peek_conditions);
  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback.ToOnceCallback(), base::DoNothing());
  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(mock_callback.has_run);
  ExpectResponsesMatch(mock_callback, ContextualSuggestionsResult());

  // The cached result we get back should be empty, since its confidence is
  // below the threshold.
  source()->FetchContextualSuggestionClusters(
      context_url, mock_callback2.ToOnceCallback(), base::DoNothing());
  EXPECT_TRUE(mock_callback2.has_run);
  ExpectResponsesMatch(mock_callback2, ContextualSuggestionsResult());
}

}  // namespace contextual_suggestions