OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 <set> | |
6 #include <vector> | |
7 | |
8 #include "base/rand_util.h" | |
9 #include "base/stl_util.h" | |
10 #include "chrome/browser/download/download_history.h" | |
11 #include "chrome/browser/history/download_database.h" | |
12 #include "chrome/browser/history/download_row.h" | |
13 #include "chrome/browser/history/history.h" | |
14 #include "content/public/test/mock_download_item.h" | |
15 #include "content/public/test/mock_download_manager.h" | |
16 #include "content/public/test/test_browser_thread.h" | |
17 #include "content/public/test/test_utils.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using testing::DoAll; | |
21 using testing::Invoke; | |
22 using testing::Return; | |
23 using testing::ReturnRef; | |
24 using testing::SetArgPointee; | |
25 using testing::WithArg; | |
26 using testing::_; | |
27 | |
28 namespace { | |
29 | |
30 void CheckInfoEqual(const history::DownloadRow& left, | |
31 const history::DownloadRow& right) { | |
32 EXPECT_EQ(left.path.value(), right.path.value()); | |
33 EXPECT_EQ(left.url.spec(), right.url.spec()); | |
34 EXPECT_EQ(left.referrer_url.spec(), right.referrer_url.spec()); | |
35 EXPECT_EQ(left.start_time.ToTimeT(), right.start_time.ToTimeT()); | |
36 EXPECT_EQ(left.end_time.ToTimeT(), right.end_time.ToTimeT()); | |
37 EXPECT_EQ(left.received_bytes, right.received_bytes); | |
38 EXPECT_EQ(left.total_bytes, right.total_bytes); | |
39 EXPECT_EQ(left.state, right.state); | |
40 EXPECT_EQ(left.db_handle, right.db_handle); | |
41 EXPECT_EQ(left.opened, right.opened); | |
42 } | |
43 | |
44 typedef std::set<int64> HandleSet; | |
45 typedef std::vector<history::DownloadRow> InfoVector; | |
46 typedef testing::NiceMock<content::MockDownloadItem> NiceMockDownloadItem; | |
47 | |
48 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter { | |
49 public: | |
50 FakeHistoryAdapter() | |
51 : DownloadHistory::HistoryAdapter(NULL), | |
52 slow_create_download_(false), | |
53 fail_create_download_(false), | |
54 handle_counter_(0) { | |
55 } | |
56 | |
57 virtual ~FakeHistoryAdapter() {} | |
58 | |
59 virtual void QueryDownloads( | |
60 const HistoryService::DownloadQueryCallback& callback) OVERRIDE { | |
61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
62 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, | |
63 base::Bind(&FakeHistoryAdapter::QueryDownloadsDone, | |
64 base::Unretained(this), callback)); | |
65 } | |
66 | |
67 void QueryDownloadsDone( | |
68 const HistoryService::DownloadQueryCallback& callback) { | |
69 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
70 CHECK(expect_query_downloads_.get()); | |
71 callback.Run(expect_query_downloads_.get()); | |
72 expect_query_downloads_.reset(); | |
73 } | |
74 | |
75 void set_slow_create_download(bool slow) { slow_create_download_ = slow; } | |
76 | |
77 virtual void CreateDownload( | |
78 const history::DownloadRow& info, | |
79 const HistoryService::DownloadCreateCallback& callback) OVERRIDE { | |
80 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
81 create_download_info_ = info; | |
82 // Must not call CreateDownload() again before FinishCreateDownload()! | |
83 DCHECK(create_download_callback_.is_null()); | |
84 create_download_callback_ = base::Bind( | |
85 callback, | |
86 (fail_create_download_ ? | |
87 history::DownloadDatabase::kUninitializedHandle : | |
88 handle_counter_++)); | |
89 fail_create_download_ = false; | |
90 if (!slow_create_download_) | |
91 FinishCreateDownload(); | |
92 } | |
93 | |
94 void FinishCreateDownload() { | |
95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
96 create_download_callback_.Run(); | |
97 create_download_callback_.Reset(); | |
98 } | |
99 | |
100 virtual void UpdateDownload( | |
101 const history::DownloadRow& info) OVERRIDE { | |
102 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
103 update_download_ = info; | |
104 } | |
105 | |
106 virtual void RemoveDownloads(const HandleSet& handles) OVERRIDE { | |
107 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
108 for (HandleSet::const_iterator it = handles.begin(); | |
109 it != handles.end(); ++it) { | |
110 remove_downloads_.insert(*it); | |
111 } | |
112 } | |
113 | |
114 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | |
115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
116 expect_query_downloads_ = infos.Pass(); | |
117 } | |
118 | |
119 void ExpectQueryDownloadsDone() { | |
120 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
121 EXPECT_TRUE(NULL == expect_query_downloads_.get()); | |
122 } | |
123 | |
124 void FailCreateDownload() { | |
125 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
126 fail_create_download_ = true; | |
127 } | |
128 | |
129 void ExpectDownloadCreated( | |
130 const history::DownloadRow& info) { | |
131 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
132 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
133 CheckInfoEqual(info, create_download_info_); | |
134 create_download_info_ = history::DownloadRow(); | |
135 } | |
136 | |
137 void ExpectNoDownloadCreated() { | |
138 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
139 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
140 CheckInfoEqual(history::DownloadRow(), create_download_info_); | |
141 } | |
142 | |
143 void ExpectDownloadUpdated(const history::DownloadRow& info) { | |
144 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
145 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
146 CheckInfoEqual(update_download_, info); | |
147 update_download_ = history::DownloadRow(); | |
148 } | |
149 | |
150 void ExpectNoDownloadUpdated() { | |
151 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
152 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
153 CheckInfoEqual(history::DownloadRow(), update_download_); | |
154 } | |
155 | |
156 void ExpectNoDownloadsRemoved() { | |
157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
158 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
159 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size())); | |
160 } | |
161 | |
162 void ExpectDownloadsRemoved(const HandleSet& handles) { | |
163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
164 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
165 HandleSet differences; | |
166 std::insert_iterator<HandleSet> differences_iter( | |
167 differences, differences.begin()); | |
168 std::set_difference(handles.begin(), handles.end(), | |
169 remove_downloads_.begin(), | |
170 remove_downloads_.end(), | |
171 differences_iter); | |
172 for (HandleSet::const_iterator different = differences.begin(); | |
173 different != differences.end(); ++different) { | |
174 EXPECT_TRUE(false) << *different; | |
175 } | |
176 remove_downloads_.clear(); | |
177 } | |
178 | |
179 private: | |
180 bool slow_create_download_; | |
181 bool fail_create_download_; | |
182 base::Closure create_download_callback_; | |
183 int handle_counter_; | |
184 history::DownloadRow update_download_; | |
185 scoped_ptr<InfoVector> expect_query_downloads_; | |
186 HandleSet remove_downloads_; | |
187 history::DownloadRow create_download_info_; | |
188 | |
189 DISALLOW_COPY_AND_ASSIGN(FakeHistoryAdapter); | |
190 }; | |
191 | |
192 class DownloadHistoryTest : public testing::Test { | |
193 public: | |
194 DownloadHistoryTest() | |
195 : ui_thread_(content::BrowserThread::UI, &loop_), | |
196 manager_(new content::MockDownloadManager()), | |
197 history_(NULL), | |
198 download_history_(NULL), | |
199 manager_observer_(NULL), | |
200 item_observer_(NULL), | |
201 download_created_index_(0) { | |
202 } | |
203 virtual ~DownloadHistoryTest() { | |
204 STLDeleteElements(&items_); | |
205 } | |
206 | |
207 protected: | |
208 virtual void TearDown() OVERRIDE { | |
209 download_history_.reset(); | |
210 } | |
211 | |
212 content::MockDownloadManager& manager() { return *manager_.get(); } | |
213 content::MockDownloadItem& item(size_t index) { return *items_[index]; } | |
214 | |
215 void SetManagerObserver( | |
216 content::DownloadManager::Observer* manager_observer) { | |
217 manager_observer_ = manager_observer; | |
218 } | |
219 content::DownloadManager::Observer* manager_observer() { | |
220 return manager_observer_; | |
221 } | |
222 void SetItemObserver( | |
223 content::DownloadItem::Observer* item_observer) { | |
224 item_observer_ = item_observer; | |
225 } | |
226 content::DownloadItem::Observer* item_observer() { | |
227 return item_observer_; | |
228 } | |
229 | |
230 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | |
231 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
232 CHECK(infos.get()); | |
233 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | |
234 this, &DownloadHistoryTest::SetManagerObserver))); | |
235 EXPECT_CALL(manager(), RemoveObserver(_)); | |
236 download_created_index_ = 0; | |
237 for (size_t index = 0; index < infos->size(); ++index) { | |
238 EXPECT_CALL(manager(), CreateDownloadItem( | |
239 infos->at(index).path, | |
240 infos->at(index).url, | |
241 infos->at(index).referrer_url, | |
242 infos->at(index).start_time, | |
243 infos->at(index).end_time, | |
244 infos->at(index).received_bytes, | |
245 infos->at(index).total_bytes, | |
246 infos->at(index).state, | |
247 infos->at(index).opened)) | |
248 .WillOnce(DoAll( | |
249 InvokeWithoutArgs( | |
250 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), | |
251 Return(&item(index)))); | |
252 } | |
253 EXPECT_CALL(manager(), CheckForHistoryFilesRemoval()); | |
254 history_ = new FakeHistoryAdapter(); | |
255 history_->ExpectWillQueryDownloads(infos.Pass()); | |
256 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); | |
257 download_history_.reset(new DownloadHistory( | |
258 &manager(), scoped_ptr<DownloadHistory::HistoryAdapter>(history_))); | |
259 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | |
260 history_->ExpectQueryDownloadsDone(); | |
261 } | |
262 | |
263 void CallOnDownloadCreated(size_t index) { | |
264 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
265 manager_observer()->OnDownloadCreated(&manager(), &item(index)); | |
266 } | |
267 | |
268 void CallOnDownloadCreatedInOrder() { | |
269 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
270 // Gmock doesn't appear to support something like InvokeWithTheseArgs. Maybe | |
271 // gmock needs to learn about base::Callback. | |
272 CallOnDownloadCreated(download_created_index_++); | |
273 } | |
274 | |
275 void set_slow_create_download(bool slow) { | |
276 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
277 history_->set_slow_create_download(slow); | |
278 } | |
279 | |
280 void FinishCreateDownload() { | |
281 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
282 history_->FinishCreateDownload(); | |
283 } | |
284 | |
285 void FailCreateDownload() { | |
286 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
287 history_->FailCreateDownload(); | |
288 } | |
289 | |
290 void ExpectDownloadCreated( | |
291 const history::DownloadRow& info) { | |
292 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
293 history_->ExpectDownloadCreated(info); | |
294 } | |
295 | |
296 void ExpectNoDownloadCreated() { | |
297 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
298 history_->ExpectNoDownloadCreated(); | |
299 } | |
300 | |
301 void ExpectDownloadUpdated(const history::DownloadRow& info) { | |
302 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
303 history_->ExpectDownloadUpdated(info); | |
304 } | |
305 | |
306 void ExpectNoDownloadUpdated() { | |
307 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
308 history_->ExpectNoDownloadUpdated(); | |
309 } | |
310 | |
311 void ExpectNoDownloadsRemoved() { | |
312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
313 history_->ExpectNoDownloadsRemoved(); | |
314 } | |
315 | |
316 void ExpectDownloadsRemoved(const HandleSet& handles) { | |
317 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
318 history_->ExpectDownloadsRemoved(handles); | |
319 } | |
320 | |
321 void InitItem( | |
322 int32 id, | |
323 const FilePath& path, | |
324 const GURL& url, | |
325 const GURL& referrer, | |
326 const base::Time& start_time, | |
327 const base::Time& end_time, | |
328 int64 received_bytes, | |
329 int64 total_bytes, | |
330 content::DownloadItem::DownloadState state, | |
331 int64 db_handle, | |
332 bool opened, | |
333 history::DownloadRow* info) { | |
334 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
335 int32 index = items_.size(); | |
336 NiceMockDownloadItem* mock_item = new NiceMockDownloadItem(); | |
337 items_.push_back(mock_item); | |
338 | |
339 info->path = path; | |
340 info->url = url; | |
341 info->referrer_url = referrer; | |
342 info->start_time = start_time; | |
343 info->end_time = end_time; | |
344 info->received_bytes = received_bytes; | |
345 info->total_bytes = total_bytes; | |
346 info->state = state; | |
347 info->db_handle = db_handle; | |
348 info->opened = opened; | |
349 | |
350 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id)); | |
351 EXPECT_CALL(item(index), GetFullPath()).WillRepeatedly(ReturnRef(path)); | |
Randy Smith (Not in Mondays)
2012/12/04 21:48:32
Apologies for not catching this on the first time
Randy Smith (Not in Mondays)
2012/12/05 13:00:30
Whoops, I see; the argument to the function is a r
| |
352 EXPECT_CALL(item(index), GetURL()).WillRepeatedly(ReturnRef(url)); | |
353 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return( | |
354 "application/octet-stream")); | |
355 EXPECT_CALL(item(index), GetReferrerUrl()).WillRepeatedly(ReturnRef( | |
356 referrer)); | |
357 EXPECT_CALL(item(index), GetStartTime()).WillRepeatedly(Return(start_time)); | |
358 EXPECT_CALL(item(index), GetEndTime()).WillRepeatedly(Return(end_time)); | |
359 EXPECT_CALL(item(index), GetReceivedBytes()) | |
360 .WillRepeatedly(Return(received_bytes)); | |
361 EXPECT_CALL(item(index), GetTotalBytes()).WillRepeatedly(Return( | |
362 total_bytes)); | |
363 EXPECT_CALL(item(index), GetState()).WillRepeatedly(Return(state)); | |
364 EXPECT_CALL(item(index), GetOpened()).WillRepeatedly(Return(opened)); | |
365 EXPECT_CALL(item(index), GetTargetDisposition()).WillRepeatedly(Return( | |
366 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | |
367 EXPECT_CALL(manager(), GetDownload(id)) | |
368 .WillRepeatedly(Return(&item(index))); | |
369 EXPECT_CALL(item(index), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | |
370 this, &DownloadHistoryTest::SetItemObserver))); | |
371 EXPECT_CALL(item(index), RemoveObserver(_)); | |
372 | |
373 std::vector<content::DownloadItem*> items; | |
374 for (size_t i = 0; i < items_.size(); ++i) { | |
375 items.push_back(&item(i)); | |
376 } | |
377 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)) | |
378 .WillRepeatedly(SetArgPointee<0>(items)); | |
379 } | |
380 | |
381 private: | |
382 MessageLoopForUI loop_; | |
383 content::TestBrowserThread ui_thread_; | |
384 std::vector<NiceMockDownloadItem*> items_; | |
385 scoped_refptr<content::MockDownloadManager> manager_; | |
386 FakeHistoryAdapter* history_; | |
387 scoped_ptr<DownloadHistory> download_history_; | |
388 content::DownloadManager::Observer* manager_observer_; | |
389 content::DownloadItem::Observer* item_observer_; | |
390 size_t download_created_index_; | |
391 | |
392 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); | |
393 }; | |
394 | |
395 } // anonymous namespace | |
396 | |
397 // Test loading an item from the database, changing it, saving it back, removing | |
398 // it. | |
399 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { | |
400 // Load a download from history, create the item, OnDownloadCreated, | |
401 // OnDownloadUpdated, OnDownloadRemoved. | |
402 history::DownloadRow info; | |
403 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
404 GURL url("http://example.com/bar.pdf"); | |
405 GURL referrer("http://example.com/referrer.html"); | |
406 InitItem(base::RandInt(0, 1 << 20), | |
407 path, | |
408 url, | |
409 referrer, | |
410 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
411 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
412 100, | |
413 100, | |
414 content::DownloadItem::COMPLETE, | |
415 base::RandInt(0, 1 << 20), | |
416 false, | |
417 &info); | |
418 { | |
419 scoped_ptr<InfoVector> infos(new InfoVector()); | |
420 infos->push_back(info); | |
421 ExpectWillQueryDownloads(infos.Pass()); | |
422 ExpectNoDownloadCreated(); | |
423 } | |
424 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
425 | |
426 // Pretend that something changed on the item. | |
427 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | |
428 item_observer()->OnDownloadUpdated(&item(0)); | |
429 info.opened = true; | |
430 ExpectDownloadUpdated(info); | |
431 | |
432 // Pretend that the user removed the item. | |
433 HandleSet handles; | |
434 handles.insert(info.db_handle); | |
435 item_observer()->OnDownloadRemoved(&item(0)); | |
436 ExpectDownloadsRemoved(handles); | |
437 } | |
438 | |
439 // Test creating an item, saving it to the database, changing it, saving it | |
440 // back, removing it. | |
441 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { | |
442 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | |
443 // OnDownloadRemoved. | |
444 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | |
445 | |
446 // Note that db_handle must be -1 at first because it isn't in the db yet. | |
447 history::DownloadRow info; | |
448 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
449 GURL url("http://example.com/bar.pdf"); | |
450 GURL referrer("http://example.com/referrer.html"); | |
451 InitItem(base::RandInt(0, 1 << 20), | |
452 path, | |
453 url, | |
454 referrer, | |
455 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
456 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
457 100, | |
458 100, | |
459 content::DownloadItem::COMPLETE, | |
460 -1, | |
461 false, | |
462 &info); | |
463 | |
464 // Pretend the manager just created |item|. | |
465 CallOnDownloadCreated(0); | |
466 // CreateDownload() always gets db_handle=-1. | |
467 ExpectDownloadCreated(info); | |
468 info.db_handle = 0; | |
469 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
470 | |
471 // Pretend that something changed on the item. | |
472 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | |
473 item_observer()->OnDownloadUpdated(&item(0)); | |
474 info.opened = true; | |
475 ExpectDownloadUpdated(info); | |
476 | |
477 // Pretend that the user removed the item. | |
478 HandleSet handles; | |
479 handles.insert(info.db_handle); | |
480 item_observer()->OnDownloadRemoved(&item(0)); | |
481 ExpectDownloadsRemoved(handles); | |
482 } | |
483 | |
484 // Test creating a new item, saving it, removing it by setting it Temporary, | |
485 // changing it without saving it back because it's Temporary, clearing | |
486 // IsTemporary, saving it back, changing it, saving it back because it isn't | |
487 // Temporary anymore. | |
488 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { | |
489 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | |
490 // OnDownloadRemoved. | |
491 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | |
492 | |
493 // Note that db_handle must be -1 at first because it isn't in the db yet. | |
494 history::DownloadRow info; | |
495 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
496 GURL url("http://example.com/bar.pdf"); | |
497 GURL referrer("http://example.com/referrer.html"); | |
498 InitItem(base::RandInt(0, 1 << 20), | |
499 path, | |
500 url, | |
501 referrer, | |
502 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
503 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
504 100, | |
505 100, | |
506 content::DownloadItem::COMPLETE, | |
507 -1, | |
508 false, | |
509 &info); | |
510 | |
511 // Pretend the manager just created |item|. | |
512 CallOnDownloadCreated(0); | |
513 // CreateDownload() always gets db_handle=-1. | |
514 ExpectDownloadCreated(info); | |
515 info.db_handle = 0; | |
516 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
517 | |
518 // Pretend the item was marked temporary. DownloadHistory should remove it | |
519 // from history and start ignoring it. | |
520 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(true)); | |
521 item_observer()->OnDownloadUpdated(&item(0)); | |
522 HandleSet handles; | |
523 handles.insert(info.db_handle); | |
524 ExpectDownloadsRemoved(handles); | |
525 | |
526 // Change something that would make DownloadHistory call UpdateDownload if the | |
527 // item weren't temporary. | |
528 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(4200)); | |
529 item_observer()->OnDownloadUpdated(&item(0)); | |
530 ExpectNoDownloadUpdated(); | |
531 | |
532 // Changing a temporary item back to a non-temporary item should make | |
533 // DownloadHistory call CreateDownload. | |
534 EXPECT_CALL(item(0), IsTemporary()).WillRepeatedly(Return(false)); | |
535 item_observer()->OnDownloadUpdated(&item(0)); | |
536 info.received_bytes = 4200; | |
537 info.db_handle = -1; | |
538 // CreateDownload() always gets db_handle=-1. | |
539 ExpectDownloadCreated(info); | |
540 info.db_handle = 1; | |
541 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
542 | |
543 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | |
544 item_observer()->OnDownloadUpdated(&item(0)); | |
545 info.received_bytes = 100; | |
546 ExpectDownloadUpdated(info); | |
547 } | |
548 | |
549 // Test removing downloads while they're still being added. | |
550 TEST_F(DownloadHistoryTest, | |
551 DownloadHistoryTest_RemoveWhileAdding) { | |
552 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | |
553 | |
554 // Note that db_handle must be -1 at first because it isn't in the db yet. | |
555 history::DownloadRow info; | |
556 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
557 GURL url("http://example.com/bar.pdf"); | |
558 GURL referrer("http://example.com/referrer.html"); | |
559 InitItem(base::RandInt(0, 1 << 20), | |
560 path, | |
561 url, | |
562 referrer, | |
563 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
564 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
565 100, | |
566 100, | |
567 content::DownloadItem::COMPLETE, | |
568 -1, | |
569 false, | |
570 &info); | |
571 | |
572 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | |
573 // but to wait for FinishCreateDownload(). | |
574 set_slow_create_download(true); | |
575 | |
576 // Pretend the manager just created |item|. | |
577 CallOnDownloadCreated(0); | |
578 // CreateDownload() always gets db_handle=-1. | |
579 ExpectDownloadCreated(info); | |
580 info.db_handle = 0; | |
581 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | |
582 | |
583 // Call OnDownloadRemoved before calling back to DownloadHistory::ItemAdded(). | |
584 // Instead of calling RemoveDownloads() immediately, DownloadHistory should | |
585 // add the item's id to removed_while_adding_. Then, ItemAdded should | |
586 // immediately remove the item's record from history. | |
587 item_observer()->OnDownloadRemoved(&item(0)); | |
588 EXPECT_CALL(manager(), GetDownload(item(0).GetId())) | |
589 .WillRepeatedly(Return(static_cast<content::DownloadItem*>(NULL))); | |
590 ExpectNoDownloadsRemoved(); | |
591 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | |
592 | |
593 // Now callback to DownloadHistory::ItemAdded(), and expect a call to | |
594 // RemoveDownloads() for the item that was removed while it was being added. | |
595 FinishCreateDownload(); | |
596 HandleSet handles; | |
597 handles.insert(info.db_handle); | |
598 ExpectDownloadsRemoved(handles); | |
599 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | |
600 } | |
601 | |
602 // Test loading multiple items from the database and removing them all. | |
603 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Multiple) { | |
604 // Load a download from history, create the item, OnDownloadCreated, | |
605 // OnDownloadUpdated, OnDownloadRemoved. | |
606 history::DownloadRow info0, info1; | |
607 FilePath path0(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
608 GURL url0("http://example.com/bar.pdf"); | |
609 GURL referrer0("http://example.com/referrer.html"); | |
610 InitItem(base::RandInt(0, 1 << 10), | |
611 path0, | |
612 url0, | |
613 referrer0, | |
614 (base::Time::Now() - base::TimeDelta::FromMinutes(11)), | |
615 (base::Time::Now() - base::TimeDelta::FromMinutes(2)), | |
616 100, | |
617 100, | |
618 content::DownloadItem::COMPLETE, | |
619 base::RandInt(0, 1 << 10), | |
620 false, | |
621 &info0); | |
622 FilePath path1(FILE_PATH_LITERAL("/foo/qux.pdf")); | |
623 GURL url1("http://example.com/qux.pdf"); | |
624 GURL referrer1("http://example.com/referrer.html"); | |
625 InitItem(item(0).GetId() + base::RandInt(1, 1 << 10), | |
626 path1, | |
627 url1, | |
628 referrer1, | |
629 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
630 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
631 200, | |
632 200, | |
633 content::DownloadItem::COMPLETE, | |
634 info0.db_handle + base::RandInt(1, 1 << 10), | |
635 false, | |
636 &info1); | |
637 { | |
638 scoped_ptr<InfoVector> infos(new InfoVector()); | |
639 infos->push_back(info0); | |
640 infos->push_back(info1); | |
641 ExpectWillQueryDownloads(infos.Pass()); | |
642 ExpectNoDownloadCreated(); | |
643 } | |
644 | |
645 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
646 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(1))); | |
647 | |
648 // Pretend that the user removed both items. | |
649 HandleSet handles; | |
650 handles.insert(info0.db_handle); | |
651 handles.insert(info1.db_handle); | |
652 item_observer()->OnDownloadRemoved(&item(0)); | |
653 item_observer()->OnDownloadRemoved(&item(1)); | |
654 ExpectDownloadsRemoved(handles); | |
655 } | |
656 | |
657 // Test what happens when HistoryService/CreateDownload::CreateDownload() fails. | |
658 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { | |
659 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | |
660 // OnDownloadRemoved. | |
661 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | |
662 | |
663 // Note that db_handle must be -1 at first because it isn't in the db yet. | |
664 history::DownloadRow info; | |
665 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
666 GURL url("http://example.com/bar.pdf"); | |
667 GURL referrer("http://example.com/referrer.html"); | |
668 InitItem(base::RandInt(0, 1 << 20), | |
669 path, | |
670 url, | |
671 referrer, | |
672 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
673 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
674 100, | |
675 100, | |
676 content::DownloadItem::COMPLETE, | |
677 -1, | |
678 false, | |
679 &info); | |
680 | |
681 FailCreateDownload(); | |
682 // Pretend the manager just created |item|. | |
683 CallOnDownloadCreated(0); | |
684 // CreateDownload() always gets db_handle=-1. | |
685 ExpectDownloadCreated(info); | |
686 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | |
687 | |
688 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | |
689 item_observer()->OnDownloadUpdated(&item(0)); | |
690 info.received_bytes = 100; | |
691 ExpectDownloadCreated(info); | |
692 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
693 } | |
694 | |
695 TEST_F(DownloadHistoryTest, | |
696 DownloadHistoryTest_UpdateWhileAdding) { | |
697 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | |
698 // OnDownloadRemoved. | |
699 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | |
700 | |
701 // Note that db_handle must be -1 at first because it isn't in the db yet. | |
702 history::DownloadRow info; | |
703 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | |
704 GURL url("http://example.com/bar.pdf"); | |
705 GURL referrer("http://example.com/referrer.html"); | |
706 InitItem(base::RandInt(0, 1 << 20), | |
707 path, | |
708 url, | |
709 referrer, | |
710 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | |
711 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | |
712 100, | |
713 100, | |
714 content::DownloadItem::COMPLETE, | |
715 -1, | |
716 false, | |
717 &info); | |
718 | |
719 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | |
720 // but to wait for FinishCreateDownload(). | |
721 set_slow_create_download(true); | |
722 | |
723 // Pretend the manager just created |item|. | |
724 CallOnDownloadCreated(0); | |
725 // CreateDownload() always gets db_handle=-1. | |
726 ExpectDownloadCreated(info); | |
727 info.db_handle = 0; | |
728 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | |
729 | |
730 // Pretend that something changed on the item. | |
731 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | |
732 item_observer()->OnDownloadUpdated(&item(0)); | |
733 | |
734 FinishCreateDownload(); | |
735 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | |
736 | |
737 // ItemAdded should call OnDownloadUpdated, which should detect that the item | |
738 // changed while it was being added and call UpdateDownload immediately. | |
739 info.opened = true; | |
740 ExpectDownloadUpdated(info); | |
741 } | |
OLD | NEW |