| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdint.h> | 5 #include <stdint.h> |
| 6 #include <memory> |
| 6 | 7 |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/callback.h" | 9 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/test/histogram_tester.h" |
| 15 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 16 #include "content/browser/renderer_host/render_process_host_impl.h" | 18 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 17 #include "content/common/frame_messages.h" | 19 #include "content/common/frame_messages.h" |
| 18 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 20 #include "content/public/common/mhtml_generation_params.h" | 22 #include "content/public/common/mhtml_generation_params.h" |
| 21 #include "content/public/test/browser_test_utils.h" | 23 #include "content/public/test/browser_test_utils.h" |
| 22 #include "content/public/test/content_browser_test.h" | 24 #include "content/public/test/content_browser_test.h" |
| 23 #include "content/public/test/content_browser_test_utils.h" | 25 #include "content/public/test/content_browser_test_utils.h" |
| 24 #include "content/public/test/test_utils.h" | 26 #include "content/public/test/test_utils.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 GenerateMHTML(MHTMLGenerationParams(path), url); | 106 GenerateMHTML(MHTMLGenerationParams(path), url); |
| 105 } | 107 } |
| 106 | 108 |
| 107 void GenerateMHTML(const MHTMLGenerationParams& params, const GURL& url) { | 109 void GenerateMHTML(const MHTMLGenerationParams& params, const GURL& url) { |
| 108 NavigateToURL(shell(), url); | 110 NavigateToURL(shell(), url); |
| 109 GenerateMHTMLForCurrentPage(params); | 111 GenerateMHTMLForCurrentPage(params); |
| 110 } | 112 } |
| 111 | 113 |
| 112 void GenerateMHTMLForCurrentPage(const MHTMLGenerationParams& params) { | 114 void GenerateMHTMLForCurrentPage(const MHTMLGenerationParams& params) { |
| 113 base::RunLoop run_loop; | 115 base::RunLoop run_loop; |
| 116 histogram_tester_.reset(new base::HistogramTester()); |
| 114 | 117 |
| 115 shell()->web_contents()->GenerateMHTML( | 118 shell()->web_contents()->GenerateMHTML( |
| 116 params, base::Bind(&MHTMLGenerationTest::MHTMLGenerated, | 119 params, base::Bind(&MHTMLGenerationTest::MHTMLGenerated, |
| 117 base::Unretained(this), | 120 base::Unretained(this), |
| 118 run_loop.QuitClosure())); | 121 run_loop.QuitClosure())); |
| 119 | 122 |
| 120 // Block until the MHTML is generated. | 123 // Block until the MHTML is generated. |
| 121 run_loop.Run(); | 124 run_loop.Run(); |
| 122 | 125 |
| 123 EXPECT_TRUE(has_mhtml_callback_run()); | 126 EXPECT_TRUE(has_mhtml_callback_run()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 int actual_number_of_matches = delegate.Wait(shell()->web_contents()); | 190 int actual_number_of_matches = delegate.Wait(shell()->web_contents()); |
| 188 EXPECT_EQ(0, actual_number_of_matches) | 191 EXPECT_EQ(0, actual_number_of_matches) |
| 189 << "Verifying that \"" << forbidden_substring << "\" doesn't " | 192 << "Verifying that \"" << forbidden_substring << "\" doesn't " |
| 190 << "appear in the text of web contents of " | 193 << "appear in the text of web contents of " |
| 191 << shell()->web_contents()->GetURL().spec(); | 194 << shell()->web_contents()->GetURL().spec(); |
| 192 } | 195 } |
| 193 } | 196 } |
| 194 | 197 |
| 195 bool has_mhtml_callback_run() const { return has_mhtml_callback_run_; } | 198 bool has_mhtml_callback_run() const { return has_mhtml_callback_run_; } |
| 196 int64_t file_size() const { return file_size_; } | 199 int64_t file_size() const { return file_size_; } |
| 200 base::HistogramTester* histogram_tester() { return histogram_tester_.get(); } |
| 197 | 201 |
| 198 base::ScopedTempDir temp_dir_; | 202 base::ScopedTempDir temp_dir_; |
| 199 | 203 |
| 200 private: | 204 private: |
| 201 void MHTMLGenerated(base::Closure quit_closure, int64_t size) { | 205 void MHTMLGenerated(base::Closure quit_closure, int64_t size) { |
| 202 has_mhtml_callback_run_ = true; | 206 has_mhtml_callback_run_ = true; |
| 203 file_size_ = size; | 207 file_size_ = size; |
| 204 quit_closure.Run(); | 208 quit_closure.Run(); |
| 205 } | 209 } |
| 206 | 210 |
| 207 bool has_mhtml_callback_run_; | 211 bool has_mhtml_callback_run_; |
| 208 int64_t file_size_; | 212 int64_t file_size_; |
| 213 std::unique_ptr<base::HistogramTester> histogram_tester_; |
| 209 }; | 214 }; |
| 210 | 215 |
| 211 // Tests that generating a MHTML does create contents. | 216 // Tests that generating a MHTML does create contents. |
| 212 // Note that the actual content of the file is not tested, the purpose of this | 217 // Note that the actual content of the file is not tested, the purpose of this |
| 213 // test is to ensure we were successful in creating the MHTML data from the | 218 // test is to ensure we were successful in creating the MHTML data from the |
| 214 // renderer. | 219 // renderer. |
| 215 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTML) { | 220 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTML) { |
| 216 base::FilePath path(temp_dir_.GetPath()); | 221 base::FilePath path(temp_dir_.GetPath()); |
| 217 path = path.Append(FILE_PATH_LITERAL("test.mht")); | 222 path = path.Append(FILE_PATH_LITERAL("test.mht")); |
| 218 | 223 |
| 219 GenerateMHTML(path, embedded_test_server()->GetURL("/simple_page.html")); | 224 GenerateMHTML(path, embedded_test_server()->GetURL("/simple_page.html")); |
| 220 ASSERT_FALSE(HasFailure()); | 225 ASSERT_FALSE(HasFailure()); |
| 221 | 226 |
| 222 // Make sure the actual generated file has some contents. | 227 // Make sure the actual generated file has some contents. |
| 223 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | 228 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. |
| 224 EXPECT_GT(ReadFileSizeFromDisk(path), 100); // Verify the actual file size. | 229 EXPECT_GT(ReadFileSizeFromDisk(path), 100); // Verify the actual file size. |
| 225 | 230 |
| 226 { | 231 { |
| 227 base::ThreadRestrictions::ScopedAllowIO allow_io_for_content_verification; | 232 base::ThreadRestrictions::ScopedAllowIO allow_io_for_content_verification; |
| 228 std::string mhtml; | 233 std::string mhtml; |
| 229 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | 234 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); |
| 230 EXPECT_THAT(mhtml, | 235 EXPECT_THAT(mhtml, |
| 231 HasSubstr("Content-Transfer-Encoding: quoted-printable")); | 236 HasSubstr("Content-Transfer-Encoding: quoted-printable")); |
| 232 } | 237 } |
| 238 |
| 239 // Checks that the final status reported to UMA is correct. |
| 240 histogram_tester()->ExpectUniqueSample( |
| 241 "PageSerialization.MhtmlGeneration.FinalSaveStatus", |
| 242 static_cast<int>(MhtmlSaveStatus::SUCCESS), 1); |
| 233 } | 243 } |
| 234 | 244 |
| 235 class GenerateMHTMLAndExitRendererMessageFilter : public BrowserMessageFilter { | 245 class GenerateMHTMLAndExitRendererMessageFilter : public BrowserMessageFilter { |
| 236 public: | 246 public: |
| 237 GenerateMHTMLAndExitRendererMessageFilter( | 247 GenerateMHTMLAndExitRendererMessageFilter( |
| 238 RenderProcessHostImpl* render_process_host) | 248 RenderProcessHostImpl* render_process_host) |
| 239 : BrowserMessageFilter(FrameMsgStart), | 249 : BrowserMessageFilter(FrameMsgStart), |
| 240 render_process_host_(render_process_host) {} | 250 render_process_host_(render_process_host) {} |
| 241 | 251 |
| 242 protected: | 252 protected: |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 } | 358 } |
| 349 | 359 |
| 350 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, InvalidPath) { | 360 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, InvalidPath) { |
| 351 base::FilePath path(FILE_PATH_LITERAL("/invalid/file/path")); | 361 base::FilePath path(FILE_PATH_LITERAL("/invalid/file/path")); |
| 352 | 362 |
| 353 GenerateMHTML(path, embedded_test_server()->GetURL( | 363 GenerateMHTML(path, embedded_test_server()->GetURL( |
| 354 "/download/local-about-blank-subframes.html")); | 364 "/download/local-about-blank-subframes.html")); |
| 355 ASSERT_FALSE(HasFailure()); // No failures with the invocation itself? | 365 ASSERT_FALSE(HasFailure()); // No failures with the invocation itself? |
| 356 | 366 |
| 357 EXPECT_EQ(file_size(), -1); // Expecting that the callback reported failure. | 367 EXPECT_EQ(file_size(), -1); // Expecting that the callback reported failure. |
| 368 |
| 369 // Checks that the final status reported to UMA is correct. |
| 370 histogram_tester()->ExpectUniqueSample( |
| 371 "PageSerialization.MhtmlGeneration.FinalSaveStatus", |
| 372 static_cast<int>(MhtmlSaveStatus::FILE_CREATION_ERROR), 1); |
| 358 } | 373 } |
| 359 | 374 |
| 360 // Tests that MHTML generated using the default 'quoted-printable' encoding does | 375 // Tests that MHTML generated using the default 'quoted-printable' encoding does |
| 361 // not contain the 'binary' Content-Transfer-Encoding header, and generates | 376 // not contain the 'binary' Content-Transfer-Encoding header, and generates |
| 362 // base64 encoding for the image part. | 377 // base64 encoding for the image part. |
| 363 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateNonBinaryMHTMLWithImage) { | 378 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateNonBinaryMHTMLWithImage) { |
| 364 base::FilePath path(temp_dir_.GetPath()); | 379 base::FilePath path(temp_dir_.GetPath()); |
| 365 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | 380 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); |
| 366 | 381 |
| 367 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | 382 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 EXPECT_EQ(-1, file_size()); | 462 EXPECT_EQ(-1, file_size()); |
| 448 | 463 |
| 449 std::string mhtml; | 464 std::string mhtml; |
| 450 { | 465 { |
| 451 base::ThreadRestrictions::ScopedAllowIO allow_io_for_content_verification; | 466 base::ThreadRestrictions::ScopedAllowIO allow_io_for_content_verification; |
| 452 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | 467 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); |
| 453 } | 468 } |
| 454 | 469 |
| 455 // Make sure the contents are missing. | 470 // Make sure the contents are missing. |
| 456 EXPECT_THAT(mhtml, Not(HasSubstr("test body"))); | 471 EXPECT_THAT(mhtml, Not(HasSubstr("test body"))); |
| 472 |
| 473 // Checks that the final status reported to UMA is correct. |
| 474 histogram_tester()->ExpectUniqueSample( |
| 475 "PageSerialization.MhtmlGeneration.FinalSaveStatus", |
| 476 static_cast<int>(MhtmlSaveStatus::FRAME_SERIALIZATION_FORBIDDEN), 1); |
| 457 } | 477 } |
| 458 | 478 |
| 459 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, | 479 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, |
| 460 GenerateMHTMLIgnoreNoStoreSubFrame) { | 480 GenerateMHTMLIgnoreNoStoreSubFrame) { |
| 461 base::FilePath path(temp_dir_.GetPath()); | 481 base::FilePath path(temp_dir_.GetPath()); |
| 462 path = path.Append(FILE_PATH_LITERAL("test.mht")); | 482 path = path.Append(FILE_PATH_LITERAL("test.mht")); |
| 463 | 483 |
| 464 GURL url(embedded_test_server()->GetURL("/page_with_nostore_iframe.html")); | 484 GURL url(embedded_test_server()->GetURL("/page_with_nostore_iframe.html")); |
| 465 | 485 |
| 466 // Generate MHTML, specifying the FailForNoStoreMainFrame policy. | 486 // Generate MHTML, specifying the FailForNoStoreMainFrame policy. |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 | 644 |
| 625 // Make sure that URLs of both frames are present | 645 // Make sure that URLs of both frames are present |
| 626 // (note that these are single-line regexes). | 646 // (note that these are single-line regexes). |
| 627 EXPECT_THAT( | 647 EXPECT_THAT( |
| 628 mhtml, | 648 mhtml, |
| 629 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); | 649 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); |
| 630 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); | 650 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); |
| 631 } | 651 } |
| 632 | 652 |
| 633 } // namespace content | 653 } // namespace content |
| OLD | NEW |