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
|
// Copyright (c) 2011 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 "base/message_loop.h"
#include "base/stl_util.h"
#include "base/threading/thread.h"
#include "chrome/test/base/testing_profile.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_id.h"
#include "content/browser/download/download_id_factory.h"
#include "content/browser/download/download_item_impl.h"
#include "content/browser/download/download_request_handle.h"
#include "content/browser/download/download_status_updater.h"
#include "content/browser/download/interrupt_reasons.h"
#include "content/browser/download/mock_download_item.h"
#include "content/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using content::DownloadItem;
using content::DownloadManager;
DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain";
namespace {
class MockDelegate : public DownloadItemImpl::Delegate {
public:
MOCK_METHOD1(ShouldOpenFileBasedOnExtension, bool(const FilePath& path));
MOCK_METHOD1(ShouldOpenDownload, bool(DownloadItem* download));
MOCK_METHOD1(CheckForFileRemoval, void(DownloadItem* download));
MOCK_METHOD1(MaybeCompleteDownload, void(DownloadItem* download));
MOCK_CONST_METHOD0(GetBrowserContext, content::BrowserContext*());
MOCK_METHOD1(DownloadCancelled, void(DownloadItem* download));
MOCK_METHOD1(DownloadCompleted, void(DownloadItem* download));
MOCK_METHOD1(DownloadOpened, void(DownloadItem* download));
MOCK_METHOD1(DownloadRemoved, void(DownloadItem* download));
MOCK_CONST_METHOD1(AssertStateConsistent, void(DownloadItem* download));
};
class MockRequestHandle : public DownloadRequestHandleInterface {
public:
MOCK_CONST_METHOD0(GetTabContents, TabContents*());
MOCK_CONST_METHOD0(GetDownloadManager, DownloadManager*());
MOCK_CONST_METHOD0(PauseRequest, void());
MOCK_CONST_METHOD0(ResumeRequest, void());
MOCK_CONST_METHOD0(CancelRequest, void());
MOCK_CONST_METHOD0(DebugString, std::string());
};
}
class DownloadItemTest : public testing::Test {
public:
class MockObserver : public content::DownloadItem::Observer {
public:
explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) {
item_->AddObserver(this);
}
~MockObserver() { item_->RemoveObserver(this); }
virtual void OnDownloadUpdated(content::DownloadItem* download) {
updated_ = true;
}
virtual void OnDownloadOpened(content::DownloadItem* download) { }
bool CheckUpdated() {
bool was_updated = updated_;
updated_ = false;
return was_updated;
}
private:
DownloadItem* item_;
bool updated_;
};
DownloadItemTest()
: id_factory_(new DownloadIdFactory(kValidDownloadItemIdDomain)),
ui_thread_(BrowserThread::UI, &loop_) {
}
~DownloadItemTest() {
}
virtual void SetUp() {
}
virtual void TearDown() {
ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending();
STLDeleteElements(&allocated_downloads_);
allocated_downloads_.clear();
}
// This class keeps ownership of the created download item; it will
// be torn down at the end of the test unless DestroyDownloadItem is
// called.
DownloadItem* CreateDownloadItem(DownloadItem::DownloadState state) {
// Normally, the download system takes ownership of info, and is
// responsible for deleting it. In these unit tests, however, we
// don't call the function that deletes it, so we do so ourselves.
scoped_ptr<DownloadCreateInfo> info_;
info_.reset(new DownloadCreateInfo());
info_->download_id = id_factory_->GetNextId();
info_->prompt_user_for_save_location = false;
info_->url_chain.push_back(GURL());
info_->state = state;
MockRequestHandle* request_handle =
new testing::NiceMock<MockRequestHandle>;
DownloadItem* download =
new DownloadItemImpl(&delegate_, *(info_.get()),
request_handle, false);
allocated_downloads_.insert(download);
return download;
}
// Destroy a previously created download item.
void DestroyDownloadItem(DownloadItem* item) {
allocated_downloads_.erase(item);
delete item;
}
protected:
DownloadStatusUpdater download_status_updater_;
private:
scoped_refptr<DownloadIdFactory> id_factory_;
MessageLoopForUI loop_;
// UI thread.
content::TestBrowserThread ui_thread_;
testing::NiceMock<MockDelegate> delegate_;
std::set<DownloadItem*> allocated_downloads_;
};
namespace {
const int kDownloadChunkSize = 1000;
const int kDownloadSpeed = 1000;
const int kDummyDBHandle = 10;
const FilePath::CharType kDummyPath[] = FILE_PATH_LITERAL("/testpath");
} // namespace
// Tests to ensure calls that change a DownloadItem generate an update to
// observers.
// State changing functions not tested:
// void OpenDownload();
// void ShowDownloadInShell();
// void CompleteDelayedDownload();
// void OnDownloadCompleting(DownloadFileManager* file_manager);
// void OnDownloadRenamedToFinalName(const FilePath& full_path);
// set_* mutators
TEST_F(DownloadItemTest, NotificationAfterUpdate) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->UpdateProgress(kDownloadChunkSize, kDownloadSpeed, "");
ASSERT_TRUE(observer.CheckUpdated());
EXPECT_EQ(kDownloadSpeed, item->CurrentSpeed());
}
TEST_F(DownloadItemTest, NotificationAfterCancel) {
DownloadItem* user_cancel = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer1(user_cancel);
user_cancel->Cancel(true);
ASSERT_TRUE(observer1.CheckUpdated());
DownloadItem* system_cancel = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer2(system_cancel);
system_cancel->Cancel(false);
ASSERT_TRUE(observer2.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterComplete) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
// Calling OnAllDataSaved does not trigger notification
item->OnAllDataSaved(kDownloadChunkSize, DownloadItem::kEmptyFileHash);
ASSERT_FALSE(observer.CheckUpdated());
item->MarkAsComplete();
ASSERT_TRUE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterDownloadedFileRemoved) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->OnDownloadedFileRemoved();
ASSERT_TRUE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterInterrupted) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->Interrupted(kDownloadChunkSize, "", DOWNLOAD_INTERRUPT_REASON_NONE);
ASSERT_TRUE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterDelete) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->Delete(DownloadItem::DELETE_DUE_TO_BROWSER_SHUTDOWN);
ASSERT_TRUE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterRemove) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->Remove();
ASSERT_TRUE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterSetFileCheckResults) {
// Setting to safe should not trigger any notifications
DownloadItem* safe_item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver safe_observer(safe_item);
DownloadStateInfo state = safe_item->GetStateInfo();;
state.danger = DownloadStateInfo::NOT_DANGEROUS;
safe_item->SetFileCheckResults(state);
ASSERT_FALSE(safe_observer.CheckUpdated());
// Setting to unsafe url or unsafe file should trigger notification
DownloadItem* unsafeurl_item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver unsafeurl_observer(unsafeurl_item);
state = unsafeurl_item->GetStateInfo();;
state.danger = DownloadStateInfo::DANGEROUS_URL;
unsafeurl_item->SetFileCheckResults(state);
ASSERT_TRUE(unsafeurl_observer.CheckUpdated());
unsafeurl_item->DangerousDownloadValidated();
ASSERT_TRUE(unsafeurl_observer.CheckUpdated());
DownloadItem* unsafefile_item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver unsafefile_observer(unsafefile_item);
state = unsafefile_item->GetStateInfo();;
state.danger = DownloadStateInfo::DANGEROUS_FILE;
unsafefile_item->SetFileCheckResults(state);
ASSERT_TRUE(unsafefile_observer.CheckUpdated());
unsafefile_item->DangerousDownloadValidated();
ASSERT_TRUE(unsafefile_observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterOnPathDetermined) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
// Calling OnPathDetermined does not trigger notification
item->OnPathDetermined(FilePath(kDummyPath));
ASSERT_FALSE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterRename) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
// Calling Rename does not trigger notification
item->Rename(FilePath(kDummyPath));
ASSERT_FALSE(observer.CheckUpdated());
}
TEST_F(DownloadItemTest, NotificationAfterTogglePause) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
MockObserver observer(item);
item->TogglePause();
ASSERT_TRUE(observer.CheckUpdated());
item->TogglePause();
ASSERT_TRUE(observer.CheckUpdated());
}
static char external_data_test_string[] = "External data test";
static int destructor_called = 0;
class TestExternalData : public content::DownloadItem::ExternalData {
public:
int value;
virtual ~TestExternalData() {
destructor_called++;
}
};
TEST_F(DownloadItemTest, ExternalData) {
DownloadItem* item = CreateDownloadItem(DownloadItem::IN_PROGRESS);
// Shouldn't be anything there before set.
EXPECT_EQ(NULL, item->GetExternalData(&external_data_test_string));
TestExternalData* test1(new TestExternalData());
test1->value = 2;
// Should be able to get back what you set.
item->SetExternalData(&external_data_test_string, test1);
TestExternalData* test_result =
static_cast<TestExternalData*>(
item->GetExternalData(&external_data_test_string));
EXPECT_EQ(test1, test_result);
// Destructor should be called if value overwritten. New value
// should then be retrievable.
TestExternalData* test2(new TestExternalData());
test2->value = 3;
EXPECT_EQ(0, destructor_called);
item->SetExternalData(&external_data_test_string, test2);
EXPECT_EQ(1, destructor_called);
EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2),
item->GetExternalData(&external_data_test_string));
// Overwriting with the same value shouldn't do anything.
EXPECT_EQ(1, destructor_called);
item->SetExternalData(&external_data_test_string, test2);
EXPECT_EQ(1, destructor_called);
EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test2),
item->GetExternalData(&external_data_test_string));
// Overwriting with NULL should result in destruction.
item->SetExternalData(&external_data_test_string, NULL);
EXPECT_EQ(2, destructor_called);
// Destroying the download item should destroy the external data.
TestExternalData* test3(new TestExternalData());
item->SetExternalData(&external_data_test_string, test3);
EXPECT_EQ(static_cast<DownloadItem::ExternalData*>(test3),
item->GetExternalData(&external_data_test_string));
DestroyDownloadItem(item);
EXPECT_EQ(3, destructor_called);
}
TEST(MockDownloadItem, Compiles) {
MockDownloadItem mock_item;
}
|