File: foreign_sessions_suggestions_provider_unittest.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (375 lines) | stat: -rw-r--r-- 15,227 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// 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/ntp_snippets/sessions/foreign_sessions_suggestions_provider.h"

#include <map>
#include <utility>

#include "base/callback_forward.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "components/ntp_snippets/category.h"
#include "components/ntp_snippets/content_suggestions_provider.h"
#include "components/ntp_snippets/mock_content_suggestions_provider_observer.h"
#include "components/prefs/testing_pref_service.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "components/sessions/core/serialized_navigation_entry_test_helper.h"
#include "components/sessions/core/session_types.h"
#include "components/sync_sessions/synced_session.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::Time;
using base::TimeDelta;
using sessions::SerializedNavigationEntry;
using sessions::SessionTab;
using sessions::SessionWindow;
using sync_sessions::SyncedSession;
using testing::ElementsAre;
using testing::IsEmpty;
using testing::Property;
using testing::Test;
using testing::_;

namespace ntp_snippets {
namespace {

const char kUrl1[] = "http://www.fake1.com/";
const char kUrl2[] = "http://www.fake2.com/";
const char kUrl3[] = "http://www.fake3.com/";
const char kUrl4[] = "http://www.fake4.com/";
const char kUrl5[] = "http://www.fake5.com/";
const char kUrl6[] = "http://www.fake6.com/";
const char kUrl7[] = "http://www.fake7.com/";
const char kUrl8[] = "http://www.fake8.com/";
const char kUrl9[] = "http://www.fake9.com/";
const char kUrl10[] = "http://www.fake10.com/";
const char kUrl11[] = "http://www.fake11.com/";
const char kTitle[] = "title is ignored";

SessionWindow* GetOrCreateWindow(SyncedSession* session, int window_id) {
  if (session->windows.find(window_id) == session->windows.end()) {
    session->windows[window_id] = base::MakeUnique<SessionWindow>();
  }

  return session->windows[window_id].get();
}

void AddTabToSession(SyncedSession* session,
                     int window_id,
                     const std::string& url,
                     TimeDelta age) {
  SerializedNavigationEntry navigation =
      sessions::SerializedNavigationEntryTestHelper::CreateNavigation(url,
                                                                      kTitle);

  std::unique_ptr<SessionTab> tab = base::MakeUnique<SessionTab>();
  tab->timestamp = Time::Now() - age;
  tab->navigations.push_back(navigation);

  SessionWindow* window = GetOrCreateWindow(session, window_id);
  // The window deletes the tabs it points at upon destruction.
  window->tabs.push_back(std::move(tab));
}

class FakeForeignSessionsProvider : public ForeignSessionsProvider {
 public:
  ~FakeForeignSessionsProvider() override = default;
  void SetAllForeignSessions(std::vector<const SyncedSession*> sessions) {
    sessions_ = std::move(sessions);
    change_callback_.Run();
  }

  // ForeignSessionsProvider implementation.
  void SubscribeForForeignTabChange(
      const base::Closure& change_callback) override {
    change_callback_ = change_callback;
  }
  bool HasSessionsData() override { return true; }
  std::vector<const sync_sessions::SyncedSession*> GetAllForeignSessions()
      override {
    return sessions_;
  }

 private:
  std::vector<const SyncedSession*> sessions_;
  base::Closure change_callback_;
};
}  // namespace

class ForeignSessionsSuggestionsProviderTest : public Test {
 public:
  ForeignSessionsSuggestionsProviderTest() {
    ForeignSessionsSuggestionsProvider::RegisterProfilePrefs(
        pref_service_.registry());

    std::unique_ptr<FakeForeignSessionsProvider>
        fake_foreign_sessions_provider =
            base::MakeUnique<FakeForeignSessionsProvider>();
    fake_foreign_sessions_provider_ = fake_foreign_sessions_provider.get();

    // During the provider's construction the following mock calls occur.
    EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty()));
    EXPECT_CALL(*observer(), OnCategoryStatusChanged(
                                 _, category(), CategoryStatus::AVAILABLE));

