| 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" |
| 11 #include "core/loader/DocumentLoader.h" | 11 #include "core/loader/DocumentLoader.h" |
| 12 #include "core/loader/LinkLoaderClient.h" | 12 #include "core/loader/LinkLoaderClient.h" |
| 13 #include "core/loader/NetworkHintsInterface.h" | 13 #include "core/loader/NetworkHintsInterface.h" |
| 14 #include "core/testing/DummyPageHolder.h" | 14 #include "core/testing/DummyPageHolder.h" |
| 15 #include "platform/network/ResourceLoadPriority.h" | 15 #include "platform/network/ResourceLoadPriority.h" |
| 16 #include "platform/testing/URLTestHelpers.h" |
| 17 #include "public/platform/Platform.h" |
| 18 #include "public/platform/WebUnitTestSupport.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include <base/macros.h> | 20 #include <base/macros.h> |
| 18 | 21 |
| 19 namespace blink { | 22 namespace blink { |
| 20 | 23 |
| 21 class MockLinkLoaderClient final : public NoBaseWillBeGarbageCollectedFinalized<
MockLinkLoaderClient>, public LinkLoaderClient { | 24 class MockLinkLoaderClient final : public NoBaseWillBeGarbageCollectedFinalized<
MockLinkLoaderClient>, public LinkLoaderClient { |
| 22 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockLinkLoaderClient); | 25 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockLinkLoaderClient); |
| 23 public: | 26 public: |
| 24 static PassOwnPtrWillBeRawPtr<MockLinkLoaderClient> create(bool shouldLoad) | 27 static PassOwnPtrWillBeRawPtr<MockLinkLoaderClient> create(bool shouldLoad) |
| 25 { | 28 { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 struct TestCase { | 90 struct TestCase { |
| 88 const char* href; | 91 const char* href; |
| 89 const char* as; | 92 const char* as; |
| 90 const char* type; | 93 const char* type; |
| 91 const ResourceLoadPriority priority; | 94 const ResourceLoadPriority priority; |
| 92 const WebURLRequest::RequestContext context; | 95 const WebURLRequest::RequestContext context; |
| 93 const bool linkLoaderShouldLoadValue; | 96 const bool linkLoaderShouldLoadValue; |
| 94 const bool expectingLoad; | 97 const bool expectingLoad; |
| 95 const char* accept; | 98 const char* accept; |
| 96 } cases[] = { | 99 } cases[] = { |
| 97 {"data://example.test/cat.jpg", "image", "", ResourceLoadPriorityVeryLow
, WebURLRequest::RequestContextImage, true, true, "image/webp,image/*,*/*;q=0.8"
}, | 100 {"http://example.test/cat.jpg", "image", "", ResourceLoadPriorityVeryLow
, WebURLRequest::RequestContextImage, true, true, "image/webp,image/*,*/*;q=0.8"
}, |
| 98 {"data://example.test/cat.js", "script", "", ResourceLoadPriorityMedium,
WebURLRequest::RequestContextScript, true, true, "*/*"}, | 101 {"http://example.test/cat.js", "script", "", ResourceLoadPriorityMedium,
WebURLRequest::RequestContextScript, true, true, "*/*"}, |
| 99 {"data://example.test/cat.css", "style", "", ResourceLoadPriorityHigh, W
ebURLRequest::RequestContextStyle, true, true, "text/css,*/*;q=0.1"}, | 102 {"http://example.test/cat.css", "style", "", ResourceLoadPriorityHigh, W
ebURLRequest::RequestContextStyle, true, true, "text/css,*/*;q=0.1"}, |
| 100 // TODO(yoav): It doesn't seem like the audio context is ever used. That
should probably be fixed (or we can consolidate audio and video). | 103 // TODO(yoav): It doesn't seem like the audio context is ever used. That
should probably be fixed (or we can consolidate audio and video). |
| 101 {"data://example.test/cat.wav", "media", "", ResourceLoadPriorityLow, We
bURLRequest::RequestContextVideo, true, true, ""}, | 104 {"http://example.test/cat.wav", "media", "", ResourceLoadPriorityLow, We
bURLRequest::RequestContextVideo, true, true, ""}, |
| 102 {"data://example.test/cat.mp4", "media", "", ResourceLoadPriorityLow, We
bURLRequest::RequestContextVideo, true, true, ""}, | 105 {"http://example.test/cat.mp4", "media", "", ResourceLoadPriorityLow, We
bURLRequest::RequestContextVideo, true, true, ""}, |
| 103 {"data://example.test/cat.vtt", "track", "", ResourceLoadPriorityLow, We
bURLRequest::RequestContextTrack, true, true, ""}, | 106 {"http://example.test/cat.vtt", "track", "", ResourceLoadPriorityLow, We
bURLRequest::RequestContextTrack, true, true, ""}, |
| 104 {"data://example.test/cat.woff", "font", "", ResourceLoadPriorityMedium,
WebURLRequest::RequestContextFont, true, true, ""}, | 107 {"http://example.test/cat.woff", "font", "", ResourceLoadPriorityMedium,
WebURLRequest::RequestContextFont, true, true, ""}, |
| 105 // TODO(yoav): subresource should be *very* low priority (rather than lo
w). | 108 // TODO(yoav): subresource should be *very* low priority (rather than lo
w). |
| 106 {"data://example.test/cat.empty", "", "", ResourceLoadPriorityLow, WebUR
LRequest::RequestContextSubresource, true, true, ""}, | 109 {"http://example.test/cat.empty", "", "", ResourceLoadPriorityLow, WebUR
LRequest::RequestContextSubresource, true, true, ""}, |
| 107 {"data://example.test/cat.blob", "blabla", "", ResourceLoadPriorityLow,
WebURLRequest::RequestContextSubresource, false, false, ""}, | 110 {"http://example.test/cat.blob", "blabla", "", ResourceLoadPriorityLow,
WebURLRequest::RequestContextSubresource, false, false, ""}, |
| 108 {"bla://example.test/cat.gif", "image", "", ResourceLoadPriorityUnresolv
ed, WebURLRequest::RequestContextImage, false, false, ""}, | 111 {"bla://example.test/cat.gif", "image", "", ResourceLoadPriorityUnresolv
ed, WebURLRequest::RequestContextImage, false, false, ""}, |
| 109 // MIME type tests | 112 // MIME type tests |
| 110 {"data://example.test/cat.webp", "image", "image/webp", ResourceLoadPrio
rityVeryLow, WebURLRequest::RequestContextImage, true, true, "image/webp,image/*
,*/*;q=0.8"}, | 113 {"http://example.test/cat.webp", "image", "image/webp", ResourceLoadPrio
rityVeryLow, WebURLRequest::RequestContextImage, true, true, "image/webp,image/*
,*/*;q=0.8"}, |
| 111 {"data://example.test/cat.svg", "image", "image/svg+xml", ResourceLoadPr
iorityVeryLow, WebURLRequest::RequestContextImage, true, true, "image/webp,image
/*,*/*;q=0.8"}, | 114 {"http://example.test/cat.svg", "image", "image/svg+xml", ResourceLoadPr
iorityVeryLow, WebURLRequest::RequestContextImage, true, true, "image/webp,image
/*,*/*;q=0.8"}, |
| 112 {"data://example.test/cat.jxr", "image", "image/jxr", ResourceLoadPriori
tyUnresolved, WebURLRequest::RequestContextImage, false, false, ""}, | 115 {"http://example.test/cat.jxr", "image", "image/jxr", ResourceLoadPriori
tyUnresolved, WebURLRequest::RequestContextImage, false, false, ""}, |
| 113 {"data://example.test/cat.js", "script", "text/javascript", ResourceLoad
PriorityMedium, WebURLRequest::RequestContextScript, true, true, "*/*"}, | 116 {"http://example.test/cat.js", "script", "text/javascript", ResourceLoad
PriorityMedium, WebURLRequest::RequestContextScript, true, true, "*/*"}, |
| 114 {"data://example.test/cat.js", "script", "text/coffeescript", ResourceLo
adPriorityUnresolved, WebURLRequest::RequestContextScript, false, false, ""}, | 117 {"http://example.test/cat.js", "script", "text/coffeescript", ResourceLo
adPriorityUnresolved, WebURLRequest::RequestContextScript, false, false, ""}, |
| 115 {"data://example.test/cat.css", "style", "text/css", ResourceLoadPriorit
yHigh, WebURLRequest::RequestContextStyle, true, true, "text/css,*/*;q=0.1"}, | 118 {"http://example.test/cat.css", "style", "text/css", ResourceLoadPriorit
yHigh, WebURLRequest::RequestContextStyle, true, true, "text/css,*/*;q=0.1"}, |
| 116 {"data://example.test/cat.css", "style", "text/sass", ResourceLoadPriori
tyUnresolved, WebURLRequest::RequestContextStyle, false, false, ""}, | 119 {"http://example.test/cat.css", "style", "text/sass", ResourceLoadPriori
tyUnresolved, WebURLRequest::RequestContextStyle, false, false, ""}, |
| 117 {"data://example.test/cat.wav", "media", "audio/wav", ResourceLoadPriori
tyLow, WebURLRequest::RequestContextVideo, true, true, ""}, | 120 {"http://example.test/cat.wav", "media", "audio/wav", ResourceLoadPriori
tyLow, WebURLRequest::RequestContextVideo, true, true, ""}, |
| 118 {"data://example.test/cat.wav", "media", "audio/mp57", ResourceLoadPrior
ityUnresolved, WebURLRequest::RequestContextVideo, false, false, ""}, | 121 {"http://example.test/cat.wav", "media", "audio/mp57", ResourceLoadPrior
ityUnresolved, WebURLRequest::RequestContextVideo, false, false, ""}, |
| 119 {"data://example.test/cat.webm", "media", "video/webm", ResourceLoadPrio
rityLow, WebURLRequest::RequestContextVideo, true, true, ""}, | 122 {"http://example.test/cat.webm", "media", "video/webm", ResourceLoadPrio
rityLow, WebURLRequest::RequestContextVideo, true, true, ""}, |
| 120 {"data://example.test/cat.mp199", "media", "video/mp199", ResourceLoadPr
iorityUnresolved, WebURLRequest::RequestContextVideo, false, false, ""}, | 123 {"http://example.test/cat.mp199", "media", "video/mp199", ResourceLoadPr
iorityUnresolved, WebURLRequest::RequestContextVideo, false, false, ""}, |
| 121 {"data://example.test/cat.vtt", "track", "text/vtt", ResourceLoadPriorit
yLow, WebURLRequest::RequestContextTrack, true, true, ""}, | 124 {"http://example.test/cat.vtt", "track", "text/vtt", ResourceLoadPriorit
yLow, WebURLRequest::RequestContextTrack, true, true, ""}, |
| 122 {"data://example.test/cat.vtt", "track", "text/subtitlething", ResourceL
oadPriorityUnresolved, WebURLRequest::RequestContextTrack, false, false, ""}, | 125 {"http://example.test/cat.vtt", "track", "text/subtitlething", ResourceL
oadPriorityUnresolved, WebURLRequest::RequestContextTrack, false, false, ""}, |
| 123 {"data://example.test/cat.woff", "font", "font/woff2", ResourceLoadPrior
ityMedium, WebURLRequest::RequestContextFont, true, true, ""}, | 126 {"http://example.test/cat.woff", "font", "font/woff2", ResourceLoadPrior
ityMedium, WebURLRequest::RequestContextFont, true, true, ""}, |
| 124 {"data://example.test/cat.woff", "font", "font/woff84", ResourceLoadPrio
rityUnresolved, WebURLRequest::RequestContextFont, false, false, ""}, | 127 {"http://example.test/cat.woff", "font", "font/woff84", ResourceLoadPrio
rityUnresolved, WebURLRequest::RequestContextFont, false, false, ""}, |
| 125 {"data://example.test/cat.empty", "", "foo/bar", ResourceLoadPriorityLow
, WebURLRequest::RequestContextSubresource, true, true, ""}, | 128 {"http://example.test/cat.empty", "", "foo/bar", ResourceLoadPriorityLow
, WebURLRequest::RequestContextSubresource, true, true, ""}, |
| 126 {"data://example.test/cat.blob", "blabla", "foo/bar", ResourceLoadPriori
tyLow, WebURLRequest::RequestContextSubresource, false, false, ""}, | 129 {"http://example.test/cat.blob", "blabla", "foo/bar", ResourceLoadPriori
tyLow, WebURLRequest::RequestContextSubresource, false, false, ""}, |
| 127 }; | 130 }; |
| 128 | 131 |
| 129 // Test the cases with a single header | 132 // Test the cases with a single header |
| 130 for (const auto& testCase : cases) { | 133 for (const auto& testCase : cases) { |
| 131 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSiz
e(500, 500)); | 134 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSiz
e(500, 500)); |
| 132 dummyPageHolder->frame().settings()->setScriptEnabled(true); | 135 dummyPageHolder->frame().settings()->setScriptEnabled(true); |
| 133 OwnPtrWillBePersistent<MockLinkLoaderClient> loaderClient = MockLinkLoad
erClient::create(testCase.linkLoaderShouldLoadValue); | 136 OwnPtrWillBePersistent<MockLinkLoaderClient> loaderClient = MockLinkLoad
erClient::create(testCase.linkLoaderShouldLoadValue); |
| 134 OwnPtrWillBeRawPtr<LinkLoader> loader = LinkLoader::create(loaderClient.
get()); | 137 OwnPtrWillBeRawPtr<LinkLoader> loader = LinkLoader::create(loaderClient.
get()); |
| 135 KURL hrefURL = KURL(KURL(), testCase.href); | 138 KURL hrefURL = KURL(KURL(), testCase.href); |
| 139 URLTestHelpers::registerMockedErrorURLLoad(hrefURL); |
| 136 loader->loadLink(LinkRelAttribute("preload"), | 140 loader->loadLink(LinkRelAttribute("preload"), |
| 137 CrossOriginAttributeNotSet, | 141 CrossOriginAttributeNotSet, |
| 138 testCase.type, | 142 testCase.type, |
| 139 testCase.as, | 143 testCase.as, |
| 140 hrefURL, | 144 hrefURL, |
| 141 dummyPageHolder->document(), | 145 dummyPageHolder->document(), |
| 142 NetworkHintsMock()); | 146 NetworkHintsMock()); |
| 143 ASSERT(dummyPageHolder->document().fetcher()); | 147 ASSERT(dummyPageHolder->document().fetcher()); |
| 144 WillBeHeapListHashSet<RefPtrWillBeMember<Resource>>* preloads = dummyPag
eHolder->document().fetcher()->preloads(); | 148 WillBeHeapListHashSet<RefPtrWillBeMember<Resource>>* preloads = dummyPag
eHolder->document().fetcher()->preloads(); |
| 145 if (testCase.expectingLoad) { | 149 if (testCase.expectingLoad) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 157 if (preloads->size() > 0) { | 161 if (preloads->size() > 0) { |
| 158 Resource* resource = preloads->begin().get()->get(); | 162 Resource* resource = preloads->begin().get()->get(); |
| 159 ASSERT_EQ(testCase.priority, resource->resourceRequest().pri
ority()); | 163 ASSERT_EQ(testCase.priority, resource->resourceRequest().pri
ority()); |
| 160 ASSERT_EQ(testCase.context, resource->resourceRequest().requ
estContext()); | 164 ASSERT_EQ(testCase.context, resource->resourceRequest().requ
estContext()); |
| 161 ASSERT_STREQ(testCase.accept, resource->accept().string().as
cii().data()); | 165 ASSERT_STREQ(testCase.accept, resource->accept().string().as
cii().data()); |
| 162 } | 166 } |
| 163 } | 167 } |
| 164 dummyPageHolder->document().fetcher()->clearPreloads(); | 168 dummyPageHolder->document().fetcher()->clearPreloads(); |
| 165 } | 169 } |
| 166 memoryCache()->evictResources(); | 170 memoryCache()->evictResources(); |
| 171 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| 167 } | 172 } |
| 168 } | 173 } |
| 169 | 174 |
| 170 TEST(LinkLoaderTest, DNSPrefetch) | 175 TEST(LinkLoaderTest, DNSPrefetch) |
| 171 { | 176 { |
| 172 struct { | 177 struct { |
| 173 const char* href; | 178 const char* href; |
| 174 const bool shouldLoad; | 179 const bool shouldLoad; |
| 175 } cases[] = { | 180 } cases[] = { |
| 176 {"http://example.com/", true}, | 181 {"http://example.com/", true}, |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 hrefURL, | 234 hrefURL, |
| 230 dummyPageHolder->document(), | 235 dummyPageHolder->document(), |
| 231 networkHints); | 236 networkHints); |
| 232 ASSERT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); | 237 ASSERT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); |
| 233 ASSERT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); | 238 ASSERT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); |
| 234 ASSERT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); | 239 ASSERT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); |
| 235 } | 240 } |
| 236 } | 241 } |
| 237 | 242 |
| 238 } // namespace blink | 243 } // namespace blink |
| OLD | NEW |