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 |
63 int64_t readFileSizeFromDisk(base::FilePath path) { | |
nasko
2016/04/20 22:22:38
Method name should start with Capital letter.
| |
64 int64_t file_size; | |
65 if (!base::GetFileSize(path, &file_size)) return -1; | |
66 return file_size; | |
67 } | |
68 | |
55 bool has_mhtml_callback_run() const { return has_mhtml_callback_run_; } | 69 bool has_mhtml_callback_run() const { return has_mhtml_callback_run_; } |
56 int64_t file_size() const { return file_size_; } | 70 int64_t file_size() const { return file_size_; } |
57 | 71 |
58 base::ScopedTempDir temp_dir_; | 72 base::ScopedTempDir temp_dir_; |
59 | 73 |
60 private: | 74 private: |
61 void MHTMLGenerated(base::Closure quit_closure, int64_t size) { | 75 void MHTMLGenerated(base::Closure quit_closure, int64_t size) { |
62 has_mhtml_callback_run_ = true; | 76 has_mhtml_callback_run_ = true; |
63 file_size_ = size; | 77 file_size_ = size; |
64 quit_closure.Run(); | 78 quit_closure.Run(); |
65 } | 79 } |
66 | 80 |
67 bool has_mhtml_callback_run_; | 81 bool has_mhtml_callback_run_; |
68 int64_t file_size_; | 82 int64_t file_size_; |
69 }; | 83 }; |
70 | 84 |
71 // Tests that generating a MHTML does create contents. | 85 // Tests that generating a MHTML does create contents. |
72 // Note that the actual content of the file is not tested, the purpose of this | 86 // Note that the actual content of the file is not tested, the purpose of this |
73 // test is to ensure we were successful in creating the MHTML data from the | 87 // test is to ensure we were successful in creating the MHTML data from the |
74 // renderer. | 88 // renderer. |
75 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTML) { | 89 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateMHTML) { |
76 base::FilePath path(temp_dir_.path()); | 90 base::FilePath path(temp_dir_.path()); |
77 path = path.Append(FILE_PATH_LITERAL("test.mht")); | 91 path = path.Append(FILE_PATH_LITERAL("test.mht")); |
78 | 92 |
79 GenerateMHTML(path, embedded_test_server()->GetURL("/simple_page.html")); | 93 GenerateMHTML(path, embedded_test_server()->GetURL("/simple_page.html")); |
80 ASSERT_FALSE(HasFailure()); | 94 ASSERT_FALSE(HasFailure()); |
81 | 95 |
82 // Make sure the actual generated file has some contents. | 96 // Make sure the actual generated file has some contents. |
83 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | 97 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. |
84 int64_t file_size; | 98 EXPECT_GT(readFileSizeFromDisk(path), 100); // Verify the actual file size. |
85 ASSERT_TRUE(base::GetFileSize(path, &file_size)); | 99 |
86 EXPECT_GT(file_size, 100); // Verify the actual file size. | 100 std::string mhtml; |
101 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
102 EXPECT_THAT(mhtml, | |
103 HasSubstr("Content-Transfer-Encoding: quoted-printable")); | |
87 } | 104 } |
88 | 105 |
89 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, InvalidPath) { | 106 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, InvalidPath) { |
90 base::FilePath path(FILE_PATH_LITERAL("/invalid/file/path")); | 107 base::FilePath path(FILE_PATH_LITERAL("/invalid/file/path")); |
91 | 108 |
92 GenerateMHTML(path, embedded_test_server()->GetURL( | 109 GenerateMHTML(path, embedded_test_server()->GetURL( |
93 "/download/local-about-blank-subframes.html")); | 110 "/download/local-about-blank-subframes.html")); |
94 ASSERT_FALSE(HasFailure()); // No failures with the invocation itself? | 111 ASSERT_FALSE(HasFailure()); // No failures with the invocation itself? |
95 | 112 |
96 EXPECT_EQ(file_size(), -1); // Expecting that the callback reported failure. | 113 EXPECT_EQ(file_size(), -1); // Expecting that the callback reported failure. |
97 } | 114 } |
98 | 115 |
116 // Tests that MHTML generated using the default 'quoted-printable' encoding does | |
117 // not contain the 'binary' Content-Transfer-Encoding header, and generates | |
118 // base64 encoding for the image part. | |
119 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateNonBinaryMHTMLWithImage) { | |
120 base::FilePath path(temp_dir_.path()); | |
121 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | |
122 | |
123 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | |
124 GenerateMHTML(path, url, false); | |
125 ASSERT_FALSE(HasFailure()); | |
126 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | |
127 EXPECT_GT(readFileSizeFromDisk(path), 100); // Verify the actual file size. | |
128 | |
129 std::string mhtml; | |
130 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
131 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: base64")); | |
132 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: binary"))); | |
133 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); | |
134 } | |
135 | |
136 // Tests that MHTML generated using the binary encoding contains the 'binary' | |
137 // Content-Transfer-Encoding header, and does not contain any base64 encoded | |
138 // parts. | |
139 IN_PROC_BROWSER_TEST_F(MHTMLGenerationTest, GenerateBinaryMHTMLWithImage) { | |
140 base::FilePath path(temp_dir_.path()); | |
141 path = path.Append(FILE_PATH_LITERAL("test_binary.mht")); | |
142 | |
143 GURL url(embedded_test_server()->GetURL("/page_with_image.html")); | |
144 GenerateMHTML(path, url, true); | |
145 ASSERT_FALSE(HasFailure()); | |
146 EXPECT_GT(file_size(), 0); // Verify the size reported by the callback. | |
147 EXPECT_GT(readFileSizeFromDisk(path), 100); // Verify the actual file size. | |
148 | |
149 std::string mhtml; | |
150 ASSERT_TRUE(base::ReadFileToString(path, &mhtml)); | |
151 EXPECT_THAT(mhtml, HasSubstr("Content-Transfer-Encoding: binary")); | |
152 EXPECT_THAT(mhtml, Not(HasSubstr("Content-Transfer-Encoding: base64"))); | |
153 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*blank.jpg")); | |
154 } | |
155 | |
99 // Test suite that allows testing --site-per-process against cross-site frames. | 156 // Test suite that allows testing --site-per-process against cross-site frames. |
100 // See http://dev.chromium.org/developers/design-documents/site-isolation. | 157 // See http://dev.chromium.org/developers/design-documents/site-isolation. |
101 class MHTMLGenerationSitePerProcessTest : public MHTMLGenerationTest { | 158 class MHTMLGenerationSitePerProcessTest : public MHTMLGenerationTest { |
102 public: | 159 public: |
103 MHTMLGenerationSitePerProcessTest() {} | 160 MHTMLGenerationSitePerProcessTest() {} |
104 | 161 |
105 protected: | 162 protected: |
106 void SetUpCommandLine(base::CommandLine* command_line) override { | 163 void SetUpCommandLine(base::CommandLine* command_line) override { |
107 MHTMLGenerationTest::SetUpCommandLine(command_line); | 164 MHTMLGenerationTest::SetUpCommandLine(command_line); |
108 | 165 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 | 201 |
145 // Make sure that URLs of both frames are present | 202 // Make sure that URLs of both frames are present |
146 // (note that these are single-line regexes). | 203 // (note that these are single-line regexes). |
147 EXPECT_THAT( | 204 EXPECT_THAT( |
148 mhtml, | 205 mhtml, |
149 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); | 206 ContainsRegex("Content-Location:.*/frame_tree/page_with_one_frame.html")); |
150 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); | 207 EXPECT_THAT(mhtml, ContainsRegex("Content-Location:.*/title1.html")); |
151 } | 208 } |
152 | 209 |
153 } // namespace content | 210 } // namespace content |
OLD | NEW |