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 "base/rand_util.h" |
| 6 #include "chrome/browser/download/download_history.h" |
| 7 #include "chrome/browser/download/download_persistent_store_info.h" |
| 8 #include "chrome/browser/history/history.h" |
| 9 #include "chrome/test/base/in_process_browser_test.h" |
| 10 #include "content/public/test/mock_download_item.h" |
| 11 #include "content/public/test/mock_download_manager.h" |
| 12 #include "content/public/test/test_utils.h" |
| 13 |
| 14 using content::BrowserThread; |
| 15 using content::DownloadItem; |
| 16 using content::DownloadManager; |
| 17 using content::MockDownloadItem; |
| 18 using content::MockDownloadManager; |
| 19 using testing::DoAll; |
| 20 using testing::Invoke; |
| 21 using testing::NiceMock; |
| 22 using testing::Return; |
| 23 using testing::ReturnRef; |
| 24 using testing::_; |
| 25 |
| 26 namespace { |
| 27 |
| 28 bool InfoEqual(const DownloadPersistentStoreInfo& left, |
| 29 const DownloadPersistentStoreInfo& right) { |
| 30 if (left.path != right.path) { |
| 31 LOG(ERROR) << left.path.value() << " != " << right.path.value(); |
| 32 return false; |
| 33 } else if (left.url != right.url) { |
| 34 LOG(ERROR) << left.url.spec() << " != " << right.url.spec(); |
| 35 return false; |
| 36 } else if (left.referrer_url != right.referrer_url) { |
| 37 LOG(ERROR) << left.referrer_url.spec() << " != " |
| 38 << right.referrer_url.spec(); |
| 39 return false; |
| 40 } else if (left.start_time != right.start_time) { |
| 41 LOG(ERROR) << left.start_time.ToTimeT() << " != " |
| 42 << right.start_time.ToTimeT(); |
| 43 return false; |
| 44 } else if (left.end_time != right.end_time) { |
| 45 LOG(ERROR) << left.end_time.ToTimeT() << " != " << right.end_time.ToTimeT(); |
| 46 return false; |
| 47 } else if (left.received_bytes != right.received_bytes) { |
| 48 LOG(ERROR) << left.received_bytes << " != " << right.received_bytes; |
| 49 return false; |
| 50 } else if (left.total_bytes != right.total_bytes) { |
| 51 LOG(ERROR) << left.total_bytes << " != " << right.total_bytes; |
| 52 return false; |
| 53 } else if (left.state != right.state) { |
| 54 LOG(ERROR) << left.state << " != " << right.state; |
| 55 return false; |
| 56 } else if (left.db_handle != right.db_handle) { |
| 57 LOG(ERROR) << left.db_handle << " != " << right.db_handle; |
| 58 return false; |
| 59 } else if (left.opened != right.opened) { |
| 60 LOG(ERROR) << left.opened << " != " << right.opened; |
| 61 return false; |
| 62 } |
| 63 return true; |
| 64 } |
| 65 |
| 66 class DownloadHistoryTest : public InProcessBrowserTest, |
| 67 public HistoryServiceDownloadAdapter { |
| 68 public: |
| 69 typedef std::set<int64> HandleSet; |
| 70 typedef std::vector<DownloadPersistentStoreInfo> InfoVector; |
| 71 |
| 72 DownloadHistoryTest() |
| 73 : HistoryServiceDownloadAdapter(NULL), |
| 74 manager_(new MockDownloadManager()), |
| 75 download_history_(NULL), |
| 76 expect_query_downloads_(NULL), |
| 77 slow_create_download_(false), |
| 78 handle_counter_(0), |
| 79 create_download_id_(-1) { |
| 80 } |
| 81 virtual ~DownloadHistoryTest() {} |
| 82 |
| 83 // HistoryServiceDownloadAdapter |
| 84 virtual void QueryDownloads( |
| 85 const HistoryService::DownloadQueryCallback& callback) OVERRIDE { |
| 86 CHECK(expect_query_downloads_); |
| 87 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 88 base::Bind(&DownloadHistoryTest::QueryDownloadsDone, this, callback)); |
| 89 } |
| 90 |
| 91 void QueryDownloadsDone( |
| 92 const HistoryService::DownloadQueryCallback& callback) { |
| 93 callback.Run(expect_query_downloads_); |
| 94 expect_query_downloads_ = NULL; |
| 95 } |
| 96 |
| 97 virtual HistoryService::Handle GetVisibleVisitCountToHost( |
| 98 const GURL& referrer_url, |
| 99 const HistoryService::GetVisibleVisitCountToHostCallback& |
| 100 callback) OVERRIDE { |
| 101 NOTIMPLEMENTED(); |
| 102 return 0; |
| 103 } |
| 104 |
| 105 void set_slow_create_download(bool slow) { slow_create_download_ = slow; } |
| 106 |
| 107 virtual void CreateDownload( |
| 108 int32 id, |
| 109 const DownloadPersistentStoreInfo& info, |
| 110 const HistoryService::DownloadCreateCallback& callback) OVERRIDE { |
| 111 create_download_id_ = id; |
| 112 create_download_info_ = info; |
| 113 create_download_callback_ = base::Bind(callback, id, handle_counter_++); |
| 114 if (!slow_create_download_) { |
| 115 FinishCreateDownload(); |
| 116 } |
| 117 } |
| 118 |
| 119 void FinishCreateDownload() { |
| 120 create_download_callback_.Run(); |
| 121 create_download_callback_.Reset(); |
| 122 } |
| 123 |
| 124 virtual void UpdateDownload( |
| 125 const DownloadPersistentStoreInfo& info) OVERRIDE { |
| 126 update_download_ = info; |
| 127 } |
| 128 |
| 129 virtual void RemoveDownloads( |
| 130 const HandleSet& handles) OVERRIDE { |
| 131 for (HandleSet::const_iterator it = handles.begin(); |
| 132 it != handles.end(); ++it) { |
| 133 remove_downloads_.insert(*it); |
| 134 } |
| 135 } |
| 136 |
| 137 virtual void OnDownloadHistoryDestroyed() OVERRIDE { |
| 138 } |
| 139 |
| 140 protected: |
| 141 virtual void CleanUpOnMainThread() OVERRIDE { |
| 142 download_history_.reset(); |
| 143 } |
| 144 |
| 145 DownloadHistory* download_history() { return download_history_.get(); } |
| 146 |
| 147 MockDownloadManager& manager() { return *manager_.get(); } |
| 148 MockDownloadItem& item() { return item_; } |
| 149 |
| 150 HistoryService::DownloadQueryCallback query_callback() { |
| 151 return base::Bind(&DownloadHistoryTest::QueryCallback, |
| 152 base::Unretained(this)); |
| 153 } |
| 154 |
| 155 void ExpectQueryDownloads(InfoVector* infos) { |
| 156 expect_query_downloads_ = infos; |
| 157 EXPECT_CALL(manager(), AddObserver(_)); |
| 158 EXPECT_CALL(manager(), RemoveObserver(_)); |
| 159 if (infos->size() != 0) { |
| 160 EXPECT_EQ(1, static_cast<int>(infos->size())); |
| 161 EXPECT_CALL(manager(), CreateDownloadItem( |
| 162 infos->at(0).path, |
| 163 infos->at(0).url, |
| 164 infos->at(0).referrer_url, |
| 165 infos->at(0).start_time, |
| 166 infos->at(0).end_time, |
| 167 infos->at(0).received_bytes, |
| 168 infos->at(0).total_bytes, |
| 169 infos->at(0).state, |
| 170 infos->at(0).opened)) |
| 171 .WillOnce(DoAll( |
| 172 InvokeWithoutArgs( |
| 173 this, &DownloadHistoryTest::CallOnDownloadCreated), |
| 174 Return(&item()))); |
| 175 } |
| 176 download_history_.reset(new DownloadHistory(&manager(), this)); |
| 177 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 178 CHECK(NULL == expect_query_downloads_); |
| 179 } |
| 180 |
| 181 void CallOnDownloadCreated() { |
| 182 download_history_->OnDownloadCreated(&manager(), &item()); |
| 183 } |
| 184 |
| 185 void ExpectCreateDownload( |
| 186 const DownloadPersistentStoreInfo& info) { |
| 187 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 188 CHECK_EQ(item().GetId(), create_download_id_); |
| 189 create_download_id_ = -1; |
| 190 CHECK(InfoEqual(info, create_download_info_)); |
| 191 create_download_info_ = DownloadPersistentStoreInfo(); |
| 192 } |
| 193 |
| 194 void ExpectNoCreateDownload() { |
| 195 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 196 CHECK_EQ(-1, create_download_id_); |
| 197 CHECK(InfoEqual(DownloadPersistentStoreInfo(), create_download_info_)); |
| 198 } |
| 199 |
| 200 void ExpectUpdateDownload(const DownloadPersistentStoreInfo& info) { |
| 201 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 202 CHECK(InfoEqual(update_download_, info)); |
| 203 update_download_ = DownloadPersistentStoreInfo(); |
| 204 } |
| 205 |
| 206 void ExpectNoUpdateDownload() { |
| 207 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 208 CHECK(InfoEqual(DownloadPersistentStoreInfo(), update_download_)); |
| 209 } |
| 210 |
| 211 void ExpectNoRemoveDownloads() { |
| 212 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 213 CHECK_EQ(0, static_cast<int>(remove_downloads_.size())); |
| 214 } |
| 215 |
| 216 void ExpectRemoveDownloads(const HandleSet& handles) { |
| 217 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 218 HandleSet difference; |
| 219 std::insert_iterator<HandleSet> insert_it( |
| 220 difference, difference.begin()); |
| 221 std::set_difference(handles.begin(), handles.end(), |
| 222 remove_downloads_.begin(), |
| 223 remove_downloads_.end(), |
| 224 insert_it); |
| 225 CHECK(difference.empty()); |
| 226 remove_downloads_.clear(); |
| 227 } |
| 228 |
| 229 void InitItem( |
| 230 int32 id, |
| 231 const FilePath& path, |
| 232 const GURL& url, |
| 233 const GURL& referrer, |
| 234 const base::Time& start_time, |
| 235 const base::Time& end_time, |
| 236 int64 received_bytes, |
| 237 int64 total_bytes, |
| 238 DownloadItem::DownloadState state, |
| 239 int64 db_handle, |
| 240 bool opened, |
| 241 DownloadPersistentStoreInfo* info) { |
| 242 info->path = path; |
| 243 info->url = url; |
| 244 info->referrer_url = referrer; |
| 245 info->start_time = start_time; |
| 246 info->end_time = end_time; |
| 247 info->received_bytes = received_bytes; |
| 248 info->total_bytes = total_bytes; |
| 249 info->state = state; |
| 250 info->db_handle = db_handle; |
| 251 info->opened = opened; |
| 252 EXPECT_CALL(item(), GetId()).WillRepeatedly(Return(id)); |
| 253 EXPECT_CALL(item(), GetFullPath()).WillRepeatedly(ReturnRef(path)); |
| 254 EXPECT_CALL(item(), GetURL()).WillRepeatedly(ReturnRef(url)); |
| 255 EXPECT_CALL(item(), GetReferrerUrl()).WillRepeatedly(ReturnRef(referrer)); |
| 256 EXPECT_CALL(item(), GetStartTime()).WillRepeatedly(Return(start_time)); |
| 257 EXPECT_CALL(item(), GetEndTime()).WillRepeatedly(Return(end_time)); |
| 258 EXPECT_CALL(item(), GetReceivedBytes()) |
| 259 .WillRepeatedly(Return(received_bytes)); |
| 260 EXPECT_CALL(item(), GetTotalBytes()).WillRepeatedly(Return(total_bytes)); |
| 261 EXPECT_CALL(item(), GetState()).WillRepeatedly(Return(state)); |
| 262 EXPECT_CALL(item(), GetOpened()).WillRepeatedly(Return(opened)); |
| 263 EXPECT_CALL(item(), GetTargetDisposition()).WillRepeatedly(Return( |
| 264 DownloadItem::TARGET_DISPOSITION_OVERWRITE)); |
| 265 EXPECT_CALL(manager(), GetDownload(id)) |
| 266 .WillRepeatedly(Return(&item())); |
| 267 EXPECT_CALL(item(), AddObserver(_)); |
| 268 EXPECT_CALL(item(), RemoveObserver(_)); |
| 269 } |
| 270 |
| 271 private: |
| 272 void QueryCallback(InfoVector* infos) { |
| 273 } |
| 274 |
| 275 testing::NiceMock<content::MockDownloadItem> item_; |
| 276 scoped_refptr<content::MockDownloadManager> manager_; |
| 277 scoped_ptr<DownloadHistory> download_history_; |
| 278 InfoVector* expect_query_downloads_; |
| 279 bool slow_create_download_; |
| 280 base::Closure create_download_callback_; |
| 281 int handle_counter_; |
| 282 DownloadPersistentStoreInfo update_download_; |
| 283 int32 create_download_id_; |
| 284 DownloadPersistentStoreInfo create_download_info_; |
| 285 HandleSet remove_downloads_; |
| 286 |
| 287 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryTest); |
| 288 }; |
| 289 |
| 290 } // anonymous namespace |
| 291 |
| 292 IN_PROC_BROWSER_TEST_F(DownloadHistoryTest, |
| 293 DownloadHistoryTest_Load) { |
| 294 // Load a download from history, create the item, OnDownloadCreated, |
| 295 // OnDownloadUpdated, OnDownloadRemoved, OnDownloadDestroyed. |
| 296 DownloadPersistentStoreInfo info; |
| 297 InitItem(base::RandInt(0, 1 << 20), |
| 298 FilePath(FILE_PATH_LITERAL("/foo/bar.pdf")), |
| 299 GURL("http://example.com/bar.pdf"), |
| 300 GURL("http://example.com/referrer.html"), |
| 301 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 302 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 303 100, |
| 304 100, |
| 305 DownloadItem::COMPLETE, |
| 306 base::RandInt(0, 1 << 20), |
| 307 false, |
| 308 &info); |
| 309 InfoVector infos; |
| 310 infos.push_back(info); |
| 311 ExpectQueryDownloads(&infos); |
| 312 ExpectNoCreateDownload(); |
| 313 |
| 314 // Pretend that something changed on the item. |
| 315 EXPECT_CALL(item(), GetOpened()).WillRepeatedly(Return(true)); |
| 316 download_history()->OnDownloadUpdated(&item()); |
| 317 info.opened = true; |
| 318 ExpectUpdateDownload(info); |
| 319 |
| 320 // Pretend that the user removed the item. |
| 321 HandleSet handles; |
| 322 handles.insert(info.db_handle); |
| 323 download_history()->OnDownloadRemoved(&item()); |
| 324 ExpectRemoveDownloads(handles); |
| 325 |
| 326 // Pretend that the browser is closing. |
| 327 download_history()->ManagerGoingDown(&manager()); |
| 328 download_history()->OnDownloadDestroyed(&item()); |
| 329 } |
| 330 |
| 331 IN_PROC_BROWSER_TEST_F(DownloadHistoryTest, |
| 332 DownloadHistoryTest_Create) { |
| 333 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 334 // OnDownloadRemoved, OnDownloadDestroyed. |
| 335 InfoVector infos; |
| 336 ExpectQueryDownloads(&infos); |
| 337 |
| 338 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 339 DownloadPersistentStoreInfo info; |
| 340 InitItem(base::RandInt(0, 1 << 20), |
| 341 FilePath(FILE_PATH_LITERAL("/foo/bar.pdf")), |
| 342 GURL("http://example.com/bar.pdf"), |
| 343 GURL("http://example.com/referrer.html"), |
| 344 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 345 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 346 100, |
| 347 100, |
| 348 DownloadItem::COMPLETE, |
| 349 -1, |
| 350 false, |
| 351 &info); |
| 352 |
| 353 // Pretend the manager just created |item|. |
| 354 download_history()->OnDownloadCreated(&manager(), &item()); |
| 355 // CreateDownload() always gets db_handle=-1. |
| 356 ExpectCreateDownload(info); |
| 357 info.db_handle = 0; |
| 358 |
| 359 // Pretend that something changed on the item. |
| 360 EXPECT_CALL(item(), GetOpened()).WillRepeatedly(Return(true)); |
| 361 download_history()->OnDownloadUpdated(&item()); |
| 362 info.opened = true; |
| 363 ExpectUpdateDownload(info); |
| 364 |
| 365 // Pretend that the user removed the item. |
| 366 HandleSet handles; |
| 367 handles.insert(info.db_handle); |
| 368 download_history()->OnDownloadRemoved(&item()); |
| 369 ExpectRemoveDownloads(handles); |
| 370 |
| 371 // Pretend that the browser is closing. |
| 372 download_history()->ManagerGoingDown(&manager()); |
| 373 download_history()->OnDownloadDestroyed(&item()); |
| 374 } |
| 375 |
| 376 IN_PROC_BROWSER_TEST_F(DownloadHistoryTest, |
| 377 DownloadHistoryTest_Temporary) { |
| 378 // Create a fresh item not from history, OnDownloadCreated, OnDownloadUpdated, |
| 379 // OnDownloadRemoved, OnDownloadDestroyed. |
| 380 InfoVector infos; |
| 381 ExpectQueryDownloads(&infos); |
| 382 |
| 383 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 384 DownloadPersistentStoreInfo info; |
| 385 InitItem(base::RandInt(0, 1 << 20), |
| 386 FilePath(FILE_PATH_LITERAL("/foo/bar.pdf")), |
| 387 GURL("http://example.com/bar.pdf"), |
| 388 GURL("http://example.com/referrer.html"), |
| 389 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 390 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 391 100, |
| 392 100, |
| 393 DownloadItem::COMPLETE, |
| 394 -1, |
| 395 false, |
| 396 &info); |
| 397 |
| 398 // Pretend the manager just created |item|. |
| 399 download_history()->OnDownloadCreated(&manager(), &item()); |
| 400 // CreateDownload() always gets db_handle=-1. |
| 401 ExpectCreateDownload(info); |
| 402 info.db_handle = 0; |
| 403 |
| 404 // Pretend the item was marked temporary. DownloadHistory should remove it |
| 405 // from history and start ignoring it. |
| 406 EXPECT_CALL(item(), IsTemporary()).WillRepeatedly(Return(true)); |
| 407 download_history()->OnDownloadUpdated(&item()); |
| 408 HandleSet handles; |
| 409 handles.insert(info.db_handle); |
| 410 ExpectRemoveDownloads(handles); |
| 411 |
| 412 // Change something that would make DownloadHistory call UpdateDownload if the |
| 413 // item weren't temporary. |
| 414 EXPECT_CALL(item(), GetReceivedBytes()).WillRepeatedly(Return(4200)); |
| 415 download_history()->OnDownloadUpdated(&item()); |
| 416 ExpectNoUpdateDownload(); |
| 417 |
| 418 // Changing a temporary item back to a non-temporary item should make |
| 419 // DownloadHistory call CreateDownload. |
| 420 EXPECT_CALL(item(), IsTemporary()).WillRepeatedly(Return(false)); |
| 421 download_history()->OnDownloadUpdated(&item()); |
| 422 info.received_bytes = 4200; |
| 423 info.db_handle = -1; |
| 424 // CreateDownload() always gets db_handle=-1. |
| 425 ExpectCreateDownload(info); |
| 426 info.db_handle = 1; |
| 427 |
| 428 EXPECT_CALL(item(), GetReceivedBytes()).WillRepeatedly(Return(100)); |
| 429 download_history()->OnDownloadUpdated(&item()); |
| 430 info.received_bytes = 100; |
| 431 ExpectUpdateDownload(info); |
| 432 |
| 433 // Pretend that the browser is closing. |
| 434 download_history()->ManagerGoingDown(&manager()); |
| 435 download_history()->OnDownloadDestroyed(&item()); |
| 436 } |
| 437 |
| 438 IN_PROC_BROWSER_TEST_F(DownloadHistoryTest, |
| 439 DownloadHistoryTest_RemoveWhileAdding) { |
| 440 InfoVector infos; |
| 441 ExpectQueryDownloads(&infos); |
| 442 |
| 443 // Note that db_handle must be -1 at first because it isn't in the db yet. |
| 444 DownloadPersistentStoreInfo info; |
| 445 InitItem(base::RandInt(0, 1 << 20), |
| 446 FilePath(FILE_PATH_LITERAL("/foo/bar.pdf")), |
| 447 GURL("http://example.com/bar.pdf"), |
| 448 GURL("http://example.com/referrer.html"), |
| 449 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 450 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), |
| 451 100, |
| 452 100, |
| 453 DownloadItem::COMPLETE, |
| 454 -1, |
| 455 false, |
| 456 &info); |
| 457 |
| 458 // Instruct CreateDownload() to not callback to DownloadHistory immediately, |
| 459 // but to wait for FinishCreateDownload(). |
| 460 set_slow_create_download(true); |
| 461 |
| 462 // Pretend the manager just created |item|. |
| 463 download_history()->OnDownloadCreated(&manager(), &item()); |
| 464 // CreateDownload() always gets db_handle=-1. |
| 465 ExpectCreateDownload(info); |
| 466 info.db_handle = 0; |
| 467 |
| 468 // Call OnDownloadRemoved before calling back to DownloadHistory::ItemAdded(). |
| 469 // Instead of calling RemoveDownloads() immediately, it should |
| 470 download_history()->OnDownloadRemoved(&item()); |
| 471 EXPECT_CALL(manager(), GetDownload(item().GetId())) |
| 472 .WillRepeatedly(Return(static_cast<DownloadItem*>(NULL))); |
| 473 ExpectNoRemoveDownloads(); |
| 474 |
| 475 // Now callback to DownloadHistory::ItemAdded(), and expect a call to |
| 476 // RemoveDownloads() for the item that was removed while it was being added. |
| 477 FinishCreateDownload(); |
| 478 HandleSet handles; |
| 479 handles.insert(info.db_handle); |
| 480 ExpectRemoveDownloads(handles); |
| 481 |
| 482 // Pretend that the browser is closing. |
| 483 download_history()->ManagerGoingDown(&manager()); |
| 484 download_history()->OnDownloadDestroyed(&item()); |
| 485 } |
| 486 |
| 487 // TODO(benjhayden): Test Disable. |
OLD | NEW |