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