Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: content/browser/download/download_file_unittest.cc

Issue 8372034: Created an interface for DownloadFile, for use in unit tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed typo Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_impl.h" 8 #include "content/browser/browser_thread_impl.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_impl.h"
11 #include "content/browser/download/download_id.h" 11 #include "content/browser/download/download_id.h"
12 #include "content/browser/download/download_id_factory.h" 12 #include "content/browser/download/download_id_factory.h"
13 #include "content/browser/download/download_manager.h" 13 #include "content/browser/download/download_manager.h"
14 #include "content/browser/download/download_request_handle.h" 14 #include "content/browser/download/download_request_handle.h"
15 #include "content/browser/download/download_status_updater.h" 15 #include "content/browser/download/download_status_updater.h"
16 #include "content/browser/download/mock_download_manager.h" 16 #include "content/browser/download/mock_download_manager.h"
17 #include "content/browser/download/mock_download_manager_delegate.h" 17 #include "content/browser/download/mock_download_manager_delegate.h"
18 #include "net/base/file_stream.h" 18 #include "net/base/file_stream.h"
19 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 download_manager_ = NULL; 65 download_manager_ = NULL;
66 ui_thread_.message_loop()->RunAllPending(); 66 ui_thread_.message_loop()->RunAllPending();
67 } 67 }
68 68
69 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { 69 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) {
70 DownloadCreateInfo info; 70 DownloadCreateInfo info;
71 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset); 71 info.download_id = DownloadId(kValidIdDomain, kDummyDownloadId + offset);
72 // info.request_handle default constructed to null. 72 // info.request_handle default constructed to null.
73 info.save_info.file_stream = file_stream_; 73 info.save_info.file_stream = file_stream_;
74 file->reset( 74 file->reset(
75 new DownloadFile(&info, new DownloadRequestHandle(), 75 new DownloadFileImpl(&info, new DownloadRequestHandle(),
76 download_manager_)); 76 download_manager_));
77 } 77 }
78 78
79 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { 79 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) {
80 EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); 80 EXPECT_EQ(kDummyDownloadId + offset, (*file)->Id());
81 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); 81 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager());
82 EXPECT_FALSE((*file)->in_progress()); 82 EXPECT_FALSE((*file)->InProgress());
83 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 83 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
84 (*file)->bytes_so_far()); 84 (*file)->BytesSoFar());
85 85
86 // Make sure the data has been properly written to disk. 86 // Make sure the data has been properly written to disk.
87 std::string disk_data; 87 std::string disk_data;
88 EXPECT_TRUE(file_util::ReadFileToString((*file)->full_path(), 88 EXPECT_TRUE(file_util::ReadFileToString((*file)->FullPath(),
89 &disk_data)); 89 &disk_data));
90 EXPECT_EQ(expected_data_, disk_data); 90 EXPECT_EQ(expected_data_, disk_data);
91 91
92 // Make sure the Browser and File threads outlive the DownloadFile 92 // Make sure the Browser and File threads outlive the DownloadFile
93 // to satisfy thread checks inside it. 93 // to satisfy thread checks inside it.
94 file->reset(); 94 file->reset();
95 } 95 }
96 96
97 void AppendDataToFile(scoped_ptr<DownloadFile>* file, 97 void AppendDataToFile(scoped_ptr<DownloadFile>* file,
98 const std::string& data) { 98 const std::string& data) {
99 EXPECT_TRUE((*file)->in_progress()); 99 EXPECT_TRUE((*file)->InProgress());
100 (*file)->AppendDataToFile(data.data(), data.size()); 100 (*file)->AppendDataToFile(data.data(), data.size());
101 expected_data_ += data; 101 expected_data_ += data;
102 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 102 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
103 (*file)->bytes_so_far()); 103 (*file)->BytesSoFar());
104 } 104 }
105 105
106 protected: 106 protected:
107 DownloadStatusUpdater download_status_updater_; 107 DownloadStatusUpdater download_status_updater_;
108 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; 108 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_;
109 scoped_refptr<DownloadManager> download_manager_; 109 scoped_refptr<DownloadManager> download_manager_;
110 110
111 linked_ptr<net::FileStream> file_stream_; 111 linked_ptr<net::FileStream> file_stream_;
112 112
113 // DownloadFile instance we are testing. 113 // DownloadFile instance we are testing.
(...skipping 20 matching lines...) Expand all
134 134
135 const int32 DownloadFileTest::kDummyDownloadId = 23; 135 const int32 DownloadFileTest::kDummyDownloadId = 23;
136 const int DownloadFileTest::kDummyChildId = 3; 136 const int DownloadFileTest::kDummyChildId = 3;
137 const int DownloadFileTest::kDummyRequestId = 67; 137 const int DownloadFileTest::kDummyRequestId = 67;
138 138
139 // Rename the file before any data is downloaded, after some has, after it all 139 // Rename the file before any data is downloaded, after some has, after it all
140 // has, and after it's closed. 140 // has, and after it's closed.
141 TEST_F(DownloadFileTest, RenameFileFinal) { 141 TEST_F(DownloadFileTest, RenameFileFinal) {
142 CreateDownloadFile(&download_file_, 0); 142 CreateDownloadFile(&download_file_, 0);
143 ASSERT_EQ(net::OK, download_file_->Initialize(true)); 143 ASSERT_EQ(net::OK, download_file_->Initialize(true));
144 FilePath initial_path(download_file_->full_path()); 144 FilePath initial_path(download_file_->FullPath());
145 EXPECT_TRUE(file_util::PathExists(initial_path)); 145 EXPECT_TRUE(file_util::PathExists(initial_path));
146 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); 146 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1"));
147 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); 147 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2"));
148 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); 148 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3"));
149 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); 149 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4"));
150 150
151 // Rename the file before downloading any data. 151 // Rename the file before downloading any data.
152 EXPECT_EQ(net::OK, download_file_->Rename(path_1)); 152 EXPECT_EQ(net::OK, download_file_->Rename(path_1));
153 FilePath renamed_path = download_file_->full_path(); 153 FilePath renamed_path = download_file_->FullPath();
154 EXPECT_EQ(path_1, renamed_path); 154 EXPECT_EQ(path_1, renamed_path);
155 155
156 // Check the files. 156 // Check the files.
157 EXPECT_FALSE(file_util::PathExists(initial_path)); 157 EXPECT_FALSE(file_util::PathExists(initial_path));
158 EXPECT_TRUE(file_util::PathExists(path_1)); 158 EXPECT_TRUE(file_util::PathExists(path_1));
159 159
160 // Download the data. 160 // Download the data.
161 AppendDataToFile(&download_file_, kTestData1); 161 AppendDataToFile(&download_file_, kTestData1);
162 AppendDataToFile(&download_file_, kTestData2); 162 AppendDataToFile(&download_file_, kTestData2);
163 163
164 // Rename the file after downloading some data. 164 // Rename the file after downloading some data.
165 EXPECT_EQ(net::OK, download_file_->Rename(path_2)); 165 EXPECT_EQ(net::OK, download_file_->Rename(path_2));
166 renamed_path = download_file_->full_path(); 166 renamed_path = download_file_->FullPath();
167 EXPECT_EQ(path_2, renamed_path); 167 EXPECT_EQ(path_2, renamed_path);
168 168
169 // Check the files. 169 // Check the files.
170 EXPECT_FALSE(file_util::PathExists(path_1)); 170 EXPECT_FALSE(file_util::PathExists(path_1));
171 EXPECT_TRUE(file_util::PathExists(path_2)); 171 EXPECT_TRUE(file_util::PathExists(path_2));
172 172
173 AppendDataToFile(&download_file_, kTestData3); 173 AppendDataToFile(&download_file_, kTestData3);
174 174
175 // Rename the file after downloading all the data. 175 // Rename the file after downloading all the data.
176 EXPECT_EQ(net::OK, download_file_->Rename(path_3)); 176 EXPECT_EQ(net::OK, download_file_->Rename(path_3));
177 renamed_path = download_file_->full_path(); 177 renamed_path = download_file_->FullPath();
178 EXPECT_EQ(path_3, renamed_path); 178 EXPECT_EQ(path_3, renamed_path);
179 179
180 // Check the files. 180 // Check the files.
181 EXPECT_FALSE(file_util::PathExists(path_2)); 181 EXPECT_FALSE(file_util::PathExists(path_2));
182 EXPECT_TRUE(file_util::PathExists(path_3)); 182 EXPECT_TRUE(file_util::PathExists(path_3));
183 183
184 // Should not be able to get the hash until the file is closed. 184 // Should not be able to get the hash until the file is closed.
185 std::string hash; 185 std::string hash;
186 EXPECT_FALSE(download_file_->GetSha256Hash(&hash)); 186 EXPECT_FALSE(download_file_->GetSha256Hash(&hash));
187 187
188 download_file_->Finish(); 188 download_file_->Finish();
189 189
190 // Rename the file after downloading all the data and closing the file. 190 // Rename the file after downloading all the data and closing the file.
191 EXPECT_EQ(net::OK, download_file_->Rename(path_4)); 191 EXPECT_EQ(net::OK, download_file_->Rename(path_4));
192 renamed_path = download_file_->full_path(); 192 renamed_path = download_file_->FullPath();
193 EXPECT_EQ(path_4, renamed_path); 193 EXPECT_EQ(path_4, renamed_path);
194 194
195 // Check the files. 195 // Check the files.
196 EXPECT_FALSE(file_util::PathExists(path_3)); 196 EXPECT_FALSE(file_util::PathExists(path_3));
197 EXPECT_TRUE(file_util::PathExists(path_4)); 197 EXPECT_TRUE(file_util::PathExists(path_4));
198 198
199 // Check the hash. 199 // Check the hash.
200 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); 200 EXPECT_TRUE(download_file_->GetSha256Hash(&hash));
201 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); 201 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size()));
202 202
203 DestroyDownloadFile(&download_file_, 0); 203 DestroyDownloadFile(&download_file_, 0);
204 } 204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698