| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" | 5 #include "chrome/browser/android/offline_pages/offline_page_mhtml_archiver.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 namespace offline_pages { | 23 namespace offline_pages { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const char kTestURL[] = "http://example.com/"; | 27 const char kTestURL[] = "http://example.com/"; |
| 28 const base::FilePath::CharType kTestFilePath[] = FILE_PATH_LITERAL( | 28 const base::FilePath::CharType kTestFilePath[] = FILE_PATH_LITERAL( |
| 29 "/archive_dir/offline_page.mhtml"); | 29 "/archive_dir/offline_page.mhtml"); |
| 30 const int64_t kTestFileSize = 123456LL; | 30 const int64_t kTestFileSize = 123456LL; |
| 31 const int64_t kTestArcihveId = 123456789LL; | |
| 32 const base::string16 kTestTitle = base::UTF8ToUTF16("a title"); | 31 const base::string16 kTestTitle = base::UTF8ToUTF16("a title"); |
| 33 | 32 |
| 34 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { | 33 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { |
| 35 public: | 34 public: |
| 36 enum class TestScenario { | 35 enum class TestScenario { |
| 37 SUCCESS, | 36 SUCCESS, |
| 38 NOT_ABLE_TO_ARCHIVE, | 37 NOT_ABLE_TO_ARCHIVE, |
| 39 WEB_CONTENTS_MISSING, | 38 WEB_CONTENTS_MISSING, |
| 40 CONNECTION_SECURITY_ERROR, | 39 CONNECTION_SECURITY_ERROR, |
| 41 }; | 40 }; |
| 42 | 41 |
| 43 TestMHTMLArchiver(const GURL& url, const TestScenario test_scenario); | 42 TestMHTMLArchiver(const GURL& url, const TestScenario test_scenario); |
| 44 ~TestMHTMLArchiver() override; | 43 ~TestMHTMLArchiver() override; |
| 45 | 44 |
| 46 private: | 45 private: |
| 47 void GenerateMHTML(const base::FilePath& archives_dir, | 46 void GenerateMHTML(const base::FilePath& archives_dir, |
| 48 int64_t archive_id) override; | 47 const CreateArchiveParams& create_archive_params) override; |
| 49 bool HasConnectionSecurityError() override; | 48 bool HasConnectionSecurityError() override; |
| 50 | 49 |
| 51 const GURL url_; | 50 const GURL url_; |
| 52 const TestScenario test_scenario_; | 51 const TestScenario test_scenario_; |
| 53 | 52 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); | 53 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); |
| 55 }; | 54 }; |
| 56 | 55 |
| 57 TestMHTMLArchiver::TestMHTMLArchiver(const GURL& url, | 56 TestMHTMLArchiver::TestMHTMLArchiver(const GURL& url, |
| 58 const TestScenario test_scenario) | 57 const TestScenario test_scenario) |
| 59 : url_(url), | 58 : url_(url), |
| 60 test_scenario_(test_scenario) { | 59 test_scenario_(test_scenario) { |
| 61 } | 60 } |
| 62 | 61 |
| 63 TestMHTMLArchiver::~TestMHTMLArchiver() { | 62 TestMHTMLArchiver::~TestMHTMLArchiver() { |
| 64 } | 63 } |
| 65 | 64 |
| 66 void TestMHTMLArchiver::GenerateMHTML(const base::FilePath& archives_dir, | 65 void TestMHTMLArchiver::GenerateMHTML( |
| 67 int64_t archive_id) { | 66 const base::FilePath& archives_dir, |
| 67 const CreateArchiveParams& create_archive_params) { |
| 68 if (test_scenario_ == TestScenario::WEB_CONTENTS_MISSING) { | 68 if (test_scenario_ == TestScenario::WEB_CONTENTS_MISSING) { |
| 69 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 69 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
| 70 return; | 70 return; |
| 71 } | 71 } |
| 72 | 72 |
| 73 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) { | 73 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) { |
| 74 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); | 74 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); |
| 75 return; | 75 return; |
| 76 } | 76 } |
| 77 | 77 |
| 78 base::ThreadTaskRunnerHandle::Get()->PostTask( | 78 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 79 FROM_HERE, | 79 FROM_HERE, |
| 80 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone, | 80 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone, |
| 81 base::Unretained(this), url_, base::FilePath(kTestFilePath), | 81 base::Unretained(this), url_, base::FilePath(kTestFilePath), |
| 82 kTestTitle, kTestFileSize)); | 82 kTestTitle, kTestFileSize)); |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool TestMHTMLArchiver::HasConnectionSecurityError() { | 85 bool TestMHTMLArchiver::HasConnectionSecurityError() { |
| 86 return test_scenario_ == TestScenario::CONNECTION_SECURITY_ERROR; | 86 return test_scenario_ == TestScenario::CONNECTION_SECURITY_ERROR; |
| 87 } | 87 } |
| 88 | 88 |
| 89 } // namespace | 89 } // namespace |
| 90 | 90 |
| 91 class OfflinePageMHTMLArchiverTest : public testing::Test { | 91 class OfflinePageMHTMLArchiverTest : public testing::Test { |
| 92 public: | 92 public: |
| 93 OfflinePageMHTMLArchiverTest(); | 93 OfflinePageMHTMLArchiverTest(); |
| 94 ~OfflinePageMHTMLArchiverTest() override; | 94 ~OfflinePageMHTMLArchiverTest() override; |
| 95 | 95 |
| 96 // Creates an archiver for testing and specifies a scenario to be used. | 96 // Creates an archiver for testing scenario and uses it to create an archive. |
| 97 std::unique_ptr<TestMHTMLArchiver> CreateArchiver( | 97 std::unique_ptr<TestMHTMLArchiver> CreateArchive( |
| 98 const GURL& url, | 98 const GURL& url, |
| 99 TestMHTMLArchiver::TestScenario scenario); | 99 TestMHTMLArchiver::TestScenario scenario); |
| 100 | 100 |
| 101 // Test tooling methods. | 101 // Test tooling methods. |
| 102 void PumpLoop(); | 102 void PumpLoop(); |
| 103 | 103 |
| 104 base::FilePath GetTestFilePath() const { | 104 base::FilePath GetTestFilePath() const { |
| 105 return base::FilePath(kTestFilePath); | 105 return base::FilePath(kTestFilePath); |
| 106 } | 106 } |
| 107 | 107 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 last_result_(OfflinePageArchiver::ArchiverResult:: | 142 last_result_(OfflinePageArchiver::ArchiverResult:: |
| 143 ERROR_ARCHIVE_CREATION_FAILED), | 143 ERROR_ARCHIVE_CREATION_FAILED), |
| 144 last_file_size_(0L), | 144 last_file_size_(0L), |
| 145 task_runner_(new base::TestSimpleTaskRunner), | 145 task_runner_(new base::TestSimpleTaskRunner), |
| 146 task_runner_handle_(task_runner_) { | 146 task_runner_handle_(task_runner_) { |
| 147 } | 147 } |
| 148 | 148 |
| 149 OfflinePageMHTMLArchiverTest::~OfflinePageMHTMLArchiverTest() { | 149 OfflinePageMHTMLArchiverTest::~OfflinePageMHTMLArchiverTest() { |
| 150 } | 150 } |
| 151 | 151 |
| 152 std::unique_ptr<TestMHTMLArchiver> OfflinePageMHTMLArchiverTest::CreateArchiver( | 152 std::unique_ptr<TestMHTMLArchiver> OfflinePageMHTMLArchiverTest::CreateArchive( |
| 153 const GURL& url, | 153 const GURL& url, |
| 154 TestMHTMLArchiver::TestScenario scenario) { | 154 TestMHTMLArchiver::TestScenario scenario) { |
| 155 return std::unique_ptr<TestMHTMLArchiver>( | 155 std::unique_ptr<TestMHTMLArchiver> archiver( |
| 156 new TestMHTMLArchiver(url, scenario)); | 156 new TestMHTMLArchiver(url, scenario)); |
| 157 archiver->CreateArchive(GetTestFilePath(), |
| 158 OfflinePageArchiver::CreateArchiveParams(), |
| 159 callback()); |
| 160 PumpLoop(); |
| 161 return archiver; |
| 157 } | 162 } |
| 158 | 163 |
| 159 void OfflinePageMHTMLArchiverTest::OnCreateArchiveDone( | 164 void OfflinePageMHTMLArchiverTest::OnCreateArchiveDone( |
| 160 OfflinePageArchiver* archiver, | 165 OfflinePageArchiver* archiver, |
| 161 OfflinePageArchiver::ArchiverResult result, | 166 OfflinePageArchiver::ArchiverResult result, |
| 162 const GURL& url, | 167 const GURL& url, |
| 163 const base::FilePath& file_path, | 168 const base::FilePath& file_path, |
| 164 const base::string16& title, | 169 const base::string16& title, |
| 165 int64_t file_size) { | 170 int64_t file_size) { |
| 166 last_url_ = url; | 171 last_url_ = url; |
| 167 last_archiver_ = archiver; | 172 last_archiver_ = archiver; |
| 168 last_result_ = result; | 173 last_result_ = result; |
| 169 last_file_path_ = file_path; | 174 last_file_path_ = file_path; |
| 170 last_file_size_ = file_size; | 175 last_file_size_ = file_size; |
| 171 } | 176 } |
| 172 | 177 |
| 173 void OfflinePageMHTMLArchiverTest::PumpLoop() { | 178 void OfflinePageMHTMLArchiverTest::PumpLoop() { |
| 174 task_runner_->RunUntilIdle(); | 179 task_runner_->RunUntilIdle(); |
| 175 } | 180 } |
| 176 | 181 |
| 177 // Tests that creation of an archiver fails when web contents is missing. | 182 // Tests that creation of an archiver fails when web contents is missing. |
| 178 TEST_F(OfflinePageMHTMLArchiverTest, WebContentsMissing) { | 183 TEST_F(OfflinePageMHTMLArchiverTest, WebContentsMissing) { |
| 179 GURL page_url = GURL(kTestURL); | 184 GURL page_url = GURL(kTestURL); |
| 180 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchiver( | 185 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchive( |
| 181 page_url, TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING)); | 186 page_url, TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING)); |
| 182 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); | |
| 183 PumpLoop(); | |
| 184 | 187 |
| 185 EXPECT_EQ(archiver.get(), last_archiver()); | 188 EXPECT_EQ(archiver.get(), last_archiver()); |
| 186 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE, | 189 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE, |
| 187 last_result()); | 190 last_result()); |
| 188 EXPECT_EQ(base::FilePath(), last_file_path()); | 191 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 189 } | 192 } |
| 190 | 193 |
| 191 // Tests for archiver failing save an archive. | 194 // Tests for archiver failing save an archive. |
| 192 TEST_F(OfflinePageMHTMLArchiverTest, NotAbleToGenerateArchive) { | 195 TEST_F(OfflinePageMHTMLArchiverTest, NotAbleToGenerateArchive) { |
| 193 GURL page_url = GURL(kTestURL); | 196 GURL page_url = GURL(kTestURL); |
| 194 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchiver( | 197 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchive( |
| 195 page_url, TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE)); | 198 page_url, TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE)); |
| 196 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); | |
| 197 PumpLoop(); | |
| 198 | 199 |
| 199 EXPECT_EQ(archiver.get(), last_archiver()); | 200 EXPECT_EQ(archiver.get(), last_archiver()); |
| 200 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED, | 201 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED, |
| 201 last_result()); | 202 last_result()); |
| 202 EXPECT_EQ(base::FilePath(), last_file_path()); | 203 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 203 EXPECT_EQ(0LL, last_file_size()); | 204 EXPECT_EQ(0LL, last_file_size()); |
| 204 } | 205 } |
| 205 | 206 |
| 206 // Tests for archiver handling of non-secure connection. | 207 // Tests for archiver handling of non-secure connection. |
| 207 TEST_F(OfflinePageMHTMLArchiverTest, ConnectionNotSecure) { | 208 TEST_F(OfflinePageMHTMLArchiverTest, ConnectionNotSecure) { |
| 208 GURL page_url = GURL(kTestURL); | 209 GURL page_url = GURL(kTestURL); |
| 209 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchiver( | 210 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchive( |
| 210 page_url, TestMHTMLArchiver::TestScenario::CONNECTION_SECURITY_ERROR)); | 211 page_url, TestMHTMLArchiver::TestScenario::CONNECTION_SECURITY_ERROR)); |
| 211 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); | |
| 212 PumpLoop(); | |
| 213 | 212 |
| 214 EXPECT_EQ(archiver.get(), last_archiver()); | 213 EXPECT_EQ(archiver.get(), last_archiver()); |
| 215 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_SECURITY_CERTIFICATE, | 214 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_SECURITY_CERTIFICATE, |
| 216 last_result()); | 215 last_result()); |
| 217 EXPECT_EQ(base::FilePath(), last_file_path()); | 216 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 218 EXPECT_EQ(0LL, last_file_size()); | 217 EXPECT_EQ(0LL, last_file_size()); |
| 219 } | 218 } |
| 220 | 219 |
| 221 // Tests for successful creation of the offline page archive. | 220 // Tests for successful creation of the offline page archive. |
| 222 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { | 221 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { |
| 223 GURL page_url = GURL(kTestURL); | 222 GURL page_url = GURL(kTestURL); |
| 224 std::unique_ptr<TestMHTMLArchiver> archiver( | 223 std::unique_ptr<TestMHTMLArchiver> archiver( |
| 225 CreateArchiver(page_url, TestMHTMLArchiver::TestScenario::SUCCESS)); | 224 CreateArchive(page_url, TestMHTMLArchiver::TestScenario::SUCCESS)); |
| 226 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); | |
| 227 PumpLoop(); | 225 PumpLoop(); |
| 228 | 226 |
| 229 EXPECT_EQ(archiver.get(), last_archiver()); | 227 EXPECT_EQ(archiver.get(), last_archiver()); |
| 230 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 228 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 231 last_result()); | 229 last_result()); |
| 232 EXPECT_EQ(GetTestFilePath(), last_file_path()); | 230 EXPECT_EQ(GetTestFilePath(), last_file_path()); |
| 233 EXPECT_EQ(kTestFileSize, last_file_size()); | 231 EXPECT_EQ(kTestFileSize, last_file_size()); |
| 234 } | 232 } |
| 235 | 233 |
| 236 } // namespace offline_pages | 234 } // namespace offline_pages |
| OLD | NEW |