    provider_ = base::MakeUnique<ForeignSessionsSuggestionsProvider>(
        &observer_, std::move(fake_foreign_sessions_provider), &pref_service_);
  }

 protected:
  SyncedSession* GetOrCreateSession(int session_id) {
    if (sessions_map_.find(session_id) == sessions_map_.end()) {
      std::string id_as_string = base::IntToString(session_id);
      std::unique_ptr<SyncedSession> owned_session =
          base::MakeUnique<SyncedSession>();
      owned_session->session_tag = id_as_string;
      owned_session->session_name = id_as_string;
      sessions_map_[session_id] = std::move(owned_session);
    }
    return sessions_map_[session_id].get();
  }

  void AddTab(int session_id,
              int window_id,
              const std::string& url,
              TimeDelta age) {
    AddTabToSession(GetOrCreateSession(session_id), window_id, url, age);
  }

  void ClearSessionData() { sessions_map_.clear(); }

  void TriggerOnChange() {
    std::vector<const SyncedSession*> sessions;
    for (const auto& kv : sessions_map_) {
      sessions.push_back(kv.second.get());
    }
    fake_foreign_sessions_provider_->SetAllForeignSessions(std::move(sessions));
  }

  void Dismiss(const std::string& url) {
    // The url of a given suggestion is used as the |id_within_category|.
    provider_->DismissSuggestion(ContentSuggestion::ID(category(), url));
  }

  Category category() {
    return Category::FromKnownCategory(KnownCategories::FOREIGN_TABS);
  }

  MockContentSuggestionsProviderObserver* observer() { return &observer_; }

 private:
  FakeForeignSessionsProvider* fake_foreign_sessions_provider_;
  MockContentSuggestionsProviderObserver observer_;
  TestingPrefServiceSimple pref_service_;
  std::unique_ptr<ForeignSessionsSuggestionsProvider> provider_;
  std::map<int, std::unique_ptr<SyncedSession>> sessions_map_;

  DISALLOW_COPY_AND_ASSIGN(ForeignSessionsSuggestionsProviderTest);
};

