Chromium Code Reviews| Index: components/offline_pages/archive_manager_unittest.cc |
| diff --git a/components/offline_pages/archive_manager_unittest.cc b/components/offline_pages/archive_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..00b1f89440179703238d8540abcbd29dc03354a8 |
| --- /dev/null |
| +++ b/components/offline_pages/archive_manager_unittest.cc |
| @@ -0,0 +1,149 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/offline_pages/archive_manager.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/test/test_simple_task_runner.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace offline_pages { |
| +enum class CallbackStatus { |
| + NOT_CALLED, |
| + CALLED_FALSE, |
| + CALLED_TRUE, |
| +}; |
| + |
| +class ArchiveManagerTest : public testing::Test { |
| + public: |
| + ArchiveManagerTest(); |
| + void SetUp() override; |
| + |
| + void PumpLoop(); |
| + void ResetResults(); |
| + |
| + void ResetManager(const base::FilePath& file_path); |
| + void Callback(bool result); |
| + |
| + ArchiveManager* manager() { return manager_.get(); } |
| + const base::FilePath& temp_path() const { return temp_dir_.path(); } |
| + CallbackStatus callback_status() const { return callback_status_; } |
| + |
| + private: |
| + scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| + base::ThreadTaskRunnerHandle task_runner_handle_; |
| + base::ScopedTempDir temp_dir_; |
| + |
| + std::unique_ptr<ArchiveManager> manager_; |
| + CallbackStatus callback_status_; |
| +}; |
| + |
| +ArchiveManagerTest::ArchiveManagerTest() |
| + : task_runner_(new base::TestSimpleTaskRunner), |
| + task_runner_handle_(task_runner_), |
| + callback_status_(CallbackStatus::NOT_CALLED) {} |
| + |
| +void ArchiveManagerTest::SetUp() { |
| + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + ResetManager(temp_dir_.path()); |
| +} |
| + |
| +void ArchiveManagerTest::PumpLoop() { |
| + task_runner_->RunUntilIdle(); |
| +} |
| + |
| +void ArchiveManagerTest::ResetResults() { |
| + callback_status_ = CallbackStatus::NOT_CALLED; |
| +} |
| + |
| +void ArchiveManagerTest::ResetManager(const base::FilePath& file_path) { |
| + manager_.reset( |
| + new ArchiveManager(file_path, base::ThreadTaskRunnerHandle::Get())); |
| +} |
| + |
| +void ArchiveManagerTest::Callback(bool result) { |
| + callback_status_ = |
| + result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; |
| +} |
| + |
| +TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { |
| + base::FilePath archive_dir = |
| + temp_path().Append(FILE_PATH_LITERAL("test_path")); |
| + ResetManager(archive_dir); |
| + EXPECT_FALSE(base::PathExists(archive_dir)); |
| + |
| + // Ensure archives dir exists, when it doesn't. |
| + manager()->EnsureArchivesDirCreated( |
| + base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); |
| + PumpLoop(); |
| + EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); |
| + EXPECT_TRUE(base::PathExists(archive_dir)); |
| + |
| + // Try again when the file already exists. |
| + ResetResults(); |
| + manager()->EnsureArchivesDirCreated( |
| + base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); |
| + PumpLoop(); |
| + EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); |
| + EXPECT_TRUE(base::PathExists(archive_dir)); |
| +} |
| + |
| +TEST_F(ArchiveManagerTest, ArchiveExists) { |
| + base::FilePath archive_path = |
| + temp_path().Append(FILE_PATH_LITERAL("missing_file.mhtml")); |
| + manager()->ArchiveExists( |
| + archive_path, |
| + base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); |
| + PumpLoop(); |
| + EXPECT_EQ(CallbackStatus::CALLED_FALSE, callback_status()); |
| + |
| + ResetResults(); |
| + EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path)); |
| + |
| + manager()->ArchiveExists( |
| + archive_path, |
| + base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); |
| + PumpLoop(); |
| + EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); |
| +} |
| + |
| +TEST_F(ArchiveManagerTest, DeleteArchives) { |
|
jianli
2016/05/13 23:50:49
Please also test the cases
1) All the archive file
fgorski
2016/05/16 17:46:52
Done.
|
| + base::FilePath archive_path_1; |
| + EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); |
| + base::FilePath archive_path_2; |
| + EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); |
| + base::FilePath archive_path_3; |
| + EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); |
| + |
| + std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2}; |
| + |
| + manager()->DeleteArchives( |
| + archive_paths, |
| + base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); |
| + PumpLoop(); |
| + EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); |
| + EXPECT_FALSE(base::PathExists(archive_path_1)); |
| + EXPECT_FALSE(base::PathExists(archive_path_2)); |
| + EXPECT_TRUE(base::PathExists(archive_path_3)); |
| +} |
| + |
| +TEST_F(ArchiveManagerTest, DeleteArchive) { |
|
jianli
2016/05/13 23:50:48
Please also test the case that the archive does no
fgorski
2016/05/16 17:46:52
Done.
|
| + base::FilePath archive_path; |
| + EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path)); |
| + |
| + manager()->DeleteArchive( |
| + archive_path, |
| + base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); |
| + PumpLoop(); |
| + EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); |
| + EXPECT_FALSE(base::PathExists(archive_path)); |
| +} |
| + |
| +} // namespace offline_pages |