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