| 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 19 matching lines...) Expand all Loading... |
| 30 const int64_t kTestFileSize = 123456LL; | 30 const int64_t kTestFileSize = 123456LL; |
| 31 const base::string16 kTestTitle = base::UTF8ToUTF16("a title"); | 31 const base::string16 kTestTitle = base::UTF8ToUTF16("a title"); |
| 32 | 32 |
| 33 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { | 33 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { |
| 34 public: | 34 public: |
| 35 enum class TestScenario { | 35 enum class TestScenario { |
| 36 SUCCESS, | 36 SUCCESS, |
| 37 NOT_ABLE_TO_ARCHIVE, | 37 NOT_ABLE_TO_ARCHIVE, |
| 38 WEB_CONTENTS_MISSING, | 38 WEB_CONTENTS_MISSING, |
| 39 CONNECTION_SECURITY_ERROR, | 39 CONNECTION_SECURITY_ERROR, |
| 40 ERROR_PAGE, |
| 41 INTERSTITIAL_PAGE, |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 TestMHTMLArchiver(const GURL& url, const TestScenario test_scenario); | 44 TestMHTMLArchiver(const GURL& url, const TestScenario test_scenario); |
| 43 ~TestMHTMLArchiver() override; | 45 ~TestMHTMLArchiver() override; |
| 44 | 46 |
| 45 private: | 47 private: |
| 46 void GenerateMHTML(const base::FilePath& archives_dir, | 48 void GenerateMHTML(const base::FilePath& archives_dir, |
| 47 const CreateArchiveParams& create_archive_params) override; | 49 const CreateArchiveParams& create_archive_params) override; |
| 48 bool HasConnectionSecurityError() override; | 50 bool HasConnectionSecurityError() override; |
| 51 content::PageType GetPageType() override; |
| 49 | 52 |
| 50 const GURL url_; | 53 const GURL url_; |
| 51 const TestScenario test_scenario_; | 54 const TestScenario test_scenario_; |
| 52 | 55 |
| 53 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); | 56 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 TestMHTMLArchiver::TestMHTMLArchiver(const GURL& url, | 59 TestMHTMLArchiver::TestMHTMLArchiver(const GURL& url, |
| 57 const TestScenario test_scenario) | 60 const TestScenario test_scenario) |
| 58 : url_(url), | 61 : url_(url), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 79 FROM_HERE, | 82 FROM_HERE, |
| 80 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone, | 83 base::Bind(&TestMHTMLArchiver::OnGenerateMHTMLDone, |
| 81 base::Unretained(this), url_, base::FilePath(kTestFilePath), | 84 base::Unretained(this), url_, base::FilePath(kTestFilePath), |
| 82 kTestTitle, kTestFileSize)); | 85 kTestTitle, kTestFileSize)); |
| 83 } | 86 } |
| 84 | 87 |
| 85 bool TestMHTMLArchiver::HasConnectionSecurityError() { | 88 bool TestMHTMLArchiver::HasConnectionSecurityError() { |
| 86 return test_scenario_ == TestScenario::CONNECTION_SECURITY_ERROR; | 89 return test_scenario_ == TestScenario::CONNECTION_SECURITY_ERROR; |
| 87 } | 90 } |
| 88 | 91 |
| 92 content::PageType TestMHTMLArchiver::GetPageType() { |
| 93 if (test_scenario_ == TestScenario::ERROR_PAGE) |
| 94 return content::PageType::PAGE_TYPE_ERROR; |
| 95 if (test_scenario_ == TestScenario::INTERSTITIAL_PAGE) |
| 96 return content::PageType::PAGE_TYPE_INTERSTITIAL; |
| 97 return content::PageType::PAGE_TYPE_NORMAL; |
| 98 } |
| 99 |
| 89 } // namespace | 100 } // namespace |
| 90 | 101 |
| 91 class OfflinePageMHTMLArchiverTest : public testing::Test { | 102 class OfflinePageMHTMLArchiverTest : public testing::Test { |
| 92 public: | 103 public: |
| 93 OfflinePageMHTMLArchiverTest(); | 104 OfflinePageMHTMLArchiverTest(); |
| 94 ~OfflinePageMHTMLArchiverTest() override; | 105 ~OfflinePageMHTMLArchiverTest() override; |
| 95 | 106 |
| 96 // Creates an archiver for testing scenario and uses it to create an archive. | 107 // Creates an archiver for testing scenario and uses it to create an archive. |
| 97 std::unique_ptr<TestMHTMLArchiver> CreateArchive( | 108 std::unique_ptr<TestMHTMLArchiver> CreateArchive( |
| 98 const GURL& url, | 109 const GURL& url, |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchive( | 221 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchive( |
| 211 page_url, TestMHTMLArchiver::TestScenario::CONNECTION_SECURITY_ERROR)); | 222 page_url, TestMHTMLArchiver::TestScenario::CONNECTION_SECURITY_ERROR)); |
| 212 | 223 |
| 213 EXPECT_EQ(archiver.get(), last_archiver()); | 224 EXPECT_EQ(archiver.get(), last_archiver()); |
| 214 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_SECURITY_CERTIFICATE, | 225 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_SECURITY_CERTIFICATE, |
| 215 last_result()); | 226 last_result()); |
| 216 EXPECT_EQ(base::FilePath(), last_file_path()); | 227 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 217 EXPECT_EQ(0LL, last_file_size()); | 228 EXPECT_EQ(0LL, last_file_size()); |
| 218 } | 229 } |
| 219 | 230 |
| 231 // Tests for archiver handling of an error page. |
| 232 TEST_F(OfflinePageMHTMLArchiverTest, PageError) { |
| 233 GURL page_url = GURL(kTestURL); |
| 234 std::unique_ptr<TestMHTMLArchiver> archiver( |
| 235 CreateArchive(page_url, TestMHTMLArchiver::TestScenario::ERROR_PAGE)); |
| 236 |
| 237 EXPECT_EQ(archiver.get(), last_archiver()); |
| 238 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ERROR_PAGE, |
| 239 last_result()); |
| 240 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 241 EXPECT_EQ(0LL, last_file_size()); |
| 242 } |
| 243 |
| 244 // Tests for archiver handling of an interstitial page. |
| 245 TEST_F(OfflinePageMHTMLArchiverTest, InterstitialPage) { |
| 246 GURL page_url = GURL(kTestURL); |
| 247 std::unique_ptr<TestMHTMLArchiver> archiver(CreateArchive( |
| 248 page_url, TestMHTMLArchiver::TestScenario::INTERSTITIAL_PAGE)); |
| 249 EXPECT_EQ(archiver.get(), last_archiver()); |
| 250 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_INTERSTITIAL_PAGE, |
| 251 last_result()); |
| 252 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 253 EXPECT_EQ(0LL, last_file_size()); |
| 254 } |
| 255 |
| 220 // Tests for successful creation of the offline page archive. | 256 // Tests for successful creation of the offline page archive. |
| 221 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { | 257 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { |
| 222 GURL page_url = GURL(kTestURL); | 258 GURL page_url = GURL(kTestURL); |
| 223 std::unique_ptr<TestMHTMLArchiver> archiver( | 259 std::unique_ptr<TestMHTMLArchiver> archiver( |
| 224 CreateArchive(page_url, TestMHTMLArchiver::TestScenario::SUCCESS)); | 260 CreateArchive(page_url, TestMHTMLArchiver::TestScenario::SUCCESS)); |
| 225 PumpLoop(); | 261 PumpLoop(); |
| 226 | 262 |
| 227 EXPECT_EQ(archiver.get(), last_archiver()); | 263 EXPECT_EQ(archiver.get(), last_archiver()); |
| 228 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 264 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 229 last_result()); | 265 last_result()); |
| 230 EXPECT_EQ(GetTestFilePath(), last_file_path()); | 266 EXPECT_EQ(GetTestFilePath(), last_file_path()); |
| 231 EXPECT_EQ(kTestFileSize, last_file_size()); | 267 EXPECT_EQ(kTestFileSize, last_file_size()); |
| 232 } | 268 } |
| 233 | 269 |
| 234 } // namespace offline_pages | 270 } // namespace offline_pages |
| OLD | NEW |