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 { | |
dcheng
2014/05/20 22:40:51
Unless an acronym is at the beginning of a variabl
mlamouri (slow - plz ping)
2014/05/21 09:51:12
Done.
| |
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 = new KURL(ParsedURLString, url); | |
68 } | |
69 | |
70 void setupSuccessResponse(); | |
71 void setup404Response(); | |
72 void setupLinkManifest(CorsType); | |
73 void resolveManifestLoading(); | |
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; | |
dcheng
2014/05/20 22:40:51
Why is this a pointer?
mlamouri (slow - plz ping)
2014/05/21 09:51:12
One test doesn't use m_url and I used to call unre
| |
82 }; | |
83 | |
84 void ManifestLoaderTest::SetUp() | |
85 { | |
86 m_url = 0; | |
87 m_dummyPageHolder = DummyPageHolder::create(); | |
88 KURL url(ParsedURLString, "http://foo.com/index.html"); | |
89 document()->setURL(url); | |
90 document()->updateSecurityOrigin(SecurityOrigin::create(url)); | |
91 } | |
92 | |
93 void ManifestLoaderTest::TearDown() | |
94 { | |
95 blink::Platform::current()->unitTestSupport()->unregisterAllMockedURLs(); | |
96 | |
97 if (m_url) { | |
98 delete m_url; | |
99 } | |
100 } | |
101 | |
102 void ManifestLoaderTest::setupSuccessResponse() | |
103 { | |
104 ASSERT(m_url); | |
105 | |
106 blink::WebURLResponse response; | |
107 response.initialize(); | |
108 response.setMIMEType("application/manifest+json"); | |
109 response.setURL(*m_url); | |
110 WTF::String localPath = String(blink::Platform::current()->unitTestSupport() ->webKitRootDir()) + "/Source/web/tests/data/manifest.json"; | |
111 blink::Platform::current()->unitTestSupport()->registerMockedURL(*m_url, res ponse, localPath); | |
112 } | |
113 | |
114 void ManifestLoaderTest::setup404Response() | |
115 { | |
116 ASSERT(m_url); | |
117 | |
118 blink::WebURLResponse response; | |
119 response.initialize(); | |
120 response.setMIMEType("application/manifest+json"); | |
121 response.setHTTPStatusCode(404); | |
122 response.setURL(*m_url); | |
123 | |
124 blink::WebURLError error; | |
125 error.reason = 404; // We don't really care but we need a value. | |
126 blink::Platform::current()->unitTestSupport()->registerMockedErrorURL(*m_url , response, error); | |
127 } | |
128 | |
129 void ManifestLoaderTest::setupLinkManifest(CorsType cors) | |
130 { | |
131 ASSERT(m_url); | |
132 | |
133 RefPtrWillBeRawPtr<HTMLLinkElement> link = HTMLLinkElement::create(*document (), false); | |
134 link->setAttribute(WebCore::HTMLNames::relAttr, "manifest"); | |
135 link->setAttribute(WebCore::HTMLNames::hrefAttr, AtomicString(m_url->string( ))); | |
136 switch (cors) { | |
137 case CorsEmptyString: | |
138 link->setAttribute(WebCore::HTMLNames::crossoriginAttr, ""); | |
139 break; | |
140 case CorsAnonymous: | |
141 link->setAttribute(WebCore::HTMLNames::crossoriginAttr, "anonymous"); | |
142 break; | |
143 case CorsUseCredentials: | |
144 link->setAttribute(WebCore::HTMLNames::crossoriginAttr, "use-credentials "); | |
145 break; | |
146 case NoCors: | |
147 // do nothing. | |
148 break; | |
149 } | |
150 document()->head()->appendChild(link); | |
151 } | |
152 | |
153 void ManifestLoaderTest::resolveManifestLoading() | |
154 { | |
dcheng
2014/05/20 22:40:51
Please move ManifestLoader::LoadManifest(request()
mlamouri (slow - plz ping)
2014/05/21 09:51:12
Done.
| |
155 // This call is wrapped in a helper call on purpose because we want to get | |
156 // rid of it. It will make the transition to the new model easier. | |
157 blink::Platform::current()->unitTestSupport()->serveAsynchronousMockedReques ts(); | |
158 } | |
159 | |
160 TEST_F(ManifestLoaderTest, NoLinkManifest) | |
161 { | |
162 ManifestLoader::LoadManifest(request(), document()); | |
163 | |
164 EXPECT_FALSE(request()->succeeded()); | |
165 EXPECT_TRUE(request()->failed()); | |
166 EXPECT_EQ(blink::WebManifestRequest::NoLinkManifest, request()->reason()); | |
167 } | |
168 | |
169 TEST_F(ManifestLoaderTest, NoCorsValidURL) | |
170 { | |
171 setURL("http://foo.com/manifest.json"); | |
172 setupSuccessResponse(); | |
173 setupLinkManifest(NoCors); | |
174 | |
175 ManifestLoader::LoadManifest(request(), document()); | |
176 resolveManifestLoading(); | |
177 | |
178 EXPECT_TRUE(request()->succeeded()); | |
179 EXPECT_FALSE(request()->failed()); | |
180 } | |
181 | |
182 TEST_F(ManifestLoaderTest, NoCorsCrossOriginURL) | |
183 { | |
184 setURL("http://example.com/manifest.json"); | |
185 setupSuccessResponse(); | |
186 setupLinkManifest(NoCors); | |
187 | |
188 ManifestLoader::LoadManifest(request(), document()); | |
189 resolveManifestLoading(); | |
190 | |
191 EXPECT_TRUE(request()->succeeded()); | |
192 EXPECT_FALSE(request()->failed()); | |
193 } | |
194 | |
195 TEST_F(ManifestLoaderTest, CorsAnonymousValidURL) | |
196 { | |
197 setURL("http://foo.com/manifest.json"); | |
198 setupSuccessResponse(); | |
199 setupLinkManifest(CorsAnonymous); | |
200 | |
201 ManifestLoader::LoadManifest(request(), document()); | |
202 resolveManifestLoading(); | |
203 | |
204 EXPECT_TRUE(request()->succeeded()); | |
205 EXPECT_FALSE(request()->failed()); | |
206 } | |
207 | |
208 TEST_F(ManifestLoaderTest, CorsDefaultValidURL) | |
209 { | |
210 setURL("http://foo.com/manifest.json"); | |
211 setupSuccessResponse(); | |
212 setupLinkManifest(CorsEmptyString); | |
213 | |
214 ManifestLoader::LoadManifest(request(), document()); | |
215 resolveManifestLoading(); | |
216 | |
217 EXPECT_TRUE(request()->succeeded()); | |
218 EXPECT_FALSE(request()->failed()); | |
219 } | |
220 | |
221 TEST_F(ManifestLoaderTest, CorsUseCredentialsValidURL) | |
222 { | |
223 setURL("http://foo.com/manifest.json"); | |
224 setupSuccessResponse(); | |
225 setupLinkManifest(CorsUseCredentials); | |
226 | |
227 ManifestLoader::LoadManifest(request(), document()); | |
228 resolveManifestLoading(); | |
229 | |
230 EXPECT_TRUE(request()->succeeded()); | |
231 EXPECT_FALSE(request()->failed()); | |
232 } | |
233 | |
234 TEST_F(ManifestLoaderTest, CorsAnonymousCrossOriginURL) | |
235 { | |
236 setURL("http://example.com/manifest.json"); | |
237 setupSuccessResponse(); | |
238 setupLinkManifest(CorsAnonymous); | |
239 | |
240 ManifestLoader::LoadManifest(request(), document()); | |
241 resolveManifestLoading(); | |
242 | |
243 EXPECT_FALSE(request()->succeeded()); | |
244 EXPECT_TRUE(request()->failed()); | |
245 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); | |
246 } | |
247 | |
248 TEST_F(ManifestLoaderTest, CorsDefaultCrossOriginURL) | |
249 { | |
250 setURL("http://example.com/manifest.json"); | |
251 setupSuccessResponse(); | |
252 setupLinkManifest(CorsEmptyString); | |
253 | |
254 ManifestLoader::LoadManifest(request(), document()); | |
255 resolveManifestLoading(); | |
256 | |
257 EXPECT_FALSE(request()->succeeded()); | |
258 EXPECT_TRUE(request()->failed()); | |
259 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); | |
260 } | |
261 | |
262 TEST_F(ManifestLoaderTest, CorsUseCredentialsCrossOriginURL) | |
263 { | |
264 setURL("http://example.com/manifest.json"); | |
265 setupSuccessResponse(); | |
266 setupLinkManifest(CorsUseCredentials); | |
267 | |
268 ManifestLoader::LoadManifest(request(), document()); | |
269 resolveManifestLoading(); | |
270 | |
271 EXPECT_FALSE(request()->succeeded()); | |
272 EXPECT_TRUE(request()->failed()); | |
273 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); | |
274 } | |
275 | |
276 TEST_F(ManifestLoaderTest, NoCors404URL) | |
277 { | |
278 setURL("http://foo.com/manifest.json"); | |
279 setup404Response(); | |
280 setupLinkManifest(NoCors); | |
281 | |
282 ManifestLoader::LoadManifest(request(), document()); | |
283 resolveManifestLoading(); | |
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, Cors404URL) | |
291 { | |
292 setURL("http://foo.com/manifest.json"); | |
293 setup404Response(); | |
294 setupLinkManifest(CorsEmptyString); | |
295 | |
296 ManifestLoader::LoadManifest(request(), document()); | |
297 resolveManifestLoading(); | |
298 | |
299 EXPECT_FALSE(request()->succeeded()); | |
300 EXPECT_TRUE(request()->failed()); | |
301 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); | |
302 } | |
303 | |
304 TEST_F(ManifestLoaderTest, Cors404CrossOriginURL) | |
305 { | |
306 setURL("http://example.com/manifest.json"); | |
307 setup404Response(); | |
308 setupLinkManifest(CorsEmptyString); | |
309 | |
310 ManifestLoader::LoadManifest(request(), document()); | |
311 resolveManifestLoading(); | |
312 | |
313 EXPECT_FALSE(request()->succeeded()); | |
314 EXPECT_TRUE(request()->failed()); | |
315 EXPECT_EQ(blink::WebManifestRequest::NetworkError, request()->reason()); | |
316 } | |
317 | |
318 } // anonymous namespace | |
OLD | NEW |