| 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 "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/test/test_simple_task_runner.h" | 17 #include "base/test/test_simple_task_runner.h" |
| 18 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace offline_pages { | 21 namespace offline_pages { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 const char kTestURL[] = "http://example.com/"; | 25 const char kTestURL[] = "http://example.com/"; |
| 26 const base::FilePath::CharType kTestFilePath[] = FILE_PATH_LITERAL( | 26 const base::FilePath::CharType kTestFilePath[] = FILE_PATH_LITERAL( |
| 27 "/archive_dir/offline_page.mhtml"); | 27 "/archive_dir/offline_page.mhtml"); |
| 28 const int64_t kTestFileSize = 123456LL; | 28 const int64_t kTestFileSize = 123456LL; |
| 29 const int64_t kTestArcihveId = 123456789LL; |
| 29 | 30 |
| 30 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { | 31 class TestMHTMLArchiver : public OfflinePageMHTMLArchiver { |
| 31 public: | 32 public: |
| 32 enum class TestScenario { | 33 enum class TestScenario { |
| 33 SUCCESS, | 34 SUCCESS, |
| 34 NOT_ABLE_TO_ARCHIVE, | 35 NOT_ABLE_TO_ARCHIVE, |
| 35 WEB_CONTENTS_MISSING, | 36 WEB_CONTENTS_MISSING, |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 TestMHTMLArchiver(const GURL& url, const TestScenario test_scenario); | 39 TestMHTMLArchiver(const GURL& url, const TestScenario test_scenario); |
| 39 ~TestMHTMLArchiver() override; | 40 ~TestMHTMLArchiver() override; |
| 40 | 41 |
| 41 private: | 42 private: |
| 42 void GenerateMHTML(const base::FilePath& archives_dir) override; | 43 void GenerateMHTML(const base::FilePath& archives_dir, |
| 44 int64_t archive_id) override; |
| 43 | 45 |
| 44 const GURL url_; | 46 const GURL url_; |
| 45 const TestScenario test_scenario_; | 47 const TestScenario test_scenario_; |
| 46 | 48 |
| 47 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); | 49 DISALLOW_COPY_AND_ASSIGN(TestMHTMLArchiver); |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 TestMHTMLArchiver::TestMHTMLArchiver(const GURL& url, | 52 TestMHTMLArchiver::TestMHTMLArchiver(const GURL& url, |
| 51 const TestScenario test_scenario) | 53 const TestScenario test_scenario) |
| 52 : url_(url), | 54 : url_(url), |
| 53 test_scenario_(test_scenario) { | 55 test_scenario_(test_scenario) { |
| 54 } | 56 } |
| 55 | 57 |
| 56 TestMHTMLArchiver::~TestMHTMLArchiver() { | 58 TestMHTMLArchiver::~TestMHTMLArchiver() { |
| 57 } | 59 } |
| 58 | 60 |
| 59 void TestMHTMLArchiver::GenerateMHTML(const base::FilePath& archives_dir) { | 61 void TestMHTMLArchiver::GenerateMHTML(const base::FilePath& archives_dir, |
| 62 int64_t archive_id) { |
| 60 if (test_scenario_ == TestScenario::WEB_CONTENTS_MISSING) { | 63 if (test_scenario_ == TestScenario::WEB_CONTENTS_MISSING) { |
| 61 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); | 64 ReportFailure(ArchiverResult::ERROR_CONTENT_UNAVAILABLE); |
| 62 return; | 65 return; |
| 63 } | 66 } |
| 64 | 67 |
| 65 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) { | 68 if (test_scenario_ == TestScenario::NOT_ABLE_TO_ARCHIVE) { |
| 66 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); | 69 ReportFailure(ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED); |
| 67 return; | 70 return; |
| 68 } | 71 } |
| 69 | 72 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 void OfflinePageMHTMLArchiverTest::PumpLoop() { | 163 void OfflinePageMHTMLArchiverTest::PumpLoop() { |
| 161 task_runner_->RunUntilIdle(); | 164 task_runner_->RunUntilIdle(); |
| 162 } | 165 } |
| 163 | 166 |
| 164 // Tests that creation of an archiver fails when web contents is missing. | 167 // Tests that creation of an archiver fails when web contents is missing. |
| 165 TEST_F(OfflinePageMHTMLArchiverTest, WebContentsMissing) { | 168 TEST_F(OfflinePageMHTMLArchiverTest, WebContentsMissing) { |
| 166 GURL page_url = GURL(kTestURL); | 169 GURL page_url = GURL(kTestURL); |
| 167 scoped_ptr<TestMHTMLArchiver> archiver( | 170 scoped_ptr<TestMHTMLArchiver> archiver( |
| 168 CreateArchiver(page_url, | 171 CreateArchiver(page_url, |
| 169 TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING)); | 172 TestMHTMLArchiver::TestScenario::WEB_CONTENTS_MISSING)); |
| 170 archiver->CreateArchive(GetTestFilePath(), callback()); | 173 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); |
| 171 | 174 |
| 172 EXPECT_EQ(archiver.get(), last_archiver()); | 175 EXPECT_EQ(archiver.get(), last_archiver()); |
| 173 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE, | 176 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_CONTENT_UNAVAILABLE, |
| 174 last_result()); | 177 last_result()); |
| 175 EXPECT_EQ(base::FilePath(), last_file_path()); | 178 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 176 } | 179 } |
| 177 | 180 |
| 178 // Tests for successful creation of the offline page archive. | 181 // Tests for successful creation of the offline page archive. |
| 179 TEST_F(OfflinePageMHTMLArchiverTest, NotAbleToGenerateArchive) { | 182 TEST_F(OfflinePageMHTMLArchiverTest, NotAbleToGenerateArchive) { |
| 180 GURL page_url = GURL(kTestURL); | 183 GURL page_url = GURL(kTestURL); |
| 181 scoped_ptr<TestMHTMLArchiver> archiver( | 184 scoped_ptr<TestMHTMLArchiver> archiver( |
| 182 CreateArchiver(page_url, | 185 CreateArchiver(page_url, |
| 183 TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE)); | 186 TestMHTMLArchiver::TestScenario::NOT_ABLE_TO_ARCHIVE)); |
| 184 archiver->CreateArchive(GetTestFilePath(), callback()); | 187 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); |
| 185 | 188 |
| 186 EXPECT_EQ(archiver.get(), last_archiver()); | 189 EXPECT_EQ(archiver.get(), last_archiver()); |
| 187 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED, | 190 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::ERROR_ARCHIVE_CREATION_FAILED, |
| 188 last_result()); | 191 last_result()); |
| 189 EXPECT_EQ(base::FilePath(), last_file_path()); | 192 EXPECT_EQ(base::FilePath(), last_file_path()); |
| 190 EXPECT_EQ(0LL, last_file_size()); | 193 EXPECT_EQ(0LL, last_file_size()); |
| 191 } | 194 } |
| 192 | 195 |
| 193 // Tests for successful creation of the offline page archive. | 196 // Tests for successful creation of the offline page archive. |
| 194 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { | 197 TEST_F(OfflinePageMHTMLArchiverTest, SuccessfullyCreateOfflineArchive) { |
| 195 GURL page_url = GURL(kTestURL); | 198 GURL page_url = GURL(kTestURL); |
| 196 scoped_ptr<TestMHTMLArchiver> archiver( | 199 scoped_ptr<TestMHTMLArchiver> archiver( |
| 197 CreateArchiver(page_url, | 200 CreateArchiver(page_url, |
| 198 TestMHTMLArchiver::TestScenario::SUCCESS)); | 201 TestMHTMLArchiver::TestScenario::SUCCESS)); |
| 199 archiver->CreateArchive(GetTestFilePath(), callback()); | 202 archiver->CreateArchive(GetTestFilePath(), kTestArcihveId, callback()); |
| 200 PumpLoop(); | 203 PumpLoop(); |
| 201 | 204 |
| 202 EXPECT_EQ(archiver.get(), last_archiver()); | 205 EXPECT_EQ(archiver.get(), last_archiver()); |
| 203 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, | 206 EXPECT_EQ(OfflinePageArchiver::ArchiverResult::SUCCESSFULLY_CREATED, |
| 204 last_result()); | 207 last_result()); |
| 205 EXPECT_EQ(GetTestFilePath(), last_file_path()); | 208 EXPECT_EQ(GetTestFilePath(), last_file_path()); |
| 206 EXPECT_EQ(kTestFileSize, last_file_size()); | 209 EXPECT_EQ(kTestFileSize, last_file_size()); |
| 207 } | 210 } |
| 208 | 211 |
| 209 TEST_F(OfflinePageMHTMLArchiverTest, GenerateFileName) { | 212 TEST_F(OfflinePageMHTMLArchiverTest, GenerateFileName) { |
| 210 GURL url_1("http://news.google.com/page1"); | 213 GURL url_1("http://news.google.com/page1"); |
| 211 std::string title_1("Google News Page"); | 214 std::string title_1("Google News Page"); |
| 212 base::FilePath expected_1(FILE_PATH_LITERAL( | 215 base::FilePath expected_1(FILE_PATH_LITERAL( |
| 213 "news.google.com-Google_News_Page-mD2VzX6-h86e+Wl20CXh6VEkPXU=.mhtml")); | 216 "news.google.com-Google_News_Page-1234.mhtml")); |
| 214 base::FilePath actual_1( | 217 base::FilePath actual_1( |
| 215 OfflinePageMHTMLArchiver::GenerateFileName(url_1, title_1)); | 218 OfflinePageMHTMLArchiver::GenerateFileName(url_1, title_1, 1234LL)); |
| 216 EXPECT_EQ(expected_1, actual_1); | 219 EXPECT_EQ(expected_1, actual_1); |
| 217 | 220 |
| 218 GURL url_2("https://en.m.wikipedia.org/Sample_page_about_stuff"); | 221 GURL url_2("https://en.m.wikipedia.org/Sample_page_about_stuff"); |
| 219 std::string title_2("Some Wiki Page"); | 222 std::string title_2("Some Wiki Page"); |
| 220 base::FilePath expected_2(FILE_PATH_LITERAL( | 223 base::FilePath expected_2(FILE_PATH_LITERAL( |
| 221 "en.m.wikipedia.org-Some_Wiki_Page-rEdSruS+14jgpnwJN9PGRUDpx9c=.mhtml")); | 224 "en.m.wikipedia.org-Some_Wiki_Page-56789.mhtml")); |
| 222 base::FilePath actual_2( | 225 base::FilePath actual_2( |
| 223 OfflinePageMHTMLArchiver::GenerateFileName(url_2, title_2)); | 226 OfflinePageMHTMLArchiver::GenerateFileName(url_2, title_2, 56789LL)); |
| 224 EXPECT_EQ(expected_2, actual_2); | 227 EXPECT_EQ(expected_2, actual_2); |
| 225 | 228 |
| 226 GURL url_3("https://www.google.com/search"); | 229 GURL url_3("https://www.google.com/search"); |
| 227 std::string title_3 = | 230 std::string title_3 = |
| 228 "A really really really really really long title " | 231 "A really really really really really long title " |
| 229 "that is over 80 chars long here^ - TRUNCATE THIS PART"; | 232 "that is over 80 chars long here^ - TRUNCATE THIS PART"; |
| 230 std::string expected_title_3_part = | 233 std::string expected_title_3_part = |
| 231 "A_really_really_really_really_really_long_title_" | 234 "A_really_really_really_really_really_long_title_" |
| 232 "that_is_over_80_chars_long_here^"; | 235 "that_is_over_80_chars_long_here^"; |
| 233 base::FilePath expected_3( | 236 base::FilePath expected_3( |
| 234 FILE_PATH_LITERAL("www.google.com-" + | 237 FILE_PATH_LITERAL("www.google.com-" + |
| 235 expected_title_3_part + | 238 expected_title_3_part + |
| 236 "-ko+SHbxDoN0rARsFf82l4QubaJE=.mhtml")); | 239 "-123456789.mhtml")); |
| 237 base::FilePath actual_3( | 240 base::FilePath actual_3( |
| 238 OfflinePageMHTMLArchiver::GenerateFileName(url_3, title_3)); | 241 OfflinePageMHTMLArchiver::GenerateFileName(url_3, title_3, 123456789LL)); |
| 239 EXPECT_EQ(expected_3, actual_3);} | 242 EXPECT_EQ(expected_3, actual_3);} |
| 240 | 243 |
| 241 } // namespace offline_pages | 244 } // namespace offline_pages |
| OLD | NEW |