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..21f69d986232fd98bc8776685acf0c47b49d7de7 |
| --- /dev/null |
| +++ b/Source/core/loader/ManifestLoaderTest.cpp |
| @@ -0,0 +1,306 @@ |
| +// 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/dom/Document.h" |
| +#include "core/html/HTMLHeadElement.h" |
| +#include "core/html/HTMLLinkElement.h" |
| +#include "core/loader/EmptyClients.h" |
| +#include "core/testing/DummyPageHolder.h" |
| +#include "core/testing/URLTestHelpers.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebURL.h" |
| +#include "public/platform/WebURLResponse.h" |
| +#include "public/platform/WebUnitTestSupport.h" |
| +#include <gtest/gtest.h> |
| + |
| +using namespace blink; |
| + |
| +namespace { |
| + |
| +class ManifestFrameLoaderClient : public EmptyFrameLoaderClient { |
| + WTF_MAKE_NONCOPYABLE(ManifestFrameLoaderClient); WTF_MAKE_FAST_ALLOCATED; |
| +public: |
| + ManifestFrameLoaderClient() |
| + : m_succeeded(false) |
| + , m_failed(false) |
| + { |
| + } |
| + |
| + ~ManifestFrameLoaderClient() { } |
| + |
| + virtual void dispatchDidLoadManifest(WebManifest) OVERRIDE |
| + { |
| + ASSERT(!m_failed && !m_succeeded); |
| + m_succeeded = true; |
| + } |
| + |
| + virtual void dispatchDidFailLoadManifest(WebManifestError error) OVERRIDE |
| + { |
| + ASSERT(!m_failed && !m_succeeded); |
| + m_failed = true; |
| + m_error = error; |
| + } |
| + |
| + bool succeeded() const { return m_succeeded; } |
| + bool failed() const { return m_failed; } |
| + WebManifestError error() const { return m_error; } |
| + |
| +private: |
| + bool m_succeeded; |
| + bool m_failed; |
| + WebManifestError m_error; |
| +}; |
| + |
| +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(); } |
| + LocalFrame* frame() { return &m_dummyPageHolder->frame(); } |
| + ManifestFrameLoaderClient* frameLoaderClient() |
| + { |
| + return static_cast<ManifestFrameLoaderClient*>(m_dummyPageHolder->frame().loader().client()); |
| + } |
| + |
| +private: |
| + OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| + KURL m_url; |
| +}; |
| + |
| +void ManifestLoaderTest::SetUp() |
| +{ |
| + m_dummyPageHolder = DummyPageHolder::create(IntSize(), 0, adoptPtr(new ManifestFrameLoaderClient)); |
| + KURL url(ParsedURLString, "http://foo.com/index.html"); |
| + document()->setURL(url); |
| + document()->updateSecurityOrigin(SecurityOrigin::create(url)); |
| +} |
| + |
| +void ManifestLoaderTest::TearDown() |
| +{ |
| + Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| +} |
| + |
| +void ManifestLoaderTest::setupSuccessResponse() |
| +{ |
| + ASSERT(!m_url.isNull()); |
| + |
| + URLTestHelpers::registerMockedURLLoad(m_url, "dummy_manifest.json", "manifest/", "application/manifest+json"); |
| +} |
| + |
| +void ManifestLoaderTest::setup404Response() |
| +{ |
| + ASSERT(!m_url.isNull()); |
| + |
| + WebURLResponse response; |
| + response.initialize(); |
| + response.setMIMEType("application/manifest+json"); |
| + response.setHTTPStatusCode(404); |
| + response.setURL(m_url); |
| + |
| + WebURLError error; |
| + error.reason = 404; // We don't really care but we need a value. |
| + Platform::current()->unitTestSupport()->registerMockedErrorURL(m_url, response, error); |
| +} |
| + |
| +void ManifestLoaderTest::setupLinkManifest(CORSType cors) |
| +{ |
| + ASSERT(!m_url.isNull()); |
| + |
| + RefPtrWillBeRawPtr<HTMLLinkElement> link = HTMLLinkElement::create(*document(), false); |
| + link->setAttribute(HTMLNames::relAttr, "manifest"); |
| + link->setAttribute(HTMLNames::hrefAttr, AtomicString(m_url.string())); |
| + switch (cors) { |
| + case CORSEmptyString: |
| + link->setAttribute(HTMLNames::crossoriginAttr, ""); |
| + break; |
| + case CORSAnonymous: |
| + link->setAttribute(HTMLNames::crossoriginAttr, "anonymous"); |
|
kenneth.christiansen
2014/08/27 14:49:37
I kind of agree that using separate .html files fo
mlamouri (slow - plz ping)
2014/09/01 19:23:50
Done.
|
| + break; |
| + case CORSUseCredentials: |
| + link->setAttribute(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(PassRefPtr<LocalFrame>(frame())); |
| + Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoLinkManifest) |
| +{ |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::NoManifest, frameLoaderClient()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORSValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(NoCORS); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(frameLoaderClient()->succeeded()); |
| + EXPECT_FALSE(frameLoaderClient()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORSCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(NoCORS); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(frameLoaderClient()->succeeded()); |
| + EXPECT_FALSE(frameLoaderClient()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSAnonymousValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSAnonymous); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(frameLoaderClient()->succeeded()); |
| + EXPECT_FALSE(frameLoaderClient()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSDefaultValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(frameLoaderClient()->succeeded()); |
| + EXPECT_FALSE(frameLoaderClient()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSUseCredentialsValidURL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSUseCredentials); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_TRUE(frameLoaderClient()->succeeded()); |
| + EXPECT_FALSE(frameLoaderClient()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSAnonymousCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSAnonymous); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, frameLoaderClient()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSDefaultCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, frameLoaderClient()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSUseCredentialsCrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setupSuccessResponse(); |
| + setupLinkManifest(CORSUseCredentials); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, frameLoaderClient()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORS404URL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setup404Response(); |
| + setupLinkManifest(NoCORS); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, frameLoaderClient()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORS404URL) |
| +{ |
| + setURL("http://foo.com/manifest.json"); |
| + setup404Response(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, frameLoaderClient()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORS404CrossOriginURL) |
| +{ |
| + setURL("http://example.com/manifest.json"); |
| + setup404Response(); |
| + setupLinkManifest(CORSEmptyString); |
| + |
| + loadManifestSynchronously(); |
| + |
| + EXPECT_FALSE(frameLoaderClient()->succeeded()); |
| + EXPECT_TRUE(frameLoaderClient()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, frameLoaderClient()->error()); |
| +} |
| + |
| +} // anonymous namespace |