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
|
// 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 "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.h"
#include "content/browser/download/download_status_updater.h"
#include "content/browser/download/interrupt_reasons.h"
#include "content/browser/download/mock_download_manager.h"
#include "content/browser/download/mock_download_manager_delegate.h"
#include "content/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
DownloadId::Domain kValidDownloadItemIdDomain = "valid DownloadId::Domain";
class DownloadItemTest : public testing::Test {
public:
class MockObserver : public DownloadItem::Observer {
public:
explicit MockObserver(DownloadItem* item) : item_(item), updated_(false) {
item_->AddObserver(this);
}
~MockObserver() { item_->RemoveObserver(this); }
virtual void OnDownloadUpdated(DownloadItem* download) { updated_ = true; }
virtual void OnDownloadOpened(DownloadItem* download) { }
bool CheckUpdated() {
bool was_updated = updated_;
updated_ = false;
return was_updated;
}
private:
DownloadItem* item_;
bool updated_;
};
DownloadItemTest()
: id_factory_(new DownloadIdFactory(kValidDownloadItemIdDomain)),
profile_(new TestingProfile()),
ui_thread_(BrowserThread::UI, &loop_) {
}
~DownloadItemTest() {
}
virtual void SetUp() {
download_manager_delegate_.reset(new MockDownloadManagerDelegate());
download_manager_ = new MockDownloadManager(
download_manager_delegate_.get(),
id_factory_,
&download_status_updater_);
download_manager_->Init(profile_.get());
}
virtual void TearDown() {
download_manager_->Shutdown();
// When a DownloadManager's reference count drops to 0, it is not
// deleted immediately. Instead, a task is posted to the UI thread's
// message loop to delete it.
// So, drop the reference count to 0 and run the message loop once
// to ensure that all resources are cleaned up before the test exits.
download_manager_ = NULL;
profile_.reset(NULL);
ui_thread_.DeprecatedGetThreadObject()->message_loop()->RunAllPending();
}
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;
download_manager_->CreateDownloadItem(info_.get(), DownloadRequestHandle());
return download_manager_->GetActiveDownloadItem(info_->download_id.local());
}
protected:
DownloadStatusUpdater download_status_updater_;
scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_;
scoped_refptr<DownloadManager> download_manager_;
private:
scoped_refptr<DownloadIdFactory> id_factory_;
scoped_ptr<TestingProfile> profile_;
MessageLoopForUI loop_;
// UI thread.
content::TestBrowserThread ui_thread_;
};
namespace {
const int kDownloadChunkSize = 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->Update(kDownloadChunkSize);
ASSERT_TRUE(observer.CheckUpdated());
}
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);
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->state_info();;
state.is_dangerous_file = false;
state.is_dangerous_url = false;
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->state_info();;
state.is_dangerous_file = false;
state.is_dangerous_url = true;
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->state_info();;
state.is_dangerous_file = true;
state.is_dangerous_url = false;
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());
}
|