Chromium Code Reviews| Index: Source/web/tests/ManifestLoaderTest.cpp |
| diff --git a/Source/web/tests/ManifestLoaderTest.cpp b/Source/web/tests/ManifestLoaderTest.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a4e7773e035f01cc535092110c551587911ba757 |
| --- /dev/null |
| +++ b/Source/web/tests/ManifestLoaderTest.cpp |
| @@ -0,0 +1,233 @@ |
| +// 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/testing/URLTestHelpers.h" |
| +#include "public/platform/Platform.h" |
| +#include "public/platform/WebURLResponse.h" |
| +#include "public/platform/WebUnitTestSupport.h" |
| +#include "web/WebLocalFrameImpl.h" |
| +#include "web/tests/FrameTestHelpers.h" |
| +#include <gtest/gtest.h> |
| + |
| +using namespace blink; |
| + |
| +namespace { |
| + |
| +class TestManifestLoaderFrameClient : public FrameTestHelpers::TestWebFrameClient { |
| +public: |
| + TestManifestLoaderFrameClient() |
| + : m_succeeded(false) |
| + , m_failed(false) |
| + { |
| + } |
| + |
| + ~TestManifestLoaderFrameClient() { } |
| + |
| + virtual void didLoadManifest(WebLocalFrame* frame, const WebManifest&) OVERRIDE |
| + { |
| + ASSERT(!m_failed && !m_succeeded); |
| + m_succeeded = true; |
| + } |
| + |
| + virtual void didFailLoadManifest(WebLocalFrame* frame, 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 ManifestResponseType { |
| + NoManifestResponse, |
| + ManifestResponseSuccess, |
| + ManifestResponseError, |
| + }; |
| + |
| + virtual void TearDown() OVERRIDE; |
| + |
| + void setupAndLoad(const std::string& filename, const std::string& url, ManifestResponseType); |
| + |
| + TestManifestLoaderFrameClient* client() |
| + { |
| + return &m_client; |
| + } |
| + |
| +private: |
| + void setupSuccessResponse(); |
| + void setup404Response(); |
| + void loadManifestSynchronously(); |
| + |
| + LocalFrame* frame() |
| + { |
| + return m_webViewHelper.webViewImpl()->mainFrameImpl()->frame(); |
| + } |
| + |
| + FrameTestHelpers::WebViewHelper m_webViewHelper; |
| + TestManifestLoaderFrameClient m_client; |
| +}; |
| + |
| +void ManifestLoaderTest::TearDown() |
| +{ |
| + Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| +} |
| + |
| +void ManifestLoaderTest::setupSuccessResponse() |
| +{ |
| + URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL("http://foo.com/manifest.json"), "dummy_manifest.json", "manifest/", "application/manifest+json"); |
|
kenneth.christiansen
2014/09/02 08:42:23
Other unittests use m_baseURL and www.test.com. I
mlamouri (slow - plz ping)
2014/09/02 15:24:39
Done.
|
| +} |
| + |
| +void ManifestLoaderTest::setup404Response() |
|
kenneth.christiansen
2014/09/02 08:42:23
I rather have ::addMockedURLWith404Response(url).
mlamouri (slow - plz ping)
2014/09/02 15:24:39
I've added the url as a parameter. Not sure what y
|
| +{ |
| + KURL url(ParsedURLString, "http://foo.com/manifest.json"); |
| + |
| + WebURLResponse response; |
| + response.initialize(); |
| + response.setMIMEType("application/manifest+json"); |
| + response.setHTTPStatusCode(404); |
| + response.setURL(url); |
| + |
| + WebURLError error; |
| + error.reason = 404; // We don't really care but we need a value. |
| + Platform::current()->unitTestSupport()->registerMockedErrorURL(url, response, error); |
| +} |
| + |
| +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(); |
| +} |
| + |
| +void ManifestLoaderTest::setupAndLoad(const std::string& filename, const std::string& url, ManifestResponseType manifestResponse) |
| +{ |
| + URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL(url), WebString::fromUTF8(filename), "manifest/", "text/html"); |
| + m_webViewHelper.initializeAndLoad(url, false, &m_client); |
| + |
| + if (manifestResponse == ManifestResponseSuccess) |
| + setupSuccessResponse(); |
|
kenneth.christiansen
2014/09/02 08:42:23
I think its a bit unclear here that this method ac
mlamouri (slow - plz ping)
2014/09/02 15:24:39
I've renamed the method to include "Manifest" and
|
| + else if (manifestResponse == ManifestResponseError) |
| + setup404Response(); |
| + |
| + loadManifestSynchronously(); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoLinkManifest) |
| +{ |
| + setupAndLoad("no-manifest.html", "http://foo.com/index.html", NoManifestResponse); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::NoManifest, client()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORSValidURL) |
| +{ |
| + setupAndLoad("no-cors.html", "http://foo.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_TRUE(client()->succeeded()); |
| + EXPECT_FALSE(client()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORSCrossOriginURL) |
| +{ |
| + setupAndLoad("no-cors.html", "http://bar.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_TRUE(client()->succeeded()); |
| + EXPECT_FALSE(client()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSAnonymousValidURL) |
| +{ |
| + setupAndLoad("cors-anonymous.html", "http://foo.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_TRUE(client()->succeeded()); |
| + EXPECT_FALSE(client()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSDefaultValidURL) |
| +{ |
| + setupAndLoad("cors-default.html", "http://foo.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_TRUE(client()->succeeded()); |
| + EXPECT_FALSE(client()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSUseCredentialsValidURL) |
| +{ |
| + setupAndLoad("cors-use-credentials.html", "http://foo.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_TRUE(client()->succeeded()); |
| + EXPECT_FALSE(client()->failed()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSAnonymousCrossOriginURL) |
| +{ |
| + setupAndLoad("cors-anonymous.html", "http://bar.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, client()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSDefaultCrossOriginURL) |
| +{ |
| + setupAndLoad("cors-default.html", "http://bar.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, client()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORSUseCredentialsCrossOriginURL) |
| +{ |
| + setupAndLoad("cors-use-credentials.html", "http://bar.com/index.html", ManifestResponseSuccess); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, client()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, NoCORS404URL) |
| +{ |
| + setupAndLoad("no-cors.html", "http://foo.com/index.html", ManifestResponseError); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, client()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORS404URL) |
| +{ |
| + setupAndLoad("cors-default.html", "http://foo.com/index.html", ManifestResponseError); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, client()->error()); |
| +} |
| + |
| +TEST_F(ManifestLoaderTest, CORS404CrossOriginURL) |
| +{ |
| + setupAndLoad("cors-default.html", "http://bar.com/index.html", ManifestResponseError); |
| + |
| + EXPECT_FALSE(client()->succeeded()); |
| + EXPECT_TRUE(client()->failed()); |
| + EXPECT_EQ(WebManifestError::FetchError, client()->error()); |
| +} |
| + |
| +} // anonymous namespace |