OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 13 matching lines...) Expand all Loading... |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "public/web/WebFrameSerializer.h" | 31 #include "public/web/WebFrameSerializer.h" |
32 | 32 |
33 #include "platform/testing/URLTestHelpers.h" | 33 #include "platform/testing/URLTestHelpers.h" |
| 34 #include "platform/testing/UnitTestHelpers.h" |
34 #include "platform/weborigin/KURL.h" | 35 #include "platform/weborigin/KURL.h" |
35 #include "public/platform/Platform.h" | 36 #include "public/platform/Platform.h" |
36 #include "public/platform/WebCString.h" | 37 #include "public/platform/WebCString.h" |
37 #include "public/platform/WebString.h" | 38 #include "public/platform/WebString.h" |
38 #include "public/platform/WebURL.h" | 39 #include "public/platform/WebURL.h" |
39 #include "public/platform/WebURLLoaderMockFactory.h" | 40 #include "public/platform/WebURLLoaderMockFactory.h" |
40 #include "public/web/WebCache.h" | 41 #include "public/web/WebCache.h" |
41 #include "public/web/WebFrameSerializerClient.h" | 42 #include "public/web/WebFrameSerializerClient.h" |
42 #include "testing/gtest/include/gtest/gtest.h" | 43 #include "testing/gtest/include/gtest/gtest.h" |
43 #include "web/WebLocalFrameImpl.h" | 44 #include "web/WebLocalFrameImpl.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 if (pos == WTF::kNotFound) | 95 if (pos == WTF::kNotFound) |
95 break; | 96 break; |
96 matches++; | 97 matches++; |
97 start = pos + size; | 98 start = pos + size; |
98 } | 99 } |
99 return matches; | 100 return matches; |
100 } | 101 } |
101 | 102 |
102 } // namespace | 103 } // namespace |
103 | 104 |
104 class WebFrameSerializerTest : public testing::Test { | 105 class WebFrameSerializerTest : public ::testing::Test { |
105 protected: | 106 protected: |
106 WebFrameSerializerTest() { m_helper.initialize(); } | 107 WebFrameSerializerTest() { m_helper.initialize(); } |
107 | 108 |
108 ~WebFrameSerializerTest() override { | 109 ~WebFrameSerializerTest() override { |
109 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); | 110 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
110 WebCache::clear(); | 111 WebCache::clear(); |
111 } | 112 } |
112 | 113 |
113 void registerMockedImageURL(const String& url) { | 114 void registerMockedImageURLLoad(const String& url) { |
114 // Image resources need to be mocked, but irrelevant here what image they | 115 // Image resources need to be mocked, but irrelevant here what image they |
115 // map to. | 116 // map to. |
116 URLTestHelpers::registerMockedURLLoad(KURL(ParsedURLString, url), | 117 registerMockedFileURLLoad(URLTestHelpers::toKURL(url.utf8().data()), |
117 "frameserialization/awesome.png"); | 118 "frameserialization/awesome.png"); |
| 119 } |
| 120 void registerMockedFileURLLoad(const KURL& url, |
| 121 const String& filePath, |
| 122 const String& mimeType = "image/png") { |
| 123 URLTestHelpers::registerMockedURLLoad( |
| 124 url, testing::webTestDataPath(filePath.utf8().data()), mimeType); |
118 } | 125 } |
119 | 126 |
120 class SingleLinkRewritingDelegate | 127 class SingleLinkRewritingDelegate |
121 : public WebFrameSerializer::LinkRewritingDelegate { | 128 : public WebFrameSerializer::LinkRewritingDelegate { |
122 public: | 129 public: |
123 SingleLinkRewritingDelegate(const WebURL& url, const WebString& localPath) | 130 SingleLinkRewritingDelegate(const WebURL& url, const WebString& localPath) |
124 : m_url(url), m_localPath(localPath) {} | 131 : m_url(url), m_localPath(localPath) {} |
125 | 132 |
126 bool rewriteFrameSource(WebFrame* frame, | 133 bool rewriteFrameSource(WebFrame* frame, |
127 WebString* rewrittenLink) override { | 134 WebString* rewrittenLink) override { |
128 return false; | 135 return false; |
129 } | 136 } |
130 | 137 |
131 bool rewriteLink(const WebURL& url, WebString* rewrittenLink) override { | 138 bool rewriteLink(const WebURL& url, WebString* rewrittenLink) override { |
132 if (url != m_url) | 139 if (url != m_url) |
133 return false; | 140 return false; |
134 | 141 |
135 *rewrittenLink = m_localPath; | 142 *rewrittenLink = m_localPath; |
136 return true; | 143 return true; |
137 } | 144 } |
138 | 145 |
139 private: | 146 private: |
140 const WebURL m_url; | 147 const WebURL m_url; |
141 const WebString m_localPath; | 148 const WebString m_localPath; |
142 }; | 149 }; |
143 | 150 |
144 String serializeFile(const String& url, const String& fileName) { | 151 String serializeFile(const String& url, const String& fileName) { |
145 KURL parsedURL(ParsedURLString, url); | 152 KURL parsedURL(ParsedURLString, url); |
146 URLTestHelpers::registerMockedURLLoad(parsedURL, fileName, | 153 String filePath("frameserialization/" + fileName); |
147 "frameserialization/", "text/html"); | 154 registerMockedFileURLLoad(parsedURL, filePath, "text/html"); |
148 FrameTestHelpers::loadFrame(mainFrameImpl(), url.utf8().data()); | 155 FrameTestHelpers::loadFrame(mainFrameImpl(), url.utf8().data()); |
149 SingleLinkRewritingDelegate delegate(parsedURL, WebString("local")); | 156 SingleLinkRewritingDelegate delegate(parsedURL, WebString("local")); |
150 SimpleWebFrameSerializerClient serializerClient; | 157 SimpleWebFrameSerializerClient serializerClient; |
151 WebFrameSerializer::serialize(mainFrameImpl(), &serializerClient, | 158 WebFrameSerializer::serialize(mainFrameImpl(), &serializerClient, |
152 &delegate); | 159 &delegate); |
153 return serializerClient.toString(); | 160 return serializerClient.toString(); |
154 } | 161 } |
155 | 162 |
156 WebViewImpl* webView() { return m_helper.webView(); } | 163 WebViewImpl* webView() { return m_helper.webView(); } |
157 | 164 |
158 WebLocalFrameImpl* mainFrameImpl() { | 165 WebLocalFrameImpl* mainFrameImpl() { |
159 return m_helper.webView()->mainFrameImpl(); | 166 return m_helper.webView()->mainFrameImpl(); |
160 } | 167 } |
161 | 168 |
162 private: | 169 private: |
163 FrameTestHelpers::WebViewHelper m_helper; | 170 FrameTestHelpers::WebViewHelper m_helper; |
164 }; | 171 }; |
165 | 172 |
166 TEST_F(WebFrameSerializerTest, URLAttributeValues) { | 173 TEST_F(WebFrameSerializerTest, URLAttributeValues) { |
167 registerMockedImageURL("javascript:\""); | 174 registerMockedImageURLLoad("javascript:\""); |
168 | 175 |
169 const char* expectedHTML = | 176 const char* expectedHTML = |
170 "\n<!-- saved from url=(0020)http://www.test.com/ -->\n" | 177 "\n<!-- saved from url=(0020)http://www.test.com/ -->\n" |
171 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " | 178 "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; " |
172 "charset=UTF-8\">\n" | 179 "charset=UTF-8\">\n" |
173 "</head><body><img src=\"javascript:"\">\n" | 180 "</head><body><img src=\"javascript:"\">\n" |
174 "<a href=\"http://www.test.com/local#"\">local</a>\n" | 181 "<a href=\"http://www.test.com/local#"\">local</a>\n" |
175 "<a " | 182 "<a " |
176 "href=\"http://www.example.com/#"><script>alert(0)</" | 183 "href=\"http://www.example.com/#"><script>alert(0)</" |
177 "script>\">external</a>\n" | 184 "script>\">external</a>\n" |
(...skipping 29 matching lines...) Expand all Loading... |
207 class WebFrameSerializerSanitizationTest : public WebFrameSerializerTest { | 214 class WebFrameSerializerSanitizationTest : public WebFrameSerializerTest { |
208 protected: | 215 protected: |
209 WebFrameSerializerSanitizationTest() {} | 216 WebFrameSerializerSanitizationTest() {} |
210 | 217 |
211 ~WebFrameSerializerSanitizationTest() override {} | 218 ~WebFrameSerializerSanitizationTest() override {} |
212 | 219 |
213 String generateMHTMLParts(const String& url, | 220 String generateMHTMLParts(const String& url, |
214 const String& fileName, | 221 const String& fileName, |
215 const String& mimeType = "text/html") { | 222 const String& mimeType = "text/html") { |
216 KURL parsedURL(ParsedURLString, url); | 223 KURL parsedURL(ParsedURLString, url); |
217 URLTestHelpers::registerMockedURLLoad(parsedURL, fileName, | 224 String filePath("frameserialization/" + fileName); |
218 "frameserialization/", mimeType); | 225 registerMockedFileURLLoad(parsedURL, filePath, mimeType); |
219 FrameTestHelpers::loadFrame(mainFrameImpl(), url.utf8().data()); | 226 FrameTestHelpers::loadFrame(mainFrameImpl(), url.utf8().data()); |
220 WebThreadSafeData result = WebFrameSerializer::generateMHTMLParts( | 227 WebThreadSafeData result = WebFrameSerializer::generateMHTMLParts( |
221 WebString("boundary"), mainFrameImpl(), &m_mhtmlDelegate); | 228 WebString("boundary"), mainFrameImpl(), &m_mhtmlDelegate); |
222 return String(result.data(), result.size()); | 229 return String(result.data(), result.size()); |
223 } | 230 } |
224 | 231 |
225 void setRemovePopupOverlay(bool removePopupOverlay) { | 232 void setRemovePopupOverlay(bool removePopupOverlay) { |
226 m_mhtmlDelegate.setRemovePopupOverlay(removePopupOverlay); | 233 m_mhtmlDelegate.setRemovePopupOverlay(removePopupOverlay); |
227 } | 234 } |
228 | 235 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 | 303 |
297 // Regression test for crbug.com/678893, where in some cases serializing an | 304 // Regression test for crbug.com/678893, where in some cases serializing an |
298 // image document could cause code to pick an element from an empty container. | 305 // image document could cause code to pick an element from an empty container. |
299 TEST_F(WebFrameSerializerSanitizationTest, FromBrokenImageDocument) { | 306 TEST_F(WebFrameSerializerSanitizationTest, FromBrokenImageDocument) { |
300 String mhtml = generateMHTMLParts("http://www.test.com", "broken-image.png", | 307 String mhtml = generateMHTMLParts("http://www.test.com", "broken-image.png", |
301 "image/png"); | 308 "image/png"); |
302 EXPECT_TRUE(mhtml.isEmpty()); | 309 EXPECT_TRUE(mhtml.isEmpty()); |
303 } | 310 } |
304 | 311 |
305 TEST_F(WebFrameSerializerSanitizationTest, ImageLoadedFromSrcsetForHiDPI) { | 312 TEST_F(WebFrameSerializerSanitizationTest, ImageLoadedFromSrcsetForHiDPI) { |
306 URLTestHelpers::registerMockedURLLoad( | 313 registerMockedFileURLLoad(KURL(ParsedURLString, "http://www.test.com/1x.png"), |
307 KURL(ParsedURLString, "http://www.test.com/1x.png"), | 314 "frameserialization/1x.png"); |
308 "frameserialization/1x.png"); | 315 registerMockedFileURLLoad(KURL(ParsedURLString, "http://www.test.com/2x.png"), |
309 URLTestHelpers::registerMockedURLLoad( | 316 "frameserialization/2x.png"); |
310 KURL(ParsedURLString, "http://www.test.com/2x.png"), | |
311 "frameserialization/2x.png"); | |
312 | 317 |
313 // Set high DPR in order to load image from srcset, instead of src. | 318 // Set high DPR in order to load image from srcset, instead of src. |
314 webView()->setDeviceScaleFactor(2.0f); | 319 webView()->setDeviceScaleFactor(2.0f); |
315 | 320 |
316 String mhtml = generateMHTMLParts("http://www.test.com", "img_srcset.html"); | 321 String mhtml = generateMHTMLParts("http://www.test.com", "img_srcset.html"); |
317 | 322 |
318 // srcset attribute should be skipped. | 323 // srcset attribute should be skipped. |
319 EXPECT_EQ(WTF::kNotFound, mhtml.find("srcset=")); | 324 EXPECT_EQ(WTF::kNotFound, mhtml.find("srcset=")); |
320 | 325 |
321 // Width and height attributes should be set when none is present in <img>. | 326 // Width and height attributes should be set when none is present in <img>. |
322 EXPECT_NE(WTF::kNotFound, | 327 EXPECT_NE(WTF::kNotFound, |
323 mhtml.find("id=3D\"i1\" width=3D\"6\" height=3D\"6\">")); | 328 mhtml.find("id=3D\"i1\" width=3D\"6\" height=3D\"6\">")); |
324 | 329 |
325 // Height attribute should not be set if width attribute is already present in | 330 // Height attribute should not be set if width attribute is already present in |
326 // <img> | 331 // <img> |
327 EXPECT_NE(WTF::kNotFound, mhtml.find("id=3D\"i2\" width=3D\"8\">")); | 332 EXPECT_NE(WTF::kNotFound, mhtml.find("id=3D\"i2\" width=3D\"8\">")); |
328 } | 333 } |
329 | 334 |
330 TEST_F(WebFrameSerializerSanitizationTest, ImageLoadedFromSrcForNormalDPI) { | 335 TEST_F(WebFrameSerializerSanitizationTest, ImageLoadedFromSrcForNormalDPI) { |
331 URLTestHelpers::registerMockedURLLoad( | 336 registerMockedFileURLLoad(KURL(ParsedURLString, "http://www.test.com/1x.png"), |
332 KURL(ParsedURLString, "http://www.test.com/1x.png"), | 337 "frameserialization/1x.png"); |
333 "frameserialization/1x.png"); | 338 registerMockedFileURLLoad(KURL(ParsedURLString, "http://www.test.com/2x.png"), |
334 URLTestHelpers::registerMockedURLLoad( | 339 "frameserialization/2x.png"); |
335 KURL(ParsedURLString, "http://www.test.com/2x.png"), | |
336 "frameserialization/2x.png"); | |
337 | 340 |
338 String mhtml = generateMHTMLParts("http://www.test.com", "img_srcset.html"); | 341 String mhtml = generateMHTMLParts("http://www.test.com", "img_srcset.html"); |
339 | 342 |
340 // srcset attribute should be skipped. | 343 // srcset attribute should be skipped. |
341 EXPECT_EQ(WTF::kNotFound, mhtml.find("srcset=")); | 344 EXPECT_EQ(WTF::kNotFound, mhtml.find("srcset=")); |
342 | 345 |
343 // New width and height attributes should not be set. | 346 // New width and height attributes should not be set. |
344 EXPECT_NE(WTF::kNotFound, mhtml.find("id=3D\"i1\">")); | 347 EXPECT_NE(WTF::kNotFound, mhtml.find("id=3D\"i1\">")); |
345 EXPECT_NE(WTF::kNotFound, mhtml.find("id=3D\"i2\" width=3D\"8\">")); | 348 EXPECT_NE(WTF::kNotFound, mhtml.find("id=3D\"i2\" width=3D\"8\">")); |
346 } | 349 } |
347 | 350 |
348 TEST_F(WebFrameSerializerSanitizationTest, RemovePopupOverlayIfRequested) { | 351 TEST_F(WebFrameSerializerSanitizationTest, RemovePopupOverlayIfRequested) { |
349 webView()->resize(WebSize(500, 500)); | 352 webView()->resize(WebSize(500, 500)); |
350 setRemovePopupOverlay(true); | 353 setRemovePopupOverlay(true); |
351 String mhtml = generateMHTMLParts("http://www.test.com", "popup.html"); | 354 String mhtml = generateMHTMLParts("http://www.test.com", "popup.html"); |
352 EXPECT_EQ(WTF::kNotFound, mhtml.find("class=3D\"overlay")); | 355 EXPECT_EQ(WTF::kNotFound, mhtml.find("class=3D\"overlay")); |
353 EXPECT_EQ(WTF::kNotFound, mhtml.find("class=3D\"modal")); | 356 EXPECT_EQ(WTF::kNotFound, mhtml.find("class=3D\"modal")); |
354 } | 357 } |
355 | 358 |
356 TEST_F(WebFrameSerializerSanitizationTest, KeepPopupOverlayIfNotRequested) { | 359 TEST_F(WebFrameSerializerSanitizationTest, KeepPopupOverlayIfNotRequested) { |
357 webView()->resize(WebSize(500, 500)); | 360 webView()->resize(WebSize(500, 500)); |
358 setRemovePopupOverlay(false); | 361 setRemovePopupOverlay(false); |
359 String mhtml = generateMHTMLParts("http://www.test.com", "popup.html"); | 362 String mhtml = generateMHTMLParts("http://www.test.com", "popup.html"); |
360 EXPECT_NE(WTF::kNotFound, mhtml.find("class=3D\"overlay")); | 363 EXPECT_NE(WTF::kNotFound, mhtml.find("class=3D\"overlay")); |
361 EXPECT_NE(WTF::kNotFound, mhtml.find("class=3D\"modal")); | 364 EXPECT_NE(WTF::kNotFound, mhtml.find("class=3D\"modal")); |
362 } | 365 } |
363 | 366 |
364 } // namespace blink | 367 } // namespace blink |
OLD | NEW |