Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: content/renderer/manifest/manifest_parser_unittest.cc

Issue 2063003003: Implement "scope" Web Manifest parsing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'webapk_manifest_scope00' into webapk_manifest_scope0 Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/renderer/manifest/manifest_parser.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/renderer/manifest/manifest_parser.h" 5 #include "content/renderer/manifest/manifest_parser.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 // A parsing error is equivalent to an empty manifest. 94 // A parsing error is equivalent to an empty manifest.
95 ASSERT_TRUE(manifest.IsEmpty()); 95 ASSERT_TRUE(manifest.IsEmpty());
96 ASSERT_TRUE(manifest.name.is_null()); 96 ASSERT_TRUE(manifest.name.is_null());
97 ASSERT_TRUE(manifest.short_name.is_null()); 97 ASSERT_TRUE(manifest.short_name.is_null());
98 ASSERT_TRUE(manifest.start_url.is_empty()); 98 ASSERT_TRUE(manifest.start_url.is_empty());
99 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined); 99 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
100 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); 100 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
101 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 101 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
102 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 102 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
103 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); 103 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
104 ASSERT_TRUE(manifest.scope.is_empty());
104 } 105 }
105 106
106 TEST_F(ManifestParserTest, ValidNoContentParses) { 107 TEST_F(ManifestParserTest, ValidNoContentParses) {
107 Manifest manifest = ParseManifest("{}"); 108 Manifest manifest = ParseManifest("{}");
108 109
109 // Empty Manifest is not a parsing error. 110 // Empty Manifest is not a parsing error.
110 EXPECT_EQ(0u, GetErrorCount()); 111 EXPECT_EQ(0u, GetErrorCount());
111 112
112 // Check that all the fields are null in that case. 113 // Check that all the fields are null in that case.
113 ASSERT_TRUE(manifest.IsEmpty()); 114 ASSERT_TRUE(manifest.IsEmpty());
114 ASSERT_TRUE(manifest.name.is_null()); 115 ASSERT_TRUE(manifest.name.is_null());
115 ASSERT_TRUE(manifest.short_name.is_null()); 116 ASSERT_TRUE(manifest.short_name.is_null());
116 ASSERT_TRUE(manifest.start_url.is_empty()); 117 ASSERT_TRUE(manifest.start_url.is_empty());
117 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined); 118 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
118 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); 119 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
119 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 120 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
120 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 121 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
121 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); 122 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
123 ASSERT_TRUE(manifest.scope.is_empty());
122 } 124 }
123 125
124 TEST_F(ManifestParserTest, MultipleErrorsReporting) { 126 TEST_F(ManifestParserTest, MultipleErrorsReporting) {
125 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4," 127 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4,"
126 "\"orientation\": {}, \"display\": \"foo\"," 128 "\"orientation\": {}, \"display\": \"foo\","
127 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42," 129 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42,"
128 "\"background_color\": 42 }"); 130 "\"background_color\": 42 }");
129 131
130 EXPECT_EQ(8u, GetErrorCount()); 132 EXPECT_EQ(8u, GetErrorCount());
131 133
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 248
247 // Don't parse if property isn't a string. 249 // Don't parse if property isn't a string.
248 { 250 {
249 Manifest manifest = ParseManifest("{ \"start_url\": 42 }"); 251 Manifest manifest = ParseManifest("{ \"start_url\": 42 }");
250 ASSERT_TRUE(manifest.start_url.is_empty()); 252 ASSERT_TRUE(manifest.start_url.is_empty());
251 EXPECT_EQ(1u, GetErrorCount()); 253 EXPECT_EQ(1u, GetErrorCount());
252 EXPECT_EQ("property 'start_url' ignored, type string expected.", 254 EXPECT_EQ("property 'start_url' ignored, type string expected.",
253 errors()[0]); 255 errors()[0]);
254 } 256 }
255 257
258 // Don't parse if property isn't a valid URL.
259 {
260 Manifest manifest = ParseManifest("{ \"start_url\": \"http://www.google.ca:a \" }");
261 ASSERT_TRUE(manifest.start_url.is_empty());
262 EXPECT_EQ(1u, GetErrorCount());
263 EXPECT_EQ("property 'start_url' ignored, URL is invalid.", errors()[0]);
264 }
265
256 // Absolute start_url, same origin with document. 266 // Absolute start_url, same origin with document.
257 { 267 {
258 Manifest manifest = 268 Manifest manifest =
259 ParseManifestWithURLs("{ \"start_url\": \"http://foo.com/land.html\" }", 269 ParseManifestWithURLs("{ \"start_url\": \"http://foo.com/land.html\" }",
260 GURL("http://foo.com/manifest.json"), 270 GURL("http://foo.com/manifest.json"),
261 GURL("http://foo.com/index.html")); 271 GURL("http://foo.com/index.html"));
262 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/land.html"); 272 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/land.html");
263 EXPECT_EQ(0u, GetErrorCount()); 273 EXPECT_EQ(0u, GetErrorCount());
264 } 274 }
265 275
(...skipping 14 matching lines...) Expand all
280 { 290 {
281 Manifest manifest = 291 Manifest manifest =
282 ParseManifestWithURLs("{ \"start_url\": \"land.html\" }", 292 ParseManifestWithURLs("{ \"start_url\": \"land.html\" }",
283 GURL("http://foo.com/landing/manifest.json"), 293 GURL("http://foo.com/landing/manifest.json"),
284 GURL("http://foo.com/index.html")); 294 GURL("http://foo.com/index.html"));
285 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html"); 295 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html");
286 EXPECT_EQ(0u, GetErrorCount()); 296 EXPECT_EQ(0u, GetErrorCount());
287 } 297 }
288 } 298 }
289 299
300 TEST_F(ManifestParserTest, ScopeParseRules) {
301 // Smoke test.
302 {
303 Manifest manifest = ParseManifest(
304 "{ \"scope\": \"land\", \"start_url\": \"land/landing.html\" }");
305 ASSERT_EQ(manifest.scope.spec(),
306 default_document_url.Resolve("land").spec());
307 ASSERT_FALSE(manifest.IsEmpty());
308 EXPECT_EQ(0u, GetErrorCount());
309 }
310
311 // Whitespaces.
312 {
313 Manifest manifest = ParseManifest(
314 "{ \"scope\": \" land \", \"start_url\": \"land/landing.html\" }");
315 ASSERT_EQ(manifest.scope.spec(),
316 default_document_url.Resolve("land").spec());
317 EXPECT_EQ(0u, GetErrorCount());
318 }
319
320 // Don't parse if property isn't a string.
321 {
322 Manifest manifest = ParseManifest("{ \"scope\": {} }");
323 ASSERT_TRUE(manifest.scope.is_empty());
324 EXPECT_EQ(1u, GetErrorCount());
325 EXPECT_EQ("property 'scope' ignored, type string expected.", errors()[0]);
326 }
327
328 // Don't parse if property isn't a string.
329 {
330 Manifest manifest = ParseManifest("{ \"scope\": 42 }");
331 ASSERT_TRUE(manifest.scope.is_empty());
332 EXPECT_EQ(1u, GetErrorCount());
333 EXPECT_EQ("property 'scope' ignored, type string expected.", errors()[0]);
334 }
335
336 // Absolute scope, start URL is in scope.
337 {
338 Manifest manifest = ParseManifestWithURLs(
339 "{ \"scope\": \"http://foo.com/land\", "
340 "\"start_url\": \"http://foo.com/land/landing.html\" }",
341 GURL("http://foo.com/manifest.json"),
342 GURL("http://foo.com/index.html"));
343 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/land");
344 EXPECT_EQ(0u, GetErrorCount());
345 }
346
347 // Absolute scope, start URL is not in scope.
348 {
349 Manifest manifest =
350 ParseManifestWithURLs("{ \"scope\": \"http://foo.com/land\", "
351 "\"start_url\": \"http://foo.com/index.html\" }",
352 GURL("http://foo.com/manifest.json"),
353 GURL("http://foo.com/index.html"));
354 ASSERT_TRUE(manifest.scope.is_empty());
355 EXPECT_EQ(1u, GetErrorCount());
356 EXPECT_EQ("property 'scope' ignored. Start url should be within scope "
357 "of scope URL.",
358 errors()[0]);
359 }
360
361 // Absolute scope, start URL has different origin than scope URL.
362 {
363 Manifest manifest =
364 ParseManifestWithURLs("{ \"scope\": \"http://foo.com/land\", "
365 "\"start_url\": \"http://bar.com/land/landing.html \" }",
366 GURL("http://foo.com/manifest.json"),
367 GURL("http://foo.com/index.html"));
368 ASSERT_TRUE(manifest.scope.is_empty());
369 ASSERT_EQ(2u, GetErrorCount());
370 EXPECT_EQ(
371 "property 'start_url' ignored, should be same origin as document.",
372 errors()[0]);
373 EXPECT_EQ("property 'scope' ignored. Start url should be within scope "
374 "of scope URL.",
375 errors()[1]);
376 }
377
378 // scope and start URL have diferent origin than document URL.
379 {
380 Manifest manifest =
381 ParseManifestWithURLs("{ \"scope\": \"http://foo.com/land\", "
382 "\"start_url\": \"http://foo.com/land/landing.html \" }",
383 GURL("http://foo.com/manifest.json"),
384 GURL("http://bar.com/index.html"));
385 ASSERT_TRUE(manifest.scope.is_empty());
386 ASSERT_EQ(2u, GetErrorCount());
387 EXPECT_EQ(
388 "property 'start_url' ignored, should be same origin as document.",
389 errors()[0]);
390 EXPECT_EQ("property 'scope' ignored, should be same origin as document.",
391 errors()[1]);
392 }
393
394 // No start URL. Document URL is in scope.
395 {
396 Manifest manifest = ParseManifestWithURLs("{ \"scope\": \"http://foo.com/lan d\" }",
397 GURL("http://foo.com/manifest.json "),
398 GURL("http://foo.com/land/index.ht ml"));
399 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/land");
400 ASSERT_EQ(0u, GetErrorCount());
401 }
402
403 // No start URL. Document is out of scope.
404 {
405 Manifest manifest = ParseManifestWithURLs("{ \"scope\": \"http://foo.com/lan d\" }",
406 GURL("http://foo.com/manifest.json "),
407 GURL("http://foo.com/index.html")) ;
408 ASSERT_EQ(1u, GetErrorCount());
409 EXPECT_EQ("property 'scope' ignored. Start url should be within scope "
410 "of scope URL.",
411 errors()[0]);
412 }
413
414 // Resolving has to happen based on the manifest_url.
415 {
416 Manifest manifest = ParseManifestWithURLs(
417 "{ \"scope\": \"treasure\" }",
418 GURL("http://foo.com/map/manifest.json"),
419 GURL("http://foo.com/map/treasure/island/index.html"));
420 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/map/treasure");
421 EXPECT_EQ(0u, GetErrorCount());
422 }
423
424 // Scope is parent directory.
425 {
426 Manifest manifest =
427 ParseManifestWithURLs("{ \"scope\": \"..\" }",
428 GURL("http://foo.com/map/manifest.json"),
429 GURL("http://foo.com/index.html"));
430 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/");
431 EXPECT_EQ(0u, GetErrorCount());
432 }
433
434 // Scope tries to go up past domain.
435 {
436 Manifest manifest =
437 ParseManifestWithURLs("{ \"scope\": \"../..\" }",
438 GURL("http://foo.com/map/manifest.json"),
439 GURL("http://foo.com/index.html"));
440 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/");
441 EXPECT_EQ(0u, GetErrorCount());
442 }
443 }
444
290 TEST_F(ManifestParserTest, DisplayParserRules) { 445 TEST_F(ManifestParserTest, DisplayParserRules) {
291 // Smoke test. 446 // Smoke test.
292 { 447 {
293 Manifest manifest = ParseManifest("{ \"display\": \"browser\" }"); 448 Manifest manifest = ParseManifest("{ \"display\": \"browser\" }");
294 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser); 449 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser);
295 EXPECT_FALSE(manifest.IsEmpty()); 450 EXPECT_FALSE(manifest.IsEmpty());
296 EXPECT_EQ(0u, GetErrorCount()); 451 EXPECT_EQ(0u, GetErrorCount());
297 } 452 }
298 453
299 // Trim whitespaces. 454 // Trim whitespaces.
(...skipping 959 matching lines...) Expand 10 before | Expand all | Expand 10 after
1259 { 1414 {
1260 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); 1415 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }");
1261 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); 1416 EXPECT_TRUE(manifest.gcm_sender_id.is_null());
1262 EXPECT_EQ(1u, GetErrorCount()); 1417 EXPECT_EQ(1u, GetErrorCount());
1263 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.", 1418 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.",
1264 errors()[0]); 1419 errors()[0]);
1265 } 1420 }
1266 } 1421 }
1267 1422
1268 } // namespace content 1423 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/manifest/manifest_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698