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