Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: components/offline_pages/archive_manager_unittest.cc

Issue 1977853004: [Offline pages] Adding ArchiveManager to abstract offline archives management (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adding a missing destructor for OfflinePageStorageManager::Client Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
19 namespace {
20 const base::FilePath::CharType kMissingArchivePath[] = FILE_PATH_LITERAL(
21 "missing_archive.path");
22 } // namespace
23
24 enum class CallbackStatus {
25 NOT_CALLED,
26 CALLED_FALSE,
27 CALLED_TRUE,
28 };
29
30 class ArchiveManagerTest : public testing::Test {
31 public:
32 ArchiveManagerTest();
33 void SetUp() override;
34
35 void PumpLoop();
36 void ResetResults();
37
38 void ResetManager(const base::FilePath& file_path);
39 void Callback(bool result);
40
41 ArchiveManager* manager() { return manager_.get(); }
42 const base::FilePath& temp_path() const { return temp_dir_.path(); }
43 CallbackStatus callback_status() const { return callback_status_; }
44
45 private:
46 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
47 base::ThreadTaskRunnerHandle task_runner_handle_;
48 base::ScopedTempDir temp_dir_;
49
50 std::unique_ptr<ArchiveManager> manager_;
51 CallbackStatus callback_status_;
52 };
53
54 ArchiveManagerTest::ArchiveManagerTest()
55 : task_runner_(new base::TestSimpleTaskRunner),
56 task_runner_handle_(task_runner_),
57 callback_status_(CallbackStatus::NOT_CALLED) {}
58
59 void ArchiveManagerTest::SetUp() {
60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
61 ResetManager(temp_dir_.path());
62 }
63
64 void ArchiveManagerTest::PumpLoop() {
65 task_runner_->RunUntilIdle();
66 }
67
68 void ArchiveManagerTest::ResetResults() {
69 callback_status_ = CallbackStatus::NOT_CALLED;
70 }
71
72 void ArchiveManagerTest::ResetManager(const base::FilePath& file_path) {
73 manager_.reset(
74 new ArchiveManager(file_path, base::ThreadTaskRunnerHandle::Get()));
75 }
76
77 void ArchiveManagerTest::Callback(bool result) {
78 callback_status_ =
79 result ? CallbackStatus::CALLED_TRUE : CallbackStatus::CALLED_FALSE;
80 }
81
82 TEST_F(ArchiveManagerTest, EnsureArchivesDirCreated) {
83 base::FilePath archive_dir =
84 temp_path().Append(FILE_PATH_LITERAL("test_path"));
85 ResetManager(archive_dir);
86 EXPECT_FALSE(base::PathExists(archive_dir));
87
88 // Ensure archives dir exists, when it doesn't.
89 manager()->EnsureArchivesDirCreated(
90 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true));
91 PumpLoop();
92 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
93 EXPECT_TRUE(base::PathExists(archive_dir));
94
95 // Try again when the file already exists.
96 ResetResults();
97 manager()->EnsureArchivesDirCreated(
98 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this), true));
99 PumpLoop();
100 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
101 EXPECT_TRUE(base::PathExists(archive_dir));
102 }
103
104 TEST_F(ArchiveManagerTest, ExistsArchive) {
105 base::FilePath archive_path = temp_path().Append(kMissingArchivePath);
106 manager()->ExistsArchive(
107 archive_path,
108 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
109 PumpLoop();
110 EXPECT_EQ(CallbackStatus::CALLED_FALSE, callback_status());
111
112 ResetResults();
113 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path));
114
115 manager()->ExistsArchive(
116 archive_path,
117 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
118 PumpLoop();
119 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
120 }
121
122 TEST_F(ArchiveManagerTest, DeleteMultipleArchives) {
123 base::FilePath archive_path_1;
124 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_1));
125 base::FilePath archive_path_2;
126 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2));
127 base::FilePath archive_path_3;
128 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3));
129
130 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2};
131
132 manager()->DeleteMultipleArchives(
133 archive_paths,
134 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
135 PumpLoop();
136 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
137 EXPECT_FALSE(base::PathExists(archive_path_1));
138 EXPECT_FALSE(base::PathExists(archive_path_2));
139 EXPECT_TRUE(base::PathExists(archive_path_3));
140 }
141
142 TEST_F(ArchiveManagerTest, DeleteMultipleArchivesSomeDoNotExist) {
143 base::FilePath archive_path_1 = temp_path().Append(kMissingArchivePath);
144 base::FilePath archive_path_2;
145 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_2));
146 base::FilePath archive_path_3;
147 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3));
148
149 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2};
150
151 EXPECT_FALSE(base::PathExists(archive_path_1));
152
153 manager()->DeleteMultipleArchives(
154 archive_paths,
155 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
156 PumpLoop();
157 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
158 EXPECT_FALSE(base::PathExists(archive_path_1));
159 EXPECT_FALSE(base::PathExists(archive_path_2));
160 EXPECT_TRUE(base::PathExists(archive_path_3));
161 }
162
163 TEST_F(ArchiveManagerTest, DeleteMultipleArchivesNoneExist) {
164 base::FilePath archive_path_1 = temp_path().Append(kMissingArchivePath);
165 base::FilePath archive_path_2 = temp_path().Append(FILE_PATH_LITERAL(
166 "other_missing_file.mhtml"));
167 base::FilePath archive_path_3;
168 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path_3));
169
170 std::vector<base::FilePath> archive_paths = {archive_path_1, archive_path_2};
171
172 EXPECT_FALSE(base::PathExists(archive_path_1));
173 EXPECT_FALSE(base::PathExists(archive_path_2));
174
175 manager()->DeleteMultipleArchives(
176 archive_paths,
177 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
178 PumpLoop();
179 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
180 EXPECT_FALSE(base::PathExists(archive_path_1));
181 EXPECT_FALSE(base::PathExists(archive_path_2));
182 EXPECT_TRUE(base::PathExists(archive_path_3));
183 }
184
185 TEST_F(ArchiveManagerTest, DeleteArchive) {
186 base::FilePath archive_path;
187 EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_path(), &archive_path));
188
189 manager()->DeleteArchive(
190 archive_path,
191 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
192 PumpLoop();
193 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
194 EXPECT_FALSE(base::PathExists(archive_path));
195 }
196
197 TEST_F(ArchiveManagerTest, DeleteArchiveThatDoesNotExist) {
198 base::FilePath archive_path = temp_path().Append(kMissingArchivePath);
199 EXPECT_FALSE(base::PathExists(archive_path));
200
201 manager()->DeleteArchive(
202 archive_path,
203 base::Bind(&ArchiveManagerTest::Callback, base::Unretained(this)));
204 PumpLoop();
205 EXPECT_EQ(CallbackStatus::CALLED_TRUE, callback_status());
206 EXPECT_FALSE(base::PathExists(archive_path));
207 }
208
209 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/archive_manager.cc ('k') | components/offline_pages/offline_page_model.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698