Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / android / offline_pages / offline_page_mhtml_archiver_unittest.cc
blob2631f6769d0c4f09c5adb19810b706a021b7292d
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 "chrome/browser/android/offline_pages/offline_page_web_contents_observer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace offline_pages {
21 namespace {
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 {
29 public:
30 enum class TestScenario {
31 SUCCESS,
32 NOT_ABLE_TO_ARCHIVE,
33 WEB_CONTENTS_MISSING,
36 TestMHTMLArchiver(
37 const GURL& url,
38 const TestScenario test_scenario,
39 const base::FilePath& archive_dir);
40 ~TestMHTMLArchiver() override;
42 private:
43 void GenerateMHTML() override;
45 const GURL url_;
46 const base::FilePath file_path_;
47 const TestScenario test_scenario_;
49 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver);
52 TestMHTMLArchiver::TestMHTMLArchiver(
53 const GURL& url,
54 const TestScenario test_scenario,
55 const base::FilePath& archive_dir)
56 : OfflinePageMHTMLArchiver(archive_dir),
57 url_(url),
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);
68 return;
71 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) {
72 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED);
73 return;
76 base::ThreadTaskRunnerHandle::Get()->PostTask(
77 FROM_HERE,
78 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone,
79 base::Unretained(this),
80 url_,
81 base::FilePath(kTestFilePath),
82 kTestFileSize));
85 } // namespace
87 class OfflinePageMHTMLArchiverTest : public testing::Test {
88 public:
89 OfflinePageMHTMLArchiverTest();
90 ~OfflinePageMHTMLArchiverTest() override;
92 // Creates an archiver for testing and specifies a scenario to be used.
93 scoped_ptr<TestMHTMLArchiver> CreateArchiver(
94 const GURL& url,
95 TestMHTMLArchiver::TestScenario scenario);
97 // Test tooling methods.
98 void PumpLoop();
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 {
106 return last_result_;
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));
116 private:
117 void OnCreateArchiveDone(OfflinePageArchiver* archiver,
118 OfflinePageArchiver::ArchiverResult result,
119 const GURL& url,
120 const base::FilePath& file_path,
121 int64 file_size);
123 OfflinePageArchiver* last_archiver_;
124 OfflinePageArchiver::ArchiverResult last_result_;
125 GURL last_url_;
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(
146 const GURL& url,
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,
155 const GURL& url,
156 const base::FilePath& file_path,
157 int64 file_size) {
158 if (run_loop_)
159 run_loop_->Quit();
160 last_url_ = url;
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());
169 run_loop_->Run();
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,
182 last_result());
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,
196 last_result());
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());
208 PumpLoop();
210 EXPECT_EQ(archiver.get(), last_archiver());
211 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED,
212 last_result());
213 EXPECT_EQ(GetTestFilePath(), last_file_path());
214 EXPECT_EQ(kTestFileSize, last_file_size());
217 } // namespace offline_pages