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 "config.h" | 5 #include "config.h" |
6 #include "core/loader/LinkLoader.h" | 6 #include "core/loader/LinkLoader.h" |
7 | 7 |
8 #include "core/fetch/ResourceFetcher.h" | 8 #include "core/fetch/ResourceFetcher.h" |
| 9 #include "core/frame/Settings.h" |
9 #include "core/html/LinkRelAttribute.h" | 10 #include "core/html/LinkRelAttribute.h" |
10 #include "core/loader/LinkLoaderClient.h" | 11 #include "core/loader/LinkLoaderClient.h" |
| 12 #include "core/loader/NetworkHintsInterface.h" |
11 #include "core/testing/DummyPageHolder.h" | 13 #include "core/testing/DummyPageHolder.h" |
12 #include "platform/network/ResourceLoadPriority.h" | 14 #include "platform/network/ResourceLoadPriority.h" |
13 | 15 |
14 #include <base/macros.h> | 16 #include <base/macros.h> |
15 #include <gtest/gtest.h> | 17 #include <gtest/gtest.h> |
16 | 18 |
17 namespace blink { | 19 namespace blink { |
18 | 20 |
19 class MockLinkLoaderClient : public LinkLoaderClient { | 21 class MockLinkLoaderClient : public LinkLoaderClient { |
20 public: | 22 public: |
(...skipping 11 matching lines...) Expand all Loading... |
32 void linkLoadingErrored() override {} | 34 void linkLoadingErrored() override {} |
33 void didStartLinkPrerender() override {} | 35 void didStartLinkPrerender() override {} |
34 void didStopLinkPrerender() override {} | 36 void didStopLinkPrerender() override {} |
35 void didSendLoadForLinkPrerender() override {} | 37 void didSendLoadForLinkPrerender() override {} |
36 void didSendDOMContentLoadedForLinkPrerender() override {} | 38 void didSendDOMContentLoadedForLinkPrerender() override {} |
37 | 39 |
38 private: | 40 private: |
39 bool m_shouldLoad; | 41 bool m_shouldLoad; |
40 }; | 42 }; |
41 | 43 |
| 44 class NetworkHintsMock : public NetworkHintsInterface { |
| 45 public: |
| 46 NetworkHintsMock() |
| 47 : m_didDnsPrefetch(false) |
| 48 , m_didPreconnect(false) |
| 49 { |
| 50 } |
| 51 |
| 52 void dnsPrefetchHost(const String& host) const override |
| 53 { |
| 54 m_didDnsPrefetch = true; |
| 55 } |
| 56 |
| 57 void preconnectHost(const KURL& host, const CrossOriginAttributeValue crossO
rigin) const override |
| 58 { |
| 59 m_didPreconnect = true; |
| 60 m_isHTTPS = host.protocolIs("https"); |
| 61 m_isCrossOrigin = (crossOrigin == CrossOriginAttributeAnonymous); |
| 62 } |
| 63 |
| 64 bool didDnsPrefetch() { return m_didDnsPrefetch; } |
| 65 bool didPreconnect() { return m_didPreconnect; } |
| 66 bool isHTTPS() { return m_isHTTPS; } |
| 67 bool isCrossOrigin() { return m_isCrossOrigin; } |
| 68 |
| 69 private: |
| 70 mutable bool m_didDnsPrefetch; |
| 71 mutable bool m_didPreconnect; |
| 72 mutable bool m_isHTTPS; |
| 73 mutable bool m_isCrossOrigin; |
| 74 |
| 75 }; |
| 76 |
42 TEST(LinkLoaderTest, Preload) | 77 TEST(LinkLoaderTest, Preload) |
43 { | 78 { |
44 struct TestCase { | 79 struct TestCase { |
45 const char* href; | 80 const char* href; |
46 const char* as; | 81 const char* as; |
47 const ResourceLoadPriority priority; | 82 const ResourceLoadPriority priority; |
48 const bool shouldLoad; | 83 const bool shouldLoad; |
49 } cases[] = { | 84 } cases[] = { |
50 {"data://example.com/cat.jpg", "image", ResourceLoadPriorityVeryLow, tru
e}, | 85 {"data://example.com/cat.jpg", "image", ResourceLoadPriorityVeryLow, tru
e}, |
51 {"data://example.com/cat.jpg", "script", ResourceLoadPriorityMedium, tru
e}, | 86 {"data://example.com/cat.jpg", "script", ResourceLoadPriorityMedium, tru
e}, |
52 {"data://example.com/cat.jpg", "stylesheet", ResourceLoadPriorityHigh, t
rue}, | 87 {"data://example.com/cat.jpg", "stylesheet", ResourceLoadPriorityHigh, t
rue}, |
53 {"data://example.com/cat.jpg", "blabla", ResourceLoadPriorityUnresolved,
true}, | 88 {"data://example.com/cat.jpg", "blabla", ResourceLoadPriorityUnresolved,
true}, |
54 {"data://example.com/cat.jpg", "image", ResourceLoadPriorityUnresolved,
false}, | 89 {"data://example.com/cat.jpg", "image", ResourceLoadPriorityUnresolved,
false}, |
55 }; | 90 }; |
56 | 91 |
57 // Test the cases with a single header | 92 // Test the cases with a single header |
58 for (const auto& testCase : cases) { | 93 for (const auto& testCase : cases) { |
59 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSiz
e(500, 500)); | 94 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSiz
e(500, 500)); |
60 MockLinkLoaderClient loaderClient(testCase.shouldLoad); | 95 MockLinkLoaderClient loaderClient(testCase.shouldLoad); |
61 LinkLoader loader(&loaderClient); | 96 LinkLoader loader(&loaderClient); |
62 KURL hrefURL = KURL(KURL(), testCase.href); | 97 KURL hrefURL = KURL(KURL(), testCase.href); |
63 loader.loadLink(LinkRelAttribute("preload"), | 98 loader.loadLink(LinkRelAttribute("preload"), |
64 AtomicString(), | 99 AtomicString(), |
65 String(), | 100 String(), |
66 testCase.as, | 101 testCase.as, |
67 hrefURL, | 102 hrefURL, |
68 dummyPageHolder->document()); | 103 dummyPageHolder->document(), |
| 104 NetworkHintsMock()); |
69 if (testCase.priority == ResourceLoadPriorityUnresolved) { | 105 if (testCase.priority == ResourceLoadPriorityUnresolved) { |
70 ASSERT(!loader.resource()); | 106 ASSERT(!loader.resource()); |
71 } else { | 107 } else { |
72 ASSERT(loader.resource()); | 108 ASSERT(loader.resource()); |
73 ASSERT_EQ(testCase.priority, loader.resource()->resourceRequest().pr
iority()); | 109 ASSERT_EQ(testCase.priority, loader.resource()->resourceRequest().pr
iority()); |
74 } | 110 } |
75 } | 111 } |
76 } | 112 } |
77 | 113 |
| 114 TEST(LinkLoaderTest, DNSPrefetch) |
| 115 { |
| 116 struct { |
| 117 const char* href; |
| 118 const bool shouldLoad; |
| 119 } cases[] = { |
| 120 {"http://example.com/", true}, |
| 121 {"https://example.com/", true}, |
| 122 {"//example.com/", true}, |
| 123 }; |
| 124 |
| 125 // TODO(yoav): Test (and fix) shouldLoad = false |
| 126 |
| 127 // Test the cases with a single header |
| 128 for (const auto& testCase : cases) { |
| 129 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSiz
e(500, 500)); |
| 130 dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); |
| 131 MockLinkLoaderClient loaderClient(testCase.shouldLoad); |
| 132 LinkLoader loader(&loaderClient); |
| 133 KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String("http://example.co
m")), testCase.href); |
| 134 NetworkHintsMock networkHints; |
| 135 loader.loadLink(LinkRelAttribute("dns-prefetch"), |
| 136 AtomicString(), |
| 137 String(), |
| 138 String(), |
| 139 hrefURL, |
| 140 dummyPageHolder->document(), |
| 141 networkHints); |
| 142 ASSERT_FALSE(networkHints.didPreconnect()); |
| 143 ASSERT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); |
| 144 } |
| 145 } |
| 146 |
| 147 TEST(LinkLoaderTest, Preconnect) |
| 148 { |
| 149 struct { |
| 150 const char* href; |
| 151 const char* crossOrigin; |
| 152 const bool shouldLoad; |
| 153 const bool isHTTPS; |
| 154 const bool isCrossOrigin; |
| 155 } cases[] = { |
| 156 {"http://example.com/", nullptr, true, false, false}, |
| 157 {"https://example.com/", nullptr, true, true, false}, |
| 158 {"http://example.com/", "anonymous", true, false, true}, |
| 159 {"//example.com/", nullptr, true, false, false}, |
| 160 }; |
| 161 |
| 162 // Test the cases with a single header |
| 163 for (const auto& testCase : cases) { |
| 164 OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSiz
e(500, 500)); |
| 165 MockLinkLoaderClient loaderClient(testCase.shouldLoad); |
| 166 LinkLoader loader(&loaderClient); |
| 167 KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String("http://example.co
m")), testCase.href); |
| 168 NetworkHintsMock networkHints; |
| 169 loader.loadLink(LinkRelAttribute("preconnect"), |
| 170 testCase.crossOrigin, |
| 171 String(), |
| 172 String(), |
| 173 hrefURL, |
| 174 dummyPageHolder->document(), |
| 175 networkHints); |
| 176 ASSERT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); |
| 177 ASSERT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); |
| 178 ASSERT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); |
| 179 } |
| 180 } |
| 181 |
78 } // namespace blink | 182 } // namespace blink |
OLD | NEW |