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 "chrome/browser/offline_pages/offline_page_mhtml_archiver.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/files/scoped_temp_dir.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace offline_pages { | |
18 | |
19 namespace { | |
20 const char kTestURL[] = "http://example.com/"; | |
21 const base::string16 kTestTitle = base::ASCIIToUTF16("Test page title"); | |
22 const base::FilePath::CharType kTestFilePath[] = FILE_PATH_LITERAL( | |
23 "/archive_dir/offline_page.mhtml"); | |
24 const int64 kTestFileSize = 123456LL; | |
25 } // namespace | |
26 | |
27 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { | |
28 public: | |
29 enum class TestScenario { | |
30 SUCCESS, | |
31 NOT_ABLE_TO_ARCHIVE, | |
32 WEB_CONTENTS_MISSING, | |
33 }; | |
34 | |
35 TestMHTMLArchiver( | |
36 const GURL& url, | |
37 const base::string16& title, | |
38 const TestScenario test_scenario, | |
39 const base::FilePath& archive_dir, | |
40 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner); | |
41 ~TestMHTMLArchiver() override; | |
42 | |
43 private: | |
44 bool IsWebContentsValid() const override; | |
45 void GenerateMHTML() override; | |
46 | |
47 const GURL url_; | |
48 const base::string16 title_; | |
49 const TestScenario test_scenario_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); | |
52 }; | |
53 | |
54 TestMHTMLArchiver::TestMHTMLArchiver( | |
55 const GURL& url, | |
56 const base::string16& title, | |
57 const TestScenario test_scenario, | |
58 const base::FilePath& archive_dir, | |
59 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner) | |
60 : OfflinePageMHTMLArchiver(archive_dir, task_runner), | |
61 url_(url), | |
62 title_(title), | |
63 test_scenario_(test_scenario) { | |
64 } | |
65 | |
66 TestMHTMLArchiver::~TestMHTMLArchiver() { | |
67 } | |
68 | |
69 bool TestMHTMLArchiver::IsWebContentsValid() const { | |
70 return test_scenario_ != TestScenario::WEB_CONTENTS_MISSING; | |
71 } | |
72 | |
73 void TestMHTMLArchiver::GenerateMHTML() { | |
74 int64 file_size = kTestFileSize; | |
75 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) | |
76 file_size = -1; | |
77 task_runner_->PostTask(FROM_HERE, | |
78 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone, | |
79 base::Unretained(this), | |
80 url_, | |
81 title_, | |
82 file_size)); | |
83 } | |
84 | |
85 class OfflinePageMHTMLArchiverTest : public testing::Test { | |
86 public: | |
87 OfflinePageMHTMLArchiverTest(); | |
88 ~OfflinePageMHTMLArchiverTest() override; | |
89 | |
90 // Creates an archiver for testing and specifies a scenario to be used. | |
91 scoped_ptr<TestMHTMLArchiver> CreateArchiver( | |
92 const GURL& url, | |
93 const base::string16& title, | |
94 TestMHTMLArchiver::TestScenario scenario); | |
95 | |
96 // Test tooling methods. | |
97 void PumpLoop(); | |
98 | |
99 base::FilePath GetTestFilePath() const { | |
100 return base::FilePath(kTestFilePath); | |
101 } | |
102 | |
103 const OfflinePageArchiver* last_archiver() const { return last_archiver_; } | |
104 OfflinePageArchiver::ArchiverResult last_result() const { | |
105 return last_result_; | |
106 } | |
107 const base::FilePath& last_file_path() const { return last_file_path_; } | |
108 int64 last_file_size() const { return last_file_size_; } | |
109 | |
110 const OfflinePageArchiver::CreateArchiveCallback callback() { | |
111 return base::Bind(&OfflinePageMHTMLArchiverTest::OnCreateArchiveDone, | |
112 base::Unretained(this)); | |
113 } | |
114 | |
115 private: | |
116 void OnCreateArchiveDone(OfflinePageArchiver* archiver, | |
117 OfflinePageArchiver::ArchiverResult result, | |
118 const GURL& url, | |
119 const base::string16& title, | |
120 const base::FilePath& file_path, | |
121 int64 file_size); | |
122 | |
123 OfflinePageArchiver* last_archiver_; | |
124 OfflinePageArchiver::ArchiverResult last_result_; | |
125 GURL last_url_; | |
126 base::string16 last_title_; | |
127 base::FilePath last_file_path_; | |
128 int64 last_file_size_; | |
129 | |
130 base::MessageLoop message_loop_; | |
131 scoped_ptr<base::RunLoop> run_loop_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(OfflinePageMHTMLArchiverTest); | |
134 }; | |
135 | |
136 OfflinePageMHTMLArchiverTest::OfflinePageMHTMLArchiverTest() | |
137 : last_archiver_(nullptr), | |
138 last_result_(OfflinePageArchiver::ArchiverResult:: | |
139 ERROR_ARCHIVE_CREATION_FAILED), | |
140 last_file_size_(0L) { | |
141 } | |
142 | |
143 OfflinePageMHTMLArchiverTest::~OfflinePageMHTMLArchiverTest() { | |
144 } | |
145 | |
146 scoped_ptr<TestMHTMLArchiver> OfflinePageMHTMLArchiverTest::CreateArchiver( | |
147 const GURL& url, | |
148 const base::string16& title, | |
149 TestMHTMLArchiver::TestScenario scenario) { | |
150 return scoped_ptr<TestMHTMLArchiver>(new TestMHTMLArchiver( | |
151 url, title, scenario, GetTestFilePath(), message_loop_.task_runner())); | |
152 } | |
153 | |
154 void OfflinePageMHTMLArchiverTest::OnCreateArchiveDone( | |
155 OfflinePageArchiver* archiver, | |
156 OfflinePageArchiver::ArchiverResult result, | |
157 const GURL& url, | |
158 const base::string16& title, | |
159 const base::FilePath& file_path, | |
160 int64 file_size) { | |
161 run_loop_->Quit(); | |
162 last_url_ = url; | |
163 last_title_ = title; | |
164 last_archiver_ = archiver; | |
165 last_result_ = result; | |
166 last_file_path_ = file_path; | |
167 last_file_size_ = file_size; | |
168 } | |
169 | |
170 void OfflinePageMHTMLArchiverTest::PumpLoop() { | |
171 run_loop_.reset(new base::RunLoop()); | |
172 run_loop_->Run(); | |
173 } | |
174 | |
175 // Tests that creation of an archiver fails when web contents is missing. | |
176 TEST_F(OfflinePageMHTMLArchiverTest, WebContentsMissing) { | |
177 GURL page_url = GURL(kTestURL); | |
178 scoped_ptr<TestMHTMLArchiver> archiver( | |
179 CreateArchiver(page_url, | |
180 kTestTitle, | |
181 TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING)); | |
182 archiver->CreateArchive(callback()); | |
183 PumpLoop(); | |
184 | |
185 EXPECT_EQ(archiver.get(), last_archiver()); | |
186 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE, | |
187 last_result()); | |
188 EXPECT_EQ(base::FilePath(), last_file_path()); | |
189 } | |
190 | |
191 // Tests for successful creation of the offline page archive. | |
192 TEST_F(OfflinePageMHTMLArchiverTest, NotAbleToGenerateArchive) { | |
193 GURL page_url = GURL(kTestURL); | |
194 scoped_ptr<TestMHTMLArchiver> archiver( | |
195 CreateArchiver(page_url, | |
196 kTestTitle, | |
197 TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE)); | |
198 archiver->CreateArchive(callback()); | |
199 PumpLoop(); | |
200 | |
201 EXPECT_EQ(archiver.get(), last_archiver()); | |
202 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED, | |
203 last_result()); | |
204 EXPECT_EQ(base::FilePath(), last_file_path()); | |
205 EXPECT_EQ(0LL, last_file_size()); | |
206 } | |
207 | |
208 // Tests for successful creation of the offline page archive. | |
209 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { | |
210 GURL page_url = GURL(kTestURL); | |
211 scoped_ptr<TestMHTMLArchiver> archiver( | |
212 CreateArchiver(page_url, | |
213 kTestTitle, | |
214 TestMHTMLArchiver::TestScenario::SUCCESS)); | |
215 archiver->CreateArchive(callback()); | |
216 PumpLoop(); | |
217 | |
218 EXPECT_EQ(archiver.get(), last_archiver()); | |
219 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | |
220 last_result()); | |
221 EXPECT_EQ(GetTestFilePath(), last_file_path()); | |
222 EXPECT_EQ(kTestFileSize, last_file_size()); | |
223 } | |
224 | |
225 } // namespace offline_pages | |
OLD | NEW |