Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <set> | 5 #include <set> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "chrome/browser/download/download_history.h" | 10 #include "chrome/browser/download/download_history.h" |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 22 using testing::Return; | 22 using testing::Return; |
| 23 using testing::ReturnRef; | 23 using testing::ReturnRef; |
| 24 using testing::SetArgPointee; | 24 using testing::SetArgPointee; |
| 25 using testing::WithArg; | 25 using testing::WithArg; |
| 26 using testing::_; | 26 using testing::_; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 void CheckInfoEqual(const history::DownloadRow& left, | 30 void CheckInfoEqual(const history::DownloadRow& left, |
| 31 const history::DownloadRow& right) { | 31 const history::DownloadRow& right) { |
| 32 EXPECT_EQ(left.path.value(), right.path.value()); | 32 EXPECT_EQ(left.current_path.value(), right.current_path.value()); |
| 33 EXPECT_EQ(left.url.spec(), right.url.spec()); | 33 EXPECT_EQ(left.target_path.value(), right.target_path.value()); |
| 34 EXPECT_EQ(left.url_chain.size(), right.url_chain.size()); | |
| 35 for (unsigned int i = 0; | |
| 36 i < left.url_chain.size() && i < right.url_chain.size(); | |
| 37 ++i) { | |
| 38 EXPECT_EQ(left.url_chain[i].spec(), right.url_chain[i].spec()); | |
| 39 } | |
| 34 EXPECT_EQ(left.referrer_url.spec(), right.referrer_url.spec()); | 40 EXPECT_EQ(left.referrer_url.spec(), right.referrer_url.spec()); |
| 35 EXPECT_EQ(left.start_time.ToTimeT(), right.start_time.ToTimeT()); | 41 EXPECT_EQ(left.start_time.ToTimeT(), right.start_time.ToTimeT()); |
| 36 EXPECT_EQ(left.end_time.ToTimeT(), right.end_time.ToTimeT()); | 42 EXPECT_EQ(left.end_time.ToTimeT(), right.end_time.ToTimeT()); |
| 37 EXPECT_EQ(left.received_bytes, right.received_bytes); | 43 EXPECT_EQ(left.received_bytes, right.received_bytes); |
| 38 EXPECT_EQ(left.total_bytes, right.total_bytes); | 44 EXPECT_EQ(left.total_bytes, right.total_bytes); |
| 39 EXPECT_EQ(left.state, right.state); | 45 EXPECT_EQ(left.state, right.state); |
| 46 EXPECT_EQ(left.danger_type, right.danger_type); | |
| 40 EXPECT_EQ(left.db_handle, right.db_handle); | 47 EXPECT_EQ(left.db_handle, right.db_handle); |
| 41 EXPECT_EQ(left.opened, right.opened); | 48 EXPECT_EQ(left.opened, right.opened); |
| 42 } | 49 } |
| 43 | 50 |
| 44 typedef std::set<int64> HandleSet; | 51 typedef std::set<int64> HandleSet; |
| 45 typedef std::vector<history::DownloadRow> InfoVector; | 52 typedef std::vector<history::DownloadRow> InfoVector; |
| 46 typedef testing::NiceMock<content::MockDownloadItem> NiceMockDownloadItem; | 53 typedef testing::NiceMock<content::MockDownloadItem> NiceMockDownloadItem; |
| 47 | 54 |
| 48 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter { | 55 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter { |
| 49 public: | 56 public: |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 content::MockDownloadManager& manager() { return *manager_.get(); } | 218 content::MockDownloadManager& manager() { return *manager_.get(); } |
| 212 content::MockDownloadItem& item(size_t index) { return *items_[index]; } | 219 content::MockDownloadItem& item(size_t index) { return *items_[index]; } |
| 213 | 220 |
| 214 void SetManagerObserver( | 221 void SetManagerObserver( |
| 215 content::DownloadManager::Observer* manager_observer) { | 222 content::DownloadManager::Observer* manager_observer) { |
| 216 manager_observer_ = manager_observer; | 223 manager_observer_ = manager_observer; |
| 217 } | 224 } |
| 218 content::DownloadManager::Observer* manager_observer() { | 225 content::DownloadManager::Observer* manager_observer() { |
| 219 return manager_observer_; | 226 return manager_observer_; |
| 220 } | 227 } |
| 228 | |
| 229 // Relies on the same object observing all download items. | |
| 221 void SetItemObserver( | 230 void SetItemObserver( |
| 222 content::DownloadItem::Observer* item_observer) { | 231 content::DownloadItem::Observer* item_observer) { |
| 223 item_observer_ = item_observer; | 232 item_observer_ = item_observer; |
| 224 } | 233 } |
| 225 content::DownloadItem::Observer* item_observer() { | 234 content::DownloadItem::Observer* item_observer() { |
| 226 return item_observer_; | 235 return item_observer_; |
| 227 } | 236 } |
| 228 | 237 |
| 229 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { | 238 void ExpectWillQueryDownloads(scoped_ptr<InfoVector> infos) { |
| 230 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 239 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 231 CHECK(infos.get()); | 240 CHECK(infos.get()); |
| 232 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | 241 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( |
| 233 this, &DownloadHistoryTest::SetManagerObserver))); | 242 this, &DownloadHistoryTest::SetManagerObserver))); |
| 234 EXPECT_CALL(manager(), RemoveObserver(_)); | 243 EXPECT_CALL(manager(), RemoveObserver(_)); |
| 235 download_created_index_ = 0; | 244 download_created_index_ = 0; |
| 236 for (size_t index = 0; index < infos->size(); ++index) { | 245 for (size_t index = 0; index < infos->size(); ++index) { |
| 237 EXPECT_CALL(manager(), CreateDownloadItem( | 246 EXPECT_CALL(manager(), MockCreateDownloadItem1( |
|
benjhayden
2013/01/23 21:02:06
Did you consider my idea of passing a single struc
Randy Smith (Not in Mondays)
2013/01/23 23:20:39
I hadn't because its seemed more complex than I wa
| |
| 238 infos->at(index).path, | 247 infos->at(index).current_path, |
| 239 infos->at(index).url, | 248 infos->at(index).target_path, |
| 249 infos->at(index).url_chain, | |
| 240 infos->at(index).referrer_url, | 250 infos->at(index).referrer_url, |
| 241 infos->at(index).start_time, | 251 infos->at(index).start_time, |
| 242 infos->at(index).end_time, | 252 infos->at(index).end_time, |
| 243 infos->at(index).received_bytes, | 253 infos->at(index).received_bytes, |
| 244 infos->at(index).total_bytes, | 254 infos->at(index).total_bytes, |
| 245 infos->at(index).state, | 255 infos->at(index).state, |
| 256 infos->at(index).danger_type)); | |
| 257 EXPECT_CALL(manager(), MockCreateDownloadItem2( | |
| 258 infos->at(index).target_path, | |
| 259 infos->at(index).url_chain, | |
| 260 infos->at(index).interrupt_reason, | |
| 246 infos->at(index).opened)) | 261 infos->at(index).opened)) |
| 247 .WillOnce(DoAll( | 262 .WillOnce(DoAll( |
| 248 InvokeWithoutArgs( | 263 InvokeWithoutArgs( |
| 249 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), | 264 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), |
| 250 Return(&item(index)))); | 265 Return(&item(index)))); |
| 251 } | 266 } |
| 252 EXPECT_CALL(manager(), CheckForHistoryFilesRemoval()); | 267 EXPECT_CALL(manager(), CheckForHistoryFilesRemoval()); |
| 253 history_ = new FakeHistoryAdapter(); | 268 history_ = new FakeHistoryAdapter(); |
| 254 history_->ExpectWillQueryDownloads(infos.Pass()); | 269 history_->ExpectWillQueryDownloads(infos.Pass()); |
| 255 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); | 270 EXPECT_CALL(*manager_.get(), GetAllDownloads(_)).WillRepeatedly(Return()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 void ExpectNoDownloadsRemoved() { | 325 void ExpectNoDownloadsRemoved() { |
| 311 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 312 history_->ExpectNoDownloadsRemoved(); | 327 history_->ExpectNoDownloadsRemoved(); |
| 313 } | 328 } |
| 314 | 329 |
| 315 void ExpectDownloadsRemoved(const HandleSet& handles) { | 330 void ExpectDownloadsRemoved(const HandleSet& handles) { |
| 316 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 331 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 317 history_->ExpectDownloadsRemoved(handles); | 332 history_->ExpectDownloadsRemoved(handles); |
| 318 } | 333 } |
| 319 | 334 |
| 335 // Caller is responsibile for making sure reference arguments lifetime is | |
| 336 // greater than the lifetime of the NiceMockDownloadItem() created by this | |
| 337 // routine. | |
| 320 void InitItem( | 338 void InitItem( |
| 321 int32 id, | 339 int32 id, |
| 322 const FilePath& path, | 340 const FilePath& current_path, |
| 323 const GURL& url, | 341 const FilePath& target_path, |
| 342 const std::vector<GURL>& url_chain, | |
| 324 const GURL& referrer, | 343 const GURL& referrer, |
| 325 const base::Time& start_time, | 344 const base::Time& start_time, |
| 326 const base::Time& end_time, | 345 const base::Time& end_time, |
| 327 int64 received_bytes, | 346 int64 received_bytes, |
| 328 int64 total_bytes, | 347 int64 total_bytes, |
| 329 content::DownloadItem::DownloadState state, | 348 content::DownloadItem::DownloadState state, |
| 349 content::DownloadDangerType danger_type, | |
| 350 content::DownloadInterruptReason interrupt_reason, | |
| 330 int64 db_handle, | 351 int64 db_handle, |
| 331 bool opened, | 352 bool opened, |
| 332 history::DownloadRow* info) { | 353 history::DownloadRow* info) { |
| 333 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 354 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 334 int32 index = items_.size(); | 355 int32 index = items_.size(); |
| 335 NiceMockDownloadItem* mock_item = new NiceMockDownloadItem(); | 356 NiceMockDownloadItem* mock_item = new NiceMockDownloadItem(); |
| 336 items_.push_back(mock_item); | 357 items_.push_back(mock_item); |
| 337 | 358 |
| 338 info->path = path; | 359 info->current_path = current_path; |
| 339 info->url = url; | 360 info->target_path = target_path; |
| 361 info->url_chain = url_chain; | |
| 340 info->referrer_url = referrer; | 362 info->referrer_url = referrer; |
| 341 info->start_time = start_time; | 363 info->start_time = start_time; |
| 342 info->end_time = end_time; | 364 info->end_time = end_time; |
| 343 info->received_bytes = received_bytes; | 365 info->received_bytes = received_bytes; |
| 344 info->total_bytes = total_bytes; | 366 info->total_bytes = total_bytes; |
| 345 info->state = state; | 367 info->state = state; |
| 368 info->danger_type = danger_type; | |
| 369 info->interrupt_reason = interrupt_reason; | |
| 346 info->db_handle = db_handle; | 370 info->db_handle = db_handle; |
| 347 info->opened = opened; | 371 info->opened = opened; |
| 348 | 372 |
| 349 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id)); | 373 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id)); |
| 350 EXPECT_CALL(item(index), GetFullPath()).WillRepeatedly(ReturnRef(path)); | 374 EXPECT_CALL(item(index), |
| 351 EXPECT_CALL(item(index), GetURL()).WillRepeatedly(ReturnRef(url)); | 375 GetFullPath()).WillRepeatedly(ReturnRef(current_path)); |
| 376 EXPECT_CALL(item(index), | |
| 377 GetTargetFilePath()).WillRepeatedly(ReturnRef(target_path)); | |
| 378 DCHECK_LE(1u, url_chain.size()); | |
| 379 EXPECT_CALL(item(index), GetURL()).WillRepeatedly(ReturnRef(url_chain[0])); | |
| 380 EXPECT_CALL(item(index), | |
| 381 GetUrlChain()).WillRepeatedly(ReturnRef(url_chain)); | |
| 352 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return( | 382 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return( |
| 353 "application/octet-stream")); | 383 "application/octet-stream")); |
| 354 EXPECT_CALL(item(index), GetReferrerUrl()).WillRepeatedly(ReturnRef( | 384 EXPECT_CALL(item(index), GetReferrerUrl()).WillRepeatedly(ReturnRef( |
| 355 referrer)); | 385 referrer)); |
| 356 EXPECT_CALL(item(index), GetStartTime()).WillRepeatedly(Return(start_time)); | 386 EXPECT_CALL(item(index), GetStartTime()).WillRepeatedly(Return(start_time)); |
| 357 EXPECT_CALL(item(index), GetEndTime()).WillRepeatedly(Return(end_time)); | 387 EXPECT_CALL(item(index), GetEndTime()).WillRepeatedly(Return(end_time)); |
| 358 EXPECT_CALL(item(index), GetReceivedBytes()) | 388 EXPECT_CALL(item(index), GetReceivedBytes()) |
| 359 .WillRepeatedly(Return(received_bytes)); | 389 .WillRepeatedly(Return(received_bytes)); |
| 360 EXPECT_CALL(item(index), GetTotalBytes()).WillRepeatedly(Return( | 390 EXPECT_CALL(item(index), GetTotalBytes()).WillRepeatedly(Return( |
| 361 total_bytes)); | 391 total_bytes)); |
| 362 EXPECT_CALL(item(index), GetState()).WillRepeatedly(Return(state)); | 392 EXPECT_CALL(item(index), GetState()).WillRepeatedly(Return(state)); |
| 393 EXPECT_CALL(item(index), GetDangerType()) | |
| 394 .WillRepeatedly(Return(danger_type)); | |
| 395 EXPECT_CALL(item(index), GetLastReason()).WillRepeatedly( | |
| 396 Return(interrupt_reason)); | |
| 363 EXPECT_CALL(item(index), GetOpened()).WillRepeatedly(Return(opened)); | 397 EXPECT_CALL(item(index), GetOpened()).WillRepeatedly(Return(opened)); |
| 364 EXPECT_CALL(item(index), GetTargetDisposition()).WillRepeatedly(Return( | 398 EXPECT_CALL(item(index), GetTargetDisposition()).WillRepeatedly(Return( |
| 365 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); | 399 content::DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 366 EXPECT_CALL(manager(), GetDownload(id)) | 400 EXPECT_CALL(manager(), GetDownload(id)) |
| 367 .WillRepeatedly(Return(&item(index))); | 401 .WillRepeatedly(Return(&item(index))); |
| 368 EXPECT_CALL(item(index), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | 402 EXPECT_CALL(item(index), AddObserver(_)).WillOnce(WithArg<0>(Invoke( |
| 369 this, &DownloadHistoryTest::SetItemObserver))); | 403 this, &DownloadHistoryTest::SetItemObserver))); |
| 370 EXPECT_CALL(item(index), RemoveObserver(_)); | 404 EXPECT_CALL(item(index), RemoveObserver(_)); |
| 371 | 405 |
| 372 std::vector<content::DownloadItem*> items; | 406 std::vector<content::DownloadItem*> items; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 395 | 429 |
| 396 // Test loading an item from the database, changing it, saving it back, removing | 430 // Test loading an item from the database, changing it, saving it back, removing |
| 397 // it. | 431 // it. |
| 398 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { | 432 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Load) { |
| 399 // Load a download from history, create the item, OnDownloadCreated, | 433 // Load a download from history, create the item, OnDownloadCreated, |
| 400 // OnDownloadUpdated, OnDownloadRemoved. | 434 // OnDownloadUpdated, OnDownloadRemoved. |
| 401 history::DownloadRow info; | 435 history::DownloadRow info; |
| 402 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | 436 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 403 GURL url("http://example.com/bar.pdf"); | 437 GURL url("http://example.com/bar.pdf"); |
| 404 GURL referrer("http://example.com/referrer.html"); | 438 GURL referrer("http://example.com/referrer.html"); |
| 439 std::vector<GURL> url_chain; | |
| 440 url_chain.push_back(url); | |
| 405 InitItem(base::RandInt(0, 1 << 20), | 441 InitItem(base::RandInt(0, 1 << 20), |
| 406 path, | 442 path, |
| 407 url, | 443 path, |
| 444 url_chain, | |
| 408 referrer, | 445 referrer, |
| 409 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 446 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 410 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 447 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 411 100, | 448 100, |
| 412 100, | 449 100, |
| 413 content::DownloadItem::COMPLETE, | 450 content::DownloadItem::COMPLETE, |
| 451 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 452 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 414 base::RandInt(0, 1 << 20), | 453 base::RandInt(0, 1 << 20), |
| 415 false, | 454 false, |
| 416 &info); | 455 &info); |
| 417 { | 456 { |
| 418 scoped_ptr<InfoVector> infos(new InfoVector()); | 457 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 419 infos->push_back(info); | 458 infos->push_back(info); |
| 420 ExpectWillQueryDownloads(infos.Pass()); | 459 ExpectWillQueryDownloads(infos.Pass()); |
| 421 ExpectNoDownloadCreated(); | 460 ExpectNoDownloadCreated(); |
| 422 } | 461 } |
| 423 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 462 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 440 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { | 479 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Create) { |
| 441 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 480 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 442 // OnDownloadRemoved. | 481 // OnDownloadRemoved. |
| 443 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 482 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 444 | 483 |
| 445 // Note that db_handle must be -1 at first because it isn't in the db yet. | 484 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 446 history::DownloadRow info; | 485 history::DownloadRow info; |
| 447 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | 486 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 448 GURL url("http://example.com/bar.pdf"); | 487 GURL url("http://example.com/bar.pdf"); |
| 449 GURL referrer("http://example.com/referrer.html"); | 488 GURL referrer("http://example.com/referrer.html"); |
| 489 std::vector<GURL> url_chain; | |
| 490 url_chain.push_back(url); | |
| 450 InitItem(base::RandInt(0, 1 << 20), | 491 InitItem(base::RandInt(0, 1 << 20), |
| 451 path, | 492 path, |
| 452 url, | 493 path, |
| 494 url_chain, | |
| 453 referrer, | 495 referrer, |
| 454 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 496 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 455 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 497 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 456 100, | 498 100, |
| 457 100, | 499 100, |
| 458 content::DownloadItem::COMPLETE, | 500 content::DownloadItem::COMPLETE, |
| 501 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 502 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 459 -1, | 503 -1, |
| 460 false, | 504 false, |
| 461 &info); | 505 &info); |
| 462 | 506 |
| 463 // Pretend the manager just created |item|. | 507 // Pretend the manager just created |item|. |
| 464 CallOnDownloadCreated(0); | 508 CallOnDownloadCreated(0); |
| 465 // CreateDownload() always gets db_handle=-1. | 509 // CreateDownload() always gets db_handle=-1. |
| 466 ExpectDownloadCreated(info); | 510 ExpectDownloadCreated(info); |
| 467 info.db_handle = 0; | 511 info.db_handle = 0; |
| 468 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 512 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 487 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { | 531 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Temporary) { |
| 488 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 532 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 489 // OnDownloadRemoved. | 533 // OnDownloadRemoved. |
| 490 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 534 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 491 | 535 |
| 492 // Note that db_handle must be -1 at first because it isn't in the db yet. | 536 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 493 history::DownloadRow info; | 537 history::DownloadRow info; |
| 494 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | 538 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 495 GURL url("http://example.com/bar.pdf"); | 539 GURL url("http://example.com/bar.pdf"); |
| 496 GURL referrer("http://example.com/referrer.html"); | 540 GURL referrer("http://example.com/referrer.html"); |
| 541 std::vector<GURL> url_chain; | |
| 542 url_chain.push_back(url); | |
| 497 InitItem(base::RandInt(0, 1 << 20), | 543 InitItem(base::RandInt(0, 1 << 20), |
| 498 path, | 544 path, |
| 499 url, | 545 path, |
| 546 url_chain, | |
| 500 referrer, | 547 referrer, |
| 501 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 548 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 502 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 549 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 503 100, | 550 100, |
| 504 100, | 551 100, |
| 505 content::DownloadItem::COMPLETE, | 552 content::DownloadItem::COMPLETE, |
| 553 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 554 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 506 -1, | 555 -1, |
| 507 false, | 556 false, |
| 508 &info); | 557 &info); |
| 509 | 558 |
| 510 // Pretend the manager just created |item|. | 559 // Pretend the manager just created |item|. |
| 511 CallOnDownloadCreated(0); | 560 CallOnDownloadCreated(0); |
| 512 // CreateDownload() always gets db_handle=-1. | 561 // CreateDownload() always gets db_handle=-1. |
| 513 ExpectDownloadCreated(info); | 562 ExpectDownloadCreated(info); |
| 514 info.db_handle = 0; | 563 info.db_handle = 0; |
| 515 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 564 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 548 // Test removing downloads while they're still being added. | 597 // Test removing downloads while they're still being added. |
| 549 TEST_F(DownloadHistoryTest, | 598 TEST_F(DownloadHistoryTest, |
| 550 DownloadHistoryTest_RemoveWhileAdding) { | 599 DownloadHistoryTest_RemoveWhileAdding) { |
| 551 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 600 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 552 | 601 |
| 553 // Note that db_handle must be -1 at first because it isn't in the db yet. | 602 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 554 history::DownloadRow info; | 603 history::DownloadRow info; |
| 555 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | 604 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 556 GURL url("http://example.com/bar.pdf"); | 605 GURL url("http://example.com/bar.pdf"); |
| 557 GURL referrer("http://example.com/referrer.html"); | 606 GURL referrer("http://example.com/referrer.html"); |
| 607 std::vector<GURL> url_chain; | |
| 608 url_chain.push_back(url); | |
| 558 InitItem(base::RandInt(0, 1 << 20), | 609 InitItem(base::RandInt(0, 1 << 20), |
| 559 path, | 610 path, |
| 560 url, | 611 path, |
| 612 url_chain, | |
| 561 referrer, | 613 referrer, |
| 562 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 614 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 563 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 615 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 564 100, | 616 100, |
| 565 100, | 617 100, |
| 566 content::DownloadItem::COMPLETE, | 618 content::DownloadItem::COMPLETE, |
| 619 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 620 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 567 -1, | 621 -1, |
| 568 false, | 622 false, |
| 569 &info); | 623 &info); |
| 570 | 624 |
| 571 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | 625 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
| 572 // but to wait for FinishCreateDownload(). | 626 // but to wait for FinishCreateDownload(). |
| 573 set_slow_create_download(true); | 627 set_slow_create_download(true); |
| 574 | 628 |
| 575 // Pretend the manager just created |item|. | 629 // Pretend the manager just created |item|. |
| 576 CallOnDownloadCreated(0); | 630 CallOnDownloadCreated(0); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 599 } | 653 } |
| 600 | 654 |
| 601 // Test loading multiple items from the database and removing them all. | 655 // Test loading multiple items from the database and removing them all. |
| 602 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Multiple) { | 656 TEST_F(DownloadHistoryTest, DownloadHistoryTest_Multiple) { |
| 603 // Load a download from history, create the item, OnDownloadCreated, | 657 // Load a download from history, create the item, OnDownloadCreated, |
| 604 // OnDownloadUpdated, OnDownloadRemoved. | 658 // OnDownloadUpdated, OnDownloadRemoved. |
| 605 history::DownloadRow info0, info1; | 659 history::DownloadRow info0, info1; |
| 606 FilePath path0(FILE_PATH_LITERAL("/foo/bar.pdf")); | 660 FilePath path0(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 607 GURL url0("http://example.com/bar.pdf"); | 661 GURL url0("http://example.com/bar.pdf"); |
| 608 GURL referrer0("http://example.com/referrer.html"); | 662 GURL referrer0("http://example.com/referrer.html"); |
| 663 std::vector<GURL> url0_chain; | |
| 664 url0_chain.push_back(url0); | |
| 609 InitItem(base::RandInt(0, 1 << 10), | 665 InitItem(base::RandInt(0, 1 << 10), |
| 610 path0, | 666 path0, |
| 611 url0, | 667 path0, |
| 668 url0_chain, | |
| 612 referrer0, | 669 referrer0, |
| 613 (base::Time::Now() - base::TimeDelta::FromMinutes(11)), | 670 (base::Time::Now() - base::TimeDelta::FromMinutes(11)), |
| 614 (base::Time::Now() - base::TimeDelta::FromMinutes(2)), | 671 (base::Time::Now() - base::TimeDelta::FromMinutes(2)), |
| 615 100, | 672 100, |
| 616 100, | 673 100, |
| 617 content::DownloadItem::COMPLETE, | 674 content::DownloadItem::COMPLETE, |
| 675 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 676 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 618 base::RandInt(0, 1 << 10), | 677 base::RandInt(0, 1 << 10), |
| 619 false, | 678 false, |
| 620 &info0); | 679 &info0); |
| 621 FilePath path1(FILE_PATH_LITERAL("/foo/qux.pdf")); | 680 FilePath path1(FILE_PATH_LITERAL("/foo/qux.pdf")); |
| 622 GURL url1("http://example.com/qux.pdf"); | 681 GURL url1("http://example.com/qux.pdf"); |
| 623 GURL referrer1("http://example.com/referrer.html"); | 682 GURL referrer1("http://example.com/referrer.html"); |
| 683 std::vector<GURL> url1_chain; | |
| 684 url1_chain.push_back(url0); | |
| 624 InitItem(item(0).GetId() + base::RandInt(1, 1 << 10), | 685 InitItem(item(0).GetId() + base::RandInt(1, 1 << 10), |
| 625 path1, | 686 path1, |
| 626 url1, | 687 path1, |
| 688 url1_chain, | |
| 627 referrer1, | 689 referrer1, |
| 628 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 690 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 629 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 691 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 630 200, | 692 200, |
| 631 200, | 693 200, |
| 632 content::DownloadItem::COMPLETE, | 694 content::DownloadItem::COMPLETE, |
| 695 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 696 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 633 info0.db_handle + base::RandInt(1, 1 << 10), | 697 info0.db_handle + base::RandInt(1, 1 << 10), |
| 634 false, | 698 false, |
| 635 &info1); | 699 &info1); |
| 636 { | 700 { |
| 637 scoped_ptr<InfoVector> infos(new InfoVector()); | 701 scoped_ptr<InfoVector> infos(new InfoVector()); |
| 638 infos->push_back(info0); | 702 infos->push_back(info0); |
| 639 infos->push_back(info1); | 703 infos->push_back(info1); |
| 640 ExpectWillQueryDownloads(infos.Pass()); | 704 ExpectWillQueryDownloads(infos.Pass()); |
| 641 ExpectNoDownloadCreated(); | 705 ExpectNoDownloadCreated(); |
| 642 } | 706 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 657 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { | 721 TEST_F(DownloadHistoryTest, DownloadHistoryTest_CreateFailed) { |
| 658 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 722 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 659 // OnDownloadRemoved. | 723 // OnDownloadRemoved. |
| 660 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 724 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 661 | 725 |
| 662 // Note that db_handle must be -1 at first because it isn't in the db yet. | 726 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 663 history::DownloadRow info; | 727 history::DownloadRow info; |
| 664 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | 728 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 665 GURL url("http://example.com/bar.pdf"); | 729 GURL url("http://example.com/bar.pdf"); |
| 666 GURL referrer("http://example.com/referrer.html"); | 730 GURL referrer("http://example.com/referrer.html"); |
| 731 std::vector<GURL> url_chain; | |
| 732 url_chain.push_back(url); | |
| 667 InitItem(base::RandInt(0, 1 << 20), | 733 InitItem(base::RandInt(0, 1 << 20), |
| 668 path, | 734 path, |
| 669 url, | 735 path, |
| 736 url_chain, | |
| 670 referrer, | 737 referrer, |
| 671 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 738 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 672 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 739 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 673 100, | 740 100, |
| 674 100, | 741 100, |
| 675 content::DownloadItem::COMPLETE, | 742 content::DownloadItem::COMPLETE, |
| 743 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 744 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 676 -1, | 745 -1, |
| 677 false, | 746 false, |
| 678 &info); | 747 &info); |
| 679 | 748 |
| 680 FailCreateDownload(); | 749 FailCreateDownload(); |
| 681 // Pretend the manager just created |item|. | 750 // Pretend the manager just created |item|. |
| 682 CallOnDownloadCreated(0); | 751 CallOnDownloadCreated(0); |
| 683 // CreateDownload() always gets db_handle=-1. | 752 // CreateDownload() always gets db_handle=-1. |
| 684 ExpectDownloadCreated(info); | 753 ExpectDownloadCreated(info); |
| 685 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 754 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
| 686 | 755 |
| 687 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); | 756 EXPECT_CALL(item(0), GetReceivedBytes()).WillRepeatedly(Return(100)); |
| 688 item_observer()->OnDownloadUpdated(&item(0)); | 757 item_observer()->OnDownloadUpdated(&item(0)); |
| 689 info.received_bytes = 100; | 758 info.received_bytes = 100; |
| 690 ExpectDownloadCreated(info); | 759 ExpectDownloadCreated(info); |
| 691 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 760 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 692 } | 761 } |
| 693 | 762 |
| 694 TEST_F(DownloadHistoryTest, | 763 TEST_F(DownloadHistoryTest, |
| 695 DownloadHistoryTest_UpdateWhileAdding) { | 764 DownloadHistoryTest_UpdateWhileAdding) { |
| 696 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, | 765 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 697 // OnDownloadRemoved. | 766 // OnDownloadRemoved. |
| 698 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); | 767 ExpectWillQueryDownloads(scoped_ptr<InfoVector>(new InfoVector())); |
| 699 | 768 |
| 700 // Note that db_handle must be -1 at first because it isn't in the db yet. | 769 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 701 history::DownloadRow info; | 770 history::DownloadRow info; |
| 702 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); | 771 FilePath path(FILE_PATH_LITERAL("/foo/bar.pdf")); |
| 703 GURL url("http://example.com/bar.pdf"); | 772 GURL url("http://example.com/bar.pdf"); |
| 704 GURL referrer("http://example.com/referrer.html"); | 773 GURL referrer("http://example.com/referrer.html"); |
| 774 std::vector<GURL> url_chain; | |
| 775 url_chain.push_back(url); | |
| 705 InitItem(base::RandInt(0, 1 << 20), | 776 InitItem(base::RandInt(0, 1 << 20), |
| 706 path, | 777 path, |
| 707 url, | 778 path, |
| 779 url_chain, | |
| 708 referrer, | 780 referrer, |
| 709 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 781 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 710 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), | 782 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 711 100, | 783 100, |
| 712 100, | 784 100, |
| 713 content::DownloadItem::COMPLETE, | 785 content::DownloadItem::COMPLETE, |
| 786 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
| 787 content::DOWNLOAD_INTERRUPT_REASON_NONE, | |
| 714 -1, | 788 -1, |
| 715 false, | 789 false, |
| 716 &info); | 790 &info); |
| 717 | 791 |
| 718 // Instruct CreateDownload() to not callback to DownloadHistory immediately, | 792 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
| 719 // but to wait for FinishCreateDownload(). | 793 // but to wait for FinishCreateDownload(). |
| 720 set_slow_create_download(true); | 794 set_slow_create_download(true); |
| 721 | 795 |
| 722 // Pretend the manager just created |item|. | 796 // Pretend the manager just created |item|. |
| 723 CallOnDownloadCreated(0); | 797 CallOnDownloadCreated(0); |
| 724 // CreateDownload() always gets db_handle=-1. | 798 // CreateDownload() always gets db_handle=-1. |
| 725 ExpectDownloadCreated(info); | 799 ExpectDownloadCreated(info); |
| 726 info.db_handle = 0; | 800 info.db_handle = 0; |
| 727 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); | 801 EXPECT_FALSE(DownloadHistory::IsPersisted(&item(0))); |
| 728 | 802 |
| 729 // Pretend that something changed on the item. | 803 // Pretend that something changed on the item. |
| 730 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); | 804 EXPECT_CALL(item(0), GetOpened()).WillRepeatedly(Return(true)); |
| 731 item_observer()->OnDownloadUpdated(&item(0)); | 805 item_observer()->OnDownloadUpdated(&item(0)); |
| 732 | 806 |
| 733 FinishCreateDownload(); | 807 FinishCreateDownload(); |
| 734 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 808 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 735 | 809 |
| 736 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 810 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
| 737 // changed while it was being added and call UpdateDownload immediately. | 811 // changed while it was being added and call UpdateDownload immediately. |
| 738 info.opened = true; | 812 info.opened = true; |
| 739 ExpectDownloadUpdated(info); | 813 ExpectDownloadUpdated(info); |
| 740 } | 814 } |
| OLD | NEW |