Chromium Code Reviews| Index: Source/core/loader/ManifestLoaderTest.cpp |
| diff --git a/Source/core/loader/ManifestLoaderTest.cpp b/Source/core/loader/ManifestLoaderTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..08e9d939cffc51a9f6cff7eb690a68538b66d096 |
| --- /dev/null |
| +++ b/Source/core/loader/ManifestLoaderTest.cpp |
| @@ -0,0 +1,303 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "config.h" |
| +#include "core/loader/ManifestLoader.h" |
| + |
| +#include "core/html/HTMLHeadElement.h" |
| +#include "core/html/HTMLLinkElement.h" |
| +#include "core/testing/DummyPageHolder.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebManifestRequest.h" |
| +#include "public/platform/WebURL.h" |
| +#include "public/platform/WebURLResponse.h" |
| +#include "public/platform/WebUnitTestSupport.h" |
| +#include <gtest/gtest.h> |
| + |
| +using namespace WebCore; |
| + |
| +namespace { |
| + |
| +class ManifestRequest : public blink::WebManifestRequest { |
| +public: |
| + ManifestRequest() |
| + : m_succeeded(false) |
| + , m_failed(false) |
| + { |
| + } |
| + |
| + virtual void requestSucceeded(const blink::WebManifest&) OVERRIDE |
| + { |
| + ASSERT(!m_failed && !m_succeeded); |
| + m_succeeded = true; |
| + } |
| + |
| + virtual void requestFailed(FailureReason reason) OVERRIDE |
| + { |
| + ASSERT(!m_failed && !m_succeeded); |
| + m_failed = true; |
| + m_reason = reason; |
| + } |
| + |
| + bool succeeded() const { return m_succeeded; } |
| + bool failed() const { return m_failed; } |
| + FailureReason reason() const { return m_reason; } |
| + |
| +private: |
| + bool m_succeeded; |
| + bool m_failed; |
| + FailureReason m_reason; |
| +}; |
| + |
| +class ManifestLoaderTest : public ::testing::Test { |
| +protected: |
| + enum CORSType { |
| + NoCORS, |
| + CORSEmptyString, |
| + CORSAnonymous, |
| + CORSUseCredentials |
| + }; |
| + |
| + virtual void SetUp() OVERRIDE; |
| + virtual void TearDown() OVERRIDE; |
| + |
| + void setURL(const String& url) |
| + { |
| + m_url = KURL(ParsedURLString, url); |
| + } |
| + |
| + void setupSuccessResponse(); |
| + void setup404Response(); |
| + void setupLinkManifest(CORSType); |
| + void loadManifestSynchronously(); |
| + |
| + Document* document() { return &m_dummyPageHolder->document(); } |
| + ManifestRequest* request() { return &m_request; } |
| + |
| +private: |
| + OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| + ManifestRequest m_request; |
| + KURL m_url; |
| +}; |
| + |
| +void ManifestLoaderTest::SetUp() |
| +{ |
| + m_dummyPageHolder = DummyPageHolder::create(); |
| + KURL url(ParsedURLString, "http://foo.com/index.html"); |
| + document()->setURL(url); |
| + document()->updateSecurityOrigin(SecurityOrigin::create(url)); |
|
dcheng
2014/05/23 01:17:45
I'm not wildly enthusiastic about using DummyPageH
mlamouri (slow - plz ping)
2014/05/29 15:14:05
I understand what you mean here but I don't think
|
| +} |
| + |
| +void ManifestLoaderTest::TearDown() |
| +{ |
| + blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| +} |
| + |
| +void ManifestLoaderTest::setupSuccessResponse() |
| +{ |
| + ASSERT(!m_url.isNull()); |
| + |
| + blink::WebURLResponse response; |
| + response.initialize(); |
| + response.setMIMEType("application/manifest+json"); |
| + response.setURL(m_url); |
| + WTF::String localPath = String(blink::Platform::current()->unitTestSupport()->webKitRootDir()) + "/Source/web/tests/data/manifest.json"; |
| + blink::Platform::current()->unitTestSupport()->registerMockedURL(m_url, response, localPath); |
| +} |
| + |
| +void ManifestLoaderTest::setup404Response() |
| +{ |
| + ASSERT(!m_url.isNull()); |
| + |
| + blink::WebURLResponse response; |
| + response.initialize(); |
| + response.setMIMEType("application/manifest+json"); |
| + response.setHTTPStatusCode(404); |
| + response.setURL(m_url); |
| + |
| + blink::WebURLError error; |
| + error.reason = 404; // We don't really care but we need a value. |
| + blink::Platform::current()->unitTestSupport()->registerMockedErrorURL(m_url, response, error); |
| +} |
| + |
| +void ManifestLoaderTest::setupLinkManifest(CORSType CORS) |
|
dcheng
2014/05/23 01:17:45
The exception for capitalization is variable names
mlamouri (slow - plz ping)
2014/05/29 15:14:05
Ok.
|
| +{ |
| + ASSERT(!m_url.isNull()); |
| + |
| + RefPtrWillBeRawPtr<HTMLLinkElement> link = HTMLLinkElement::create(*document(), false); |
| + link->setAttribute(WebCore::HTMLNames::relAttr, "manifest"); |
| + link->setAttribute(WebCore::HTMLNames::hrefAttr, AtomicString(m_url.string())); |
| + switch (CORS) { |
| + case CORSEmptyString: |
| + link->setAttribute(WebCore::HTMLNames::crossoriginAttr, ""); |
| + break; |
| + case CORSAnonymous: |
| + link->setAttribute(WebCore::HTMLNames::crossoriginAttr, "anonymous"); |
| + break; |
| + case CORSUseCredentials: |
| + link->setAttribute(WebCore::HTMLNames::crossoriginAttr, "use-credentials"); |
| + break; |
| + case NoCORS: |
| + // do nothing. |
| + break; |
| + } |
| + document()->head()->appendChild(link); |
| +} |
| + |
| +void ManifestLoaderTest::loadManifestSynchronously() |
| +{ |
| + // This call is wrapped in a helper call on purpose because we want to get |
| + // rid of it. It will make the transition to the new model easier. |
| + ManifestLoader::loadManifest(request(), document()); |
| + blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoLinkManifest) |
| +{ |
| + ManifestLoader::loadManifest(request(), document()); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NoLinkManifest, request()->reason()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORSValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(NoCORS); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(request()->succeeded()); |
| + EXPECT_FALSE(request()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORSCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(NoCORS); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(request()->succeeded()); |
| + EXPECT_FALSE(request()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSAnonymousValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSAnonymous); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(request()->succeeded()); |
| + EXPECT_FALSE(request()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSDefaultValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(request()->succeeded()); |
| + EXPECT_FALSE(request()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSUseCredentialsValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSUseCredentials); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(request()->succeeded()); |
| + EXPECT_FALSE(request()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSAnonymousCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSAnonymous); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSDefaultCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSUseCredentialsCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSUseCredentials); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORS404URL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setup404Response(); |
| + setupLinkManifest(NoCORS); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORS404URL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setup404Response(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORS404CrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setup404Response(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(request()->succeeded()); |
| + EXPECT_TRUE(request()->failed()); |
| + EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| +} |
| + |
| +} // anonymous namespace |