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

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
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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 // This Manifest is not a valid JSON object, it's a parsing error. 89 // This Manifest is not a valid JSON object, it's a parsing error.
90 EXPECT_EQ(1u, GetErrorCount()); 90 EXPECT_EQ(1u, GetErrorCount());
91 EXPECT_EQ("Line: 1, column: 1, Unexpected token.", 91 EXPECT_EQ("Line: 1, column: 1, Unexpected token.",
92 errors()[0]); 92 errors()[0]);
93 93
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_TRUE(manifest.scope.is_empty());
99 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined); 100 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
100 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); 101 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
101 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 102 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
102 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 103 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
103 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); 104 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
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());
118 ASSERT_TRUE(manifest.scope.is_empty());
117 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined); 119 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
118 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); 120 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
119 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor); 121 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
120 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor); 122 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
121 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); 123 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
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\","
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 { 282 {
281 Manifest manifest = 283 Manifest manifest =
282 ParseManifestWithURLs("{ \"start_url\": \"land.html\" }", 284 ParseManifestWithURLs("{ \"start_url\": \"land.html\" }",
283 GURL("http://foo.com/landing/manifest.json"), 285 GURL("http://foo.com/landing/manifest.json"),
284 GURL("http://foo.com/index.html")); 286 GURL("http://foo.com/index.html"));
285 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html"); 287 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html");
286 EXPECT_EQ(0u, GetErrorCount()); 288 EXPECT_EQ(0u, GetErrorCount());
287 } 289 }
288 } 290 }
289 291
292 TEST_F(ManifestParserTest, ScopeParseRules) {
293 // Smoke test.
294 {
295 Manifest manifest = ParseManifest(
296 "{ \"scope\": \"land\", \"start_url\": \"land/landing.html\" }");
297 ASSERT_EQ(manifest.scope.spec(),
298 default_document_url.Resolve("land").spec());
299 ASSERT_FALSE(manifest.IsEmpty());
300 EXPECT_EQ(0u, GetErrorCount());
301 }
302
303 // Whitespaces.
304 {
305 Manifest manifest = ParseManifest(
306 "{ \"scope\": \" land \", \"start_url\": \"land/landing.html\" }");
307 ASSERT_EQ(manifest.scope.spec(),
308 default_document_url.Resolve("land").spec());
309 EXPECT_EQ(0u, GetErrorCount());
310 }
311
312 // Don't parse if property isn't a string.
313 {
314 Manifest manifest = ParseManifest("{ \"scope\": {} }");
315 ASSERT_TRUE(manifest.scope.is_empty());
316 EXPECT_EQ(1u, GetErrorCount());
317 EXPECT_EQ("property 'scope' ignored, type string expected.", errors()[0]);
318 }
319
320 // Don't parse if property isn't a string.
321 {
322 Manifest manifest = ParseManifest("{ \"scope\": 42 }");
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 // Absolute scope, start URL is in scope.
329 {
330 Manifest manifest = ParseManifestWithURLs(
331 "{ \"scope\": \"http://foo.com/land\", "
332 "\"start_url\": \"http://foo.com/land/landing.html\" }",
333 GURL("http://foo.com/manifest.json"),
334 GURL("http://foo.com/index.html"));
335 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/land");
336 EXPECT_EQ(0u, GetErrorCount());
337 }
338
339 // Absolute scope, start URL is not in scope.
340 {
341 Manifest manifest =
342 ParseManifestWithURLs("{ \"scope\": \"http://foo.com/land\", "
343 "\"start_url\": \"http://foo.com/index.html\" }",
344 GURL("http://foo.com/manifest.json"),
345 GURL("http://foo.com/index.html"));
346 ASSERT_TRUE(manifest.scope.is_empty());
347 EXPECT_EQ(1u, GetErrorCount());
348 EXPECT_EQ("property 'scope' ignored. Start url should be within scope "
349 "of scope URL.",
350 errors()[0]);
351 }
352
353 // Absolute scope, start URL has different origin than scope URL.
354 {
355 Manifest manifest =
356 ParseManifestWithURLs("{ \"scope\": \"http://foo.com/land\", "
357 "\"start_url\": \"http://bar.com/land/landing.html \" }",
358 GURL("http://foo.com/manifest.json"),
359 GURL("http://foo.com/index.html"));
360 ASSERT_TRUE(manifest.scope.is_empty());
361 ASSERT_EQ(2u, GetErrorCount());
362 EXPECT_EQ(
363 "property 'start_url' ignored, should be same origin as document.",
364 errors()[0]);
365 EXPECT_EQ("property 'scope' ignored. Start url should be within scope "
366 "of scope URL.",
367 errors()[1]);
368 }
369
370 // scope and start URL have diferent origin than document URL.
371 {
372 Manifest manifest =
373 ParseManifestWithURLs("{ \"scope\": \"http://foo.com/land\", "
374 "\"start_url\": \"http://foo.com/land/landing.html \" }",
375 GURL("http://foo.com/manifest.json"),
376 GURL("http://bar.com/index.html"));
377 ASSERT_TRUE(manifest.scope.is_empty());
378 ASSERT_EQ(2u, GetErrorCount());
379 EXPECT_EQ(
380 "property 'start_url' ignored, should be same origin as document.",
381 errors()[0]);
382 EXPECT_EQ("property 'scope' ignored, should be same origin as document.",
383 errors()[1]);
384 }
385
386 // No start URL. Document URL is in scope.
387 {
388 Manifest manifest = ParseManifestWithURLs("{ \"scope\": \"http://foo.com/lan d\" }",
389 GURL("http://foo.com/manifest.json "),
390 GURL("http://foo.com/land/index.ht ml"));
391 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/land");
392 ASSERT_EQ(0u, GetErrorCount());
393 }
394
395 // No start URL. Document is out of scope.
396 {
397 Manifest manifest = ParseManifestWithURLs("{ \"scope\": \"http://foo.com/lan d\" }",
398 GURL("http://foo.com/manifest.json "),
399 GURL("http://foo.com/index.html")) ;
400 ASSERT_EQ(1u, GetErrorCount());
401 EXPECT_EQ("property 'scope' ignored. Start url should be within scope "
402 "of scope URL.",
403 errors()[0]);
404 }
405
406 // Resolving has to happen based on the manifest_url.
407 {
408 Manifest manifest = ParseManifestWithURLs(
409 "{ \"scope\": \"treasure\" }",
410 GURL("http://foo.com/map/manifest.json"),
411 GURL("http://foo.com/map/treasure/island/index.html"));
412 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/map/treasure");
413 EXPECT_EQ(0u, GetErrorCount());
414 }
415
416 // Scope is parent directory.
417 {
418 Manifest manifest =
419 ParseManifestWithURLs("{ \"scope\": \"..\" }",
420 GURL("http://foo.com/map/manifest.json"),
421 GURL("http://foo.com/index.html"));
422 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/");
423 EXPECT_EQ(0u, GetErrorCount());
424 }
425
426 // Scope tries to go up past domain.
427 {
428 Manifest manifest =
429 ParseManifestWithURLs("{ \"scope\": \"../..\" }",
430 GURL("http://foo.com/map/manifest.json"),
431 GURL("http://foo.com/index.html"));
432 ASSERT_EQ(manifest.scope.spec(), "http://foo.com/");
433 EXPECT_EQ(0u, GetErrorCount());
434 }
435 }
436
290 TEST_F(ManifestParserTest, DisplayParserRules) { 437 TEST_F(ManifestParserTest, DisplayParserRules) {
291 // Smoke test. 438 // Smoke test.
292 { 439 {
293 Manifest manifest = ParseManifest("{ \"display\": \"browser\" }"); 440 Manifest manifest = ParseManifest("{ \"display\": \"browser\" }");
294 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser); 441 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser);
295 EXPECT_FALSE(manifest.IsEmpty()); 442 EXPECT_FALSE(manifest.IsEmpty());
296 EXPECT_EQ(0u, GetErrorCount()); 443 EXPECT_EQ(0u, GetErrorCount());
297 } 444 }
298 445
299 // Trim whitespaces. 446 // Trim whitespaces.
(...skipping 960 matching lines...) Expand 10 before | Expand all | Expand 10 after
1260 { 1407 {
1261 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); 1408 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }");
1262 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); 1409 EXPECT_TRUE(manifest.gcm_sender_id.is_null());
1263 EXPECT_EQ(1u, GetErrorCount()); 1410 EXPECT_EQ(1u, GetErrorCount());
1264 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.", 1411 EXPECT_EQ("property 'gcm_sender_id' ignored, type string expected.",
1265 errors()[0]); 1412 errors()[0]);
1266 } 1413 }
1267 } 1414 }
1268 1415
1269 } // namespace content 1416 } // namespace content
OLDNEW
« content/renderer/manifest/manifest_parser.cc ('K') | « content/renderer/manifest/manifest_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698