| 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 <memory> | 8 #include <memory> |
| 9 #include <set> |
| 10 #include <vector> |
| 8 | 11 |
| 9 #include "base/bind.h" | 12 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/test/test_simple_task_runner.h" | 16 #include "base/test/test_simple_task_runner.h" |
| 14 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 19 |
| 17 namespace offline_pages { | 20 namespace offline_pages { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 30 class ArchiveManagerTest : public testing::Test { | 33 class ArchiveManagerTest : public testing::Test { |
| 31 public: | 34 public: |
| 32 ArchiveManagerTest(); | 35 ArchiveManagerTest(); |
| 33 void SetUp() override; | 36 void SetUp() override; |
| 34 | 37 |
| 35 void PumpLoop(); | 38 void PumpLoop(); |
| 36 void ResetResults(); | 39 void ResetResults(); |
| 37 | 40 |
| 38 void ResetManager(const base::FilePath& file_path); | 41 void ResetManager(const base::FilePath& file_path); |
| 39 void Callback(bool result); | 42 void Callback(bool result); |
| 43 void GetAllArchivesCallback(const std::set<base::FilePath>& archive_paths); |
| 40 | 44 |
| 41 ArchiveManager* manager() { return manager_.get(); } | 45 ArchiveManager* manager() { return manager_.get(); } |
| 42 const base::FilePath& temp_path() const { return temp_dir_.path(); } | 46 const base::FilePath& temp_path() const { return temp_dir_.path(); } |
| 43 CallbackStatus callback_status() const { return callback_status_; } | 47 CallbackStatus callback_status() const { return callback_status_; } |
| 48 const std::set<base::FilePath>& last_archive_paths() const { |
| 49 return last_archvie_paths_; |
| 50 } |
| 44 | 51 |
| 45 private: | 52 private: |
| 46 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 53 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 47 base::ThreadTaskRunnerHandle task_runner_handle_; | 54 base::ThreadTaskRunnerHandle task_runner_handle_; |
| 48 base::ScopedTempDir temp_dir_; | 55 base::ScopedTempDir temp_dir_; |
| 49 | 56 |
| 50 std::unique_ptr<ArchiveManager> manager_; | 57 std::unique_ptr<ArchiveManager> manager_; |
| 51 CallbackStatus callback_status_; | 58 CallbackStatus callback_status_; |
| 59 std::set<base::FilePath> last_archvie_paths_; |
| 52 }; | 60 }; |
| 53 | 61 |
| 54 ArchiveManagerTest::ArchiveManagerTest() | 62 ArchiveManagerTest::ArchiveManagerTest() |
| 55 : task_runner_(new base::TestSimpleTaskRunner), | 63 : task_runner_(new base::TestSimpleTaskRunner), |
| 56 task_runner_handle_(task_runner_), | 64 task_runner_handle_(task_runner_), |
| 57 callback_status_(CallbackStatus::NOT_CALLED) {} | 65 callback_status_(CallbackStatus::NOT_CALLED) {} |
| 58 | 66 |
| 59 void ArchiveManagerTest::SetUp() { | 67 void ArchiveManagerTest::SetUp() { |
| 60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 68 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 61 ResetManager(temp_dir_.path()); | 69 ResetManager(temp_dir_.path()); |
| 62 } | 70 } |
| 63 | 71 |
| 64 void ArchiveManagerTest::PumpLoop() { | 72 void ArchiveManagerTest::PumpLoop() { |
| 65 task_runner_->RunUntilIdle(); | 73 task_runner_->RunUntilIdle(); |
| 66 } | 74 } |
| 67 | 75 |
| 68 void ArchiveManagerTest::ResetResults() { | 76 void ArchiveManagerTest::ResetResults() { |
| 69 callback_status_ = CallbackStatus::NOT_CALLED; | 77 callback_status_ = CallbackStatus::NOT_CALLED; |
| 78 last_archvie_paths_.clear(); |
| 70 } | 79 } |
| 71 | 80 |
| 72 void ArchiveManagerTest::ResetManager(const base::FilePath& file_path) { | 81 void ArchiveManagerTest::ResetManager(const base::FilePath& file_path) { |
| 73 manager_.reset( | 82 manager_.reset( |
| 74 new ArchiveManager(file_path, base::ThreadTaskRunnerHandle::Get())); | 83 new ArchiveManager(file_path, base::ThreadTaskRunnerHandle::Get())); |
| 75 } | 84 } |
| 76 | 85 |
| 77 void ArchiveManagerTest::Callback(bool result) { | 86 void ArchiveManagerTest::Callback(bool result) { |
| 78 callback_status_ = | 87 callback_status_ = |
| 79 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; | 88 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; |
| 80 } | 89 } |
| 81 | 90 |
| 91 void ArchiveManagerTest::GetAllArchivesCallback( |
| 92 const std::set<base::FilePath>& archive_paths) { |
| 93 last_archvie_paths_ = archive_paths; |
| 94 } |
| 95 |
| 82 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { | 96 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { |
| 83 base::FilePath archive_dir = | 97 base::FilePath archive_dir = |
| 84 temp_path().Append(FILE_PATH_LITERAL("test_path")); | 98 temp_path().Append(FILE_PATH_LITERAL("test_path")); |
| 85 ResetManager(archive_dir); | 99 ResetManager(archive_dir); |
| 86 EXPECT_FALSE(base::PathExists(archive_dir)); | 100 EXPECT_FALSE(base::PathExists(archive_dir)); |
| 87 | 101 |
| 88 // Ensure archives dir exists, when it doesn't. | 102 // Ensure archives dir exists, when it doesn't. |
| 89 manager()->EnsureArchivesDirCreated( | 103 manager()->EnsureArchivesDirCreated( |
| 90 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | 104 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); |
| 91 PumpLoop(); | 105 PumpLoop(); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 EXPECT_FALSE(base::PathExists(archive_path)); | 213 EXPECT_FALSE(base::PathExists(archive_path)); |
| 200 | 214 |
| 201 manager()->DeleteArchive( | 215 manager()->DeleteArchive( |
| 202 archive_path, | 216 archive_path, |
| 203 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | 217 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); |
| 204 PumpLoop(); | 218 PumpLoop(); |
| 205 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | 219 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); |
| 206 EXPECT_FALSE(base::PathExists(archive_path)); | 220 EXPECT_FALSE(base::PathExists(archive_path)); |
| 207 } | 221 } |
| 208 | 222 |
| 223 TEST_F(ArchiveManagerTest, GetAllArchives) { |
| 224 base::FilePath archive_path_1; |
| 225 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); |
| 226 base::FilePath archive_path_2; |
| 227 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); |
| 228 base::FilePath archive_path_3; |
| 229 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); |
| 230 std::vector<base::FilePath> expected_paths{archive_path_1, archive_path_2, |
| 231 archive_path_3}; |
| 232 std::sort(expected_paths.begin(), expected_paths.end()); |
| 233 |
| 234 manager()->GetAllArchives(base::Bind( |
| 235 &ArchiveManagerTest::GetAllArchivesCallback, base::Unretained(this))); |
| 236 PumpLoop(); |
| 237 ASSERT_EQ(3UL, last_archive_paths().size()); |
| 238 std::vector<base::FilePath> actual_paths(last_archive_paths().begin(), |
| 239 last_archive_paths().end()); |
| 240 // 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 |
| 242 // CHROME~2), this test only focuses on file name. |
| 243 EXPECT_EQ(expected_paths[0].BaseName(), actual_paths[0].BaseName()); |
| 244 EXPECT_EQ(expected_paths[1].BaseName(), actual_paths[1].BaseName()); |
| 245 EXPECT_EQ(expected_paths[2].BaseName(), actual_paths[2].BaseName()); |
| 246 } |
| 247 |
| 209 } // namespace offline_pages | 248 } // namespace offline_pages |
| OLD | NEW |