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