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 "chrome/browser/android/offline_pages/offline_page_web_contents_observer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace offline_pages
{
23 const char kTestURL
[] = "http://example.com/";
24 const base::FilePath::CharType kTestFilePath
[] = FILE_PATH_LITERAL(
25 "/archive_dir/offline_page.mhtml");
26 const int64 kTestFileSize
= 123456LL;
28 class TestMHTMLArchiver
: public OfflinePageMHTMLArchiver
{
30 enum class TestScenario
{
38 const TestScenario test_scenario
,
39 const base::FilePath
& archive_dir
);
40 ~TestMHTMLArchiver() override
;
43 void GenerateMHTML() override
;
46 const base::FilePath file_path_
;
47 const TestScenario test_scenario_
;
49 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver
);
52 TestMHTMLArchiver::TestMHTMLArchiver(
54 const TestScenario test_scenario
,
55 const base::FilePath
& archive_dir
)
56 : OfflinePageMHTMLArchiver(archive_dir
),
58 file_path_(archive_dir
),
59 test_scenario_(test_scenario
) {
62 TestMHTMLArchiver::~TestMHTMLArchiver() {
65 void TestMHTMLArchiver::GenerateMHTML() {
66 if (test_scenario_
== TestScenario::WEB_CONTENTS_MISSING
) {
67 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE
);
71 if (test_scenario_
== TestScenario::NOT_ABLE_TO_ARCHIVE
) {
72 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
);
76 base::ThreadTaskRunnerHandle::Get()->PostTask(
78 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone
,
79 base::Unretained(this),
81 base::FilePath(kTestFilePath
),
87 class OfflinePageMHTMLArchiverTest
: public testing::Test
{
89 OfflinePageMHTMLArchiverTest();
90 ~OfflinePageMHTMLArchiverTest() override
;
92 // Creates an archiver for testing and specifies a scenario to be used.
93 scoped_ptr
<TestMHTMLArchiver
> CreateArchiver(
95 TestMHTMLArchiver::TestScenario scenario
);
97 // Test tooling methods.
100 base::FilePath
GetTestFilePath() const {
101 return base::FilePath(kTestFilePath
);
104 const OfflinePageArchiver
* last_archiver() const { return last_archiver_
; }
105 OfflinePageArchiver::ArchiverResult
last_result() const {
108 const base::FilePath
& last_file_path() const { return last_file_path_
; }
109 int64
last_file_size() const { return last_file_size_
; }
111 const OfflinePageArchiver::CreateArchiveCallback
callback() {
112 return base::Bind(&OfflinePageMHTMLArchiverTest::OnCreateArchiveDone
,
113 base::Unretained(this));
117 void OnCreateArchiveDone(OfflinePageArchiver
* archiver
,
118 OfflinePageArchiver::ArchiverResult result
,
120 const base::FilePath
& file_path
,
123 OfflinePageArchiver
* last_archiver_
;
124 OfflinePageArchiver::ArchiverResult last_result_
;
126 base::FilePath last_file_path_
;
127 int64 last_file_size_
;
129 base::MessageLoop message_loop_
;
130 scoped_ptr
<base::RunLoop
> run_loop_
;
132 DISALLOW_COPY_AND_ASSIGN(OfflinePageMHTMLArchiverTest
);
135 OfflinePageMHTMLArchiverTest::OfflinePageMHTMLArchiverTest()
136 : last_archiver_(nullptr),
137 last_result_(OfflinePageArchiver::ArchiverResult::
138 ERROR_ARCHIVE_CREATION_FAILED
),
139 last_file_size_(0L) {
142 OfflinePageMHTMLArchiverTest::~OfflinePageMHTMLArchiverTest() {
145 scoped_ptr
<TestMHTMLArchiver
> OfflinePageMHTMLArchiverTest::CreateArchiver(
147 TestMHTMLArchiver::TestScenario scenario
) {
148 return scoped_ptr
<TestMHTMLArchiver
>(
149 new TestMHTMLArchiver(url
, scenario
, GetTestFilePath()));
152 void OfflinePageMHTMLArchiverTest::OnCreateArchiveDone(
153 OfflinePageArchiver
* archiver
,
154 OfflinePageArchiver::ArchiverResult result
,
156 const base::FilePath
& file_path
,
161 last_archiver_
= archiver
;
162 last_result_
= result
;
163 last_file_path_
= file_path
;
164 last_file_size_
= file_size
;
167 void OfflinePageMHTMLArchiverTest::PumpLoop() {
168 run_loop_
.reset(new base::RunLoop());
172 // Tests that creation of an archiver fails when web contents is missing.
173 TEST_F(OfflinePageMHTMLArchiverTest
, WebContentsMissing
) {
174 GURL page_url
= GURL(kTestURL
);
175 scoped_ptr
<TestMHTMLArchiver
> archiver(
176 CreateArchiver(page_url
,
177 TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING
));
178 archiver
->CreateArchive(callback());
180 EXPECT_EQ(archiver
.get(), last_archiver());
181 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE
,
183 EXPECT_EQ(base::FilePath(), last_file_path());
186 // Tests for successful creation of the offline page archive.
187 TEST_F(OfflinePageMHTMLArchiverTest
, NotAbleToGenerateArchive
) {
188 GURL page_url
= GURL(kTestURL
);
189 scoped_ptr
<TestMHTMLArchiver
> archiver(
190 CreateArchiver(page_url
,
191 TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE
));
192 archiver
->CreateArchive(callback());
194 EXPECT_EQ(archiver
.get(), last_archiver());
195 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED
,
197 EXPECT_EQ(base::FilePath(), last_file_path());
198 EXPECT_EQ(0LL, last_file_size());
201 // Tests for successful creation of the offline page archive.
202 TEST_F(OfflinePageMHTMLArchiverTest
, SuccessfullyCreateOfflineArchive
) {
203 GURL page_url
= GURL(kTestURL
);
204 scoped_ptr
<TestMHTMLArchiver
> archiver(
205 CreateArchiver(page_url
,
206 TestMHTMLArchiver::TestScenario::SUCCESS
));
207 archiver
->CreateArchive(callback());
210 EXPECT_EQ(archiver
.get(), last_archiver());
211 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED
,
213 EXPECT_EQ(GetTestFilePath(), last_file_path());
214 EXPECT_EQ(kTestFileSize
, last_file_size());
217 } // namespace offline_pages