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