Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(642)

Unified Diff: Source/core/loader/ManifestLoaderTest.cpp

Issue 295063002: [NotLanded] Implement the fetching algorithm of the Web Manifest. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/loader/ManifestLoader.cpp ('k') | Source/platform/RuntimeEnabledFeatures.in » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..f3bb6038831eb653ec8f9c8fa7e821902fa48cf6
--- /dev/null
+++ b/Source/core/loader/ManifestLoaderTest.cpp
@@ -0,0 +1,322 @@
+// 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; }
+ bool finished() const { return m_succeeded || 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 = new KURL(ParsedURLString, url);
+ }
+
+ void setupSuccessResponse();
+ void setup404Response();
+ void setupLinkManifest(CorsType);
+
+ 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_url = 0;
+ m_dummyPageHolder = DummyPageHolder::create();
+ KURL url(ParsedURLString, "http://foo.com/index.html");
+ document()->setURL(url);
+ document()->updateSecurityOrigin(SecurityOrigin::create(url));
+}
+
+void ManifestLoaderTest::TearDown()
+{
+ blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
+
+ if (m_url) {
+ delete m_url;
+ }
+}
+
+void ManifestLoaderTest::setupSuccessResponse()
+{
+ ASSERT(m_url);
+
+ 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);
+
+ 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)
+{
+ ASSERT(m_url);
+
+ 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);
+}
+
+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);
+
+ ManifestLoader::LoadManifest(request(), document());
dcheng 2014/05/20 20:11:30 Is there any particular reason not to use WebViewH
mlamouri (slow - plz ping) 2014/05/20 20:17:03 I had no need for it so I didn't use it.
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ EXPECT_TRUE(request()->succeeded());
+ EXPECT_FALSE(request()->failed());
+}
+
+TEST_F(ManifestLoaderTest, NoCorsCrossOriginURL)
+{
+ setURL("http://example.com/manifest.json");
+ setupSuccessResponse();
+ setupLinkManifest(NoCors);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ EXPECT_TRUE(request()->succeeded());
+ EXPECT_FALSE(request()->failed());
+}
+
+TEST_F(ManifestLoaderTest, CorsAnonymousValidURL)
+{
+ setURL("http://foo.com/manifest.json");
+ setupSuccessResponse();
+ setupLinkManifest(CorsAnonymous);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ EXPECT_TRUE(request()->succeeded());
+ EXPECT_FALSE(request()->failed());
+}
+
+TEST_F(ManifestLoaderTest, CorsDefaultValidURL)
+{
+ setURL("http://foo.com/manifest.json");
+ setupSuccessResponse();
+ setupLinkManifest(CorsEmptyString);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ EXPECT_TRUE(request()->succeeded());
+ EXPECT_FALSE(request()->failed());
+}
+
+TEST_F(ManifestLoaderTest, CorsUseCredentialsValidURL)
+{
+ setURL("http://foo.com/manifest.json");
+ setupSuccessResponse();
+ setupLinkManifest(CorsUseCredentials);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ EXPECT_TRUE(request()->succeeded());
+ EXPECT_FALSE(request()->failed());
+}
+
+TEST_F(ManifestLoaderTest, CorsAnonymousCrossOriginURL)
+{
+ setURL("http://example.com/manifest.json");
+ setupSuccessResponse();
+ setupLinkManifest(CorsAnonymous);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ 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);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ 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);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ 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);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ 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);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ 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);
+
+ ManifestLoader::LoadManifest(request(), document());
+
+ blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
+
+ EXPECT_FALSE(request()->succeeded());
+ EXPECT_TRUE(request()->failed());
+ EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason());
+}
+
+} // anonymous namespace
« no previous file with comments | « Source/core/loader/ManifestLoader.cpp ('k') | Source/platform/RuntimeEnabledFeatures.in » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698