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 "content/browser/download/download_file_manager.h" | 5 #include "content/browser/download/download_file_manager.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
12 #include "content/browser/browser_thread_impl.h" | 12 #include "content/browser/browser_thread_impl.h" |
13 #include "content/browser/download/byte_stream.h" | 13 #include "content/browser/download/byte_stream.h" |
14 #include "content/browser/download/download_create_info.h" | 14 #include "content/browser/download/download_create_info.h" |
15 #include "content/browser/download/download_interrupt_reasons_impl.h" | 15 #include "content/browser/download/download_interrupt_reasons_impl.h" |
| 16 #include "content/browser/download/download_file_factory.h" |
16 #include "content/browser/download/download_request_handle.h" | 17 #include "content/browser/download/download_request_handle.h" |
17 #include "content/browser/download/mock_download_file.h" | 18 #include "content/browser/download/mock_download_file.h" |
18 #include "content/public/browser/download_id.h" | 19 #include "content/public/browser/download_id.h" |
19 #include "content/public/test/mock_download_manager.h" | 20 #include "content/public/test/mock_download_manager.h" |
20 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
21 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 using content::BrowserThread; | 26 using content::BrowserThread; |
(...skipping 15 matching lines...) Expand all Loading... |
41 class TestDownloadManager : public MockDownloadManager { | 42 class TestDownloadManager : public MockDownloadManager { |
42 public: | 43 public: |
43 MOCK_METHOD3(OnDownloadRenamed, | 44 MOCK_METHOD3(OnDownloadRenamed, |
44 void(int download_id, | 45 void(int download_id, |
45 content::DownloadInterruptReason reason, | 46 content::DownloadInterruptReason reason, |
46 const FilePath& full_path)); | 47 const FilePath& full_path)); |
47 private: | 48 private: |
48 ~TestDownloadManager() {} | 49 ~TestDownloadManager() {} |
49 }; | 50 }; |
50 | 51 |
51 class MockDownloadFileFactory : | 52 class MockDownloadFileFactory : public content::DownloadFileFactory { |
52 public DownloadFileManager::DownloadFileFactory { | |
53 | 53 |
54 public: | 54 public: |
55 MockDownloadFileFactory() {} | 55 MockDownloadFileFactory() {} |
56 virtual ~MockDownloadFileFactory() {} | 56 virtual ~MockDownloadFileFactory() {} |
57 | 57 |
58 virtual content::DownloadFile* CreateFile( | 58 virtual content::DownloadFile* CreateFile( |
59 DownloadCreateInfo* info, | 59 DownloadCreateInfo* info, |
60 scoped_ptr<content::ByteStreamReader> stream, | 60 scoped_ptr<content::ByteStreamReader> stream, |
61 content::DownloadManager* download_manager, | 61 content::DownloadManager* download_manager, |
62 bool calculate_hash, | 62 bool calculate_hash, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 Mock::VerifyAndClearExpectations(file); | 177 Mock::VerifyAndClearExpectations(file); |
178 Mock::VerifyAndClearExpectations(download_manager_); | 178 Mock::VerifyAndClearExpectations(download_manager_); |
179 } | 179 } |
180 | 180 |
181 void OnDownloadFileCreated(content::DownloadInterruptReason reason) { | 181 void OnDownloadFileCreated(content::DownloadInterruptReason reason) { |
182 last_reason_ = reason; | 182 last_reason_ = reason; |
183 } | 183 } |
184 | 184 |
185 // Create a download item on the DFM. | 185 // Create a download item on the DFM. |
186 // |info| is the information needed to create a new download file. | 186 // |info| is the information needed to create a new download file. |
187 // |id| is the download ID of the new download file. | |
188 void CreateDownloadFile(scoped_ptr<DownloadCreateInfo> info) { | 187 void CreateDownloadFile(scoped_ptr<DownloadCreateInfo> info) { |
| 188 // Anything that isn't DOWNLOAD_INTERRUPT_REASON_NONE. |
| 189 last_reason_ = content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED; |
| 190 |
189 // Mostly null out args; they'll be passed to MockDownloadFileFactory | 191 // Mostly null out args; they'll be passed to MockDownloadFileFactory |
190 // to be ignored anyway. | 192 // to be ignored anyway. |
191 download_file_manager_->CreateDownloadFile( | 193 download_file_manager_->CreateDownloadFile( |
192 info.Pass(), scoped_ptr<content::ByteStreamReader>(), | 194 info.Pass(), scoped_ptr<content::ByteStreamReader>(), |
193 download_manager_, true, net::BoundNetLog(), | 195 download_manager_, true, net::BoundNetLog(), |
194 base::Bind(&DownloadFileManagerTest::OnDownloadFileCreated, | 196 base::Bind(&DownloadFileManagerTest::OnDownloadFileCreated, |
195 // The test jig will outlive all download files. | 197 // The test jig will outlive all download files. |
196 base::Unretained(this))); | 198 base::Unretained(this))); |
197 | 199 |
198 // Anything that isn't DOWNLOAD_INTERRUPT_REASON_NONE. | |
199 last_reason_ = content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED; | |
200 ProcessAllPendingMessages(); | 200 ProcessAllPendingMessages(); |
201 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, last_reason_); | 201 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, last_reason_); |
202 } | 202 } |
203 | 203 |
204 // Renames the download file. | 204 // Renames the download file. |
205 // |id| is the download ID of the download file. | 205 // |id| is the download ID of the download file. |
206 // |new_path| is the new file path. | 206 // |new_path| is the new file path. |
207 // |unique_path| is the actual path that the download file will be | 207 // |unique_path| is the actual path that the download file will be |
208 // renamed to. If there is an existing file at | 208 // renamed to. If there is an existing file at |
209 // |new_path| and |replace| is false, then |new_path| | 209 // |new_path| and |replace| is false, then |new_path| |
(...skipping 20 matching lines...) Expand all Loading... |
230 download_file_manager_->RenameDownloadFile( | 230 download_file_manager_->RenameDownloadFile( |
231 id, new_path, should_overwrite, passed_callback); | 231 id, new_path, should_overwrite, passed_callback); |
232 | 232 |
233 EXPECT_TRUE(rename_callback.Equals(passed_callback)); | 233 EXPECT_TRUE(rename_callback.Equals(passed_callback)); |
234 } | 234 } |
235 | 235 |
236 void Complete(DownloadId id) { | 236 void Complete(DownloadId id) { |
237 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 237 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
238 ASSERT_TRUE(file != NULL); | 238 ASSERT_TRUE(file != NULL); |
239 | 239 |
240 EXPECT_CALL(*file, AnnotateWithSourceInformation()) | 240 EXPECT_CALL(*file, Detach(_)) |
241 .WillOnce(Return()); | |
242 EXPECT_CALL(*file, Detach()) | |
243 .WillOnce(Return()); | 241 .WillOnce(Return()); |
244 int num_downloads = download_file_manager_->NumberOfActiveDownloads(); | 242 int num_downloads = download_file_manager_->NumberOfActiveDownloads(); |
245 EXPECT_LT(0, num_downloads); | 243 EXPECT_LT(0, num_downloads); |
246 download_file_manager_->CompleteDownload(id, base::Bind(NullCallback)); | 244 download_file_manager_->CompleteDownload(id, base::Bind(NullCallback)); |
247 EXPECT_EQ(num_downloads - 1, | 245 EXPECT_EQ(num_downloads - 1, |
248 download_file_manager_->NumberOfActiveDownloads()); | 246 download_file_manager_->NumberOfActiveDownloads()); |
249 EXPECT_EQ(NULL, download_file_manager_->GetDownloadFile(id)); | 247 EXPECT_EQ(NULL, download_file_manager_->GetDownloadFile(id)); |
250 } | 248 } |
251 | 249 |
252 void CleanUp(DownloadId id) { | 250 void CleanUp(DownloadId id) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 | 402 |
405 CleanUp(dummy_id); | 403 CleanUp(dummy_id); |
406 } | 404 } |
407 | 405 |
408 // TODO(ahendrickson) -- A test for download manager shutdown. | 406 // TODO(ahendrickson) -- A test for download manager shutdown. |
409 // Expected call sequence: | 407 // Expected call sequence: |
410 // OnDownloadManagerShutdown | 408 // OnDownloadManagerShutdown |
411 // DownloadFile::GetDownloadManager | 409 // DownloadFile::GetDownloadManager |
412 // DownloadFile::CancelDownloadRequest | 410 // DownloadFile::CancelDownloadRequest |
413 // DownloadFile::~DownloadFile | 411 // DownloadFile::~DownloadFile |
OLD | NEW |