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

Side by Side Diff: chrome/browser/download/base_file_unittest.cc

Issue 6973052: When the download folder does not exist, change the download folder to a user's "Downloads" (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Overrides the user's "Downloads" folder in DownloadPrefs Created 9 years, 6 months 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/scoped_temp_dir.h" 7 #include "base/scoped_temp_dir.h"
8 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
9 #include "chrome/browser/download/base_file.h" 9 #include "chrome/browser/download/base_file.h"
10 #include "content/browser/browser_thread.h" 10 #include "content/browser/browser_thread.h"
11 #include "net/base/file_stream.h" 11 #include "net/base/file_stream.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace { 14 namespace {
15 15
16 const char kTestData1[] = "Let's write some data to the file!\n"; 16 const char kTestData1[] = "Let's write some data to the file!\n";
17 const char kTestData2[] = "Writing more data.\n"; 17 const char kTestData2[] = "Writing more data.\n";
18 const char kTestData3[] = "Final line."; 18 const char kTestData3[] = "Final line.";
19 19
20 class BaseFileTest : public testing::Test { 20 class BaseFileTest : public testing::Test {
21 public: 21 public:
22 BaseFileTest() 22 BaseFileTest()
23 : expect_file_survives_(false), 23 : expect_file_survives_(false),
24 file_thread_(BrowserThread::FILE, &message_loop_) { 24 file_thread_(BrowserThread::FILE, &message_loop_) {
25 } 25 }
26 26
27 virtual void SetUp() { 27 virtual void SetUp() {
28 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 28 ASSERT_TRUE(save_dir_.CreateUniqueTempDir());
29 ASSERT_TRUE(renamed_dir_.CreateUniqueTempDir());
29 base_file_.reset( 30 base_file_.reset(
30 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_)); 31 new BaseFile(FilePath(), GURL(), GURL(), 0, file_stream_));
31 } 32 }
32 33
33 virtual void TearDown() { 34 virtual void TearDown() {
34 EXPECT_FALSE(base_file_->in_progress()); 35 EXPECT_FALSE(base_file_->in_progress());
35 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 36 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
36 base_file_->bytes_so_far()); 37 base_file_->bytes_so_far());
37 38
38 FilePath full_path = base_file_->full_path(); 39 FilePath full_path = base_file_->full_path();
(...skipping 19 matching lines...) Expand all
58 EXPECT_EQ(static_cast<int64>(expected_data_.size()), 59 EXPECT_EQ(static_cast<int64>(expected_data_.size()),
59 base_file_->bytes_so_far()); 60 base_file_->bytes_so_far());
60 } 61 }
61 62
62 protected: 63 protected:
63 linked_ptr<net::FileStream> file_stream_; 64 linked_ptr<net::FileStream> file_stream_;
64 65
65 // BaseClass instance we are testing. 66 // BaseClass instance we are testing.
66 scoped_ptr<BaseFile> base_file_; 67 scoped_ptr<BaseFile> base_file_;
67 68
69 // Temporary directory for downloaded files.
70 ScopedTempDir save_dir_;
71
68 // Temporary directory for renamed downloads. 72 // Temporary directory for renamed downloads.
69 ScopedTempDir temp_dir_; 73 ScopedTempDir renamed_dir_;
70 74
71 // Expect the file to survive deletion of the BaseFile instance. 75 // Expect the file to survive deletion of the BaseFile instance.
72 bool expect_file_survives_; 76 bool expect_file_survives_;
73 77
74 private: 78 private:
75 // Keep track of what data should be saved to the disk file. 79 // Keep track of what data should be saved to the disk file.
76 std::string expected_data_; 80 std::string expected_data_;
77 81
78 // Mock file thread to satisfy debug checks in BaseFile. 82 // Mock file thread to satisfy debug checks in BaseFile.
79 MessageLoop message_loop_; 83 MessageLoop message_loop_;
80 BrowserThread file_thread_; 84 BrowserThread file_thread_;
81 }; 85 };
82 86
83 // Test the most basic scenario: just create the object and do a sanity check 87 // Test the most basic scenario: just create the object and do a sanity check
84 // on all its accessors. This is actually a case that rarely happens 88 // on all its accessors. This is actually a case that rarely happens
85 // in production, where we would at least Initialize it. 89 // in production, where we would at least Initialize it.
86 TEST_F(BaseFileTest, CreateDestroy) { 90 TEST_F(BaseFileTest, CreateDestroy) {
87 EXPECT_EQ(FilePath().value(), base_file_->full_path().value()); 91 EXPECT_EQ(FilePath().value(), base_file_->full_path().value());
88 } 92 }
89 93
90 // Cancel the download explicitly. 94 // Cancel the download explicitly.
91 TEST_F(BaseFileTest, Cancel) { 95 TEST_F(BaseFileTest, Cancel) {
92 ASSERT_TRUE(base_file_->Initialize(false)); 96 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
93 EXPECT_TRUE(file_util::PathExists(base_file_->full_path())); 97 EXPECT_TRUE(file_util::PathExists(base_file_->full_path()));
94 base_file_->Cancel(); 98 base_file_->Cancel();
95 EXPECT_FALSE(file_util::PathExists(base_file_->full_path())); 99 EXPECT_FALSE(file_util::PathExists(base_file_->full_path()));
96 EXPECT_NE(FilePath().value(), base_file_->full_path().value()); 100 EXPECT_NE(FilePath().value(), base_file_->full_path().value());
97 } 101 }
98 102
99 // Write data to the file and detach it, so it doesn't get deleted 103 // Write data to the file and detach it, so it doesn't get deleted
100 // automatically when base_file_ is destructed. 104 // automatically when base_file_ is destructed.
101 TEST_F(BaseFileTest, WriteAndDetach) { 105 TEST_F(BaseFileTest, WriteAndDetach) {
102 ASSERT_TRUE(base_file_->Initialize(false)); 106 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
103 AppendDataToFile(kTestData1); 107 AppendDataToFile(kTestData1);
104 base_file_->Finish(); 108 base_file_->Finish();
105 base_file_->Detach(); 109 base_file_->Detach();
106 expect_file_survives_ = true; 110 expect_file_survives_ = true;
107 } 111 }
108 112
109 // Write data to the file and detach it, and calculate its sha256 hash. 113 // Write data to the file and detach it, and calculate its sha256 hash.
110 TEST_F(BaseFileTest, WriteWithHashAndDetach) { 114 TEST_F(BaseFileTest, WriteWithHashAndDetach) {
111 ASSERT_TRUE(base_file_->Initialize(true)); 115 ASSERT_TRUE(base_file_->Initialize(true, save_dir_.path()));
112 AppendDataToFile(kTestData1); 116 AppendDataToFile(kTestData1);
113 base_file_->Finish(); 117 base_file_->Finish();
114 118
115 std::string hash; 119 std::string hash;
116 base_file_->GetSha256Hash(&hash); 120 base_file_->GetSha256Hash(&hash);
117 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 121 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE",
118 base::HexEncode(hash.data(), hash.size())); 122 base::HexEncode(hash.data(), hash.size()));
119 123
120 base_file_->Detach(); 124 base_file_->Detach();
121 expect_file_survives_ = true; 125 expect_file_survives_ = true;
122 } 126 }
123 127
124 // Rename the file after writing to it, then detach. 128 // Rename the file after writing to it, then detach.
125 TEST_F(BaseFileTest, WriteThenRenameAndDetach) { 129 TEST_F(BaseFileTest, WriteThenRenameAndDetach) {
126 ASSERT_TRUE(base_file_->Initialize(false)); 130 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
127 131
128 FilePath initial_path(base_file_->full_path()); 132 FilePath initial_path(base_file_->full_path());
129 EXPECT_TRUE(file_util::PathExists(initial_path)); 133 EXPECT_TRUE(file_util::PathExists(initial_path));
130 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 134 FilePath new_path(renamed_dir_.path().AppendASCII("NewFile"));
131 EXPECT_FALSE(file_util::PathExists(new_path)); 135 EXPECT_FALSE(file_util::PathExists(new_path));
132 136
133 AppendDataToFile(kTestData1); 137 AppendDataToFile(kTestData1);
134 138
135 EXPECT_TRUE(base_file_->Rename(new_path)); 139 EXPECT_TRUE(base_file_->Rename(new_path));
136 EXPECT_FALSE(file_util::PathExists(initial_path)); 140 EXPECT_FALSE(file_util::PathExists(initial_path));
137 EXPECT_TRUE(file_util::PathExists(new_path)); 141 EXPECT_TRUE(file_util::PathExists(new_path));
138 142
139 base_file_->Finish(); 143 base_file_->Finish();
140 base_file_->Detach(); 144 base_file_->Detach();
141 expect_file_survives_ = true; 145 expect_file_survives_ = true;
142 } 146 }
143 147
144 // Write data to the file once. 148 // Write data to the file once.
145 TEST_F(BaseFileTest, SingleWrite) { 149 TEST_F(BaseFileTest, SingleWrite) {
146 ASSERT_TRUE(base_file_->Initialize(false)); 150 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
147 AppendDataToFile(kTestData1); 151 AppendDataToFile(kTestData1);
148 base_file_->Finish(); 152 base_file_->Finish();
149 } 153 }
150 154
151 // Write data to the file multiple times. 155 // Write data to the file multiple times.
152 TEST_F(BaseFileTest, MultipleWrites) { 156 TEST_F(BaseFileTest, MultipleWrites) {
153 ASSERT_TRUE(base_file_->Initialize(false)); 157 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
154 AppendDataToFile(kTestData1); 158 AppendDataToFile(kTestData1);
155 AppendDataToFile(kTestData2); 159 AppendDataToFile(kTestData2);
156 AppendDataToFile(kTestData3); 160 AppendDataToFile(kTestData3);
157 std::string hash; 161 std::string hash;
158 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 162 EXPECT_FALSE(base_file_->GetSha256Hash(&hash));
159 base_file_->Finish(); 163 base_file_->Finish();
160 } 164 }
161 165
162 // Write data to the file once and calculate its sha256 hash. 166 // Write data to the file once and calculate its sha256 hash.
163 TEST_F(BaseFileTest, SingleWriteWithHash) { 167 TEST_F(BaseFileTest, SingleWriteWithHash) {
164 ASSERT_TRUE(base_file_->Initialize(true)); 168 ASSERT_TRUE(base_file_->Initialize(true, save_dir_.path()));
165 AppendDataToFile(kTestData1); 169 AppendDataToFile(kTestData1);
166 base_file_->Finish(); 170 base_file_->Finish();
167 171
168 std::string hash; 172 std::string hash;
169 base_file_->GetSha256Hash(&hash); 173 base_file_->GetSha256Hash(&hash);
170 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE", 174 EXPECT_EQ("0B2D3F3F7943AD64B860DF94D05CB56A8A97C6EC5768B5B70B930C5AA7FA9ADE",
171 base::HexEncode(hash.data(), hash.size())); 175 base::HexEncode(hash.data(), hash.size()));
172 } 176 }
173 177
174 // Write data to the file multiple times and calculate its sha256 hash. 178 // Write data to the file multiple times and calculate its sha256 hash.
175 TEST_F(BaseFileTest, MultipleWritesWithHash) { 179 TEST_F(BaseFileTest, MultipleWritesWithHash) {
176 std::string hash; 180 std::string hash;
177 181
178 ASSERT_TRUE(base_file_->Initialize(true)); 182 ASSERT_TRUE(base_file_->Initialize(true, save_dir_.path()));
179 AppendDataToFile(kTestData1); 183 AppendDataToFile(kTestData1);
180 AppendDataToFile(kTestData2); 184 AppendDataToFile(kTestData2);
181 AppendDataToFile(kTestData3); 185 AppendDataToFile(kTestData3);
182 // no hash before Finish() is called either. 186 // no hash before Finish() is called either.
183 EXPECT_FALSE(base_file_->GetSha256Hash(&hash)); 187 EXPECT_FALSE(base_file_->GetSha256Hash(&hash));
184 base_file_->Finish(); 188 base_file_->Finish();
185 189
186 EXPECT_TRUE(base_file_->GetSha256Hash(&hash)); 190 EXPECT_TRUE(base_file_->GetSha256Hash(&hash));
187 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8", 191 EXPECT_EQ("CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8",
188 base::HexEncode(hash.data(), hash.size())); 192 base::HexEncode(hash.data(), hash.size()));
189 } 193 }
190 194
191 // Rename the file after all writes to it. 195 // Rename the file after all writes to it.
192 TEST_F(BaseFileTest, WriteThenRename) { 196 TEST_F(BaseFileTest, WriteThenRename) {
193 ASSERT_TRUE(base_file_->Initialize(false)); 197 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
194 198
195 FilePath initial_path(base_file_->full_path()); 199 FilePath initial_path(base_file_->full_path());
196 EXPECT_TRUE(file_util::PathExists(initial_path)); 200 EXPECT_TRUE(file_util::PathExists(initial_path));
197 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 201 FilePath new_path(renamed_dir_.path().AppendASCII("NewFile"));
198 EXPECT_FALSE(file_util::PathExists(new_path)); 202 EXPECT_FALSE(file_util::PathExists(new_path));
199 203
200 AppendDataToFile(kTestData1); 204 AppendDataToFile(kTestData1);
201 205
202 EXPECT_TRUE(base_file_->Rename(new_path)); 206 EXPECT_TRUE(base_file_->Rename(new_path));
203 EXPECT_FALSE(file_util::PathExists(initial_path)); 207 EXPECT_FALSE(file_util::PathExists(initial_path));
204 EXPECT_TRUE(file_util::PathExists(new_path)); 208 EXPECT_TRUE(file_util::PathExists(new_path));
205 209
206 base_file_->Finish(); 210 base_file_->Finish();
207 } 211 }
208 212
209 // Rename the file while the download is still in progress. 213 // Rename the file while the download is still in progress.
210 TEST_F(BaseFileTest, RenameWhileInProgress) { 214 TEST_F(BaseFileTest, RenameWhileInProgress) {
211 ASSERT_TRUE(base_file_->Initialize(false)); 215 ASSERT_TRUE(base_file_->Initialize(false, save_dir_.path()));
212 216
213 FilePath initial_path(base_file_->full_path()); 217 FilePath initial_path(base_file_->full_path());
214 EXPECT_TRUE(file_util::PathExists(initial_path)); 218 EXPECT_TRUE(file_util::PathExists(initial_path));
215 FilePath new_path(temp_dir_.path().AppendASCII("NewFile")); 219 FilePath new_path(renamed_dir_.path().AppendASCII("NewFile"));
216 EXPECT_FALSE(file_util::PathExists(new_path)); 220 EXPECT_FALSE(file_util::PathExists(new_path));
217 221
218 AppendDataToFile(kTestData1); 222 AppendDataToFile(kTestData1);
219 223
220 EXPECT_TRUE(base_file_->in_progress()); 224 EXPECT_TRUE(base_file_->in_progress());
221 EXPECT_TRUE(base_file_->Rename(new_path)); 225 EXPECT_TRUE(base_file_->Rename(new_path));
222 EXPECT_FALSE(file_util::PathExists(initial_path)); 226 EXPECT_FALSE(file_util::PathExists(initial_path));
223 EXPECT_TRUE(file_util::PathExists(new_path)); 227 EXPECT_TRUE(file_util::PathExists(new_path));
224 228
225 AppendDataToFile(kTestData2); 229 AppendDataToFile(kTestData2);
226 230
227 base_file_->Finish(); 231 base_file_->Finish();
228 } 232 }
229 233
230 } // namespace 234 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698