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