summaryrefslogtreecommitdiff
path: root/chromium/components/offline_items_collection/core/offline_content_aggregator_unittest.cc
blob: 361934084453b308d79fab96a625c10af07bb0e1 (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright (c) 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/offline_items_collection/core/offline_content_aggregator.h"

#include <map>

#include "base/memory/ptr_util.h"
#include "base/test/test_mock_time_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/offline_items_collection/core/offline_item.h"
#include "components/offline_items_collection/core/test_support/mock_offline_content_provider.h"
#include "components/offline_items_collection/core/test_support/scoped_mock_offline_content_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::ContainerEq;
using testing::Return;

namespace offline_items_collection {
namespace {

struct CompareOfflineItemsById {
  bool operator()(const OfflineItem& a, const OfflineItem& b) const {
    return a.id < b.id;
  }
};

// A custom comparator that makes sure two vectors contain the same elements.
// TODO(dtrainor): Look into building a better matcher that works with gmock.
template <typename T>
bool VectorContentsEq(const std::vector<T>& list1,
                      const std::vector<T>& list2) {
  if (list1.size() != list2.size())
    return false;

  std::map<T, int, CompareOfflineItemsById> occurance_counts;
  for (auto it = list1.begin(); it != list1.end(); it++)
    occurance_counts[*it]++;

  for (auto it = list2.begin(); it != list2.end(); it++)
    occurance_counts[*it]--;

  for (auto it = occurance_counts.begin(); it != occurance_counts.end(); it++) {
    if (it->second != 0)
      return false;
  }

  return true;
}

// Helper class that automatically unregisters itself from the aggregator in the
// case that someone calls OpenItem on it.
class OpenItemRemovalOfflineContentProvider
    : public ScopedMockOfflineContentProvider {
 public:
  OpenItemRemovalOfflineContentProvider(const std::string& name_space,
                                        OfflineContentAggregator* aggregator)
      : ScopedMockOfflineContentProvider(name_space, aggregator) {}
  ~OpenItemRemovalOfflineContentProvider() override {}

  void OpenItem(const ContentId& id) override {
    ScopedMockOfflineContentProvider::OpenItem(id);
    Unregister();
  }
};

class OfflineContentAggregatorTest : public testing::Test {
 public:
  OfflineContentAggregatorTest()
      : task_runner_(new base::TestMockTimeTaskRunner), handle_(task_runner_) {}
  ~OfflineContentAggregatorTest() override {}

 protected:
  scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
  base::ThreadTaskRunnerHandle handle_;
  OfflineContentAggregator aggregator_;
};

TEST_F(OfflineContentAggregatorTest, ObserversAddedBeforeProvidersAvailable) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  EXPECT_FALSE(aggregator_.AreItemsAvailable());
  EXPECT_TRUE(provider1.HasObserver(&aggregator_));
  EXPECT_TRUE(provider2.HasObserver(&aggregator_));

  ScopedMockOfflineContentProvider::ScopedMockObserver observer1(&aggregator_);
  ScopedMockOfflineContentProvider::ScopedMockObserver observer2(&aggregator_);
  task_runner_->RunUntilIdle();

  {
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(0);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(0);
    provider1.NotifyOnItemsAvailable();
  }

  {
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);
    provider2.NotifyOnItemsAvailable();
  }
}

TEST_F(OfflineContentAggregatorTest, ObserversAddedAfterProvidersAvailable) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  EXPECT_FALSE(aggregator_.AreItemsAvailable());
  EXPECT_TRUE(provider1.HasObserver(&aggregator_));
  EXPECT_TRUE(provider2.HasObserver(&aggregator_));

  provider1.NotifyOnItemsAvailable();
  provider2.NotifyOnItemsAvailable();

  {
    ScopedMockOfflineContentProvider::ScopedMockObserver observer1(
        &aggregator_);
    ScopedMockOfflineContentProvider::ScopedMockObserver observer2(
        &aggregator_);
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);
    task_runner_->RunUntilIdle();
  }
}

TEST_F(OfflineContentAggregatorTest,
       ProvidersAddedAfterObserversNotifiedAvailable) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  EXPECT_FALSE(aggregator_.AreItemsAvailable());
  EXPECT_TRUE(provider1.HasObserver(&aggregator_));

  ScopedMockOfflineContentProvider::ScopedMockObserver observer1(&aggregator_);
  ScopedMockOfflineContentProvider::ScopedMockObserver observer2(&aggregator_);
  task_runner_->RunUntilIdle();

  {
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);
    provider1.NotifyOnItemsAvailable();
  }

  {
    OfflineContentProvider::OfflineItemList items;
    items.push_back(OfflineItem());

    ScopedMockOfflineContentProvider provider2("2", &aggregator_);
    EXPECT_TRUE(provider2.HasObserver(&aggregator_));

    EXPECT_CALL(provider2, GetAllItems()).WillOnce(Return(items));
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(0);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(0);
    EXPECT_CALL(observer1, OnItemsAdded(items)).Times(1);
    EXPECT_CALL(observer2, OnItemsAdded(items)).Times(1);
    provider2.NotifyOnItemsAvailable();
  }
}

