OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/file_util.h" | |
6 #include "base/message_loop.h" | |
7 #include "base/scoped_temp_dir.h" | |
8 #include "base/string_number_conversions.h" | |
9 #include "chrome/browser/download/download_create_info.h" | |
10 #include "chrome/browser/download/download_file.h" | |
11 #include "chrome/browser/download/download_manager.h" | |
12 #include "chrome/browser/download/download_request_handle.h" | |
13 #include "chrome/browser/download/download_status_updater.h" | |
14 #include "chrome/browser/download/download_util.h" | |
15 #include "chrome/browser/download/mock_download_manager.h" | |
16 #include "chrome/browser/download/mock_download_manager_delegate.h" | |
17 #include "content/browser/browser_thread.h" | |
18 #include "net/base/file_stream.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 class DownloadFileTest : public testing::Test { | |
22 public: | |
23 | |
24 static const char* kTestData1; | |
25 static const char* kTestData2; | |
26 static const char* kTestData3; | |
27 static const char* kDataHash; | |
28 static const int32 kDummyDownloadId; | |
29 static const int kDummyChildId; | |
30 static const int kDummyRequestId; | |
31 | |
32 // We need a UI |BrowserThread| in order to destruct |download_manager_|, | |
33 // which has trait |BrowserThread::DeleteOnUIThread|. Without this, | |
34 // calling Release() on |download_manager_| won't ever result in its | |
35 // destructor being called and we get a leak. | |
36 DownloadFileTest() : | |
37 ui_thread_(BrowserThread::UI, &loop_), | |
38 file_thread_(BrowserThread::FILE, &loop_) { | |
39 } | |
40 | |
41 ~DownloadFileTest() { | |
42 } | |
43 | |
44 virtual void SetUp() { | |
45 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
46 download_manager_delegate_.reset(new MockDownloadManagerDelegate()); | |
47 download_manager_ = new MockDownloadManager( | |
48 download_manager_delegate_.get(), &download_status_updater_); | |
49 } | |
50 | |
51 virtual void TearDown() { | |
52 // When a DownloadManager's reference count drops to 0, it is not | |
53 // deleted immediately. Instead, a task is posted to the UI thread's | |
54 // message loop to delete it. | |
55 // So, drop the reference count to 0 and run the message loop once | |
56 // to ensure that all resources are cleaned up before the test exits. | |
57 download_manager_ = NULL; | |
58 ui_thread_.message_loop()->RunAllPending(); | |
59 } | |
60 | |
61 virtual void CreateDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | |
62 DownloadCreateInfo info; | |
63 info.download_id = kDummyDownloadId + offset; | |
64 // info.request_handle default constructed to null. | |
65 info.save_info.file_stream = file_stream_; | |
66 file->reset(new DownloadFile(&info, download_manager_)); | |
67 } | |
68 | |
69 virtual void DestroyDownloadFile(scoped_ptr<DownloadFile>* file, int offset) { | |
70 EXPECT_EQ(kDummyDownloadId + offset, (*file)->id()); | |
71 EXPECT_EQ(download_manager_, (*file)->GetDownloadManager()); | |
72 EXPECT_FALSE((*file)->in_progress()); | |
73 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | |
74 (*file)->bytes_so_far()); | |
75 | |
76 // Make sure the data has been properly written to disk. | |
77 std::string disk_data; | |
78 EXPECT_TRUE(file_util::ReadFileToString((*file)->full_path(), | |
79 &disk_data)); | |
80 EXPECT_EQ(expected_data_, disk_data); | |
81 | |
82 // Make sure the mock BrowserThread outlives the DownloadFile to satisfy | |
83 // thread checks inside it. | |
84 file->reset(); | |
85 } | |
86 | |
87 void AppendDataToFile(scoped_ptr<DownloadFile>* file, | |
88 const std::string& data) { | |
89 EXPECT_TRUE((*file)->in_progress()); | |
90 (*file)->AppendDataToFile(data.data(), data.size()); | |
91 expected_data_ += data; | |
92 EXPECT_EQ(static_cast<int64>(expected_data_.size()), | |
93 (*file)->bytes_so_far()); | |
94 } | |
95 | |
96 protected: | |
97 // Temporary directory for renamed downloads. | |
98 ScopedTempDir temp_dir_; | |
99 | |
100 DownloadStatusUpdater download_status_updater_; | |
101 scoped_ptr<MockDownloadManagerDelegate> download_manager_delegate_; | |
102 scoped_refptr<DownloadManager> download_manager_; | |
103 | |
104 linked_ptr<net::FileStream> file_stream_; | |
105 | |
106 // DownloadFile instance we are testing. | |
107 scoped_ptr<DownloadFile> download_file_; | |
108 | |
109 private: | |
110 MessageLoop loop_; | |
111 // UI thread. | |
112 BrowserThread ui_thread_; | |
113 // File thread to satisfy debug checks in DownloadFile. | |
114 BrowserThread file_thread_; | |
115 | |
116 // Keep track of what data should be saved to the disk file. | |
117 std::string expected_data_; | |
118 }; | |
119 | |
120 const char* DownloadFileTest::kTestData1 = | |
121 "Let's write some data to the file!\n"; | |
122 const char* DownloadFileTest::kTestData2 = "Writing more data.\n"; | |
123 const char* DownloadFileTest::kTestData3 = "Final line."; | |
124 const char* DownloadFileTest::kDataHash = | |
125 "CBF68BF10F8003DB86B31343AFAC8C7175BD03FB5FC905650F8C80AF087443A8"; | |
126 | |
127 const int32 DownloadFileTest::kDummyDownloadId = 23; | |
128 const int DownloadFileTest::kDummyChildId = 3; | |
129 const int DownloadFileTest::kDummyRequestId = 67; | |
130 | |
131 // Rename the file before any data is downloaded, after some has, after it all | |
132 // has, and after it's closed. | |
133 TEST_F(DownloadFileTest, RenameFileFinal) { | |
134 CreateDownloadFile(&download_file_, 0); | |
135 ASSERT_TRUE(download_file_->Initialize(true)); | |
136 FilePath initial_path(download_file_->full_path()); | |
137 EXPECT_TRUE(file_util::PathExists(initial_path)); | |
138 FilePath path_1(initial_path.InsertBeforeExtensionASCII("_1")); | |
139 FilePath path_2(initial_path.InsertBeforeExtensionASCII("_2")); | |
140 FilePath path_3(initial_path.InsertBeforeExtensionASCII("_3")); | |
141 FilePath path_4(initial_path.InsertBeforeExtensionASCII("_4")); | |
142 | |
143 // Rename the file before downloading any data. | |
144 EXPECT_TRUE(download_file_->Rename(path_1)); | |
145 FilePath renamed_path = download_file_->full_path(); | |
146 EXPECT_EQ(path_1, renamed_path); | |
147 | |
148 // Check the files. | |
149 EXPECT_FALSE(file_util::PathExists(initial_path)); | |
150 EXPECT_TRUE(file_util::PathExists(path_1)); | |
151 | |
152 // Download the data. | |
153 AppendDataToFile(&download_file_, kTestData1); | |
154 AppendDataToFile(&download_file_, kTestData2); | |
155 | |
156 // Rename the file after downloading some data. | |
157 EXPECT_TRUE(download_file_->Rename(path_2)); | |
158 renamed_path = download_file_->full_path(); | |
159 EXPECT_EQ(path_2, renamed_path); | |
160 | |
161 // Check the files. | |
162 EXPECT_FALSE(file_util::PathExists(path_1)); | |
163 EXPECT_TRUE(file_util::PathExists(path_2)); | |
164 | |
165 AppendDataToFile(&download_file_, kTestData3); | |
166 | |
167 // Rename the file after downloading all the data. | |
168 EXPECT_TRUE(download_file_->Rename(path_3)); | |
169 renamed_path = download_file_->full_path(); | |
170 EXPECT_EQ(path_3, renamed_path); | |
171 | |
172 // Check the files. | |
173 EXPECT_FALSE(file_util::PathExists(path_2)); | |
174 EXPECT_TRUE(file_util::PathExists(path_3)); | |
175 | |
176 // Should not be able to get the hash until the file is closed. | |
177 std::string hash; | |
178 EXPECT_FALSE(download_file_->GetSha256Hash(&hash)); | |
179 | |
180 download_file_->Finish(); | |
181 | |
182 // Rename the file after downloading all the data and closing the file. | |
183 EXPECT_TRUE(download_file_->Rename(path_4)); | |
184 renamed_path = download_file_->full_path(); | |
185 EXPECT_EQ(path_4, renamed_path); | |
186 | |
187 // Check the files. | |
188 EXPECT_FALSE(file_util::PathExists(path_3)); | |
189 EXPECT_TRUE(file_util::PathExists(path_4)); | |
190 | |
191 // Check the hash. | |
192 EXPECT_TRUE(download_file_->GetSha256Hash(&hash)); | |
193 EXPECT_EQ(kDataHash, base::HexEncode(hash.data(), hash.size())); | |
194 | |
195 DestroyDownloadFile(&download_file_, 0); | |
196 } | |
OLD | NEW |