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