| 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/archive_manager.h" | 5 #include "components/offline_pages/archive_manager.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 public: | 32 public: |
| 33 ArchiveManagerTest(); | 33 ArchiveManagerTest(); |
| 34 void SetUp() override; | 34 void SetUp() override; |
| 35 | 35 |
| 36 void PumpLoop(); | 36 void PumpLoop(); |
| 37 void ResetResults(); | 37 void ResetResults(); |
| 38 | 38 |
| 39 void ResetManager(const base::FilePath& file_path); | 39 void ResetManager(const base::FilePath& file_path); |
| 40 void Callback(bool result); | 40 void Callback(bool result); |
| 41 void GetAllArchivesCallback(const std::set<base::FilePath>& archive_paths); | 41 void GetAllArchivesCallback(const std::set<base::FilePath>& archive_paths); |
| 42 void GetStorageSizesCallback(ArchiveManager::StorageSizes storage_sizes); |
| 42 | 43 |
| 43 ArchiveManager* manager() { return manager_.get(); } | 44 ArchiveManager* manager() { return manager_.get(); } |
| 44 const base::FilePath& temp_path() const { return temp_dir_.path(); } | 45 const base::FilePath& temp_path() const { return temp_dir_.path(); } |
| 45 CallbackStatus callback_status() const { return callback_status_; } | 46 CallbackStatus callback_status() const { return callback_status_; } |
| 46 const std::set<base::FilePath>& last_archvie_paths() const { | 47 const std::set<base::FilePath>& last_archvie_paths() const { |
| 47 return last_archvie_paths_; | 48 return last_archvie_paths_; |
| 48 } | 49 } |
| 50 ArchiveManager::StorageSizes last_storage_sizes() const { |
| 51 return last_storage_sizes_; |
| 52 } |
| 49 | 53 |
| 50 private: | 54 private: |
| 51 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 55 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 52 base::ThreadTaskRunnerHandle task_runner_handle_; | 56 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 53 base::ScopedTempDir temp_dir_; | 57 base::ScopedTempDir temp_dir_; |
| 54 | 58 |
| 55 std::unique_ptr<ArchiveManager> manager_; | 59 std::unique_ptr<ArchiveManager> manager_; |
| 56 CallbackStatus callback_status_; | 60 CallbackStatus callback_status_; |
| 57 std::set<base::FilePath> last_archvie_paths_; | 61 std::set<base::FilePath> last_archvie_paths_; |
| 62 ArchiveManager::StorageSizes last_storage_sizes_; |
| 58 }; | 63 }; |
| 59 | 64 |
| 60 ArchiveManagerTest::ArchiveManagerTest() | 65 ArchiveManagerTest::ArchiveManagerTest() |
| 61 : task_runner_(new base::TestSimpleTaskRunner), | 66 : task_runner_(new base::TestSimpleTaskRunner), |
| 62 task_runner_handle_(task_runner_), | 67 task_runner_handle_(task_runner_), |
| 63 callback_status_(CallbackStatus::NOT_CALLED) {} | 68 callback_status_(CallbackStatus::NOT_CALLED), |
| 69 last_storage_sizes_({0, 0}) {} |
| 64 | 70 |
| 65 void ArchiveManagerTest::SetUp() { | 71 void ArchiveManagerTest::SetUp() { |
| 66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 67 ResetManager(temp_dir_.path()); | 73 ResetManager(temp_dir_.path()); |
| 68 } | 74 } |
| 69 | 75 |
| 70 void ArchiveManagerTest::PumpLoop() { | 76 void ArchiveManagerTest::PumpLoop() { |
| 71 task_runner_->RunUntilIdle(); | 77 task_runner_->RunUntilIdle(); |
| 72 } | 78 } |
| 73 | 79 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 84 void ArchiveManagerTest::Callback(bool result) { | 90 void ArchiveManagerTest::Callback(bool result) { |
| 85 callback_status_ = | 91 callback_status_ = |
| 86 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; | 92 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; |
| 87 } | 93 } |
| 88 | 94 |
| 89 void ArchiveManagerTest::GetAllArchivesCallback( | 95 void ArchiveManagerTest::GetAllArchivesCallback( |
| 90 const std::set<base::FilePath>& archive_paths) { | 96 const std::set<base::FilePath>& archive_paths) { |
| 91 last_archvie_paths_ = archive_paths; | 97 last_archvie_paths_ = archive_paths; |
| 92 } | 98 } |
| 93 | 99 |
| 100 void ArchiveManagerTest::GetStorageSizesCallback( |
| 101 ArchiveManager::StorageSizes storage_sizes) { |
| 102 last_storage_sizes_ = storage_sizes; |
| 103 } |
| 104 |
| 94 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { | 105 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { |
| 95 base::FilePath archive_dir = | 106 base::FilePath archive_dir = |
| 96 temp_path().Append(FILE_PATH_LITERAL("test_path")); | 107 temp_path().Append(FILE_PATH_LITERAL("test_path")); |
| 97 ResetManager(archive_dir); | 108 ResetManager(archive_dir); |
| 98 EXPECT_FALSE(base::PathExists(archive_dir)); | 109 EXPECT_FALSE(base::PathExists(archive_dir)); |
| 99 | 110 |
| 100 // Ensure archives dir exists, when it doesn't. | 111 // Ensure archives dir exists, when it doesn't. |
| 101 manager()->EnsureArchivesDirCreated( | 112 manager()->EnsureArchivesDirCreated( |
| 102 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | 113 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); |
| 103 PumpLoop(); | 114 PumpLoop(); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 | 239 |
| 229 manager()->GetAllArchives(base::Bind( | 240 manager()->GetAllArchives(base::Bind( |
| 230 &ArchiveManagerTest::GetAllArchivesCallback, base::Unretained(this))); | 241 &ArchiveManagerTest::GetAllArchivesCallback, base::Unretained(this))); |
| 231 PumpLoop(); | 242 PumpLoop(); |
| 232 ASSERT_EQ(3UL, last_archvie_paths().size()); | 243 ASSERT_EQ(3UL, last_archvie_paths().size()); |
| 233 EXPECT_EQ(1UL, last_archvie_paths().count(archive_path_1)); | 244 EXPECT_EQ(1UL, last_archvie_paths().count(archive_path_1)); |
| 234 EXPECT_EQ(1UL, last_archvie_paths().count(archive_path_2)); | 245 EXPECT_EQ(1UL, last_archvie_paths().count(archive_path_2)); |
| 235 EXPECT_EQ(1UL, last_archvie_paths().count(archive_path_3)); | 246 EXPECT_EQ(1UL, last_archvie_paths().count(archive_path_3)); |
| 236 } | 247 } |
| 237 | 248 |
| 249 TEST_F(ArchiveManagerTest, GetStorageSizes) { |
| 250 base::FilePath archive_path_1; |
| 251 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); |
| 252 base::FilePath archive_path_2; |
| 253 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); |
| 254 |
| 255 manager()->GetStorageSizes(base::Bind( |
| 256 &ArchiveManagerTest::GetStorageSizesCallback, base::Unretained(this))); |
| 257 PumpLoop(); |
| 258 EXPECT_GT(last_storage_sizes().free_disk_space, 0); |
| 259 EXPECT_EQ(last_storage_sizes().total_archives_size, |
| 260 base::ComputeDirectorySize(temp_path())); |
| 261 } |
| 262 |
| 238 } // namespace offline_pages | 263 } // namespace offline_pages |
| OLD | NEW |