OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "components/offline_pages/archive_manager.h" | |
6 | |
7 #include <algorithm> | |
8 #include <memory> | |
9 #include <set> | |
10 #include <vector> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/files/file_path.h" | |
14 #include "base/files/file_util.h" | |
15 #include "base/files/scoped_temp_dir.h" | |
16 #include "base/test/test_simple_task_runner.h" | |
17 #include "base/threading/thread_task_runner_handle.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 namespace offline_pages { | |
21 | |
22 namespace { | |
23 const base::FilePath::CharType kMissingArchivePath[] = FILE_PATH_LITERAL( | |
24 "missing_archive.path"); | |
25 } // namespace | |
26 | |
27 enum class CallbackStatus { | |
28 NOT_CALLED, | |
29 CALLED_FALSE, | |
30 CALLED_TRUE, | |
31 }; | |
32 | |
33 class ArchiveManagerTest : public testing::Test { | |
34 public: | |
35 ArchiveManagerTest(); | |
36 void SetUp() override; | |
37 | |
38 void PumpLoop(); | |
39 void ResetResults(); | |
40 | |
41 void ResetManager(const base::FilePath& file_path); | |
42 void Callback(bool result); | |
43 void GetAllArchivesCallback(const std::set<base::FilePath>& archive_paths); | |
44 void GetStorageStatsCallback( | |
45 const ArchiveManager::StorageStats& storage_sizes); | |
46 | |
47 ArchiveManager* manager() { return manager_.get(); } | |
48 const base::FilePath& temp_path() const { return temp_dir_.GetPath(); } | |
49 CallbackStatus callback_status() const { return callback_status_; } | |
50 const std::set<base::FilePath>& last_archive_paths() const { | |
51 return last_archvie_paths_; | |
52 } | |
53 ArchiveManager::StorageStats last_storage_sizes() const { | |
54 return last_storage_sizes_; | |
55 } | |
56 | |
57 private: | |
58 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
59 base::ThreadTaskRunnerHandle task_runner_handle_; | |
60 base::ScopedTempDir temp_dir_; | |
61 | |
62 std::unique_ptr<ArchiveManager> manager_; | |
63 CallbackStatus callback_status_; | |
64 std::set<base::FilePath> last_archvie_paths_; | |
65 ArchiveManager::StorageStats last_storage_sizes_; | |
66 }; | |
67 | |
68 ArchiveManagerTest::ArchiveManagerTest() | |
69 : task_runner_(new base::TestSimpleTaskRunner), | |
70 task_runner_handle_(task_runner_), | |
71 callback_status_(CallbackStatus::NOT_CALLED), | |
72 last_storage_sizes_({0, 0}) {} | |
73 | |
74 void ArchiveManagerTest::SetUp() { | |
75 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
76 ResetManager(temp_dir_.GetPath()); | |
77 } | |
78 | |
79 void ArchiveManagerTest::PumpLoop() { | |
80 task_runner_->RunUntilIdle(); | |
81 } | |
82 | |
83 void ArchiveManagerTest::ResetResults() { | |
84 callback_status_ = CallbackStatus::NOT_CALLED; | |
85 last_archvie_paths_.clear(); | |
86 } | |
87 | |
88 void ArchiveManagerTest::ResetManager(const base::FilePath& file_path) { | |
89 manager_.reset( | |
90 new ArchiveManager(file_path, base::ThreadTaskRunnerHandle::Get())); | |
91 } | |
92 | |
93 void ArchiveManagerTest::Callback(bool result) { | |
94 callback_status_ = | |
95 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; | |
96 } | |
97 | |
98 void ArchiveManagerTest::GetAllArchivesCallback( | |
99 const std::set<base::FilePath>& archive_paths) { | |
100 last_archvie_paths_ = archive_paths; | |
101 } | |
102 | |
103 void ArchiveManagerTest::GetStorageStatsCallback( | |
104 const ArchiveManager::StorageStats& storage_sizes) { | |
105 last_storage_sizes_ = storage_sizes; | |
106 } | |
107 | |
108 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { | |
109 base::FilePath archive_dir = | |
110 temp_path().Append(FILE_PATH_LITERAL("test_path")); | |
111 ResetManager(archive_dir); | |
112 EXPECT_FALSE(base::PathExists(archive_dir)); | |
113 | |
114 // Ensure archives dir exists, when it doesn't. | |
115 manager()->EnsureArchivesDirCreated( | |
116 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | |
117 PumpLoop(); | |
118 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
119 EXPECT_TRUE(base::PathExists(archive_dir)); | |
120 | |
121 // Try again when the file already exists. | |
122 ResetResults(); | |
123 manager()->EnsureArchivesDirCreated( | |
124 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | |
125 PumpLoop(); | |
126 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
127 EXPECT_TRUE(base::PathExists(archive_dir)); | |
128 } | |
129 | |
130 TEST_F(ArchiveManagerTest, ExistsArchive) { | |
131 base::FilePath archive_path = temp_path().Append(kMissingArchivePath); | |
132 manager()->ExistsArchive( | |
133 archive_path, | |
134 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
135 PumpLoop(); | |
136 EXPECT_EQ(CallbackStatus::CALLED_FALSE, callback_status()); | |
137 | |
138 ResetResults(); | |
139 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path)); | |
140 | |
141 manager()->ExistsArchive( | |
142 archive_path, | |
143 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
144 PumpLoop(); | |
145 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
146 } | |
147 | |
148 TEST_F(ArchiveManagerTest, DeleteMultipleArchives) { | |
149 base::FilePath archive_path_1; | |
150 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); | |
151 base::FilePath archive_path_2; | |
152 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); | |
153 base::FilePath archive_path_3; | |
154 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); | |
155 | |
156 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2}; | |
157 | |
158 manager()->DeleteMultipleArchives( | |
159 archive_paths, | |
160 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
161 PumpLoop(); | |
162 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
163 EXPECT_FALSE(base::PathExists(archive_path_1)); | |
164 EXPECT_FALSE(base::PathExists(archive_path_2)); | |
165 EXPECT_TRUE(base::PathExists(archive_path_3)); | |
166 } | |
167 | |
168 TEST_F(ArchiveManagerTest, DeleteMultipleArchivesSomeDoNotExist) { | |
169 base::FilePath archive_path_1 = temp_path().Append(kMissingArchivePath); | |
170 base::FilePath archive_path_2; | |
171 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); | |
172 base::FilePath archive_path_3; | |
173 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); | |
174 | |
175 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2}; | |
176 | |
177 EXPECT_FALSE(base::PathExists(archive_path_1)); | |
178 | |
179 manager()->DeleteMultipleArchives( | |
180 archive_paths, | |
181 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
182 PumpLoop(); | |
183 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
184 EXPECT_FALSE(base::PathExists(archive_path_1)); | |
185 EXPECT_FALSE(base::PathExists(archive_path_2)); | |
186 EXPECT_TRUE(base::PathExists(archive_path_3)); | |
187 } | |
188 | |
189 TEST_F(ArchiveManagerTest, DeleteMultipleArchivesNoneExist) { | |
190 base::FilePath archive_path_1 = temp_path().Append(kMissingArchivePath); | |
191 base::FilePath archive_path_2 = temp_path().Append(FILE_PATH_LITERAL( | |
192 "other_missing_file.mhtml")); | |
193 base::FilePath archive_path_3; | |
194 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); | |
195 | |
196 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2}; | |
197 | |
198 EXPECT_FALSE(base::PathExists(archive_path_1)); | |
199 EXPECT_FALSE(base::PathExists(archive_path_2)); | |
200 | |
201 manager()->DeleteMultipleArchives( | |
202 archive_paths, | |
203 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
204 PumpLoop(); | |
205 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
206 EXPECT_FALSE(base::PathExists(archive_path_1)); | |
207 EXPECT_FALSE(base::PathExists(archive_path_2)); | |
208 EXPECT_TRUE(base::PathExists(archive_path_3)); | |
209 } | |
210 | |
211 TEST_F(ArchiveManagerTest, DeleteArchive) { | |
212 base::FilePath archive_path; | |
213 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path)); | |
214 | |
215 manager()->DeleteArchive( | |
216 archive_path, | |
217 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
218 PumpLoop(); | |
219 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
220 EXPECT_FALSE(base::PathExists(archive_path)); | |
221 } | |
222 | |
223 TEST_F(ArchiveManagerTest, DeleteArchiveThatDoesNotExist) { | |
224 base::FilePath archive_path = temp_path().Append(kMissingArchivePath); | |
225 EXPECT_FALSE(base::PathExists(archive_path)); | |
226 | |
227 manager()->DeleteArchive( | |
228 archive_path, | |
229 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
230 PumpLoop(); | |
231 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
232 EXPECT_FALSE(base::PathExists(archive_path)); | |
233 } | |
234 | |
235 TEST_F(ArchiveManagerTest, GetAllArchives) { | |
236 base::FilePath archive_path_1; | |
237 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); | |
238 base::FilePath archive_path_2; | |
239 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); | |
240 base::FilePath archive_path_3; | |
241 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); | |
242 std::vector<base::FilePath> expected_paths{archive_path_1, archive_path_2, | |
243 archive_path_3}; | |
244 std::sort(expected_paths.begin(), expected_paths.end()); | |
245 | |
246 manager()->GetAllArchives(base::Bind( | |
247 &ArchiveManagerTest::GetAllArchivesCallback, base::Unretained(this))); | |
248 PumpLoop(); | |
249 ASSERT_EQ(3UL, last_archive_paths().size()); | |
250 std::vector<base::FilePath> actual_paths(last_archive_paths().begin(), | |
251 last_archive_paths().end()); | |
252 // Comparing one to one works because last_archive_paths set is sorted. | |
253 // Because some windows bots provide abbreviated path (e.g. chrome-bot becomes | |
254 // CHROME~2), this test only focuses on file name. | |
255 EXPECT_EQ(expected_paths[0].BaseName(), actual_paths[0].BaseName()); | |
256 EXPECT_EQ(expected_paths[1].BaseName(), actual_paths[1].BaseName()); | |
257 EXPECT_EQ(expected_paths[2].BaseName(), actual_paths[2].BaseName()); | |
258 } | |
259 | |
260 TEST_F(ArchiveManagerTest, GetStorageStats) { | |
261 base::FilePath archive_path_1; | |
262 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); | |
263 base::FilePath archive_path_2; | |
264 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); | |
265 | |
266 manager()->GetStorageStats(base::Bind( | |
267 &ArchiveManagerTest::GetStorageStatsCallback, base::Unretained(this))); | |
268 PumpLoop(); | |
269 EXPECT_GT(last_storage_sizes().free_disk_space, 0); | |
270 EXPECT_EQ(last_storage_sizes().total_archives_size, | |
271 base::ComputeDirectorySize(temp_path())); | |
272 } | |
273 | |
274 } // namespace offline_pages | |
OLD | NEW |