TEST_F(OfflineContentAggregatorTest, QueryingItemsWithProviderThatIsntReady) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  EXPECT_FALSE(aggregator_.AreItemsAvailable());

  OfflineContentProvider::OfflineItemList items1;
  items1.push_back(OfflineItem(ContentId("1", "A")));
  items1.push_back(OfflineItem(ContentId("1", "B")));

  OfflineContentProvider::OfflineItemList items2;
  items2.push_back(OfflineItem(ContentId("2", "C")));
  items2.push_back(OfflineItem(ContentId("2", "D")));

  EXPECT_CALL(provider1, GetAllItems()).WillRepeatedly(Return(items1));
  EXPECT_CALL(provider2, GetAllItems()).WillRepeatedly(Return(items2));

  provider1.NotifyOnItemsAvailable();
  EXPECT_TRUE(VectorContentsEq(items1, aggregator_.GetAllItems()));

  OfflineContentProvider::OfflineItemList combined_items(items1);
  combined_items.insert(combined_items.end(), items2.begin(), items2.end());
  provider2.NotifyOnItemsAvailable();

  EXPECT_TRUE(VectorContentsEq(combined_items, aggregator_.GetAllItems()));
}

TEST_F(OfflineContentAggregatorTest, QueryingItemFromRemovedProvider) {
  ContentId id("1", "A");
  OfflineItem item(id);

  {
    ScopedMockOfflineContentProvider provider("1", &aggregator_);
    provider.NotifyOnItemsAvailable();
    EXPECT_TRUE(aggregator_.AreItemsAvailable());

    EXPECT_CALL(provider, GetItemById(id)).WillRepeatedly(Return(&item));
    EXPECT_EQ(&item, aggregator_.GetItemById(id));
  }

  EXPECT_EQ(nullptr, aggregator_.GetItemById(id));
}

TEST_F(OfflineContentAggregatorTest, QueryingItemWithProviderThatIsntReady) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  EXPECT_FALSE(aggregator_.AreItemsAvailable());

  ContentId id1("1", "A");
  ContentId id2("2", "B");
  ContentId id3("3", "C");

  OfflineItem item1(id1);
  OfflineItem item2(id2);

  EXPECT_CALL(provider1, GetItemById(id1)).WillRepeatedly(Return(&item1));
  EXPECT_CALL(provider2, GetItemById(id2)).WillRepeatedly(Return(&item2));

  EXPECT_EQ(nullptr, aggregator_.GetItemById(id1));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id2));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id3));

  provider1.NotifyOnItemsAvailable();
  EXPECT_EQ(&item1, aggregator_.GetItemById(id1));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id2));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id3));

  provider2.NotifyOnItemsAvailable();
  EXPECT_EQ(&item1, aggregator_.GetItemById(id1));
  EXPECT_EQ(&item2, aggregator_.GetItemById(id2));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id3));
}

TEST_F(OfflineContentAggregatorTest, GetItemByIdPropagatesToRightProvider) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  provider1.NotifyOnItemsAvailable();
  provider2.NotifyOnItemsAvailable();

  ContentId id1("1", "A");
  ContentId id2("2", "B");
  ContentId id3("1", "C");
  ContentId id4("3", "D");
  OfflineItem item1(id1);
  OfflineItem item2(id2);

  EXPECT_CALL(provider1, GetItemById(id1)).WillRepeatedly(Return(&item1));
  EXPECT_CALL(provider2, GetItemById(id2)).WillRepeatedly(Return(&item2));
  EXPECT_CALL(provider1, GetItemById(id3)).WillRepeatedly(Return(nullptr));

  EXPECT_EQ(&item1, aggregator_.GetItemById(id1));
  EXPECT_EQ(&item2, aggregator_.GetItemById(id2));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id3));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id4));
}

