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.
5 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.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 "base/thread_task_runner_handle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace offline_pages
{
22 const char kTestURL
[] = "http://example.com/";
23 const base::FilePath::CharType kTestFilePath
[] = FILE_PATH_LITERAL(
24 "/archive_dir/offline_page.mhtml");
25 const int64 kTestFileSize
= 123456LL;
27 class TestMHTMLArchiver
: public OfflinePageMHTMLArchiver
{
29 enum class TestScenario
{
37 const TestScenario test_scenario
,
38 const base::FilePath
& archive_dir
);
39 ~TestMHTMLArchiver() override
;
42 void GenerateMHTML() override
;
45 const base::FilePath file_path_
;
46 const TestScenario test_scenario_
;
48 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver
);
51 TestMHTMLArchiver::TestMHTMLArchiver(
53 const TestScenario test_scenario
,
54 const base::FilePath
& archive_dir
)
55 : OfflinePageMHTMLArchiver(archive_dir
),
57 file_path_(archive_dir
),
58 test_scenario_(test_scenario
) {
61 TestMHTMLArchiver::~TestMHTMLArchiver() {
64 void TestMHTMLArchiver::GenerateMHTML() {
65 if (test_scenario_
== TestScenario::WEB_CONTENTS_MISSING
) {
66 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE
);
70 if (test_scenario_
== TestScenario::NOT_ABLE_TO_ARCHIVE
) {
71 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
);
75 base::ThreadTaskRunnerHandle::Get()->PostTask(
77 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone
,
78 base::Unretained(this),
80 base::FilePath(kTestFilePath
),
86 class OfflinePageMHTMLArchiverTest
: public testing::Test
{
88 OfflinePageMHTMLArchiverTest();
89 ~OfflinePageMHTMLArchiverTest() override
;
91 // Creates an archiver for testing and specifies a scenario to be used.
92 scoped_ptr
<TestMHTMLArchiver
> CreateArchiver(
94 TestMHTMLArchiver::TestScenario scenario
);
96 // Test tooling methods.
99 base::FilePath
GetTestFilePath() const {
100 return base::FilePath(kTestFilePath
);
103 const OfflinePageArchiver
* last_archiver() const { return last_archiver_
; }
104 OfflinePageArchiver::ArchiverResult
last_result() const {
107 const base::FilePath
& last_file_path() const { return last_file_path_
; }
108 int64
last_file_size() const { return last_file_size_
; }
110 const OfflinePageArchiver::CreateArchiveCallback
callback() {
111 return base::Bind(&OfflinePageMHTMLArchiverTest::OnCreateArchiveDone
,
112 base::Unretained(this));
116 void OnCreateArchiveDone(OfflinePageArchiver
* archiver
,
117 OfflinePageArchiver::ArchiverResult result
,
119 const base::FilePath
& file_path
,
122 OfflinePageArchiver
* last_archiver_
;
123 OfflinePageArchiver::ArchiverResult last_result_
;
125 base::FilePath last_file_path_
;
126 int64 last_file_size_
;
128 base::MessageLoop message_loop_
;
129 scoped_ptr
<base::RunLoop
> run_loop_
;
131 DISALLOW_COPY_AND_ASSIGN(OfflinePageMHTMLArchiverTest
);
134 OfflinePageMHTMLArchiverTest::OfflinePageMHTMLArchiverTest()
135 : last_archiver_(nullptr),
136 last_result_(OfflinePageArchiver::ArchiverResult::
137 ERROR_ARCHIVE_CREATION_FAILED
),
138 last_file_size_(0L) {
141 OfflinePageMHTMLArchiverTest::~OfflinePageMHTMLArchiverTest() {
144 scoped_ptr
<TestMHTMLArchiver
> OfflinePageMHTMLArchiverTest::CreateArchiver(
146 TestMHTMLArchiver::TestScenario scenario
) {
147 return scoped_ptr
<TestMHTMLArchiver
>(
148 new TestMHTMLArchiver(url
, scenario
, GetTestFilePath()));
151 void OfflinePageMHTMLArchiverTest::OnCreateArchiveDone(
152 OfflinePageArchiver
* archiver
,
153 OfflinePageArchiver::ArchiverResult result
,
155 const base::FilePath
& file_path
,
160 last_archiver_
= archiver
;
161 last_result_
= result
;
162 last_file_path_
= file_path
;
163 last_file_size_
= file_size
;
166 void OfflinePageMHTMLArchiverTest::PumpLoop() {
167 run_loop_
.reset(new base::RunLoop());
171 // Tests that creation of an archiver fails when web contents is missing.
172 TEST_F(OfflinePageMHTMLArchiverTest
, WebContentsMissing
) {
173 GURL page_url
= GURL(kTestURL
);
174 scoped_ptr
<TestMHTMLArchiver
> archiver(
175 CreateArchiver(page_url
,
176 TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING
));
177 archiver
->CreateArchive(callback());
179 EXPECT_EQ(archiver
.get(), last_archiver());
180 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE
,
182 EXPECT_EQ(base::FilePath(), last_file_path());
185 // Tests for successful creation of the offline page archive.
186 TEST_F(OfflinePageMHTMLArchiverTest
, NotAbleToGenerateArchive
) {
187 GURL page_url
= GURL(kTestURL
);
188 scoped_ptr
<TestMHTMLArchiver
> archiver(
189 CreateArchiver(page_url
,
190 TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE
));
191 archiver
->CreateArchive(callback());
193 EXPECT_EQ(archiver
.get(), last_archiver());
194 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
,
196 EXPECT_EQ(base::FilePath(), last_file_path());
197 EXPECT_EQ(0LL, last_file_size());
200 // Tests for successful creation of the offline page archive.
201 TEST_F(OfflinePageMHTMLArchiverTest
, SuccessfullyCreateOfflineArchive
) {
202 GURL page_url
= GURL(kTestURL
);
203 scoped_ptr
<TestMHTMLArchiver
> archiver(
204 CreateArchiver(page_url
,
205 TestMHTMLArchiver::TestScenario::SUCCESS
));
206 archiver
->CreateArchive(callback());
209 EXPECT_EQ(archiver
.get(), last_archiver());
210 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
212 EXPECT_EQ(GetTestFilePath(), last_file_path());
213 EXPECT_EQ(kTestFileSize
, last_file_size());
216 } // namespace offline_pages