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/browser_thread.h" | 8 #include "content/browser/browser_thread.h" |
9 #include "content/browser/download/download_create_info.h" | 9 #include "content/browser/download/download_create_info.h" |
10 #include "content/browser/download/download_file.h" | 10 #include "content/browser/download/download_file.h" |
| 11 #include "content/browser/download/download_id.h" |
| 12 #include "content/browser/download/download_id_factory.h" |
11 #include "content/browser/download/download_manager.h" | 13 #include "content/browser/download/download_manager.h" |
12 #include "content/browser/download/download_request_handle.h" | 14 #include "content/browser/download/download_request_handle.h" |
13 #include "content/browser/download/download_status_updater.h" | 15 #include "content/browser/download/download_status_updater.h" |
14 #include "content/browser/download/mock_download_manager.h" | 16 #include "content/browser/download/mock_download_manager.h" |
15 #include "content/browser/download/mock_download_manager_delegate.h" | 17 #include "content/browser/download/mock_download_manager_delegate.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(new DownloadFile(&info, download_manager_)); | 71 file->reset(new DownloadFile(&info, download_manager_)); |
65 } | 72 } |
66 | 73 |
67 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | 74 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { |
68 EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); | 75 EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); |
69 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); | 76 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); |
70 EXPECT_FALSE((*file)->in_progress()); | 77 EXPECT_FALSE((*file)->in_progress()); |
71 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | 78 EXPECT_EQ(static_cast<int64>(expected_data_.size()), |
(...skipping 24 matching lines...) Expand all Loading... |
96 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; | 103 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; |
97 scoped_refptr<DownloadManager> download_manager_; | 104 scoped_refptr<DownloadManager> download_manager_; |
98 | 105 |
99 linked_ptr<net::FileStream> file_stream_; | 106 linked_ptr<net::FileStream> file_stream_; |
100 | 107 |
101 // DownloadFile instance we are testing. | 108 // DownloadFile instance we are testing. |
102 scoped_ptr<DownloadFile> download_file_; | 109 scoped_ptr<DownloadFile> download_file_; |
103 | 110 |
104 private: | 111 private: |
105 MessageLoop loop_; | 112 MessageLoop loop_; |
| 113 scoped_refptr<DownloadIdFactory> id_factory_; |
106 // UI thread. | 114 // UI thread. |
107 BrowserThread ui_thread_; | 115 BrowserThread ui_thread_; |
108 // File thread to satisfy debug checks in DownloadFile. | 116 // File thread to satisfy debug checks in DownloadFile. |
109 BrowserThread file_thread_; | 117 BrowserThread file_thread_; |
110 | 118 |
111 // Keep track of what data should be saved to the disk file. | 119 // Keep track of what data should be saved to the disk file. |
112 std::string expected_data_; | 120 std::string expected_data_; |
113 }; | 121 }; |
114 | 122 |
115 const char* DownloadFileTest::kTestData1 = | 123 const char* DownloadFileTest::kTestData1 = |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // Check the files. | 190 // Check the files. |
183 EXPECT_FALSE(file_util::PathExists(path_3)); | 191 EXPECT_FALSE(file_util::PathExists(path_3)); |
184 EXPECT_TRUE(file_util::PathExists(path_4)); | 192 EXPECT_TRUE(file_util::PathExists(path_4)); |
185 | 193 |
186 // Check the hash. | 194 // Check the hash. |
187 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); | 195 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); |
188 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | 196 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); |
189 | 197 |
190 DestroyDownloadFile(&download_file_, 0); | 198 DestroyDownloadFile(&download_file_, 0); |
191 } | 199 } |
OLD | NEW |