TEST_F(OfflineContentAggregatorTest, AreItemsAvailable) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);

  ScopedMockOfflineContentProvider::ScopedMockObserver observer1(&aggregator_);
  ScopedMockOfflineContentProvider::ScopedMockObserver observer2(&aggregator_);
  task_runner_->RunUntilIdle();

  EXPECT_FALSE(aggregator_.AreItemsAvailable());

  {
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(0);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(0);

    provider1.NotifyOnItemsAvailable();
  }

  EXPECT_FALSE(aggregator_.AreItemsAvailable());

  {
    EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
    EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);

    provider2.NotifyOnItemsAvailable();
  }

  EXPECT_TRUE(aggregator_.AreItemsAvailable());
}

TEST_F(OfflineContentAggregatorTest, ActionPropagatesToRightProvider) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  provider1.NotifyOnItemsAvailable();
  provider2.NotifyOnItemsAvailable();

  testing::InSequence sequence;
  ContentId id1("1", "A");
  ContentId id2("2", "B");
  EXPECT_CALL(provider1, OpenItem(id1)).Times(1);
  EXPECT_CALL(provider2, OpenItem(id2)).Times(1);
  EXPECT_CALL(provider1, RemoveItem(id1)).Times(1);
  EXPECT_CALL(provider2, RemoveItem(id2)).Times(1);
  EXPECT_CALL(provider1, CancelDownload(id1)).Times(1);
  EXPECT_CALL(provider2, CancelDownload(id2)).Times(1);
  EXPECT_CALL(provider1, ResumeDownload(id1)).Times(1);
  EXPECT_CALL(provider2, ResumeDownload(id2)).Times(1);
  EXPECT_CALL(provider1, PauseDownload(id1)).Times(1);
  EXPECT_CALL(provider2, PauseDownload(id2)).Times(1);
  aggregator_.OpenItem(id1);
  aggregator_.OpenItem(id2);
  aggregator_.RemoveItem(id1);
  aggregator_.RemoveItem(id2);
  aggregator_.CancelDownload(id1);
  aggregator_.CancelDownload(id2);
  aggregator_.ResumeDownload(id1);
  aggregator_.ResumeDownload(id2);
  aggregator_.PauseDownload(id1);
  aggregator_.PauseDownload(id2);
}

TEST_F(OfflineContentAggregatorTest, ActionPropagatesAfterInitialize) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);

  ContentId id1("1", "A");
  ContentId id2("2", "B");
  ContentId id3("2", "C");

  {
    EXPECT_CALL(provider1, PauseDownload(id1)).Times(0);
    aggregator_.PauseDownload(id1);
  }

  {
    testing::InSequence sequence;
    EXPECT_CALL(provider1, PauseDownload(id1)).Times(1);
    EXPECT_CALL(provider1, ResumeDownload(id1)).Times(1);
    EXPECT_CALL(provider1, OpenItem(id1)).Times(1);
    EXPECT_CALL(provider2, OpenItem(id2)).Times(0);

    aggregator_.ResumeDownload(id1);
    aggregator_.OpenItem(id1);
    provider1.NotifyOnItemsAvailable();
    aggregator_.OpenItem(id2);
  }

  {
    testing::InSequence sequence;
    EXPECT_CALL(provider2, OpenItem(id2)).Times(1);
    EXPECT_CALL(provider2, RemoveItem(id3)).Times(1);
    aggregator_.RemoveItem(id3);
    provider2.NotifyOnItemsAvailable();
  }
}

TEST_F(OfflineContentAggregatorTest, OnItemsAddedPropagatedToObservers) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  provider1.NotifyOnItemsAvailable();
  provider2.NotifyOnItemsAvailable();

  ScopedMockOfflineContentProvider::ScopedMockObserver observer1(&aggregator_);
  ScopedMockOfflineContentProvider::ScopedMockObserver observer2(&aggregator_);

  EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
  EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);
  task_runner_->RunUntilIdle();

  OfflineContentProvider::OfflineItemList items1;
  items1.push_back(OfflineItem(ContentId("1", "A")));
  items1.push_back(OfflineItem(ContentId("1", "B")));

  OfflineContentProvider::OfflineItemList items2;
  items2.push_back(OfflineItem(ContentId("2", "C")));
  items2.push_back(OfflineItem(ContentId("2", "D")));

  EXPECT_CALL(observer1, OnItemsAdded(items1)).Times(1);
  EXPECT_CALL(observer1, OnItemsAdded(items2)).Times(1);
  EXPECT_CALL(observer2, OnItemsAdded(items1)).Times(1);
  EXPECT_CALL(observer2, OnItemsAdded(items2)).Times(1);
  provider1.NotifyOnItemsAdded(items1);
  provider2.NotifyOnItemsAdded(items2);
}

