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/html/parser/HTMLResourcePreloader.h" | 5 #include "core/html/parser/HTMLResourcePreloader.h" |
6 | 6 |
7 #include "core/html/parser/PreloadRequest.h" | 7 #include "core/html/parser/PreloadRequest.h" |
8 #include "core/testing/DummyPageHolder.h" | 8 #include "core/testing/DummyPageHolder.h" |
9 #include "platform/testing/PlatformMojoMock.h" | |
10 #include "testing/gmock/include/gmock/gmock.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 namespace blink { | 13 namespace blink { |
12 | 14 |
15 using ::testing::_; | |
16 | |
13 struct PreconnectTestCase { | 17 struct PreconnectTestCase { |
14 const char* baseURL; | 18 const char* baseURL; |
15 const char* url; | 19 const char* url; |
16 bool isCORS; | 20 bool isCORS; |
17 bool isHTTPS; | 21 bool isHTTPS; |
18 }; | 22 }; |
19 | 23 |
20 class PreloaderNetworkHintsMock : public NetworkHintsInterface { | |
21 public: | |
22 PreloaderNetworkHintsMock() | |
23 : m_didPreconnect(false) | |
24 { | |
25 } | |
26 | |
27 void dnsPrefetchHost(const String& host) const { } | |
28 void preconnectHost(const KURL& host, const CrossOriginAttributeValue crossO rigin) const override | |
29 { | |
30 m_didPreconnect = true; | |
31 m_isHTTPS = host.protocolIs("https"); | |
32 m_isCrossOrigin = (crossOrigin == CrossOriginAttributeAnonymous); | |
33 } | |
34 | |
35 bool didPreconnect() { return m_didPreconnect; } | |
36 bool isHTTPS() { return m_isHTTPS; } | |
37 bool isCrossOrigin() { return m_isCrossOrigin; } | |
38 | |
39 private: | |
40 mutable bool m_didPreconnect; | |
41 mutable bool m_isHTTPS; | |
42 mutable bool m_isCrossOrigin; | |
43 }; | |
44 | |
45 class HTMLResourcePreloaderTest : public testing::Test { | 24 class HTMLResourcePreloaderTest : public testing::Test { |
46 protected: | 25 protected: |
47 HTMLResourcePreloaderTest() | 26 HTMLResourcePreloaderTest() |
48 : m_dummyPageHolder(DummyPageHolder::create()) | 27 : m_dummyPageHolder(DummyPageHolder::create()) |
49 { | 28 { |
50 } | 29 } |
51 | 30 |
52 void test(PreconnectTestCase testCase) | 31 void test(PreconnectTestCase testCase) |
53 { | 32 { |
54 // TODO(yoav): Need a mock loader here to verify things are happenning b eyond preconnect. | 33 // TODO(yoav): Need a mock loader here to verify things are happenning b eyond preconnect. |
55 PreloaderNetworkHintsMock networkHints; | 34 KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String(testCase.baseURL)) , testCase.url); |
35 | |
36 PlatformMojoMock mockPlatform; | |
37 EXPECT_CALL(mockPlatform, preconnect(hrefURL, ::testing::Ne(testCase.isC ORS), 1)) | |
38 .Times(1); | |
39 EXPECT_CALL(mockPlatform, preresolve(_)) | |
40 .Times(0); | |
41 | |
56 OwnPtr<PreloadRequest> preloadRequest = PreloadRequest::create(String(), | 42 OwnPtr<PreloadRequest> preloadRequest = PreloadRequest::create(String(), |
57 TextPosition(), | 43 TextPosition(), |
58 testCase.url, | 44 testCase.url, |
59 KURL(ParsedURLStringTag(), testCase.baseURL), | 45 KURL(ParsedURLStringTag(), testCase.baseURL), |
60 Resource::Image, | 46 Resource::Image, |
61 ReferrerPolicy(), | 47 ReferrerPolicy(), |
62 FetchRequest::ResourceWidth(), | 48 FetchRequest::ResourceWidth(), |
63 ClientHintsPreferences(), | 49 ClientHintsPreferences(), |
64 PreloadRequest::RequestTypePreconnect); | 50 PreloadRequest::RequestTypePreconnect); |
65 if (testCase.isCORS) | 51 if (testCase.isCORS) |
66 preloadRequest->setCrossOrigin(CrossOriginAttributeAnonymous); | 52 preloadRequest->setCrossOrigin(CrossOriginAttributeAnonymous); |
67 HTMLResourcePreloader* preloader = HTMLResourcePreloader::create(m_dummy PageHolder->document()); | 53 HTMLResourcePreloader* preloader = HTMLResourcePreloader::create(m_dummy PageHolder->document()); |
68 preloader->preload(std::move(preloadRequest), networkHints); | 54 preloader->preload(std::move(preloadRequest)); |
Yoav Weiss
2016/06/13 08:26:08
Where are we verifying that we're actually preconn
Charlie Harrison
2016/06/14 19:28:45
The protocol is included in the EXPECT_CALL with t
| |
69 ASSERT_TRUE(networkHints.didPreconnect()); | |
70 ASSERT_EQ(testCase.isCORS, networkHints.isCrossOrigin()); | |
71 ASSERT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); | |
72 } | 55 } |
73 | 56 |
74 private: | 57 private: |
75 OwnPtr<DummyPageHolder> m_dummyPageHolder; | 58 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
76 }; | 59 }; |
77 | 60 |
78 TEST_F(HTMLResourcePreloaderTest, testPreconnect) | 61 TEST_F(HTMLResourcePreloaderTest, testPreconnect) |
79 { | 62 { |
80 PreconnectTestCase testCases[] = { | 63 PreconnectTestCase testCases[] = { |
81 { "http://example.test", "http://example.com", false, false }, | 64 { "http://example.test", "http://example.com", false, false }, |
82 { "http://example.test", "http://example.com", true, false }, | 65 { "http://example.test", "http://example.com", true, false }, |
83 { "http://example.test", "https://example.com", true, true }, | 66 { "http://example.test", "https://example.com", true, true }, |
84 { "http://example.test", "https://example.com", false, true }, | 67 { "http://example.test", "https://example.com", false, true }, |
85 { "http://example.test", "//example.com", false, false }, | 68 { "http://example.test", "//example.com", false, false }, |
86 { "http://example.test", "//example.com", true, false }, | 69 { "http://example.test", "//example.com", true, false }, |
87 { "https://example.test", "//example.com", false, true }, | 70 { "https://example.test", "//example.com", false, true }, |
88 { "https://example.test", "//example.com", true, true }, | 71 { "https://example.test", "//example.com", true, true }, |
89 }; | 72 }; |
90 | 73 |
91 for (const auto& testCase : testCases) | 74 for (const auto& testCase : testCases) |
92 test(testCase); | 75 test(testCase); |
93 } | 76 } |
94 | 77 |
95 } // namespace blink | 78 } // namespace blink |
OLD | NEW |