Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "core/loader/ManifestLoader.h" | |
| 7 | |
| 8 #include "core/testing/URLTestHelpers.h" | |
| 9 #include "public/platform/Platform.h" | |
| 10 #include "public/platform/WebURLResponse.h" | |
| 11 #include "public/platform/WebUnitTestSupport.h" | |
| 12 #include "web/WebLocalFrameImpl.h" | |
| 13 #include "web/tests/FrameTestHelpers.h" | |
| 14 #include <gtest/gtest.h> | |
| 15 | |
| 16 using namespace blink; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class TestManifestLoaderFrameClient : public FrameTestHelpers::TestWebFrameClien t { | |
| 21 public: | |
| 22 TestManifestLoaderFrameClient() | |
| 23 : m_succeeded(false) | |
| 24 , m_failed(false) | |
| 25 { | |
| 26 } | |
| 27 | |
| 28 ~TestManifestLoaderFrameClient() { } | |
| 29 | |
| 30 virtual void didLoadManifest(WebLocalFrame* frame, const WebManifest&) OVERR IDE | |
| 31 { | |
| 32 ASSERT(!m_failed && !m_succeeded); | |
| 33 m_succeeded = true; | |
| 34 } | |
| 35 | |
| 36 virtual void didFailLoadManifest(WebLocalFrame* frame, WebManifestError erro r) OVERRIDE | |
| 37 { | |
| 38 ASSERT(!m_failed && !m_succeeded); | |
| 39 m_failed = true; | |
| 40 m_error = error; | |
| 41 } | |
| 42 | |
| 43 bool succeeded() const { return m_succeeded; } | |
| 44 bool failed() const { return m_failed; } | |
| 45 WebManifestError error() const { return m_error; } | |
| 46 | |
| 47 private: | |
| 48 bool m_succeeded; | |
| 49 bool m_failed; | |
| 50 WebManifestError m_error; | |
| 51 }; | |
| 52 | |
| 53 class ManifestLoaderTest : public ::testing::Test { | |
| 54 protected: | |
| 55 enum ManifestResponseType { | |
| 56 NoManifestResponse, | |
| 57 ManifestResponseSuccess, | |
| 58 ManifestResponseError, | |
| 59 }; | |
| 60 | |
| 61 virtual void TearDown() OVERRIDE; | |
| 62 | |
| 63 void setupAndLoad(const std::string filename, const std::string url, Manifes tResponseType); | |
|
dcheng
2014/09/01 21:31:14
Nit: const std::string&
mlamouri (slow - plz ping)
2014/09/01 22:49:13
Done.
| |
| 64 | |
| 65 TestManifestLoaderFrameClient* client() | |
| 66 { | |
| 67 return &m_client; | |
| 68 } | |
| 69 | |
| 70 private: | |
| 71 void setupSuccessResponse(); | |
| 72 void setup404Response(); | |
| 73 void loadManifestSynchronously(); | |
| 74 | |
| 75 LocalFrame* frame() | |
| 76 { | |
| 77 return m_webViewHelper.webViewImpl()->mainFrameImpl()->frame(); | |
| 78 } | |
| 79 | |
| 80 FrameTestHelpers::WebViewHelper m_webViewHelper; | |
| 81 TestManifestLoaderFrameClient m_client; | |
| 82 }; | |
| 83 | |
| 84 void ManifestLoaderTest::TearDown() | |
| 85 { | |
| 86 Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
| 87 } | |
| 88 | |
| 89 void ManifestLoaderTest::setupSuccessResponse() | |
| 90 { | |
| 91 URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL("http://foo.com /manifest.json"), "dummy_manifest.json", "manifest/", "application/manifest+json "); | |
| 92 } | |
| 93 | |
| 94 void ManifestLoaderTest::setup404Response() | |
| 95 { | |
| 96 KURL url(ParsedURLString, "http://foo.com/manifest.json"); | |
| 97 | |
| 98 WebURLResponse response; | |
| 99 response.initialize(); | |
| 100 response.setMIMEType("application/manifest+json"); | |
| 101 response.setHTTPStatusCode(404); | |
| 102 response.setURL(url); | |
| 103 | |
| 104 WebURLError error; | |
| 105 error.reason = 404; // We don't really care but we need a value. | |
| 106 Platform::current()->unitTestSupport()->registerMockedErrorURL(url, response , error); | |
| 107 } | |
| 108 | |
| 109 void ManifestLoaderTest::loadManifestSynchronously() | |
| 110 { | |
| 111 // This call is wrapped in a helper call on purpose because we want to get | |
| 112 // rid of it. It will make the transition to the new model easier. | |
| 113 ManifestLoader::loadManifest(PassRefPtr<LocalFrame>(frame())); | |
| 114 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); | |
| 115 } | |
| 116 | |
| 117 void ManifestLoaderTest::setupAndLoad(const std::string filename, const std::str ing url, ManifestResponseType manifestResponse) | |
| 118 { | |
| 119 URLTestHelpers::registerMockedURLLoad(URLTestHelpers::toKURL(url), WebString ::fromUTF8(filename), "manifest/", "text/html"); | |
| 120 m_webViewHelper.initializeAndLoad(url, false, &m_client); | |
| 121 | |
| 122 if (manifestResponse == ManifestResponseSuccess) | |
| 123 setupSuccessResponse(); | |
| 124 else if (manifestResponse == ManifestResponseError) | |
| 125 setup404Response(); | |
| 126 | |
| 127 loadManifestSynchronously(); | |
| 128 } | |
| 129 | |
| 130 TEST_F(ManifestLoaderTest, NoLinkManifest) | |
| 131 { | |
| 132 setupAndLoad("no-manifest.html", "http://foo.com/index.html", NoManifestResp onse); | |
| 133 | |
| 134 EXPECT_FALSE(client()->succeeded()); | |
| 135 EXPECT_TRUE(client()->failed()); | |
| 136 EXPECT_EQ(WebManifestError::NoManifest, client()->error()); | |
| 137 } | |
| 138 | |
| 139 TEST_F(ManifestLoaderTest, NoCORSValidURL) | |
| 140 { | |
| 141 setupAndLoad("no-cors.html", "http://foo.com/index.html", ManifestResponseSu ccess); | |
| 142 | |
| 143 EXPECT_TRUE(client()->succeeded()); | |
| 144 EXPECT_FALSE(client()->failed()); | |
| 145 } | |
| 146 | |
| 147 TEST_F(ManifestLoaderTest, NoCORSCrossOriginURL) | |
| 148 { | |
| 149 setupAndLoad("no-cors.html", "http://bar.com/index.html", ManifestResponseSu ccess); | |
| 150 | |
| 151 EXPECT_TRUE(client()->succeeded()); | |
| 152 EXPECT_FALSE(client()->failed()); | |
| 153 } | |
| 154 | |
| 155 TEST_F(ManifestLoaderTest, CORSAnonymousValidURL) | |
| 156 { | |
| 157 setupAndLoad("cors-anonymous.html", "http://foo.com/index.html", ManifestRes ponseSuccess); | |
| 158 | |
| 159 EXPECT_TRUE(client()->succeeded()); | |
| 160 EXPECT_FALSE(client()->failed()); | |
| 161 } | |
| 162 | |
| 163 TEST_F(ManifestLoaderTest, CORSDefaultValidURL) | |
| 164 { | |
| 165 setupAndLoad("cors-default.html", "http://foo.com/index.html", ManifestRespo nseSuccess); | |
| 166 | |
| 167 EXPECT_TRUE(client()->succeeded()); | |
| 168 EXPECT_FALSE(client()->failed()); | |
| 169 } | |
| 170 | |
| 171 TEST_F(ManifestLoaderTest, CORSUseCredentialsValidURL) | |
| 172 { | |
| 173 setupAndLoad("cors-use-credentials.html", "http://foo.com/index.html", Manif estResponseSuccess); | |
| 174 | |
| 175 EXPECT_TRUE(client()->succeeded()); | |
| 176 EXPECT_FALSE(client()->failed()); | |
| 177 } | |
| 178 | |
| 179 TEST_F(ManifestLoaderTest, CORSAnonymousCrossOriginURL) | |
| 180 { | |
| 181 setupAndLoad("cors-anonymous.html", "http://bar.com/index.html", ManifestRes ponseSuccess); | |
| 182 | |
| 183 EXPECT_FALSE(client()->succeeded()); | |
| 184 EXPECT_TRUE(client()->failed()); | |
| 185 EXPECT_EQ(WebManifestError::FetchError, client()->error()); | |
| 186 } | |
| 187 | |
| 188 TEST_F(ManifestLoaderTest, CORSDefaultCrossOriginURL) | |
| 189 { | |
| 190 setupAndLoad("cors-default.html", "http://bar.com/index.html", ManifestRespo nseSuccess); | |
| 191 | |
| 192 EXPECT_FALSE(client()->succeeded()); | |
| 193 EXPECT_TRUE(client()->failed()); | |
| 194 EXPECT_EQ(WebManifestError::FetchError, client()->error()); | |
| 195 } | |
| 196 | |
| 197 TEST_F(ManifestLoaderTest, CORSUseCredentialsCrossOriginURL) | |
| 198 { | |
| 199 setupAndLoad("cors-use-credentials.html", "http://bar.com/index.html", Manif estResponseSuccess); | |
| 200 | |
| 201 EXPECT_FALSE(client()->succeeded()); | |
| 202 EXPECT_TRUE(client()->failed()); | |
| 203 EXPECT_EQ(WebManifestError::FetchError, client()->error()); | |
| 204 } | |
| 205 | |
| 206 TEST_F(ManifestLoaderTest, NoCORS404URL) | |
| 207 { | |
| 208 setupAndLoad("no-cors.html", "http://foo.com/index.html", ManifestResponseEr ror); | |
| 209 | |
| 210 EXPECT_FALSE(client()->succeeded()); | |
| 211 EXPECT_TRUE(client()->failed()); | |
| 212 EXPECT_EQ(WebManifestError::FetchError, client()->error()); | |
| 213 } | |
| 214 | |
| 215 TEST_F(ManifestLoaderTest, CORS404URL) | |
| 216 { | |
| 217 setupAndLoad("cors-default.html", "http://foo.com/index.html", ManifestRespo nseError); | |
| 218 | |
| 219 EXPECT_FALSE(client()->succeeded()); | |
| 220 EXPECT_TRUE(client()->failed()); | |
| 221 EXPECT_EQ(WebManifestError::FetchError, client()->error()); | |
| 222 } | |
| 223 | |
| 224 TEST_F(ManifestLoaderTest, CORS404CrossOriginURL) | |
| 225 { | |
| 226 setupAndLoad("cors-default.html", "http://bar.com/index.html", ManifestRespo nseError); | |
| 227 | |
| 228 EXPECT_FALSE(client()->succeeded()); | |
| 229 EXPECT_TRUE(client()->failed()); | |
| 230 EXPECT_EQ(WebManifestError::FetchError, client()->error()); | |
| 231 } | |
| 232 | |
| 233 } // anonymous namespace | |
| OLD | NEW |