| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "content/browser/download/download_create_info.h" | 8 #include "content/browser/download/download_create_info.h" |
| 9 #include "content/browser/download/download_file.h" | 9 #include "content/browser/download/download_file.h" |
| 10 #include "content/browser/download/download_id.h" |
| 11 #include "content/browser/download/download_id_factory.h" |
| 10 #include "content/browser/download/download_manager.h" | 12 #include "content/browser/download/download_manager.h" |
| 11 #include "content/browser/download/download_request_handle.h" | 13 #include "content/browser/download/download_request_handle.h" |
| 12 #include "content/browser/download/download_status_updater.h" | 14 #include "content/browser/download/download_status_updater.h" |
| 13 #include "content/browser/download/mock_download_manager.h" | 15 #include "content/browser/download/mock_download_manager.h" |
| 14 #include "content/browser/download/mock_download_manager_delegate.h" | 16 #include "content/browser/download/mock_download_manager_delegate.h" |
| 15 #include "content/test/test_browser_thread.h" | 17 #include "content/test/test_browser_thread.h" |
| 16 #include "net/base/file_stream.h" | 18 #include "net/base/file_stream.h" |
| 17 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 22 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
| 23 |
| 20 class DownloadFileTest : public testing::Test { | 24 class DownloadFileTest : public testing::Test { |
| 21 public: | 25 public: |
| 22 | 26 |
| 23 static const char* kTestData1; | 27 static const char* kTestData1; |
| 24 static const char* kTestData2; | 28 static const char* kTestData2; |
| 25 static const char* kTestData3; | 29 static const char* kTestData3; |
| 26 static const char* kDataHash; | 30 static const char* kDataHash; |
| 27 static const int32 kDummyDownloadId; | 31 static const int32 kDummyDownloadId; |
| 28 static const int kDummyChildId; | 32 static const int kDummyChildId; |
| 29 static const int kDummyRequestId; | 33 static const int kDummyRequestId; |
| 30 | 34 |
| 31 // We need a UI |BrowserThread| in order to destruct |download_manager_|, | 35 // We need a UI |BrowserThread| in order to destruct |download_manager_|, |
| 32 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, | 36 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, |
| 33 // calling Release() on |download_manager_| won't ever result in its | 37 // calling Release() on |download_manager_| won't ever result in its |
| 34 // destructor being called and we get a leak. | 38 // destructor being called and we get a leak. |
| 35 DownloadFileTest() : | 39 DownloadFileTest() : |
| 40 id_factory_(new DownloadIdFactory(kValidIdDomain)), |
| 36 ui_thread_(BrowserThread::UI, &loop_), | 41 ui_thread_(BrowserThread::UI, &loop_), |
| 37 file_thread_(BrowserThread::FILE, &loop_) { | 42 file_thread_(BrowserThread::FILE, &loop_) { |
| 38 } | 43 } |
| 39 | 44 |
| 40 ~DownloadFileTest() { | 45 ~DownloadFileTest() { |
| 41 } | 46 } |
| 42 | 47 |
| 43 virtual void SetUp() { | 48 virtual void SetUp() { |
| 44 download_manager_delegate_.reset(new MockDownloadManagerDelegate()); | 49 download_manager_delegate_.reset(new MockDownloadManagerDelegate()); |
| 45 download_manager_ = new MockDownloadManager( | 50 download_manager_ = new MockDownloadManager( |
| 46 download_manager_delegate_.get(), &download_status_updater_); | 51 download_manager_delegate_.get(), |
| 52 id_factory_, |
| 53 &download_status_updater_); |
| 47 } | 54 } |
| 48 | 55 |
| 49 virtual void TearDown() { | 56 virtual void TearDown() { |
| 50 // When a DownloadManager's reference count drops to 0, it is not | 57 // When a DownloadManager's reference count drops to 0, it is not |
| 51 // deleted immediately. Instead, a task is posted to the UI thread's | 58 // deleted immediately. Instead, a task is posted to the UI thread's |
| 52 // message loop to delete it. | 59 // message loop to delete it. |
| 53 // So, drop the reference count to 0 and run the message loop once | 60 // So, drop the reference count to 0 and run the message loop once |
| 54 // to ensure that all resources are cleaned up before the test exits. | 61 // to ensure that all resources are cleaned up before the test exits. |
| 55 download_manager_ = NULL; | 62 download_manager_ = NULL; |
| 56 ui_thread_.message_loop()->RunAllPending(); | 63 ui_thread_.message_loop()->RunAllPending(); |
| 57 } | 64 } |
| 58 | 65 |
| 59 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | 66 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { |
| 60 DownloadCreateInfo info; | 67 DownloadCreateInfo info; |
| 61 info.download_id = kDummyDownloadId + offset; | 68 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); |
| 62 // info.request_handle default constructed to null. | 69 // info.request_handle default constructed to null. |
| 63 info.save_info.file_stream = file_stream_; | 70 info.save_info.file_stream = file_stream_; |
| 64 file->reset( | 71 file->reset( |
| 65 new DownloadFile(&info, DownloadRequestHandle(), download_manager_)); | 72 new DownloadFile(&info, DownloadRequestHandle(), download_manager_)); |
| 66 } | 73 } |
| 67 | 74 |
| 68 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | 75 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { |
| 69 EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); | 76 EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); |
| 70 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); | 77 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); |
| 71 EXPECT_FALSE((*file)->in_progress()); | 78 EXPECT_FALSE((*file)->in_progress()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 97 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; | 104 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; |
| 98 scoped_refptr<DownloadManager> download_manager_; | 105 scoped_refptr<DownloadManager> download_manager_; |
| 99 | 106 |
| 100 linked_ptr<net::FileStream> file_stream_; | 107 linked_ptr<net::FileStream> file_stream_; |
| 101 | 108 |
| 102 // DownloadFile instance we are testing. | 109 // DownloadFile instance we are testing. |
| 103 scoped_ptr<DownloadFile> download_file_; | 110 scoped_ptr<DownloadFile> download_file_; |
| 104 | 111 |
| 105 private: | 112 private: |
| 106 MessageLoop loop_; | 113 MessageLoop loop_; |
| 114 scoped_refptr<DownloadIdFactory> id_factory_; |
| 107 // UI thread. | 115 // UI thread. |
| 108 content::TestBrowserThread ui_thread_; | 116 content::TestBrowserThread ui_thread_; |
| 109 // File thread to satisfy debug checks in DownloadFile. | 117 // File thread to satisfy debug checks in DownloadFile. |
| 110 content::TestBrowserThread file_thread_; | 118 content::TestBrowserThread file_thread_; |
| 111 | 119 |
| 112 // Keep track of what data should be saved to the disk file. | 120 // Keep track of what data should be saved to the disk file. |
| 113 std::string expected_data_; | 121 std::string expected_data_; |
| 114 }; | 122 }; |
| 115 | 123 |
| 116 const char* DownloadFileTest::kTestData1 = | 124 const char* DownloadFileTest::kTestData1 = |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 // Check the files. | 191 // Check the files. |
| 184 EXPECT_FALSE(file_util::PathExists(path_3)); | 192 EXPECT_FALSE(file_util::PathExists(path_3)); |
| 185 EXPECT_TRUE(file_util::PathExists(path_4)); | 193 EXPECT_TRUE(file_util::PathExists(path_4)); |
| 186 | 194 |
| 187 // Check the hash. | 195 // Check the hash. |
| 188 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); | 196 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); |
| 189 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 197 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
| 190 | 198 |
| 191 DestroyDownloadFile(&download_file_, 0); | 199 DestroyDownloadFile(&download_file_, 0); |
| 192 } | 200 } |
| OLD | NEW |