| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/download/download_history.h" | 5 #include "chrome/browser/download/download_history.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/guid.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 16 #include "components/history/content/browser/download_constants_utils.h" | 17 #include "components/history/content/browser/download_constants_utils.h" |
| 17 #include "components/history/core/browser/download_constants.h" | 18 #include "components/history/core/browser/download_constants.h" |
| 18 #include "components/history/core/browser/download_row.h" | 19 #include "components/history/core/browser/download_row.h" |
| 19 #include "components/history/core/browser/history_service.h" | 20 #include "components/history/core/browser/history_service.h" |
| 20 #include "content/public/test/mock_download_item.h" | 21 #include "content/public/test/mock_download_item.h" |
| 21 #include "content/public/test/mock_download_manager.h" | 22 #include "content/public/test/mock_download_manager.h" |
| 22 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
| 23 #include "content/public/test/test_utils.h" | 24 #include "content/public/test/test_utils.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 #if defined(ENABLE_EXTENSIONS) | 27 #if defined(ENABLE_EXTENSIONS) |
| 27 #include "chrome/browser/extensions/api/downloads/downloads_api.h" | 28 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
| 28 #endif | 29 #endif |
| 29 | 30 |
| 30 using testing::DoAll; | 31 using testing::DoAll; |
| 31 using testing::Invoke; | 32 using testing::Invoke; |
| 32 using testing::Return; | 33 using testing::Return; |
| 33 using testing::ReturnRefOfCopy; | 34 using testing::ReturnRefOfCopy; |
| 34 using testing::SetArgPointee; | 35 using testing::SetArgPointee; |
| 35 using testing::WithArg; | 36 using testing::WithArg; |
| 36 using testing::_; | 37 using testing::_; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 void CheckInfoEqual(const history::DownloadRow& left, | |
| 41 const history::DownloadRow& right) { | |
| 42 EXPECT_EQ(left.current_path.value(), right.current_path.value()); | |
| 43 EXPECT_EQ(left.target_path.value(), right.target_path.value()); | |
| 44 EXPECT_EQ(left.url_chain.size(), right.url_chain.size()); | |
| 45 for (unsigned int i = 0; | |
| 46 i < left.url_chain.size() && i < right.url_chain.size(); | |
| 47 ++i) { | |
| 48 EXPECT_EQ(left.url_chain[i].spec(), right.url_chain[i].spec()); | |
| 49 } | |
| 50 EXPECT_EQ(left.referrer_url.spec(), right.referrer_url.spec()); | |
| 51 EXPECT_EQ(left.mime_type, right.mime_type); | |
| 52 EXPECT_EQ(left.original_mime_type, right.original_mime_type); | |
| 53 EXPECT_EQ(left.start_time.ToTimeT(), right.start_time.ToTimeT()); | |
| 54 EXPECT_EQ(left.end_time.ToTimeT(), right.end_time.ToTimeT()); | |
| 55 EXPECT_EQ(left.etag, right.etag); | |
| 56 EXPECT_EQ(left.last_modified, right.last_modified); | |
| 57 EXPECT_EQ(left.received_bytes, right.received_bytes); | |
| 58 EXPECT_EQ(left.total_bytes, right.total_bytes); | |
| 59 EXPECT_EQ(left.state, right.state); | |
| 60 EXPECT_EQ(left.danger_type, right.danger_type); | |
| 61 EXPECT_EQ(left.id, right.id); | |
| 62 EXPECT_EQ(left.opened, right.opened); | |
| 63 EXPECT_EQ(left.by_ext_id, right.by_ext_id); | |
| 64 EXPECT_EQ(left.by_ext_name, right.by_ext_name); | |
| 65 } | |
| 66 | |
| 67 typedef DownloadHistory::IdSet IdSet; | 41 typedef DownloadHistory::IdSet IdSet; |
| 68 typedef std::vector<history::DownloadRow> InfoVector; | 42 typedef std::vector<history::DownloadRow> InfoVector; |
| 69 typedef testing::StrictMock<content::MockDownloadItem> StrictMockDownloadItem; | 43 typedef testing::StrictMock<content::MockDownloadItem> StrictMockDownloadItem; |
| 70 | 44 |
| 71 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter { | 45 class FakeHistoryAdapter : public DownloadHistory::HistoryAdapter { |
| 72 public: | 46 public: |
| 73 FakeHistoryAdapter() | 47 FakeHistoryAdapter() |
| 74 : DownloadHistory::HistoryAdapter(NULL), | 48 : DownloadHistory::HistoryAdapter(NULL), |
| 75 slow_create_download_(false), | 49 slow_create_download_(false), |
| 76 fail_create_download_(false) { | 50 fail_create_download_(false) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 113 |
| 140 void FailCreateDownload() { | 114 void FailCreateDownload() { |
| 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 142 fail_create_download_ = true; | 116 fail_create_download_ = true; |
| 143 } | 117 } |
| 144 | 118 |
| 145 void ExpectDownloadCreated( | 119 void ExpectDownloadCreated( |
| 146 const history::DownloadRow& info) { | 120 const history::DownloadRow& info) { |
| 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 148 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 122 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 149 CheckInfoEqual(info, create_download_info_); | 123 EXPECT_EQ(info, create_download_info_); |
| 150 create_download_info_ = history::DownloadRow(); | 124 create_download_info_ = history::DownloadRow(); |
| 151 } | 125 } |
| 152 | 126 |
| 153 void ExpectNoDownloadCreated() { | 127 void ExpectNoDownloadCreated() { |
| 154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 155 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 129 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 156 CheckInfoEqual(history::DownloadRow(), create_download_info_); | 130 EXPECT_EQ(history::DownloadRow(), create_download_info_); |
| 157 } | 131 } |
| 158 | 132 |
| 159 void ExpectDownloadUpdated(const history::DownloadRow& info) { | 133 void ExpectDownloadUpdated(const history::DownloadRow& info) { |
| 160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 161 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 135 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 162 CheckInfoEqual(update_download_, info); | 136 EXPECT_EQ(update_download_, info); |
| 163 update_download_ = history::DownloadRow(); | 137 update_download_ = history::DownloadRow(); |
| 164 } | 138 } |
| 165 | 139 |
| 166 void ExpectNoDownloadUpdated() { | 140 void ExpectNoDownloadUpdated() { |
| 167 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 168 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 142 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 169 CheckInfoEqual(history::DownloadRow(), update_download_); | 143 EXPECT_EQ(history::DownloadRow(), update_download_); |
| 170 } | 144 } |
| 171 | 145 |
| 172 void ExpectNoDownloadsRemoved() { | 146 void ExpectNoDownloadsRemoved() { |
| 173 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 174 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); | 148 content::RunAllPendingInMessageLoop(content::BrowserThread::UI); |
| 175 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size())); | 149 EXPECT_EQ(0, static_cast<int>(remove_downloads_.size())); |
| 176 } | 150 } |
| 177 | 151 |
| 178 void ExpectDownloadsRemoved(const IdSet& ids) { | 152 void ExpectDownloadsRemoved(const IdSet& ids) { |
| 179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) { | 203 void CreateDownloadHistory(scoped_ptr<InfoVector> infos) { |
| 230 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 204 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 231 CHECK(infos.get()); | 205 CHECK(infos.get()); |
| 232 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( | 206 EXPECT_CALL(manager(), AddObserver(_)).WillOnce(WithArg<0>(Invoke( |
| 233 this, &DownloadHistoryTest::SetManagerObserver))); | 207 this, &DownloadHistoryTest::SetManagerObserver))); |
| 234 EXPECT_CALL(manager(), RemoveObserver(_)); | 208 EXPECT_CALL(manager(), RemoveObserver(_)); |
| 235 download_created_index_ = 0; | 209 download_created_index_ = 0; |
| 236 for (size_t index = 0; index < infos->size(); ++index) { | 210 for (size_t index = 0; index < infos->size(); ++index) { |
| 237 const history::DownloadRow& row = infos->at(index); | 211 const history::DownloadRow& row = infos->at(index); |
| 238 content::MockDownloadManager::CreateDownloadItemAdapter adapter( | 212 content::MockDownloadManager::CreateDownloadItemAdapter adapter( |
| 239 history::ToContentDownloadId(row.id), | 213 row.guid, history::ToContentDownloadId(row.id), row.current_path, |
| 240 row.current_path, | 214 row.target_path, row.url_chain, row.referrer_url, row.mime_type, |
| 241 row.target_path, | 215 row.original_mime_type, row.start_time, row.end_time, row.etag, |
| 242 row.url_chain, | 216 row.last_modified, row.received_bytes, row.total_bytes, |
| 243 row.referrer_url, | |
| 244 row.mime_type, | |
| 245 row.original_mime_type, | |
| 246 row.start_time, | |
| 247 row.end_time, | |
| 248 row.etag, | |
| 249 row.last_modified, | |
| 250 row.received_bytes, | |
| 251 row.total_bytes, | |
| 252 history::ToContentDownloadState(row.state), | 217 history::ToContentDownloadState(row.state), |
| 253 history::ToContentDownloadDangerType(row.danger_type), | 218 history::ToContentDownloadDangerType(row.danger_type), |
| 254 history::ToContentDownloadInterruptReason(row.interrupt_reason), | 219 history::ToContentDownloadInterruptReason(row.interrupt_reason), |
| 255 row.opened); | 220 row.opened); |
| 256 EXPECT_CALL(manager(), MockCreateDownloadItem(adapter)) | 221 EXPECT_CALL(manager(), MockCreateDownloadItem(adapter)) |
| 257 .WillOnce(DoAll( | 222 .WillOnce(DoAll( |
| 258 InvokeWithoutArgs( | 223 InvokeWithoutArgs( |
| 259 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), | 224 this, &DownloadHistoryTest::CallOnDownloadCreatedInOrder), |
| 260 Return(&item(index)))); | 225 Return(&item(index)))); |
| 261 } | 226 } |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 } | 309 } |
| 345 | 310 |
| 346 void InitBasicItem(const base::FilePath::CharType* path, | 311 void InitBasicItem(const base::FilePath::CharType* path, |
| 347 const char* url_string, | 312 const char* url_string, |
| 348 const char* referrer_string, | 313 const char* referrer_string, |
| 349 history::DownloadRow* info) { | 314 history::DownloadRow* info) { |
| 350 GURL url(url_string); | 315 GURL url(url_string); |
| 351 GURL referrer(referrer_string); | 316 GURL referrer(referrer_string); |
| 352 std::vector<GURL> url_chain; | 317 std::vector<GURL> url_chain; |
| 353 url_chain.push_back(url); | 318 url_chain.push_back(url); |
| 354 InitItem(static_cast<uint32_t>(items_.size() + 1), base::FilePath(path), | 319 InitItem(base::GenerateGUID(), static_cast<uint32_t>(items_.size() + 1), |
| 355 base::FilePath(path), url_chain, referrer, | 320 base::FilePath(path), base::FilePath(path), url_chain, referrer, |
| 356 "application/octet-stream", "application/octet-stream", | 321 "application/octet-stream", "application/octet-stream", |
| 357 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), | 322 (base::Time::Now() - base::TimeDelta::FromMinutes(10)), |
| 358 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), "Etag", | 323 (base::Time::Now() - base::TimeDelta::FromMinutes(1)), "Etag", |
| 359 "abc", 100, 100, content::DownloadItem::COMPLETE, | 324 "abc", 100, 100, content::DownloadItem::COMPLETE, |
| 360 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | 325 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 361 content::DOWNLOAD_INTERRUPT_REASON_NONE, false, std::string(), | 326 content::DOWNLOAD_INTERRUPT_REASON_NONE, false, std::string(), |
| 362 std::string(), info); | 327 std::string(), info); |
| 363 } | 328 } |
| 364 | 329 |
| 365 void InitItem(uint32_t id, | 330 void InitItem(const std::string& guid, |
| 331 uint32_t id, |
| 366 const base::FilePath& current_path, | 332 const base::FilePath& current_path, |
| 367 const base::FilePath& target_path, | 333 const base::FilePath& target_path, |
| 368 const std::vector<GURL>& url_chain, | 334 const std::vector<GURL>& url_chain, |
| 369 const GURL& referrer, | 335 const GURL& referrer, |
| 370 const std::string& mime_type, | 336 const std::string& mime_type, |
| 371 const std::string& original_mime_type, | 337 const std::string& original_mime_type, |
| 372 const base::Time& start_time, | 338 const base::Time& start_time, |
| 373 const base::Time& end_time, | 339 const base::Time& end_time, |
| 374 const std::string& etag, | 340 const std::string& etag, |
| 375 const std::string& last_modified, | 341 const std::string& last_modified, |
| (...skipping 22 matching lines...) Expand all Loading... |
| 398 info->end_time = end_time; | 364 info->end_time = end_time; |
| 399 info->etag = etag; | 365 info->etag = etag; |
| 400 info->last_modified = last_modified; | 366 info->last_modified = last_modified; |
| 401 info->received_bytes = received_bytes; | 367 info->received_bytes = received_bytes; |
| 402 info->total_bytes = total_bytes; | 368 info->total_bytes = total_bytes; |
| 403 info->state = history::ToHistoryDownloadState(state); | 369 info->state = history::ToHistoryDownloadState(state); |
| 404 info->danger_type = history::ToHistoryDownloadDangerType(danger_type); | 370 info->danger_type = history::ToHistoryDownloadDangerType(danger_type); |
| 405 info->interrupt_reason = | 371 info->interrupt_reason = |
| 406 history::ToHistoryDownloadInterruptReason(interrupt_reason); | 372 history::ToHistoryDownloadInterruptReason(interrupt_reason); |
| 407 info->id = history::ToHistoryDownloadId(id); | 373 info->id = history::ToHistoryDownloadId(id); |
| 374 info->guid = guid; |
| 408 info->opened = opened; | 375 info->opened = opened; |
| 409 info->by_ext_id = by_extension_id; | 376 info->by_ext_id = by_extension_id; |
| 410 info->by_ext_name = by_extension_name; | 377 info->by_ext_name = by_extension_name; |
| 411 | 378 |
| 412 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id)); | 379 EXPECT_CALL(item(index), GetId()).WillRepeatedly(Return(id)); |
| 380 EXPECT_CALL(item(index), GetGuid()).WillRepeatedly(ReturnRefOfCopy(guid)); |
| 413 EXPECT_CALL(item(index), GetFullPath()) | 381 EXPECT_CALL(item(index), GetFullPath()) |
| 414 .WillRepeatedly(ReturnRefOfCopy(current_path)); | 382 .WillRepeatedly(ReturnRefOfCopy(current_path)); |
| 415 EXPECT_CALL(item(index), GetTargetFilePath()) | 383 EXPECT_CALL(item(index), GetTargetFilePath()) |
| 416 .WillRepeatedly(ReturnRefOfCopy(target_path)); | 384 .WillRepeatedly(ReturnRefOfCopy(target_path)); |
| 417 DCHECK_LE(1u, url_chain.size()); | 385 DCHECK_LE(1u, url_chain.size()); |
| 418 EXPECT_CALL(item(index), GetURL()) | 386 EXPECT_CALL(item(index), GetURL()) |
| 419 .WillRepeatedly(ReturnRefOfCopy(url_chain[0])); | 387 .WillRepeatedly(ReturnRefOfCopy(url_chain[0])); |
| 420 EXPECT_CALL(item(index), GetUrlChain()) | 388 EXPECT_CALL(item(index), GetUrlChain()) |
| 421 .WillRepeatedly(ReturnRefOfCopy(url_chain)); | 389 .WillRepeatedly(ReturnRefOfCopy(url_chain)); |
| 422 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return(mime_type)); | 390 EXPECT_CALL(item(index), GetMimeType()).WillRepeatedly(Return(mime_type)); |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 FinishCreateDownload(); | 822 FinishCreateDownload(); |
| 855 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); | 823 EXPECT_TRUE(DownloadHistory::IsPersisted(&item(0))); |
| 856 | 824 |
| 857 // ItemAdded should call OnDownloadUpdated, which should detect that the item | 825 // ItemAdded should call OnDownloadUpdated, which should detect that the item |
| 858 // changed while it was being added and call UpdateDownload immediately. | 826 // changed while it was being added and call UpdateDownload immediately. |
| 859 info.opened = true; | 827 info.opened = true; |
| 860 ExpectDownloadUpdated(info); | 828 ExpectDownloadUpdated(info); |
| 861 } | 829 } |
| 862 | 830 |
| 863 } // anonymous namespace | 831 } // anonymous namespace |
| OLD | NEW |