Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/offline_pages/archive_manager.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/files/file_util.h" | |
| 12 #include "base/files/scoped_temp_dir.h" | |
| 13 #include "base/test/test_simple_task_runner.h" | |
| 14 #include "base/threading/thread_task_runner_handle.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace offline_pages { | |
| 18 enum class CallbackStatus { | |
| 19 NOT_CALLED, | |
| 20 CALLED_FALSE, | |
| 21 CALLED_TRUE, | |
| 22 }; | |
| 23 | |
| 24 class ArchiveManagerTest : public testing::Test { | |
| 25 public: | |
| 26 ArchiveManagerTest(); | |
| 27 void SetUp() override; | |
| 28 | |
| 29 void PumpLoop(); | |
| 30 void ResetResults(); | |
| 31 | |
| 32 void ResetManager(const base::FilePath& file_path); | |
| 33 void Callback(bool result); | |
| 34 | |
| 35 ArchiveManager* manager() { return manager_.get(); } | |
| 36 const base::FilePath& temp_path() const { return temp_dir_.path(); } | |
| 37 CallbackStatus callback_status() const { return callback_status_; } | |
| 38 | |
| 39 private: | |
| 40 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 41 base::ThreadTaskRunnerHandle task_runner_handle_; | |
| 42 base::ScopedTempDir temp_dir_; | |
| 43 | |
| 44 std::unique_ptr<ArchiveManager> manager_; | |
| 45 CallbackStatus callback_status_; | |
| 46 }; | |
| 47 | |
| 48 ArchiveManagerTest::ArchiveManagerTest() | |
| 49 : task_runner_(new base::TestSimpleTaskRunner), | |
| 50 task_runner_handle_(task_runner_), | |
| 51 callback_status_(CallbackStatus::NOT_CALLED) {} | |
| 52 | |
| 53 void ArchiveManagerTest::SetUp() { | |
| 54 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 55 ResetManager(temp_dir_.path()); | |
| 56 } | |
| 57 | |
| 58 void ArchiveManagerTest::PumpLoop() { | |
| 59 task_runner_->RunUntilIdle(); | |
| 60 } | |
| 61 | |
| 62 void ArchiveManagerTest::ResetResults() { | |
| 63 callback_status_ = CallbackStatus::NOT_CALLED; | |
| 64 } | |
| 65 | |
| 66 void ArchiveManagerTest::ResetManager(const base::FilePath& file_path) { | |
| 67 manager_.reset( | |
| 68 new ArchiveManager(file_path, base::ThreadTaskRunnerHandle::Get())); | |
| 69 } | |
| 70 | |
| 71 void ArchiveManagerTest::Callback(bool result) { | |
| 72 callback_status_ = | |
| 73 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE; | |
| 74 } | |
| 75 | |
| 76 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) { | |
| 77 base::FilePath archive_dir = | |
| 78 temp_path().Append(FILE_PATH_LITERAL("test_path")); | |
| 79 ResetManager(archive_dir); | |
| 80 EXPECT_FALSE(base::PathExists(archive_dir)); | |
| 81 | |
| 82 // Ensure archives dir exists, when it doesn't. | |
| 83 manager()->EnsureArchivesDirCreated( | |
| 84 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | |
| 85 PumpLoop(); | |
| 86 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
| 87 EXPECT_TRUE(base::PathExists(archive_dir)); | |
| 88 | |
| 89 // Try again when the file already exists. | |
| 90 ResetResults(); | |
| 91 manager()->EnsureArchivesDirCreated( | |
| 92 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true)); | |
| 93 PumpLoop(); | |
| 94 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
| 95 EXPECT_TRUE(base::PathExists(archive_dir)); | |
| 96 } | |
| 97 | |
| 98 TEST_F(ArchiveManagerTest, ArchiveExists) { | |
| 99 base::FilePath archive_path = | |
| 100 temp_path().Append(FILE_PATH_LITERAL("missing_file.mhtml")); | |
| 101 manager()->ArchiveExists( | |
| 102 archive_path, | |
| 103 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
| 104 PumpLoop(); | |
| 105 EXPECT_EQ(CallbackStatus::CALLED_FALSE, callback_status()); | |
| 106 | |
| 107 ResetResults(); | |
| 108 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path)); | |
| 109 | |
| 110 manager()->ArchiveExists( | |
| 111 archive_path, | |
| 112 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
| 113 PumpLoop(); | |
| 114 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
| 115 } | |
| 116 | |
| 117 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.
| |
| 118 base::FilePath archive_path_1; | |
| 119 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1)); | |
| 120 base::FilePath archive_path_2; | |
| 121 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2)); | |
| 122 base::FilePath archive_path_3; | |
| 123 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3)); | |
| 124 | |
| 125 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2}; | |
| 126 | |
| 127 manager()->DeleteArchives( | |
| 128 archive_paths, | |
| 129 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
| 130 PumpLoop(); | |
| 131 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
| 132 EXPECT_FALSE(base::PathExists(archive_path_1)); | |
| 133 EXPECT_FALSE(base::PathExists(archive_path_2)); | |
| 134 EXPECT_TRUE(base::PathExists(archive_path_3)); | |
| 135 } | |
| 136 | |
| 137 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.
| |
| 138 base::FilePath archive_path; | |
| 139 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path)); | |
| 140 | |
| 141 manager()->DeleteArchive( | |
| 142 archive_path, | |
| 143 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this))); | |
| 144 PumpLoop(); | |
| 145 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status()); | |
| 146 EXPECT_FALSE(base::PathExists(archive_path)); | |
| 147 } | |
| 148 | |
| 149 } // namespace offline_pages | |
| OLD | NEW |