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 "core/loader/LinkLoader.h" | 5 #include "core/loader/LinkLoader.h" |
6 | 6 |
7 #include "core/fetch/MemoryCache.h" | 7 #include "core/fetch/MemoryCache.h" |
8 #include "core/fetch/ResourceFetcher.h" | 8 #include "core/fetch/ResourceFetcher.h" |
9 #include "core/frame/Settings.h" | 9 #include "core/frame/Settings.h" |
10 #include "core/html/LinkRelAttribute.h" | 10 #include "core/html/LinkRelAttribute.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 mutable bool m_isHTTPS; | 76 mutable bool m_isHTTPS; |
77 mutable bool m_isCrossOrigin; | 77 mutable bool m_isCrossOrigin; |
78 }; | 78 }; |
79 | 79 |
80 TEST(LinkLoaderTest, Preload) { | 80 TEST(LinkLoaderTest, Preload) { |
81 struct TestCase { | 81 struct TestCase { |
82 const char* href; | 82 const char* href; |
83 const char* as; | 83 const char* as; |
84 const char* type; | 84 const char* type; |
85 const char* media; | 85 const char* media; |
86 const char* referrerPolicy; | |
86 const ResourceLoadPriority priority; | 87 const ResourceLoadPriority priority; |
87 const WebURLRequest::RequestContext context; | 88 const WebURLRequest::RequestContext context; |
88 const bool linkLoaderShouldLoadValue; | 89 const bool linkLoaderShouldLoadValue; |
89 const bool expectingLoad; | 90 const bool expectingLoad; |
91 const ReferrerPolicy expectedReferrerPolicy; | |
90 } cases[] = { | 92 } cases[] = { |
91 {"http://example.test/cat.jpg", "image", "", "", ResourceLoadPriorityLow, | 93 {"http://example.test/cat.jpg", "image", "", "", "", |
92 WebURLRequest::RequestContextImage, true, true}, | 94 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
93 {"http://example.test/cat.js", "script", "", "", ResourceLoadPriorityHigh, | 95 ReferrerPolicyDefault}, |
94 WebURLRequest::RequestContextScript, true, true}, | 96 {"http://example.test/cat.js", "script", "", "", "", |
95 {"http://example.test/cat.css", "style", "", "", | 97 ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, |
98 true, ReferrerPolicyDefault}, | |
99 {"http://example.test/cat.css", "style", "", "", "", | |
96 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, | 100 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, |
97 true}, | 101 true, ReferrerPolicyDefault}, |
98 // TODO(yoav): It doesn't seem like the audio context is ever used. That s hould probably be fixed (or we can consolidate audio and video). | 102 // TODO(yoav): It doesn't seem like the audio context is ever used. That s hould probably be fixed (or we can consolidate audio and video). |
99 {"http://example.test/cat.wav", "media", "", "", ResourceLoadPriorityLow, | 103 {"http://example.test/cat.wav", "media", "", "", "", |
100 WebURLRequest::RequestContextVideo, true, true}, | 104 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
101 {"http://example.test/cat.mp4", "media", "", "", ResourceLoadPriorityLow, | 105 ReferrerPolicyDefault}, |
102 WebURLRequest::RequestContextVideo, true, true}, | 106 {"http://example.test/cat.mp4", "media", "", "", "", |
103 {"http://example.test/cat.vtt", "track", "", "", ResourceLoadPriorityLow, | 107 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
104 WebURLRequest::RequestContextTrack, true, true}, | 108 ReferrerPolicyDefault}, |
105 {"http://example.test/cat.woff", "font", "", "", | 109 {"http://example.test/cat.vtt", "track", "", "", "", |
110 ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true, | |
111 ReferrerPolicyDefault}, | |
112 {"http://example.test/cat.woff", "font", "", "", "", | |
106 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, | 113 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, |
107 true}, | 114 true, ReferrerPolicyDefault}, |
108 // TODO(yoav): subresource should be *very* low priority (rather than low) . | 115 // TODO(yoav): subresource should be *very* low priority (rather than low) . |
109 {"http://example.test/cat.empty", "", "", "", ResourceLoadPriorityHigh, | 116 {"http://example.test/cat.empty", "", "", "", "", |
110 WebURLRequest::RequestContextSubresource, true, true}, | 117 ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, |
111 {"http://example.test/cat.blob", "blabla", "", "", | 118 true, ReferrerPolicyDefault}, |
119 {"http://example.test/cat.blob", "blabla", "", "", "", | |
112 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, | 120 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, |
113 false}, | 121 false, ReferrerPolicyDefault}, |
114 {"bla://example.test/cat.gif", "image", "", "", | 122 {"bla://example.test/cat.gif", "image", "", "", "", |
115 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, | 123 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, |
116 false, false}, | 124 false, false, ReferrerPolicyDefault}, |
117 // MIME type tests | 125 // MIME type tests |
118 {"http://example.test/cat.webp", "image", "image/webp", "", | 126 {"http://example.test/cat.webp", "image", "image/webp", "", "", |
119 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true}, | 127 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
120 {"http://example.test/cat.svg", "image", "image/svg+xml", "", | 128 ReferrerPolicyDefault}, |
121 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true}, | 129 {"http://example.test/cat.svg", "image", "image/svg+xml", "", "", |
122 {"http://example.test/cat.jxr", "image", "image/jxr", "", | 130 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, |
131 ReferrerPolicyDefault}, | |
132 {"http://example.test/cat.jxr", "image", "image/jxr", "", "", | |
123 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, | 133 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, |
124 false, false}, | 134 false, false, ReferrerPolicyDefault}, |
125 {"http://example.test/cat.js", "script", "text/javascript", "", | 135 {"http://example.test/cat.js", "script", "text/javascript", "", "", |
126 ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, | 136 ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, |
127 true}, | 137 true, ReferrerPolicyDefault}, |
128 {"http://example.test/cat.js", "script", "text/coffeescript", "", | 138 {"http://example.test/cat.js", "script", "text/coffeescript", "", "", |
129 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextScript, | 139 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextScript, |
130 false, false}, | 140 false, false, ReferrerPolicyDefault}, |
131 {"http://example.test/cat.css", "style", "text/css", "", | 141 {"http://example.test/cat.css", "style", "text/css", "", "", |
132 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, | 142 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, |
133 true}, | 143 true, ReferrerPolicyDefault}, |
134 {"http://example.test/cat.css", "style", "text/sass", "", | 144 {"http://example.test/cat.css", "style", "text/sass", "", "", |
135 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextStyle, | 145 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextStyle, |
136 false, false}, | 146 false, false, ReferrerPolicyDefault}, |
137 {"http://example.test/cat.wav", "media", "audio/wav", "", | 147 {"http://example.test/cat.wav", "media", "audio/wav", "", "", |
138 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true}, | 148 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
139 {"http://example.test/cat.wav", "media", "audio/mp57", "", | 149 ReferrerPolicyDefault}, |
150 {"http://example.test/cat.wav", "media", "audio/mp57", "", "", | |
140 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, | 151 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, |
141 false, false}, | 152 false, false, ReferrerPolicyDefault}, |
142 {"http://example.test/cat.webm", "media", "video/webm", "", | 153 {"http://example.test/cat.webm", "media", "video/webm", "", "", |
143 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true}, | 154 ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, |
144 {"http://example.test/cat.mp199", "media", "video/mp199", "", | 155 ReferrerPolicyDefault}, |
156 {"http://example.test/cat.mp199", "media", "video/mp199", "", "", | |
145 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, | 157 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, |
146 false, false}, | 158 false, false, ReferrerPolicyDefault}, |
147 {"http://example.test/cat.vtt", "track", "text/vtt", "", | 159 {"http://example.test/cat.vtt", "track", "text/vtt", "", "", |
148 ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true}, | 160 ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true, |
149 {"http://example.test/cat.vtt", "track", "text/subtitlething", "", | 161 ReferrerPolicyDefault}, |
162 {"http://example.test/cat.vtt", "track", "text/subtitlething", "", "", | |
150 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextTrack, | 163 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextTrack, |
151 false, false}, | 164 false, false, ReferrerPolicyDefault}, |
152 {"http://example.test/cat.woff", "font", "font/woff2", "", | 165 {"http://example.test/cat.woff", "font", "font/woff2", "", "", |
153 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, | 166 ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, |
154 true}, | 167 true, ReferrerPolicyDefault}, |
155 {"http://example.test/cat.woff", "font", "font/woff84", "", | 168 {"http://example.test/cat.woff", "font", "font/woff84", "", "", |
156 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextFont, false, | 169 ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextFont, false, |
157 false}, | 170 false, ReferrerPolicyDefault}, |
158 {"http://example.test/cat.empty", "", "foo/bar", "", | 171 {"http://example.test/cat.empty", "", "foo/bar", "", "", |
159 ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, | 172 ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, |
160 true}, | 173 true, ReferrerPolicyDefault}, |
161 {"http://example.test/cat.blob", "blabla", "foo/bar", "", | 174 {"http://example.test/cat.blob", "blabla", "foo/bar", "", "", |
162 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, | 175 ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, |
163 false}, | 176 false, ReferrerPolicyDefault}, |
164 // Media tests | 177 // Media tests |
165 {"http://example.test/cat.gif", "image", "image/gif", | 178 {"http://example.test/cat.gif", "image", "image/gif", |
166 "(max-width: 600px)", ResourceLoadPriorityLow, | 179 "(max-width: 600px)", "", ResourceLoadPriorityLow, |
167 WebURLRequest::RequestContextImage, true, true}, | 180 WebURLRequest::RequestContextImage, true, true, ReferrerPolicyDefault}, |
168 {"http://example.test/cat.gif", "image", "image/gif", | 181 {"http://example.test/cat.gif", "image", "image/gif", |
169 "(max-width: 400px)", ResourceLoadPriorityUnresolved, | 182 "(max-width: 400px)", "", ResourceLoadPriorityUnresolved, |
170 WebURLRequest::RequestContextImage, true, false}, | 183 WebURLRequest::RequestContextImage, true, false, ReferrerPolicyDefault}, |
171 {"http://example.test/cat.gif", "image", "image/gif", | 184 {"http://example.test/cat.gif", "image", "image/gif", |
172 "(max-width: 600px)", ResourceLoadPriorityLow, | 185 "(max-width: 600px)", "", ResourceLoadPriorityLow, |
173 WebURLRequest::RequestContextImage, false, false}, | 186 WebURLRequest::RequestContextImage, false, false, ReferrerPolicyDefault}, |
187 // Referrer Policy | |
188 {"http://example.test/cat.gif", "image", "image/gif", "", "origin", | |
189 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, false, | |
190 false, ReferrerPolicyOrigin}, | |
191 {"http://example.test/cat.gif", "image", "image/gif", "", | |
192 "origin-when-crossorigin", ResourceLoadPriorityLow, | |
193 WebURLRequest::RequestContextImage, false, false, | |
194 ReferrerPolicyOriginWhenCrossOrigin}, | |
195 {"http://example.test/cat.gif", "image", "image/gif", "", "no-referrer", | |
196 ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, false, | |
197 false, ReferrerPolicyNever}, | |
174 }; | 198 }; |
175 | 199 |
176 // Test the cases with a single header | 200 // Test the cases with a single header |
177 for (const auto& testCase : cases) { | 201 for (const auto& testCase : cases) { |
178 std::unique_ptr<DummyPageHolder> dummyPageHolder = | 202 std::unique_ptr<DummyPageHolder> dummyPageHolder = |
179 DummyPageHolder::create(IntSize(500, 500)); | 203 DummyPageHolder::create(IntSize(500, 500)); |
180 dummyPageHolder->frame().settings()->setScriptEnabled(true); | 204 dummyPageHolder->frame().settings()->setScriptEnabled(true); |
181 Persistent<MockLinkLoaderClient> loaderClient = | 205 Persistent<MockLinkLoaderClient> loaderClient = |
182 MockLinkLoaderClient::create(testCase.linkLoaderShouldLoadValue); | 206 MockLinkLoaderClient::create(testCase.linkLoaderShouldLoadValue); |
183 LinkLoader* loader = LinkLoader::create(loaderClient.get()); | 207 LinkLoader* loader = LinkLoader::create(loaderClient.get()); |
184 KURL hrefURL = KURL(KURL(), testCase.href); | 208 KURL hrefURL = KURL(KURL(), testCase.href); |
185 URLTestHelpers::registerMockedErrorURLLoad(hrefURL); | 209 URLTestHelpers::registerMockedErrorURLLoad(hrefURL); |
186 loader->loadLink(LinkRelAttribute("preload"), CrossOriginAttributeNotSet, | 210 loader->loadLink(LinkRelAttribute("preload"), CrossOriginAttributeNotSet, |
187 testCase.type, testCase.as, testCase.media, hrefURL, | 211 testCase.type, testCase.as, testCase.media, |
212 testCase.referrerPolicy, hrefURL, | |
188 dummyPageHolder->document(), NetworkHintsMock()); | 213 dummyPageHolder->document(), NetworkHintsMock()); |
189 ASSERT_TRUE(dummyPageHolder->document().fetcher()); | 214 ASSERT_TRUE(dummyPageHolder->document().fetcher()); |
190 HeapListHashSet<Member<Resource>>* preloads = | 215 HeapListHashSet<Member<Resource>>* preloads = |
191 dummyPageHolder->document().fetcher()->preloads(); | 216 dummyPageHolder->document().fetcher()->preloads(); |
192 if (testCase.expectingLoad) { | 217 if (testCase.expectingLoad) { |
193 if (!preloads) | 218 if (!preloads) |
194 fprintf(stderr, "Unexpected result %s %s %s\n", testCase.href, | 219 fprintf(stderr, "Unexpected result %s %s %s\n", testCase.href, |
195 testCase.as, testCase.type); | 220 testCase.as, testCase.type); |
196 EXPECT_TRUE(preloads); | 221 EXPECT_TRUE(preloads); |
197 } else { | 222 } else { |
198 EXPECT_FALSE(preloads); | 223 EXPECT_FALSE(preloads); |
199 } | 224 } |
200 if (preloads) { | 225 if (preloads) { |
201 if (testCase.priority == ResourceLoadPriorityUnresolved) { | 226 if (testCase.priority == ResourceLoadPriorityUnresolved) { |
202 EXPECT_EQ(0u, preloads->size()); | 227 EXPECT_EQ(0u, preloads->size()); |
203 } else { | 228 } else { |
204 EXPECT_EQ(1u, preloads->size()); | 229 EXPECT_EQ(1u, preloads->size()); |
205 if (preloads->size() > 0) { | 230 if (preloads->size() > 0) { |
206 Resource* resource = preloads->begin().get()->get(); | 231 Resource* resource = preloads->begin().get()->get(); |
207 EXPECT_EQ(testCase.priority, resource->resourceRequest().priority()); | 232 EXPECT_EQ(testCase.priority, resource->resourceRequest().priority()); |
208 EXPECT_EQ(testCase.context, | 233 EXPECT_EQ(testCase.context, |
209 resource->resourceRequest().requestContext()); | 234 resource->resourceRequest().requestContext()); |
estark
2016/10/18 04:55:54
Looks like this is missing an assertion about `exp
| |
210 } | 235 } |
211 } | 236 } |
212 dummyPageHolder->document().fetcher()->clearPreloads(); | 237 dummyPageHolder->document().fetcher()->clearPreloads(); |
213 } | 238 } |
214 memoryCache()->evictResources(); | 239 memoryCache()->evictResources(); |
215 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); | 240 Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); |
216 } | 241 } |
217 } | 242 } |
218 | 243 |
219 TEST(LinkLoaderTest, DNSPrefetch) { | 244 TEST(LinkLoaderTest, DNSPrefetch) { |
(...skipping 15 matching lines...) Expand all Loading... | |
235 dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); | 260 dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); |
236 Persistent<MockLinkLoaderClient> loaderClient = | 261 Persistent<MockLinkLoaderClient> loaderClient = |
237 MockLinkLoaderClient::create(testCase.shouldLoad); | 262 MockLinkLoaderClient::create(testCase.shouldLoad); |
238 LinkLoader* loader = LinkLoader::create(loaderClient.get()); | 263 LinkLoader* loader = LinkLoader::create(loaderClient.get()); |
239 KURL hrefURL = | 264 KURL hrefURL = |
240 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), | 265 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), |
241 testCase.href); | 266 testCase.href); |
242 NetworkHintsMock networkHints; | 267 NetworkHintsMock networkHints; |
243 loader->loadLink(LinkRelAttribute("dns-prefetch"), | 268 loader->loadLink(LinkRelAttribute("dns-prefetch"), |
244 CrossOriginAttributeNotSet, String(), String(), String(), | 269 CrossOriginAttributeNotSet, String(), String(), String(), |
245 hrefURL, dummyPageHolder->document(), networkHints); | 270 String(), hrefURL, dummyPageHolder->document(), |
271 networkHints); | |
246 EXPECT_FALSE(networkHints.didPreconnect()); | 272 EXPECT_FALSE(networkHints.didPreconnect()); |
247 EXPECT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); | 273 EXPECT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); |
248 } | 274 } |
249 } | 275 } |
250 | 276 |
251 TEST(LinkLoaderTest, Preconnect) { | 277 TEST(LinkLoaderTest, Preconnect) { |
252 struct { | 278 struct { |
253 const char* href; | 279 const char* href; |
254 CrossOriginAttributeValue crossOrigin; | 280 CrossOriginAttributeValue crossOrigin; |
255 const bool shouldLoad; | 281 const bool shouldLoad; |
(...skipping 11 matching lines...) Expand all Loading... | |
267 std::unique_ptr<DummyPageHolder> dummyPageHolder = | 293 std::unique_ptr<DummyPageHolder> dummyPageHolder = |
268 DummyPageHolder::create(IntSize(500, 500)); | 294 DummyPageHolder::create(IntSize(500, 500)); |
269 Persistent<MockLinkLoaderClient> loaderClient = | 295 Persistent<MockLinkLoaderClient> loaderClient = |
270 MockLinkLoaderClient::create(testCase.shouldLoad); | 296 MockLinkLoaderClient::create(testCase.shouldLoad); |
271 LinkLoader* loader = LinkLoader::create(loaderClient.get()); | 297 LinkLoader* loader = LinkLoader::create(loaderClient.get()); |
272 KURL hrefURL = | 298 KURL hrefURL = |
273 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), | 299 KURL(KURL(ParsedURLStringTag(), String("http://example.com")), |
274 testCase.href); | 300 testCase.href); |
275 NetworkHintsMock networkHints; | 301 NetworkHintsMock networkHints; |
276 loader->loadLink(LinkRelAttribute("preconnect"), testCase.crossOrigin, | 302 loader->loadLink(LinkRelAttribute("preconnect"), testCase.crossOrigin, |
277 String(), String(), String(), hrefURL, | 303 String(), String(), String(), String(), hrefURL, |
278 dummyPageHolder->document(), networkHints); | 304 dummyPageHolder->document(), networkHints); |
279 EXPECT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); | 305 EXPECT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); |
280 EXPECT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); | 306 EXPECT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); |
281 EXPECT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); | 307 EXPECT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); |
282 } | 308 } |
283 } | 309 } |
284 | 310 |
285 } // namespace blink | 311 } // namespace blink |
OLD | NEW |