File: all_download_event_notifier_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (152 lines) | stat: -rw-r--r-- 6,084 bytes parent folder | download | duplicates (11)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/public/common/all_download_event_notifier.h"

#include "base/functional/callback_helpers.h"
#include "components/download/public/common/mock_download_item.h"
#include "components/download/public/common/mock_simple_download_manager.h"
#include "components/download/public/common/simple_download_manager_coordinator.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::_;
using testing::NiceMock;

namespace download {
namespace {

class MockNotifierObserver : public AllDownloadEventNotifier::Observer {
 public:
  MockNotifierObserver() = default;

  MockNotifierObserver(const MockNotifierObserver&) = delete;
  MockNotifierObserver& operator=(const MockNotifierObserver&) = delete;

  ~MockNotifierObserver() override = default;

  MOCK_METHOD2(OnDownloadsInitialized,
               void(SimpleDownloadManagerCoordinator* manager,
                    bool active_downloads_only));
  MOCK_METHOD2(OnDownloadCreated,
               void(SimpleDownloadManagerCoordinator* manager,
                    DownloadItem* item));
  MOCK_METHOD2(OnDownloadUpdated,
               void(SimpleDownloadManagerCoordinator* manager,
                    DownloadItem* item));
  MOCK_METHOD2(OnDownloadOpened,
               void(SimpleDownloadManagerCoordinator* manager,
                    DownloadItem* item));
  MOCK_METHOD2(OnDownloadRemoved,
               void(SimpleDownloadManagerCoordinator* manager,
                    DownloadItem* item));
};

class AllDownloadEventNotifierTest : public testing::Test {
 public:
  AllDownloadEventNotifierTest() : coordinator_(base::NullCallback()) {}

  AllDownloadEventNotifierTest(const AllDownloadEventNotifierTest&) = delete;
  AllDownloadEventNotifierTest& operator=(const AllDownloadEventNotifierTest&) =
      delete;

  ~AllDownloadEventNotifierTest() override = default;

  SimpleDownloadManagerCoordinator* coordinator() { return &coordinator_; }

  MockDownloadItem& item() { return item_; }

  MockNotifierObserver& observer() { return observer_; }

 private:
  NiceMock<MockDownloadItem> item_;
  NiceMock<MockNotifierObserver> observer_;
  SimpleDownloadManagerCoordinator coordinator_;
};

}  // namespace

// Tests that OnDownloadCreated() will be propagated correctly.
TEST_F(AllDownloadEventNotifierTest, OnDownloadCreated) {
  NiceMock<MockSimpleDownloadManager> manager;
  coordinator()->SetSimpleDownloadManager(&manager, true);
  coordinator()->GetNotifier()->AddObserver(&observer());
  EXPECT_CALL(observer(), OnDownloadCreated(coordinator(), &item()));
  manager.NotifyOnNewDownloadCreated(&item());
}

// Tests that OnDownloadsInitialized() will be propated correctly.
TEST_F(AllDownloadEventNotifierTest, OnDownloadsInitialized) {
  NiceMock<MockSimpleDownloadManager> manager;
  coordinator()->SetSimpleDownloadManager(&manager, false);
  coordinator()->GetNotifier()->AddObserver(&observer());
  EXPECT_CALL(observer(), OnDownloadsInitialized(coordinator(), true));
  manager.NotifyOnDownloadInitialized();

  NiceMock<MockSimpleDownloadManager> manager2;
  coordinator()->SetSimpleDownloadManager(&manager2, true);
  EXPECT_CALL(observer(), OnDownloadsInitialized(coordinator(), false));
  manager2.NotifyOnDownloadInitialized();
}

// Tests that the full manaager is set before the in progress manager
// initializes.
TEST_F(AllDownloadEventNotifierTest,
       SetFullManagerBeforeInProgressManagerInitializes) {
  NiceMock<MockSimpleDownloadManager> manager;
  coordinator()->SetSimpleDownloadManager(&manager, false);
  coordinator()->GetNotifier()->AddObserver(&observer());

  // Sets the full manager before the first one is initialized.
  NiceMock<MockSimpleDownloadManager> manager2;
  coordinator()->SetSimpleDownloadManager(&manager2, true);

  EXPECT_CALL(observer(), OnDownloadsInitialized(coordinator(), _)).Times(0);
  // Initializing the first manager shouldn't impact the coordinator now.
  manager.NotifyOnDownloadInitialized();
  EXPECT_CALL(observer(), OnDownloadsInitialized(coordinator(), false));
  manager2.NotifyOnDownloadInitialized();
}

// Tests that OnDownloadUpdated() will be propagated correctly.
TEST_F(AllDownloadEventNotifierTest, OnDownloadUpdated) {
  NiceMock<MockSimpleDownloadManager> manager;
  coordinator()->SetSimpleDownloadManager(&manager, true);
  coordinator()->GetNotifier()->AddObserver(&observer());
  EXPECT_CALL(observer(), OnDownloadCreated(coordinator(), &item()));
  manager.NotifyOnNewDownloadCreated(&item());

  EXPECT_CALL(observer(), OnDownloadUpdated(coordinator(), &item()));
  item().NotifyObserversDownloadUpdated();
}

// Tests that observer will be notified if it is added after manager
// initialization.
TEST_F(AllDownloadEventNotifierTest, AddObserverAfterManagerInitialization) {
  NiceMock<MockSimpleDownloadManager> manager;
  coordinator()->SetSimpleDownloadManager(&manager, true);
  manager.NotifyOnDownloadInitialized();
  EXPECT_CALL(observer(), OnDownloadsInitialized(coordinator(), false));
  coordinator()->GetNotifier()->AddObserver(&observer());
}

// Tests that observer will only be notified once if multiple manager calls
// OnDownloadCreated() on the same download.
TEST_F(AllDownloadEventNotifierTest, MultipleOnDownloadCreatedOnSameDownload) {
  NiceMock<MockSimpleDownloadManager> manager;
  coordinator()->GetNotifier()->AddObserver(&observer());
  coordinator()->SetSimpleDownloadManager(&manager, false);
  EXPECT_CALL(observer(), OnDownloadCreated(coordinator(), &item())).Times(1);
  manager.NotifyOnNewDownloadCreated(&item());

  // Sets the full manager and call OnDownloadCreated() again.
  NiceMock<MockSimpleDownloadManager> manager2;
  coordinator()->SetSimpleDownloadManager(&manager2, true);
  manager2.NotifyOnNewDownloadCreated(&item());

  EXPECT_CALL(observer(), OnDownloadUpdated(coordinator(), &item()));
  item().NotifyObserversDownloadUpdated();
}

}  // namespace download