Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / android / offline_pages / offline_page_mhtml_archiver_unittest.cc
blobfb16d384e89fe1aebdda34c1151f280a20d279b8
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"
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 "base/thread_task_runner_handle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace offline_pages {
20 namespace {
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 {
28 public:
29 enum class TestScenario {
30 SUCCESS,
31 NOT_ABLE_TO_ARCHIVE,
32 WEB_CONTENTS_MISSING,
35 TestMHTMLArchiver(
36 const GURL& url,
37 const TestScenario test_scenario,
38 const base::FilePath& archive_dir);
39 ~TestMHTMLArchiver() override;
41 private:
42 void GenerateMHTML() override;
44 const GURL url_;
45 const base::FilePath file_path_;
46 const TestScenario test_scenario_;
48 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver);
51 TestMHTMLArchiver::TestMHTMLArchiver(
52 const GURL& url,
53 const TestScenario test_scenario,
54 const base::FilePath& archive_dir)
55 : OfflinePageMHTMLArchiver(archive_dir),
56 url_(url),
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);
67 return;
70 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) {
71 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED);
72 return;
75 base::ThreadTaskRunnerHandle::Get()->PostTask(
76 FROM_HERE,
77 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone,
78 base::Unretained(this),
79 url_,
80 base::FilePath(kTestFilePath),
81 kTestFileSize));
84 } // namespace
86 class OfflinePageMHTMLArchiverTest : public testing::Test {
87 public:
88 OfflinePageMHTMLArchiverTest();
89 ~OfflinePageMHTMLArchiverTest() override;
91 // Creates an archiver for testing and specifies a scenario to be used.
92 scoped_ptr<TestMHTMLArchiver> CreateArchiver(
93 const GURL& url,
94 TestMHTMLArchiver::TestScenario scenario);
96 // Test tooling methods.
97 void PumpLoop();
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 {
105 return last_result_;
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));
115 private:
116 void OnCreateArchiveDone(OfflinePageArchiver* archiver,
117 OfflinePageArchiver::ArchiverResult result,
118 const GURL& url,
119 const base::FilePath& file_path,
120 int64 file_size);
122 OfflinePageArchiver* last_archiver_;
123 OfflinePageArchiver::ArchiverResult last_result_;
124 GURL last_url_;
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(
145 const GURL& url,
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,
154 const GURL& url,
155 const base::FilePath& file_path,
156 int64 file_size) {
157 if (run_loop_)
158 run_loop_->Quit();
159 last_url_ = url;
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());
168 run_loop_->Run();
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,
181 last_result());
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,
195 last_result());
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());
207 PumpLoop();
209 EXPECT_EQ(archiver.get(), last_archiver());
210 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED,
211 last_result());
212 EXPECT_EQ(GetTestFilePath(), last_file_path());
213 EXPECT_EQ(kTestFileSize, last_file_size());
216 } // namespace offline_pages