TEST_F(OfflineContentAggregatorTest, OnItemRemovedPropagatedToObservers) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  provider1.NotifyOnItemsAvailable();
  provider2.NotifyOnItemsAvailable();

  ScopedMockOfflineContentProvider::ScopedMockObserver observer1(&aggregator_);
  ScopedMockOfflineContentProvider::ScopedMockObserver observer2(&aggregator_);

  EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
  EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);
  task_runner_->RunUntilIdle();

  ContentId id1("1", "A");
  ContentId id2("2", "B");

  EXPECT_CALL(observer1, OnItemRemoved(id1)).Times(1);
  EXPECT_CALL(observer1, OnItemRemoved(id2)).Times(1);
  EXPECT_CALL(observer2, OnItemRemoved(id1)).Times(1);
  EXPECT_CALL(observer2, OnItemRemoved(id2)).Times(1);
  provider1.NotifyOnItemRemoved(id1);
  provider2.NotifyOnItemRemoved(id2);
}

TEST_F(OfflineContentAggregatorTest, OnItemUpdatedPropagatedToObservers) {
  ScopedMockOfflineContentProvider provider1("1", &aggregator_);
  ScopedMockOfflineContentProvider provider2("2", &aggregator_);
  provider1.NotifyOnItemsAvailable();
  provider2.NotifyOnItemsAvailable();

  ScopedMockOfflineContentProvider::ScopedMockObserver observer1(&aggregator_);
  ScopedMockOfflineContentProvider::ScopedMockObserver observer2(&aggregator_);

  EXPECT_CALL(observer1, OnItemsAvailable(&aggregator_)).Times(1);
  EXPECT_CALL(observer2, OnItemsAvailable(&aggregator_)).Times(1);
  task_runner_->RunUntilIdle();

  OfflineItem item1(ContentId("1", "A"));
  OfflineItem item2(ContentId("2", "B"));

  EXPECT_CALL(observer1, OnItemUpdated(item1)).Times(1);
  EXPECT_CALL(observer1, OnItemUpdated(item2)).Times(1);
  EXPECT_CALL(observer2, OnItemUpdated(item1)).Times(1);
  EXPECT_CALL(observer2, OnItemUpdated(item2)).Times(1);
  provider1.NotifyOnItemUpdated(item1);
  provider2.NotifyOnItemUpdated(item2);
}

TEST_F(OfflineContentAggregatorTest, ProviderRemovedDuringCallbackFlush) {
  OpenItemRemovalOfflineContentProvider provider1("1", &aggregator_);

  ContentId id1("1", "A");
  ContentId id2("1", "B");
  aggregator_.OpenItem(id1);
  aggregator_.OpenItem(id2);
  aggregator_.RemoveItem(id2);

  EXPECT_CALL(provider1, OpenItem(id1)).Times(1);
  EXPECT_CALL(provider1, RemoveItem(id2)).Times(0);
  provider1.NotifyOnItemsAvailable();
}

TEST_F(OfflineContentAggregatorTest, SameProviderWithMultipleNamespaces) {
  MockOfflineContentProvider provider;
  ScopedMockOfflineContentProvider::ScopedMockObserver observer(&aggregator_);

  ContentId id1("1", "A");
  ContentId id2("2", "B");
  OfflineItem item1(id1);
  OfflineItem item2(id2);
  OfflineContentProvider::OfflineItemList items;
  items.push_back(item1);
  items.push_back(item2);

  aggregator_.RegisterProvider("1", &provider);
  aggregator_.RegisterProvider("2", &provider);
  EXPECT_TRUE(provider.HasObserver(&aggregator_));

  EXPECT_CALL(provider, GetAllItems()).WillRepeatedly(Return(items));
  EXPECT_CALL(provider, GetItemById(id1)).WillRepeatedly(Return(&item1));
  EXPECT_CALL(provider, GetItemById(id2)).WillRepeatedly(Return(&item2));

  EXPECT_CALL(observer, OnItemsAvailable(&aggregator_)).Times(1);
  provider.NotifyOnItemsAvailable();

  EXPECT_TRUE(VectorContentsEq(items, aggregator_.GetAllItems()));
  EXPECT_EQ(&item1, aggregator_.GetItemById(id1));
  EXPECT_EQ(&item2, aggregator_.GetItemById(id2));

  aggregator_.UnregisterProvider("1");
  EXPECT_TRUE(provider.HasObserver(&aggregator_));
  EXPECT_EQ(nullptr, aggregator_.GetItemById(id1));
  EXPECT_EQ(&item2, aggregator_.GetItemById(id2));

  aggregator_.UnregisterProvider("2");
  EXPECT_FALSE(provider.HasObserver(&aggregator_));
}

}  // namespace
}  // namespace offline_items_collection