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. | 187 // |id| is the download ID of the new download file. |
asanka
2012/09/20 18:42:15
Nit: No |id| parameter anymore.
Randy Smith (Not in Mondays)
2012/09/21 21:58:01
Done.
| |
188 void CreateDownloadFile(scoped_ptr<DownloadCreateInfo> info) { | 188 void CreateDownloadFile(scoped_ptr<DownloadCreateInfo> info) { |
189 // Anything that isn't DOWNLOAD_INTERRUPT_REASON_NONE. | |
190 last_reason_ = content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED; | |
191 | |
189 // Mostly null out args; they'll be passed to MockDownloadFileFactory | 192 // Mostly null out args; they'll be passed to MockDownloadFileFactory |
190 // to be ignored anyway. | 193 // to be ignored anyway. |
191 download_file_manager_->CreateDownloadFile( | 194 download_file_manager_->CreateDownloadFile( |
192 info.Pass(), scoped_ptr<content::ByteStreamReader>(), | 195 info.Pass(), scoped_ptr<content::ByteStreamReader>(), |
193 download_manager_, true, net::BoundNetLog(), | 196 download_manager_, true, net::BoundNetLog(), |
194 base::Bind(&DownloadFileManagerTest::OnDownloadFileCreated, | 197 base::Bind(&DownloadFileManagerTest::OnDownloadFileCreated, |
195 // The test jig will outlive all download files. | 198 // The test jig will outlive all download files. |
196 base::Unretained(this))); | 199 base::Unretained(this))); |
197 | 200 |
198 // Anything that isn't DOWNLOAD_INTERRUPT_REASON_NONE. | |
199 last_reason_ = content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED; | |
200 ProcessAllPendingMessages(); | 201 ProcessAllPendingMessages(); |
201 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, last_reason_); | 202 EXPECT_EQ(content::DOWNLOAD_INTERRUPT_REASON_NONE, last_reason_); |
202 } | 203 } |
203 | 204 |
204 // Renames the download file. | 205 // Renames the download file. |
205 // |id| is the download ID of the download file. | 206 // |id| is the download ID of the download file. |
206 // |new_path| is the new file path. | 207 // |new_path| is the new file path. |
207 // |unique_path| is the actual path that the download file will be | 208 // |unique_path| is the actual path that the download file will be |
208 // renamed to. If there is an existing file at | 209 // renamed to. If there is an existing file at |
209 // |new_path| and |replace| is false, then |new_path| | 210 // |new_path| and |replace| is false, then |new_path| |
(...skipping 22 matching lines...) Expand all Loading... | |
232 | 233 |
233 EXPECT_TRUE(rename_callback.Equals(passed_callback)); | 234 EXPECT_TRUE(rename_callback.Equals(passed_callback)); |
234 } | 235 } |
235 | 236 |
236 void Complete(DownloadId id) { | 237 void Complete(DownloadId id) { |
237 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); | 238 MockDownloadFile* file = download_file_factory_->GetExistingFile(id); |
238 ASSERT_TRUE(file != NULL); | 239 ASSERT_TRUE(file != NULL); |
239 | 240 |
240 EXPECT_CALL(*file, AnnotateWithSourceInformation()) | 241 EXPECT_CALL(*file, AnnotateWithSourceInformation()) |
241 .WillOnce(Return()); | 242 .WillOnce(Return()); |
242 EXPECT_CALL(*file, Detach()) | 243 EXPECT_CALL(*file, Detach(_)) |
243 .WillOnce(Return()); | 244 .WillOnce(Return()); |
244 int num_downloads = download_file_manager_->NumberOfActiveDownloads(); | 245 int num_downloads = download_file_manager_->NumberOfActiveDownloads(); |
245 EXPECT_LT(0, num_downloads); | 246 EXPECT_LT(0, num_downloads); |
246 download_file_manager_->CompleteDownload(id, base::Bind(NullCallback)); | 247 download_file_manager_->CompleteDownload(id, base::Bind(NullCallback)); |
247 EXPECT_EQ(num_downloads - 1, | 248 EXPECT_EQ(num_downloads - 1, |
248 download_file_manager_->NumberOfActiveDownloads()); | 249 download_file_manager_->NumberOfActiveDownloads()); |
249 EXPECT_EQ(NULL, download_file_manager_->GetDownloadFile(id)); | 250 EXPECT_EQ(NULL, download_file_manager_->GetDownloadFile(id)); |
250 } | 251 } |
251 | 252 |
252 void CleanUp(DownloadId id) { | 253 void CleanUp(DownloadId id) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 | 405 |
405 CleanUp(dummy_id); | 406 CleanUp(dummy_id); |
406 } | 407 } |
407 | 408 |
408 // TODO(ahendrickson) -- A test for download manager shutdown. | 409 // TODO(ahendrickson) -- A test for download manager shutdown. |
409 // Expected call sequence: | 410 // Expected call sequence: |
410 // OnDownloadManagerShutdown | 411 // OnDownloadManagerShutdown |
411 // DownloadFile::GetDownloadManager | 412 // DownloadFile::GetDownloadManager |
412 // DownloadFile::CancelDownloadRequest | 413 // DownloadFile::CancelDownloadRequest |
413 // DownloadFile::~DownloadFile | 414 // DownloadFile::~DownloadFile |
OLD | NEW |