| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 public: | 34 public: |
| 35 ArchiveManagerTest(); | 35 ArchiveManagerTest(); |
| 36 void SetUp() override; | 36 void SetUp() override; |
| 37 | 37 |
| 38 void PumpLoop(); | 38 void PumpLoop(); |
| 39 void ResetResults(); | 39 void ResetResults(); |
| 40 | 40 |
| 41 void ResetManager(const base::FilePath& file_path); | 41 void ResetManager(const base::FilePath& file_path); |
| 42 void Callback(bool result); | 42 void Callback(bool result); |
| 43 void GetAllArchivesCallback(const std::set<base::FilePath>& archive_paths); | 43 void GetAllArchivesCallback(const std::set<base::FilePath>& archive_paths); |
| 44 void GetStorageStatsCallback( |
| 45 const ArchiveManager::StorageStats& storage_sizes); |
| 44 | 46 |
| 45 ArchiveManager* manager() { return manager_.get(); } | 47 ArchiveManager* manager() { return manager_.get(); } |
| 46 const base::FilePath& temp_path() const { return temp_dir_.path(); } | 48 const base::FilePath& temp_path() const { return temp_dir_.path(); } |
| 47 CallbackStatus callback_status() const { return callback_status_; } | 49 CallbackStatus callback_status() const { return callback_status_; } |
| 48 const std::set<base::FilePath>& last_archive_paths() const { | 50 const std::set<base::FilePath>& last_archive_paths() const { |
| 49 return last_archvie_paths_; | 51 return last_archvie_paths_; |
| 50 } | 52 } |
| 53 ArchiveManager::StorageStats last_storage_sizes() const { |
| 54 return last_storage_sizes_; |
| 55 } |
| 51 | 56 |
| 52 private: | 57 private: |
| 53 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 58 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 54 base::ThreadTaskRunnerHandle task_runner_handle_; | 59 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 55 base::ScopedTempDir temp_dir_; | 60 base::ScopedTempDir temp_dir_; |
| 56 | 61 |
| 57 std::unique_ptr<ArchiveManager> manager_; | 62 std::unique_ptr<ArchiveManager> manager_; |
| 58 CallbackStatus callback_status_; | 63 CallbackStatus callback_status_; |
| 59 std::set<base::FilePath> last_archvie_paths_; | 64 std::set<base::FilePath> last_archvie_paths_; |
| 65 ArchiveManager::StorageStats last_storage_sizes_; |
| 60 }; | 66 }; |
| 61 | 67 |
| 62 ArchiveManagerTest::ArchiveManagerTest() | 68 ArchiveManagerTest::ArchiveManagerTest() |
| 63 : task_runner_(new base::TestSimpleTaskRunner), | 69 : task_runner_(new base::TestSimpleTaskRunner), |
| 64 task_runner_handle_(task_runner_), | 70 task_runner_handle_(task_runner_), |
| 65 callback_status_(CallbackStatus::NOT_CALLED) {} | 71 callback_status_(CallbackStatus::NOT_CALLED), |
| 72 last_storage_sizes_({0, 0}) {} |
| 66 | 73 |
| 67 void ArchiveManagerTest::SetUp() { | 74 void ArchiveManagerTest::SetUp() { |
| 68 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 75 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 69 ResetManager(temp_dir_.path()); | 76 ResetManager(temp_dir_.path()); |
| 70 } | 77 } |
| 71 | 78 |
| 72 void ArchiveManagerTest::PumpLoop() { | 79 void ArchiveManagerTest::PumpLoop() { |
| 73 task_runner_->RunUntilIdle(); | 80 task_runner_->RunUntilIdle(); |
| 74 } | 81 } |
| 75 | 82 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 86 void ArchiveManagerTest::Callback(bool result) { | 93 void ArchiveManagerTest::Callback(bool result) { |
| 87 callback_status_ = | 94 callback_status_ = |
| 88 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; | 95 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; |
| 89 } | 96 } |
| 90 | 97 |
| 91 void ArchiveManagerTest::GetAllArchivesCallback( | 98 void ArchiveManagerTest::GetAllArchivesCallback( |
| 92 const std::set<base::FilePath>& archive_paths) { | 99 const std::set<base::FilePath>& archive_paths) { |
| 93 last_archvie_paths_ = archive_paths; | 100 last_archvie_paths_ = archive_paths; |
| 94 } | 101 } |
| 95 | 102 |
| 103 void ArchiveManagerTest::GetStorageStatsCallback( |
| 104 const ArchiveManager::StorageStats& storage_sizes) { |
| 105 last_storage_sizes_ = storage_sizes; |
| 106 } |
| 107 |
| 96 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { | 108 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { |
| 97 base::FilePath archive_dir = | 109 base::FilePath archive_dir = |
| 98 temp_path().Append(FILE_PATH_LITERAL("test_path")); | 110 temp_path().Append(FILE_PATH_LITERAL("test_path")); |
| 99 ResetManager(archive_dir); | 111 ResetManager(archive_dir); |
| 100 EXPECT_FALSE(base::PathExists(archive_dir)); | 112 EXPECT_FALSE(base::PathExists(archive_dir)); |
| 101 | 113 |
| 102 // Ensure archives dir exists, when it doesn't. | 114 // Ensure archives dir exists, when it doesn't. |
| 103 manager()->EnsureArchivesDirCreated( | 115 manager()->EnsureArchivesDirCreated( |
| 104 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | 116 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); |
| 105 PumpLoop(); | 117 PumpLoop(); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 std::vector<base::FilePath> actual_paths(last_archive_paths().begin(), | 250 std::vector<base::FilePath> actual_paths(last_archive_paths().begin(), |
| 239 last_archive_paths().end()); | 251 last_archive_paths().end()); |
| 240 // Comparing one to one works because last_archive_paths set is sorted. | 252 // Comparing one to one works because last_archive_paths set is sorted. |
| 241 // Because some windows bots provide abbreviated path (e.g. chrome-bot becomes | 253 // Because some windows bots provide abbreviated path (e.g. chrome-bot becomes |
| 242 // CHROME~2), this test only focuses on file name. | 254 // CHROME~2), this test only focuses on file name. |
| 243 EXPECT_EQ(expected_paths[0].BaseName(), actual_paths[0].BaseName()); | 255 EXPECT_EQ(expected_paths[0].BaseName(), actual_paths[0].BaseName()); |
| 244 EXPECT_EQ(expected_paths[1].BaseName(), actual_paths[1].BaseName()); | 256 EXPECT_EQ(expected_paths[1].BaseName(), actual_paths[1].BaseName()); |
| 245 EXPECT_EQ(expected_paths[2].BaseName(), actual_paths[2].BaseName()); | 257 EXPECT_EQ(expected_paths[2].BaseName(), actual_paths[2].BaseName()); |
| 246 } | 258 } |
| 247 | 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 |
| 248 } // namespace offline_pages | 274 } // namespace offline_pages |
| OLD | NEW |