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

Side by Side Diff: components/download/internal/model_impl_unittest.cc

Issue 2870753003: Add a Model to the download component. (Closed)
Patch Set: Renamed test base class Created 3 years, 7 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
« no previous file with comments | « components/download/internal/model_impl.cc ('k') | components/download/internal/noop_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 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/download/internal/model_impl.h"
6
7 #include <algorithm>
8 #include <memory>
9
10 #include "base/bind.h"
11 #include "base/guid.h"
12 #include "base/macros.h"
13 #include "base/memory/ptr_util.h"
14 #include "components/download/internal/entry.h"
15 #include "components/download/internal/test/entry_utils.h"
16 #include "components/download/internal/test/mock_model_client.h"
17 #include "components/download/internal/test/test_store.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 using ::testing::InSequence;
22 using ::testing::Return;
23 using ::testing::_;
24
25 namespace download {
26
27 namespace {
28
29 class DownloadServiceModelImplTest : public testing::Test {
30 public:
31 DownloadServiceModelImplTest() : store_(nullptr) {}
32
33 ~DownloadServiceModelImplTest() override = default;
34
35 void SetUp() override {
36 auto store = base::MakeUnique<test::TestStore>();
37 store_ = store.get();
38 model_ = base::MakeUnique<ModelImpl>(&client_, std::move(store));
39 }
40
41 protected:
42 test::MockModelClient client_;
43 test::TestStore* store_;
44 std::unique_ptr<ModelImpl> model_;
45
46 private:
47 DISALLOW_COPY_AND_ASSIGN(DownloadServiceModelImplTest);
48 };
49
50 } // namespace
51
52 TEST_F(DownloadServiceModelImplTest, SuccessfulLifecycle) {
53 InSequence sequence;
54 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
55 EXPECT_CALL(client_, OnDestroyed(true)).Times(1);
56
57 model_->Initialize();
58 EXPECT_TRUE(store_->init_called());
59 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
60
61 model_->Destroy();
62 EXPECT_TRUE(store_->destroy_called());
63 store_->TriggerDestroy(true);
64 }
65
66 TEST_F(DownloadServiceModelImplTest, SuccessfulInitWithEntries) {
67 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
68 Entry entry2 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
69 std::vector<Entry> entries = {entry1, entry2};
70
71 InSequence sequence;
72 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
73
74 model_->Initialize();
75 EXPECT_TRUE(store_->init_called());
76 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
77
78 EXPECT_TRUE(test::SuperficialEntryCompare(&entry1, model_->Get(entry1.guid)));
79 EXPECT_TRUE(test::SuperficialEntryCompare(&entry2, model_->Get(entry2.guid)));
80 }
81
82 TEST_F(DownloadServiceModelImplTest, BadInit) {
83 EXPECT_CALL(client_, OnInitialized(false)).Times(1);
84
85 model_->Initialize();
86 EXPECT_TRUE(store_->init_called());
87 store_->TriggerInit(false, base::MakeUnique<std::vector<Entry>>());
88 }
89
90 TEST_F(DownloadServiceModelImplTest, BadDestroy) {
91 InSequence sequence;
92 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
93 EXPECT_CALL(client_, OnDestroyed(false)).Times(1);
94
95 model_->Initialize();
96 EXPECT_TRUE(store_->init_called());
97 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
98
99 model_->Destroy();
100 EXPECT_TRUE(store_->destroy_called());
101 store_->TriggerDestroy(false);
102 }
103
104 TEST_F(DownloadServiceModelImplTest, Add) {
105 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
106 Entry entry2 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
107
108 InSequence sequence;
109 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
110 EXPECT_CALL(client_, OnItemAdded(true, entry1.client, entry1.guid)).Times(1);
111 EXPECT_CALL(client_, OnItemAdded(false, entry2.client, entry2.guid)).Times(1);
112
113 model_->Initialize();
114 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
115
116 model_->Add(entry1);
117 EXPECT_TRUE(test::SuperficialEntryCompare(&entry1, model_->Get(entry1.guid)));
118 EXPECT_TRUE(
119 test::SuperficialEntryCompare(&entry1, store_->LastUpdatedEntry()));
120 store_->TriggerUpdate(true);
121
122 model_->Add(entry2);
123 EXPECT_TRUE(test::SuperficialEntryCompare(&entry2, model_->Get(entry2.guid)));
124 EXPECT_TRUE(
125 test::SuperficialEntryCompare(&entry2, store_->LastUpdatedEntry()));
126
127 store_->TriggerUpdate(false);
128 EXPECT_EQ(nullptr, model_->Get(entry2.guid));
129 }
130
131 TEST_F(DownloadServiceModelImplTest, Update) {
132 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
133
134 Entry entry2(entry1);
135 entry2.state = Entry::State::AVAILABLE;
136
137 Entry entry3(entry1);
138 entry3.state = Entry::State::ACTIVE;
139
140 std::vector<Entry> entries = {entry1};
141
142 InSequence sequence;
143 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
144 EXPECT_CALL(client_, OnItemUpdated(true, entry1.client, entry1.guid))
145 .Times(1);
146 EXPECT_CALL(client_, OnItemUpdated(false, entry1.client, entry1.guid))
147 .Times(1);
148
149 model_->Initialize();
150 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
151
152 model_->Update(entry2);
153 EXPECT_TRUE(test::SuperficialEntryCompare(&entry2, model_->Get(entry2.guid)));
154 EXPECT_TRUE(
155 test::SuperficialEntryCompare(&entry2, store_->LastUpdatedEntry()));
156 store_->TriggerUpdate(true);
157
158 model_->Update(entry3);
159 EXPECT_TRUE(test::SuperficialEntryCompare(&entry3, model_->Get(entry3.guid)));
160 EXPECT_TRUE(
161 test::SuperficialEntryCompare(&entry3, store_->LastUpdatedEntry()));
162
163 store_->TriggerUpdate(false);
164 EXPECT_TRUE(test::SuperficialEntryCompare(&entry3, model_->Get(entry3.guid)));
165 }
166
167 TEST_F(DownloadServiceModelImplTest, Remove) {
168 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
169 Entry entry2 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
170 std::vector<Entry> entries = {entry1, entry2};
171
172 InSequence sequence;
173 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
174 EXPECT_CALL(client_, OnItemRemoved(true, entry1.client, entry1.guid))
175 .Times(1);
176 EXPECT_CALL(client_, OnItemRemoved(false, entry2.client, entry2.guid))
177 .Times(1);
178
179 model_->Initialize();
180 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
181
182 model_->Remove(entry1.guid);
183 EXPECT_EQ(entry1.guid, store_->LastRemovedEntry());
184 EXPECT_EQ(nullptr, model_->Get(entry1.guid));
185 store_->TriggerRemove(true);
186
187 model_->Remove(entry2.guid);
188 EXPECT_EQ(entry2.guid, store_->LastRemovedEntry());
189 EXPECT_EQ(nullptr, model_->Get(entry2.guid));
190 store_->TriggerRemove(false);
191 }
192
193 TEST_F(DownloadServiceModelImplTest, Get) {
194 Entry entry = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
195
196 std::vector<Entry> entries = {entry};
197
198 InSequence sequence;
199 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
200
201 model_->Initialize();
202 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
203
204 EXPECT_TRUE(test::SuperficialEntryCompare(&entry, model_->Get(entry.guid)));
205 EXPECT_EQ(nullptr, model_->Get(base::GenerateGUID()));
206 }
207
208 TEST_F(DownloadServiceModelImplTest, PeekEntries) {
209 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
210 Entry entry2 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
211 std::vector<Entry> entries = {entry1, entry2};
212
213 InSequence sequence;
214 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
215
216 model_->Initialize();
217 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
218
219 std::vector<Entry*> expected_peek = {&entry1, &entry2};
220
221 EXPECT_TRUE(
222 test::SuperficialEntryListCompare(expected_peek, model_->PeekEntries()));
223 }
224
225 TEST_F(DownloadServiceModelImplTest, TestRemoveAfterAdd) {
226 Entry entry = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
227
228 InSequence sequence;
229 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
230 EXPECT_CALL(client_, OnItemAdded(_, _, _)).Times(0);
231 EXPECT_CALL(client_, OnItemRemoved(true, entry.client, entry.guid)).Times(1);
232
233 model_->Initialize();
234 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>());
235
236 model_->Add(entry);
237 EXPECT_TRUE(test::SuperficialEntryCompare(&entry, model_->Get(entry.guid)));
238
239 model_->Remove(entry.guid);
240 EXPECT_EQ(nullptr, model_->Get(entry.guid));
241
242 store_->TriggerUpdate(true);
243 store_->TriggerRemove(true);
244 }
245
246 TEST_F(DownloadServiceModelImplTest, TestRemoveAfterUpdate) {
247 Entry entry1 = test::BuildEntry(DownloadClient::TEST, base::GenerateGUID());
248
249 Entry entry2(entry1);
250 entry2.state = Entry::State::AVAILABLE;
251
252 std::vector<Entry> entries = {entry1};
253
254 InSequence sequence;
255 EXPECT_CALL(client_, OnInitialized(true)).Times(1);
256 EXPECT_CALL(client_, OnItemUpdated(_, _, _)).Times(0);
257 EXPECT_CALL(client_, OnItemRemoved(true, entry1.client, entry1.guid))
258 .Times(1);
259
260 model_->Initialize();
261 store_->TriggerInit(true, base::MakeUnique<std::vector<Entry>>(entries));
262 EXPECT_TRUE(test::SuperficialEntryCompare(&entry1, model_->Get(entry1.guid)));
263
264 model_->Update(entry2);
265 EXPECT_TRUE(test::SuperficialEntryCompare(&entry2, model_->Get(entry2.guid)));
266
267 model_->Remove(entry2.guid);
268 EXPECT_EQ(nullptr, model_->Get(entry2.guid));
269
270 store_->TriggerUpdate(true);
271 store_->TriggerRemove(true);
272 }
273
274 } // namespace download
OLDNEW
« no previous file with comments | « components/download/internal/model_impl.cc ('k') | components/download/internal/noop_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698