Chromium Code Reviews| Index: Source/core/html/parser/HTMLPreloadScannerTest.cpp |
| diff --git a/Source/core/html/parser/HTMLPreloadScannerTest.cpp b/Source/core/html/parser/HTMLPreloadScannerTest.cpp |
| index 55a257f377b004b03816e6c6a391d78d35c46a69..28e3b9071111413bb88dca6b0036d21e71208a8b 100644 |
| --- a/Source/core/html/parser/HTMLPreloadScannerTest.cpp |
| +++ b/Source/core/html/parser/HTMLPreloadScannerTest.cpp |
| @@ -11,6 +11,7 @@ |
| #include "core/frame/Settings.h" |
| #include "core/html/parser/HTMLParserOptions.h" |
| #include "core/html/parser/HTMLResourcePreloader.h" |
| +#include "core/loader/Preconnecter.h" |
| #include "core/testing/DummyPageHolder.h" |
| #include <gtest/gtest.h> |
| @@ -26,6 +27,44 @@ typedef struct { |
| ClientHintsPreferences preferences; |
| } TestCase; |
| +typedef struct { |
| + const char* baseURL; |
| + const char* inputHTML; |
| + const char* preconnectedHost; |
| + CrossOriginAttributeValue crossOrigin; |
| +} PreconnectTestCase; |
| + |
| +class MockPreconnecter : public PreconnecterHost { |
|
Mike West
2015/06/02 11:18:34
Nit: I'd prefer either `MockPreconnecterHost : pub
|
| +public: |
| + static PassOwnPtr<PreconnecterHost> createLeavingReference(MockPreconnecter*& preconnector) |
| + { |
| + preconnector = new MockPreconnecter(); |
| + return adoptPtr(preconnector); |
| + } |
| + |
| + virtual void preconnect(KURL& host, CrossOriginAttributeValue crossOrigin) override |
| + { |
| + m_host = host; |
| + m_crossOrigin = crossOrigin; |
| + } |
| + |
| + void init() |
| + { |
| + m_host = KURL(); |
| + m_crossOrigin = CrossOriginAttributeNotSet; |
| + } |
| + |
| + void verify(String host, CrossOriginAttributeValue crossOrigin) |
| + { |
| + EXPECT_STREQ(m_host.string().ascii().data(), host.ascii().data()); |
| + EXPECT_EQ(m_crossOrigin, crossOrigin); |
| + } |
| + |
| +private: |
| + |
|
Mike West
2015/06/02 11:18:33
Nit: No newline.
|
| + KURL m_host; |
| + CrossOriginAttributeValue m_crossOrigin; |
| +}; |
| class MockHTMLResourcePreloader : public ResourcePreloader { |
|
Mike West
2015/06/02 11:18:34
Nit: Newline.
|
| public: |
| void preloadRequestVerification(Resource::Type type, const String& url, const String& baseURL, int width) |
| @@ -79,7 +118,7 @@ protected: |
| KURL documentURL(ParsedURLString, "http://whatever.test/"); |
| m_dummyPageHolder->document().settings()->setViewportEnabled(viewportEnabled); |
| m_dummyPageHolder->document().settings()->setViewportMetaEnabled(viewportEnabled); |
| - m_scanner = HTMLPreloadScanner::create(options, documentURL, CachedDocumentParameters::create(&m_dummyPageHolder->document(), createMediaValues())); |
| + m_scanner = HTMLPreloadScanner::create(options, documentURL, CachedDocumentParameters::create(&m_dummyPageHolder->document(), createMediaValues()), MockPreconnecter::createLeavingReference(m_preconnecter)); |
| } |
| virtual void SetUp() |
| @@ -97,9 +136,20 @@ protected: |
| preloader.preloadRequestVerification(testCase.type, testCase.preloadedURL, testCase.outputBaseURL, testCase.resourceWidth); |
| } |
| + void test(PreconnectTestCase testCase) |
| + { |
| + m_preconnecter->init(); |
| + MockHTMLResourcePreloader preloader; |
| + KURL baseURL(ParsedURLString, testCase.baseURL); |
| + m_scanner->appendToEnd(String(testCase.inputHTML)); |
| + m_scanner->scan(&preloader, baseURL); |
| + m_preconnecter->verify(testCase.preconnectedHost, testCase.crossOrigin); |
| + } |
| + |
| private: |
| OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| OwnPtr<HTMLPreloadScanner> m_scanner; |
| + MockPreconnecter* m_preconnecter; |
| }; |
| TEST_F(HTMLPreloadScannerTest, testImages) |
| @@ -204,4 +254,17 @@ TEST_F(HTMLPreloadScannerTest, testMetaAcceptCH) |
| test(testCase); |
| } |
| +TEST_F(HTMLPreloadScannerTest, testPreconnect) |
| +{ |
| + PreconnectTestCase testCases[] = { |
| + {"http://example.test", "<link rel=preconnect href=http://example2.test>", "http://example2.test/", CrossOriginAttributeNotSet}, |
| + {"http://example.test", "<link rel=preconnect href=http://example2.test crossorigin=anonymous>", "http://example2.test/", CrossOriginAttributeNotSet}, |
| + {"http://example.test", "<link rel=preconnect href=http://example2.test crossorigin='use-credentials'>", "http://example2.test/", CrossOriginAttributeNotSet}, |
| + {"http://example.test", "<link rel=preconnected href=http://example2.test crossorigin='use-credentials'>", "", CrossOriginAttributeNotSet}, |
|
Mike West
2015/06/02 11:18:34
Is it intentional that the `crossorigin` attribute
|
| + }; |
| + |
| + for (auto testCase : testCases) |
|
Mike West
2015/06/02 11:18:34
Nit: `const auto&`
|
| + test(testCase); |
| +} |
| + |
| } |