 Chromium Code Reviews
 Chromium Code Reviews Issue 1899803002:
  Offline Pages: Use 'binary encoding' to create MHTML, instead of base64.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1899803002:
  Offline Pages: Use 'binary encoding' to create MHTML, instead of base64.  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| 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 | 6 | 
| 7 #include "base/bind.h" | 7 #include "base/bind.h" | 
| 8 #include "base/callback.h" | 8 #include "base/callback.h" | 
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" | 
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.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/run_loop.h" | 13 #include "base/run_loop.h" | 
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" | 
| 15 #include "content/public/test/browser_test_utils.h" | 15 #include "content/public/test/browser_test_utils.h" | 
| 16 #include "content/public/test/content_browser_test.h" | 16 #include "content/public/test/content_browser_test.h" | 
| 17 #include "content/public/test/content_browser_test_utils.h" | 17 #include "content/public/test/content_browser_test_utils.h" | 
| 18 #include "content/public/test/test_utils.h" | 18 #include "content/public/test/test_utils.h" | 
| 19 #include "content/shell/browser/shell.h" | 19 #include "content/shell/browser/shell.h" | 
| 20 #include "net/dns/mock_host_resolver.h" | 20 #include "net/dns/mock_host_resolver.h" | 
| 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 21 #include "net/test/embedded_test_server/embedded_test_server.h" | 
| 22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" | 
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" | 
| 24 | 24 | 
| 25 using testing::ContainsRegex; | 25 using testing::ContainsRegex; | 
| 26 using testing::HasSubstr; | 26 using testing::HasSubstr; | 
| 27 using testing::Not; | |
| 27 | 28 | 
| 28 namespace content { | 29 namespace content { | 
| 29 | 30 | 
| 30 class MHTMLGenerationTest : public ContentBrowserTest { | 31 class MHTMLGenerationTest : public ContentBrowserTest { | 
| 31 public: | 32 public: | 
| 32 MHTMLGenerationTest() : has_mhtml_callback_run_(false), file_size_(0) {} | 33 MHTMLGenerationTest() : has_mhtml_callback_run_(false), file_size_(0) {} | 
| 33 | 34 | 
| 34 protected: | 35 protected: | 
| 35 void SetUp() override { | 36 void SetUp() override { | 
| 36 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 37 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 
| 37 ASSERT_TRUE(embedded_test_server()->Start()); | 38 ASSERT_TRUE(embedded_test_server()->Start()); | 
| 38 ContentBrowserTest::SetUp(); | 39 ContentBrowserTest::SetUp(); | 
| 39 } | 40 } | 
| 40 | 41 | 
| 41 void GenerateMHTML(const base::FilePath& path, const GURL& url) { | 42 void GenerateMHTML(const base::FilePath& path, const GURL& url) { | 
| 43 GenerateMHTML(path, url, false); | |
| 44 } | |
| 45 | |
| 46 void GenerateMHTML(const base::FilePath& path, | |
| 47 const GURL& url, | |
| 48 bool use_binary_encoding) { | |
| 42 NavigateToURL(shell(), url); | 49 NavigateToURL(shell(), url); | 
| 43 | 50 | 
| 44 base::RunLoop run_loop; | 51 base::RunLoop run_loop; | 
| 45 shell()->web_contents()->GenerateMHTML( | 52 shell()->web_contents()->GenerateMHTML( | 
| 46 path, base::Bind(&MHTMLGenerationTest::MHTMLGenerated, this, | 53 path, use_binary_encoding, | 
| 47 run_loop.QuitClosure())); | 54 base::Bind(&MHTMLGenerationTest::MHTMLGenerated, this, | 
| 55 run_loop.QuitClosure())); | |
| 48 | 56 | 
| 49 // Block until the MHTML is generated. | 57 // Block until the MHTML is generated. | 
| 50 run_loop.Run(); | 58 run_loop.Run(); | 
| 51 | 59 | 
| 52 EXPECT_TRUE(has_mhtml_callback_run()); | 60 EXPECT_TRUE(has_mhtml_callback_run()); | 
| 53 } | 61 } | 
| 54 | 62 | 
| 55 bool has_mhtml_callback_run() const { return has_mhtml_callback_run_; } | 63 bool has_mhtml_callback_run() const { return has_mhtml_callback_run_; } | 
| 56 int64_t file_size() const { return file_size_; } | 64 int64_t file_size() const { return file_size_; } | 
| 57 | 65 | 
| (...skipping 19 matching lines...) Expand all Loading... | |
| 77 path = path.Append(FILE_PATH_LITERAL("test.mht")); | 85 path = path.Append(FILE_PATH_LITERAL("test.mht")); | 
| 78 | 86 | 
| 79 GenerateMHTML(path, embedded_test_server()->GetURL("/simple_page.html")); | 87 GenerateMHTML(path, embedded_test_server()->GetURL("/simple_page.html")); | 
| 80 ASSERT_FALSE(HasFailure()); | 88 ASSERT_FALSE(HasFailure()); | 
| 81 | 89 | 
| 82 // Make sure the actual generated file has some contents. | 90 // Make sure the actual generated file has some contents. | 
| 83 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | 91 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | 
| 84 int64_t file_size; | 92 int64_t file_size; | 
| 85 ASSERT_TRUE(base::GetFileSize(path, &file_size)); | 93 ASSERT_TRUE(base::GetFileSize(path, &file_size)); | 
| 86 EXPECT_GT(file_size, 100); // Verify the actual file size. | 94 EXPECT_GT(file_size, 100); // Verify the actual file size. | 
| 95 | |
| 96 std::string mhtml; | |
| 97 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
| 98 EXPECT_THAT(mhtml, | |
| 99 HasSubstr("Content-Transfer-Encoding: quoted-printable")); | |
| 87 } | 100 } | 
| 88 | 101 | 
| 89 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, InvalidPath) { | 102 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, InvalidPath) { | 
| 90 base::FilePath path(FILE_PATH_LITERAL("/invalid/file/path")); | 103 base::FilePath path(FILE_PATH_LITERAL("/invalid/file/path")); | 
| 91 | 104 | 
| 92 GenerateMHTML(path, embedded_test_server()->GetURL( | 105 GenerateMHTML(path, embedded_test_server()->GetURL( | 
| 93 "/download/local-about-blank-subframes.html")); | 106 "/download/local-about-blank-subframes.html")); | 
| 94 ASSERT_FALSE(HasFailure()); // No failures with the invocation itself? | 107 ASSERT_FALSE(HasFailure()); // No failures with the invocation itself? | 
| 95 | 108 | 
| 96 EXPECT_EQ(file_size(), -1); // Expecting that the callback reported failure. | 109 EXPECT_EQ(file_size(), -1); // Expecting that the callback reported failure. | 
| 97 } | 110 } | 
| 98 | 111 | 
| 112 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateNonBinaryMHTMLWithImage) { | |
| 
nasko
2016/04/20 17:01:15
Please add a comment describing what this case is
 
dewittj
2016/04/20 20:20:59
Done.
 | |
| 113 base::FilePath path(temp_dir_.path()); | |
| 114 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | |
| 115 | |
| 116 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | |
| 117 GenerateMHTML(path, url, false); | |
| 118 ASSERT_FALSE(HasFailure()); | |
| 119 EXPECT_NE(file_size(), -1); | |
| 120 | |
| 121 std::string mhtml; | |
| 122 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
| 
nasko
2016/04/20 17:01:15
Can we also check file size? It is done in other t
 
dewittj
2016/04/20 20:20:59
Done.
 | |
| 123 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: base64")); | |
| 124 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: binary"))); | |
| 125 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); | |
| 126 } | |
| 127 | |
| 128 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateBinaryMHTML) { | |
| 
nasko
2016/04/20 17:01:15
Be consistent with naming. If the previous test ca
 
nasko
2016/04/20 17:01:15
Add a comment here too.
 
dewittj
2016/04/20 20:20:59
Done.
 | |
| 129 base::FilePath path(temp_dir_.path()); | |
| 130 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | |
| 131 | |
| 132 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | |
| 133 GenerateMHTML(path, url, true); | |
| 134 ASSERT_FALSE(HasFailure()); | |
| 135 EXPECT_NE(file_size(), -1); | |
| 136 | |
| 137 std::string mhtml; | |
| 138 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
| 139 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: binary")); | |
| 140 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: base64"))); | |
| 141 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); | |
| 142 } | |
| 143 | |
| 99 // Test suite that allows testing --site-per-process against cross-site frames. | 144 // Test suite that allows testing --site-per-process against cross-site frames. | 
| 100 // See http://dev.chromium.org/developers/design-documents/site-isolation. | 145 // See http://dev.chromium.org/developers/design-documents/site-isolation. | 
| 101 class MHTMLGenerationSitePerProcessTest : public MHTMLGenerationTest { | 146 class MHTMLGenerationSitePerProcessTest : public MHTMLGenerationTest { | 
| 102 public: | 147 public: | 
| 103 MHTMLGenerationSitePerProcessTest() {} | 148 MHTMLGenerationSitePerProcessTest() {} | 
| 104 | 149 | 
| 105 protected: | 150 protected: | 
| 106 void SetUpCommandLine(base::CommandLine* command_line) override { | 151 void SetUpCommandLine(base::CommandLine* command_line) override { | 
| 107 MHTMLGenerationTest::SetUpCommandLine(command_line); | 152 MHTMLGenerationTest::SetUpCommandLine(command_line); | 
| 108 | 153 | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 | 189 | 
| 145 // Make sure that URLs of both frames are present | 190 // Make sure that URLs of both frames are present | 
| 146 // (note that these are single-line regexes). | 191 // (note that these are single-line regexes). | 
| 147 EXPECT_THAT( | 192 EXPECT_THAT( | 
| 148 mhtml, | 193 mhtml, | 
| 149 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); | 194 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); | 
| 150 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); | 195 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); | 
| 151 } | 196 } | 
| 152 | 197 | 
| 153 } // namespace content | 198 } // namespace content | 
| OLD | NEW |