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/html/HTMLHeadElement.h" |
| 9 #include "core/html/HTMLLinkElement.h" |
| 10 #include "core/testing/DummyPageHolder.h" |
| 11 #include "public/platform/Platform.h" |
| 12 #include "public/platform/WebManifestRequest.h" |
| 13 #include "public/platform/WebURL.h" |
| 14 #include "public/platform/WebURLResponse.h" |
| 15 #include "public/platform/WebUnitTestSupport.h" |
| 16 #include <gtest/gtest.h> |
| 17 |
| 18 using namespace WebCore; |
| 19 |
| 20 namespace { |
| 21 |
| 22 class ManifestRequest : public blink::WebManifestRequest { |
| 23 public: |
| 24 ManifestRequest() |
| 25 : m_succeeded(false) |
| 26 , m_failed(false) |
| 27 { |
| 28 } |
| 29 |
| 30 virtual void requestSucceeded(const blink::WebManifest&) OVERRIDE |
| 31 { |
| 32 ASSERT(!m_failed && !m_succeeded); |
| 33 m_succeeded = true; |
| 34 } |
| 35 |
| 36 virtual void requestFailed(FailureReason reason) OVERRIDE |
| 37 { |
| 38 ASSERT(!m_failed && !m_succeeded); |
| 39 m_failed = true; |
| 40 m_reason = reason; |
| 41 } |
| 42 |
| 43 bool succeeded() const { return m_succeeded; } |
| 44 bool failed() const { return m_failed; } |
| 45 FailureReason reason() const { return m_reason; } |
| 46 |
| 47 private: |
| 48 bool m_succeeded; |
| 49 bool m_failed; |
| 50 FailureReason m_reason; |
| 51 }; |
| 52 |
| 53 class ManifestLoaderTest : public ::testing::Test { |
| 54 protected: |
| 55 enum CORSType { |
| 56 NoCORS, |
| 57 CORSEmptyString, |
| 58 CORSAnonymous, |
| 59 CORSUseCredentials |
| 60 }; |
| 61 |
| 62 virtual void SetUp() OVERRIDE; |
| 63 virtual void TearDown() OVERRIDE; |
| 64 |
| 65 void setURL(const String& url) |
| 66 { |
| 67 m_url = KURL(ParsedURLString, url); |
| 68 } |
| 69 |
| 70 void setupSuccessResponse(); |
| 71 void setup404Response(); |
| 72 void setupLinkManifest(CORSType); |
| 73 void loadManifestSynchronously(); |
| 74 |
| 75 Document* document() { return &m_dummyPageHolder->document(); } |
| 76 ManifestRequest* request() { return &m_request; } |
| 77 |
| 78 private: |
| 79 OwnPtr<DummyPageHolder> m_dummyPageHolder; |
| 80 ManifestRequest m_request; |
| 81 KURL m_url; |
| 82 }; |
| 83 |
| 84 void ManifestLoaderTest::SetUp() |
| 85 { |
| 86 m_dummyPageHolder = DummyPageHolder::create(); |
| 87 KURL url(ParsedURLString, "http://foo.com/index.html"); |
| 88 document()->setURL(url); |
| 89 document()->updateSecurityOrigin(SecurityOrigin::create(url)); |
| 90 } |
| 91 |
| 92 void ManifestLoaderTest::TearDown() |
| 93 { |
| 94 blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); |
| 95 } |
| 96 |
| 97 void ManifestLoaderTest::setupSuccessResponse() |
| 98 { |
| 99 ASSERT(!m_url.isNull()); |
| 100 |
| 101 blink::WebURLResponse response; |
| 102 response.initialize(); |
| 103 response.setMIMEType("application/manifest+json"); |
| 104 response.setURL(m_url); |
| 105 WTF::String localPath = String(blink::Platform::current()->unitTestSupport()
->webKitRootDir()) + "/Source/web/tests/data/manifest.json"; |
| 106 blink::Platform::current()->unitTestSupport()->registerMockedURL(m_url, resp
onse, localPath); |
| 107 } |
| 108 |
| 109 void ManifestLoaderTest::setup404Response() |
| 110 { |
| 111 ASSERT(!m_url.isNull()); |
| 112 |
| 113 blink::WebURLResponse response; |
| 114 response.initialize(); |
| 115 response.setMIMEType("application/manifest+json"); |
| 116 response.setHTTPStatusCode(404); |
| 117 response.setURL(m_url); |
| 118 |
| 119 blink::WebURLError error; |
| 120 error.reason = 404; // We don't really care but we need a value. |
| 121 blink::Platform::current()->unitTestSupport()->registerMockedErrorURL(m_url,
response, error); |
| 122 } |
| 123 |
| 124 void ManifestLoaderTest::setupLinkManifest(CORSType cors) |
| 125 { |
| 126 ASSERT(!m_url.isNull()); |
| 127 |
| 128 RefPtrWillBeRawPtr<HTMLLinkElement> link = HTMLLinkElement::create(*document
(), false); |
| 129 link->setAttribute(WebCore::HTMLNames::relAttr, "manifest"); |
| 130 link->setAttribute(WebCore::HTMLNames::hrefAttr, AtomicString(m_url.string()
)); |
| 131 switch (cors) { |
| 132 case CORSEmptyString: |
| 133 link->setAttribute(WebCore::HTMLNames::crossoriginAttr, ""); |
| 134 break; |
| 135 case CORSAnonymous: |
| 136 link->setAttribute(WebCore::HTMLNames::crossoriginAttr, "anonymous"); |
| 137 break; |
| 138 case CORSUseCredentials: |
| 139 link->setAttribute(WebCore::HTMLNames::crossoriginAttr, "use-credentials
"); |
| 140 break; |
| 141 case NoCORS: |
| 142 // do nothing. |
| 143 break; |
| 144 } |
| 145 document()->head()->appendChild(link); |
| 146 } |
| 147 |
| 148 void ManifestLoaderTest::loadManifestSynchronously() |
| 149 { |
| 150 // This call is wrapped in a helper call on purpose because we want to get |
| 151 // rid of it. It will make the transition to the new model easier. |
| 152 ManifestLoader::loadManifest(request(), document()); |
| 153 blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedReques
ts(); |
| 154 } |
| 155 |
| 156 TEST_F(ManifestLoaderTest, NoLinkManifest) |
| 157 { |
| 158 ManifestLoader::loadManifest(request(), document()); |
| 159 |
| 160 EXPECT_FALSE(request()->succeeded()); |
| 161 EXPECT_TRUE(request()->failed()); |
| 162 EXPECT_EQ(blink::WebManifestRequest::NoLinkManifest, request()->reason()); |
| 163 } |
| 164 |
| 165 TEST_F(ManifestLoaderTest, NoCORSValidURL) |
| 166 { |
| 167 setURL("http://foo.com/manifest.json"); |
| 168 setupSuccessResponse(); |
| 169 setupLinkManifest(NoCORS); |
| 170 |
| 171 loadManifestSynchronously(); |
| 172 |
| 173 EXPECT_TRUE(request()->succeeded()); |
| 174 EXPECT_FALSE(request()->failed()); |
| 175 } |
| 176 |
| 177 TEST_F(ManifestLoaderTest, NoCORSCrossOriginURL) |
| 178 { |
| 179 setURL("http://example.com/manifest.json"); |
| 180 setupSuccessResponse(); |
| 181 setupLinkManifest(NoCORS); |
| 182 |
| 183 loadManifestSynchronously(); |
| 184 |
| 185 EXPECT_TRUE(request()->succeeded()); |
| 186 EXPECT_FALSE(request()->failed()); |
| 187 } |
| 188 |
| 189 TEST_F(ManifestLoaderTest, CORSAnonymousValidURL) |
| 190 { |
| 191 setURL("http://foo.com/manifest.json"); |
| 192 setupSuccessResponse(); |
| 193 setupLinkManifest(CORSAnonymous); |
| 194 |
| 195 loadManifestSynchronously(); |
| 196 |
| 197 EXPECT_TRUE(request()->succeeded()); |
| 198 EXPECT_FALSE(request()->failed()); |
| 199 } |
| 200 |
| 201 TEST_F(ManifestLoaderTest, CORSDefaultValidURL) |
| 202 { |
| 203 setURL("http://foo.com/manifest.json"); |
| 204 setupSuccessResponse(); |
| 205 setupLinkManifest(CORSEmptyString); |
| 206 |
| 207 loadManifestSynchronously(); |
| 208 |
| 209 EXPECT_TRUE(request()->succeeded()); |
| 210 EXPECT_FALSE(request()->failed()); |
| 211 } |
| 212 |
| 213 TEST_F(ManifestLoaderTest, CORSUseCredentialsValidURL) |
| 214 { |
| 215 setURL("http://foo.com/manifest.json"); |
| 216 setupSuccessResponse(); |
| 217 setupLinkManifest(CORSUseCredentials); |
| 218 |
| 219 loadManifestSynchronously(); |
| 220 |
| 221 EXPECT_TRUE(request()->succeeded()); |
| 222 EXPECT_FALSE(request()->failed()); |
| 223 } |
| 224 |
| 225 TEST_F(ManifestLoaderTest, CORSAnonymousCrossOriginURL) |
| 226 { |
| 227 setURL("http://example.com/manifest.json"); |
| 228 setupSuccessResponse(); |
| 229 setupLinkManifest(CORSAnonymous); |
| 230 |
| 231 loadManifestSynchronously(); |
| 232 |
| 233 EXPECT_FALSE(request()->succeeded()); |
| 234 EXPECT_TRUE(request()->failed()); |
| 235 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| 236 } |
| 237 |
| 238 TEST_F(ManifestLoaderTest, CORSDefaultCrossOriginURL) |
| 239 { |
| 240 setURL("http://example.com/manifest.json"); |
| 241 setupSuccessResponse(); |
| 242 setupLinkManifest(CORSEmptyString); |
| 243 |
| 244 loadManifestSynchronously(); |
| 245 |
| 246 EXPECT_FALSE(request()->succeeded()); |
| 247 EXPECT_TRUE(request()->failed()); |
| 248 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| 249 } |
| 250 |
| 251 TEST_F(ManifestLoaderTest, CORSUseCredentialsCrossOriginURL) |
| 252 { |
| 253 setURL("http://example.com/manifest.json"); |
| 254 setupSuccessResponse(); |
| 255 setupLinkManifest(CORSUseCredentials); |
| 256 |
| 257 loadManifestSynchronously(); |
| 258 |
| 259 EXPECT_FALSE(request()->succeeded()); |
| 260 EXPECT_TRUE(request()->failed()); |
| 261 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| 262 } |
| 263 |
| 264 TEST_F(ManifestLoaderTest, NoCORS404URL) |
| 265 { |
| 266 setURL("http://foo.com/manifest.json"); |
| 267 setup404Response(); |
| 268 setupLinkManifest(NoCORS); |
| 269 |
| 270 loadManifestSynchronously(); |
| 271 |
| 272 EXPECT_FALSE(request()->succeeded()); |
| 273 EXPECT_TRUE(request()->failed()); |
| 274 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| 275 } |
| 276 |
| 277 TEST_F(ManifestLoaderTest, CORS404URL) |
| 278 { |
| 279 setURL("http://foo.com/manifest.json"); |
| 280 setup404Response(); |
| 281 setupLinkManifest(CORSEmptyString); |
| 282 |
| 283 loadManifestSynchronously(); |
| 284 |
| 285 EXPECT_FALSE(request()->succeeded()); |
| 286 EXPECT_TRUE(request()->failed()); |
| 287 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| 288 } |
| 289 |
| 290 TEST_F(ManifestLoaderTest, CORS404CrossOriginURL) |
| 291 { |
| 292 setURL("http://example.com/manifest.json"); |
| 293 setup404Response(); |
| 294 setupLinkManifest(CORSEmptyString); |
| 295 |
| 296 loadManifestSynchronously(); |
| 297 |
| 298 EXPECT_FALSE(request()->succeeded()); |
| 299 EXPECT_TRUE(request()->failed()); |
| 300 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); |
| 301 } |
| 302 |
| 303 } // anonymous namespace |
OLD | NEW |