TEST_F(ForeignSessionsSuggestionsProviderTest, Empty) {
  // When no sessions data is added, expect no suggestions.
  EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty()));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, Single) {
  // Expect a single valid tab because that is what has been added.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)))));
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, Old) {
  // The only sessions data is too old to be suggested, so expect empty.
  EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty()));
  AddTab(0, 0, kUrl1, TimeDelta::FromHours(4));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, Ordered) {
  // Suggestions ordering should be in reverse chronological order, or youngest
  // first.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)),
                              Property(&ContentSuggestion::url, GURL(kUrl2)),
                              Property(&ContentSuggestion::url, GURL(kUrl3)),
                              Property(&ContentSuggestion::url, GURL(kUrl4)))));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  AddTab(0, 0, kUrl4, TimeDelta::FromMinutes(4));
  AddTab(0, 1, kUrl3, TimeDelta::FromMinutes(3));
  AddTab(1, 0, kUrl1, TimeDelta::FromMinutes(1));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, MaxPerDevice) {
  // Each device, which is to equivalent a unique |session_tag|, has a limit to
  // the number of suggestions it is allowed to contribute. Here all four
  // suggestions are within the recency threshold, but only three are allowed
  // per device. As such, expect that the oldest of the four will not be
  // suggested.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)),
                              Property(&ContentSuggestion::url, GURL(kUrl2)),
                              Property(&ContentSuggestion::url, GURL(kUrl3)))));
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  AddTab(0, 0, kUrl3, TimeDelta::FromMinutes(3));
  AddTab(0, 0, kUrl4, TimeDelta::FromMinutes(4));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, MaxTotal) {
  // There's a limit to the total nubmer of suggestions that the provider will
  // ever return, which should be ten. Here there are eleven valid suggestion
  // entries, spread out over multiple devices/sessions to avoid the per device
  // cutoff. Expect that the least recent of the eleven to be dropped.
  EXPECT_CALL(
      *observer(),
      OnNewSuggestions(
          _, category(),
          ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)),
                      Property(&ContentSuggestion::url, GURL(kUrl2)),
                      Property(&ContentSuggestion::url, GURL(kUrl3)),
                      Property(&ContentSuggestion::url, GURL(kUrl4)),
                      Property(&ContentSuggestion::url, GURL(kUrl5)),
                      Property(&ContentSuggestion::url, GURL(kUrl6)),
                      Property(&ContentSuggestion::url, GURL(kUrl7)),
                      Property(&ContentSuggestion::url, GURL(kUrl8)),
                      Property(&ContentSuggestion::url, GURL(kUrl9)),
                      Property(&ContentSuggestion::url, GURL(kUrl10)))));
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  AddTab(0, 0, kUrl3, TimeDelta::FromMinutes(3));
  AddTab(1, 0, kUrl4, TimeDelta::FromMinutes(4));
  AddTab(1, 0, kUrl5, TimeDelta::FromMinutes(5));
  AddTab(1, 0, kUrl6, TimeDelta::FromMinutes(6));
  AddTab(2, 0, kUrl7, TimeDelta::FromMinutes(7));
  AddTab(2, 0, kUrl8, TimeDelta::FromMinutes(8));
  AddTab(2, 0, kUrl9, TimeDelta::FromMinutes(9));
  AddTab(3, 0, kUrl10, TimeDelta::FromMinutes(10));
  AddTab(3, 0, kUrl11, TimeDelta::FromMinutes(11));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, Duplicates) {
  // The same url is never suggested more than once at a time. All the session
  // data has the same url so only expect a single suggestion.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)))));
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  AddTab(0, 1, kUrl1, TimeDelta::FromMinutes(2));
  AddTab(1, 1, kUrl1, TimeDelta::FromMinutes(3));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, DuplicatesChangingOtherSession) {
  // Normally |kUrl4| wouldn't show up, because session_id=0 already has 3
  // younger tabs, but session_id=1 has a younger |kUrl3| which gives |kUrl4| a
  // spot.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl3)),
                              Property(&ContentSuggestion::url, GURL(kUrl1)),
                              Property(&ContentSuggestion::url, GURL(kUrl2)),
                              Property(&ContentSuggestion::url, GURL(kUrl4)))));
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  AddTab(0, 0, kUrl3, TimeDelta::FromMinutes(3));
  AddTab(0, 0, kUrl4, TimeDelta::FromMinutes(4));
  AddTab(1, 0, kUrl3, TimeDelta::FromMinutes(0));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, Dismissed) {
  // Dimissed urls should not be suggested.
  EXPECT_CALL(*observer(), OnNewSuggestions(_, category(), IsEmpty()));
  Dismiss(kUrl1);
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, DismissedChangingOwnSession) {
  // Similar to DuplicatesChangingOtherSession, without dismissals we would
  // expect urls 1-3. However, because of dismissals we reach all the down to
  // |kUrl5| before the per device cutoff is hit.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl2)),
                              Property(&ContentSuggestion::url, GURL(kUrl3)),
                              Property(&ContentSuggestion::url, GURL(kUrl5)))));
  Dismiss(kUrl1);
  Dismiss(kUrl4);
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  AddTab(0, 0, kUrl3, TimeDelta::FromMinutes(3));
  AddTab(0, 0, kUrl4, TimeDelta::FromMinutes(4));
  AddTab(0, 0, kUrl5, TimeDelta::FromMinutes(5));
  AddTab(0, 0, kUrl6, TimeDelta::FromMinutes(6));
  TriggerOnChange();
}

TEST_F(ForeignSessionsSuggestionsProviderTest, DismissedPruning) {
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)),
                              Property(&ContentSuggestion::url, GURL(kUrl2)),
                              Property(&ContentSuggestion::url, GURL(kUrl3)))));
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  AddTab(0, 0, kUrl3, TimeDelta::FromMinutes(3));
  TriggerOnChange();

  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)),
                              Property(&ContentSuggestion::url, GURL(kUrl3)))));
  Dismiss(kUrl2);
  TriggerOnChange();

  // This case is important because it verifies the dismissal of |kUrl2| from
  // above is still around.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)))));
  Dismiss(kUrl3);
  TriggerOnChange();

  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)))));
  // kUrl2 is no longer present, which should result in the dismissal being
  // pruned during the next round of suggestions.
  ClearSessionData();
  AddTab(0, 0, kUrl1, TimeDelta::FromMinutes(1));
  TriggerOnChange();

  // Verify that kUrl2 is now allowed, and the previous dismissal was pruned.
  EXPECT_CALL(*observer(),
              OnNewSuggestions(
                  _, category(),
                  ElementsAre(Property(&ContentSuggestion::url, GURL(kUrl1)),
                              Property(&ContentSuggestion::url, GURL(kUrl2)))));
  AddTab(0, 0, kUrl2, TimeDelta::FromMinutes(2));
  TriggerOnChange();
}

}  // namespace ntp_snippets