Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / renderer / manifest / manifest_parser_unittest.cc
blobd38a082e598e4469fee521ea4ecd6da1c1dbf6fc
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.
5 #include "content/renderer/manifest/manifest_parser.h"
7 #include "base/strings/string_util.h"
8 #include "content/public/common/manifest.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace content {
13 namespace {
15 uint32_t ExtractColor(int64_t color) {
16 return reinterpret_cast<uint32_t&>(color);
19 } // anonymous namespace
21 class ManifestParserTest : public testing::Test {
22 protected:
23 ManifestParserTest() {}
24 ~ManifestParserTest() override {}
26 Manifest ParseManifestWithURLs(const base::StringPiece& data,
27 const GURL& document_url,
28 const GURL& manifest_url) {
29 ManifestParser parser(data, document_url, manifest_url);
30 parser.Parse();
31 errors_ = parser.errors();
32 return parser.manifest();
35 Manifest ParseManifest(const base::StringPiece& data) {
36 return ParseManifestWithURLs(
37 data, default_document_url, default_manifest_url);
40 const std::vector<std::string>& errors() const {
41 return errors_;
44 unsigned int GetErrorCount() const {
45 return errors_.size();
48 static const GURL default_document_url;
49 static const GURL default_manifest_url;
51 private:
52 std::vector<std::string> errors_;
54 DISALLOW_COPY_AND_ASSIGN(ManifestParserTest);
57 const GURL ManifestParserTest::default_document_url(
58 "http://foo.com/index.html");
59 const GURL ManifestParserTest::default_manifest_url(
60 "http://foo.com/manifest.json");
62 TEST_F(ManifestParserTest, CrashTest) {
63 // Passing temporary variables should not crash.
64 ManifestParser parser("{\"start_url\": \"/\"}",
65 GURL("http://example.com"),
66 GURL("http://example.com"));
67 parser.Parse();
69 // .Parse() should have been call without crashing and succeeded.
70 EXPECT_EQ(0u, parser.errors().size());
71 EXPECT_FALSE(parser.manifest().IsEmpty());
74 TEST_F(ManifestParserTest, EmptyStringNull) {
75 Manifest manifest = ParseManifest("");
77 // This Manifest is not a valid JSON object, it's a parsing error.
78 EXPECT_EQ(1u, GetErrorCount());
79 EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.",
80 errors()[0]);
82 // A parsing error is equivalent to an empty manifest.
83 ASSERT_TRUE(manifest.IsEmpty());
84 ASSERT_TRUE(manifest.name.is_null());
85 ASSERT_TRUE(manifest.short_name.is_null());
86 ASSERT_TRUE(manifest.start_url.is_empty());
87 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
88 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
89 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
90 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
91 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
94 TEST_F(ManifestParserTest, ValidNoContentParses) {
95 Manifest manifest = ParseManifest("{}");
97 // Empty Manifest is not a parsing error.
98 EXPECT_EQ(0u, GetErrorCount());
100 // Check that all the fields are null in that case.
101 ASSERT_TRUE(manifest.IsEmpty());
102 ASSERT_TRUE(manifest.name.is_null());
103 ASSERT_TRUE(manifest.short_name.is_null());
104 ASSERT_TRUE(manifest.start_url.is_empty());
105 ASSERT_EQ(manifest.display, blink::WebDisplayModeUndefined);
106 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
107 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
108 ASSERT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
109 ASSERT_TRUE(manifest.gcm_sender_id.is_null());
112 TEST_F(ManifestParserTest, MultipleErrorsReporting) {
113 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4,"
114 "\"orientation\": {}, \"display\": \"foo\","
115 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42,"
116 "\"background_color\": 42 }");
118 EXPECT_EQ(8u, GetErrorCount());
120 EXPECT_EQ("Manifest parsing error: property 'name' ignored,"
121 " type string expected.",
122 errors()[0]);
123 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored,"
124 " type string expected.",
125 errors()[1]);
126 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored,"
127 " type string expected.",
128 errors()[2]);
129 EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.",
130 errors()[3]);
131 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored,"
132 " type string expected.",
133 errors()[4]);
134 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, "
135 "type array expected.",
136 errors()[5]);
137 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
138 " type string expected.",
139 errors()[6]);
140 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
141 " type string expected.",
142 errors()[7]);
145 TEST_F(ManifestParserTest, NameParseRules) {
146 // Smoke test.
148 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }");
149 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo"));
150 ASSERT_FALSE(manifest.IsEmpty());
151 EXPECT_EQ(0u, GetErrorCount());
154 // Trim whitespaces.
156 Manifest manifest = ParseManifest("{ \"name\": \" foo \" }");
157 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo"));
158 EXPECT_EQ(0u, GetErrorCount());
161 // Don't parse if name isn't a string.
163 Manifest manifest = ParseManifest("{ \"name\": {} }");
164 ASSERT_TRUE(manifest.name.is_null());
165 EXPECT_EQ(1u, GetErrorCount());
166 EXPECT_EQ("Manifest parsing error: property 'name' ignored,"
167 " type string expected.",
168 errors()[0]);
171 // Don't parse if name isn't a string.
173 Manifest manifest = ParseManifest("{ \"name\": 42 }");
174 ASSERT_TRUE(manifest.name.is_null());
175 EXPECT_EQ(1u, GetErrorCount());
176 EXPECT_EQ("Manifest parsing error: property 'name' ignored,"
177 " type string expected.",
178 errors()[0]);
182 TEST_F(ManifestParserTest, ShortNameParseRules) {
183 // Smoke test.
185 Manifest manifest = ParseManifest("{ \"short_name\": \"foo\" }");
186 ASSERT_TRUE(base::EqualsASCII(manifest.short_name.string(), "foo"));
187 ASSERT_FALSE(manifest.IsEmpty());
188 EXPECT_EQ(0u, GetErrorCount());
191 // Trim whitespaces.
193 Manifest manifest = ParseManifest("{ \"short_name\": \" foo \" }");
194 ASSERT_TRUE(base::EqualsASCII(manifest.short_name.string(), "foo"));
195 EXPECT_EQ(0u, GetErrorCount());
198 // Don't parse if name isn't a string.
200 Manifest manifest = ParseManifest("{ \"short_name\": {} }");
201 ASSERT_TRUE(manifest.short_name.is_null());
202 EXPECT_EQ(1u, GetErrorCount());
203 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored,"
204 " type string expected.",
205 errors()[0]);
208 // Don't parse if name isn't a string.
210 Manifest manifest = ParseManifest("{ \"short_name\": 42 }");
211 ASSERT_TRUE(manifest.short_name.is_null());
212 EXPECT_EQ(1u, GetErrorCount());
213 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored,"
214 " type string expected.",
215 errors()[0]);
219 TEST_F(ManifestParserTest, StartURLParseRules) {
220 // Smoke test.
222 Manifest manifest = ParseManifest("{ \"start_url\": \"land.html\" }");
223 ASSERT_EQ(manifest.start_url.spec(),
224 default_document_url.Resolve("land.html").spec());
225 ASSERT_FALSE(manifest.IsEmpty());
226 EXPECT_EQ(0u, GetErrorCount());
229 // Whitespaces.
231 Manifest manifest = ParseManifest("{ \"start_url\": \" land.html \" }");
232 ASSERT_EQ(manifest.start_url.spec(),
233 default_document_url.Resolve("land.html").spec());
234 EXPECT_EQ(0u, GetErrorCount());
237 // Don't parse if property isn't a string.
239 Manifest manifest = ParseManifest("{ \"start_url\": {} }");
240 ASSERT_TRUE(manifest.start_url.is_empty());
241 EXPECT_EQ(1u, GetErrorCount());
242 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored,"
243 " type string expected.",
244 errors()[0]);
247 // Don't parse if property isn't a string.
249 Manifest manifest = ParseManifest("{ \"start_url\": 42 }");
250 ASSERT_TRUE(manifest.start_url.is_empty());
251 EXPECT_EQ(1u, GetErrorCount());
252 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored,"
253 " type string expected.",
254 errors()[0]);
257 // Absolute start_url, same origin with document.
259 Manifest manifest =
260 ParseManifestWithURLs("{ \"start_url\": \"http://foo.com/land.html\" }",
261 GURL("http://foo.com/manifest.json"),
262 GURL("http://foo.com/index.html"));
263 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/land.html");
264 EXPECT_EQ(0u, GetErrorCount());
267 // Absolute start_url, cross origin with document.
269 Manifest manifest =
270 ParseManifestWithURLs("{ \"start_url\": \"http://bar.com/land.html\" }",
271 GURL("http://foo.com/manifest.json"),
272 GURL("http://foo.com/index.html"));
273 ASSERT_TRUE(manifest.start_url.is_empty());
274 EXPECT_EQ(1u, GetErrorCount());
275 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored, should "
276 "be same origin as document.",
277 errors()[0]);
280 // Resolving has to happen based on the manifest_url.
282 Manifest manifest =
283 ParseManifestWithURLs("{ \"start_url\": \"land.html\" }",
284 GURL("http://foo.com/landing/manifest.json"),
285 GURL("http://foo.com/index.html"));
286 ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html");
287 EXPECT_EQ(0u, GetErrorCount());
291 TEST_F(ManifestParserTest, DisplayParserRules) {
292 // Smoke test.
294 Manifest manifest = ParseManifest("{ \"display\": \"browser\" }");
295 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser);
296 EXPECT_FALSE(manifest.IsEmpty());
297 EXPECT_EQ(0u, GetErrorCount());
300 // Trim whitespaces.
302 Manifest manifest = ParseManifest("{ \"display\": \" browser \" }");
303 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser);
304 EXPECT_EQ(0u, GetErrorCount());
307 // Don't parse if name isn't a string.
309 Manifest manifest = ParseManifest("{ \"display\": {} }");
310 EXPECT_EQ(manifest.display, blink::WebDisplayModeUndefined);
311 EXPECT_EQ(1u, GetErrorCount());
312 EXPECT_EQ("Manifest parsing error: property 'display' ignored,"
313 " type string expected.",
314 errors()[0]);
317 // Don't parse if name isn't a string.
319 Manifest manifest = ParseManifest("{ \"display\": 42 }");
320 EXPECT_EQ(manifest.display, blink::WebDisplayModeUndefined);
321 EXPECT_EQ(1u, GetErrorCount());
322 EXPECT_EQ("Manifest parsing error: property 'display' ignored,"
323 " type string expected.",
324 errors()[0]);
327 // Parse fails if string isn't known.
329 Manifest manifest = ParseManifest("{ \"display\": \"browser_something\" }");
330 EXPECT_EQ(manifest.display, blink::WebDisplayModeUndefined);
331 EXPECT_EQ(1u, GetErrorCount());
332 EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.",
333 errors()[0]);
336 // Accept 'fullscreen'.
338 Manifest manifest = ParseManifest("{ \"display\": \"fullscreen\" }");
339 EXPECT_EQ(manifest.display, blink::WebDisplayModeFullscreen);
340 EXPECT_EQ(0u, GetErrorCount());
343 // Accept 'fullscreen'.
345 Manifest manifest = ParseManifest("{ \"display\": \"standalone\" }");
346 EXPECT_EQ(manifest.display, blink::WebDisplayModeStandalone);
347 EXPECT_EQ(0u, GetErrorCount());
350 // Accept 'minimal-ui'.
352 Manifest manifest = ParseManifest("{ \"display\": \"minimal-ui\" }");
353 EXPECT_EQ(manifest.display, blink::WebDisplayModeMinimalUi);
354 EXPECT_EQ(0u, GetErrorCount());
357 // Accept 'browser'.
359 Manifest manifest = ParseManifest("{ \"display\": \"browser\" }");
360 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser);
361 EXPECT_EQ(0u, GetErrorCount());
364 // Case insensitive.
366 Manifest manifest = ParseManifest("{ \"display\": \"BROWSER\" }");
367 EXPECT_EQ(manifest.display, blink::WebDisplayModeBrowser);
368 EXPECT_EQ(0u, GetErrorCount());
372 TEST_F(ManifestParserTest, OrientationParserRules) {
373 // Smoke test.
375 Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }");
376 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural);
377 EXPECT_FALSE(manifest.IsEmpty());
378 EXPECT_EQ(0u, GetErrorCount());
381 // Trim whitespaces.
383 Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }");
384 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural);
385 EXPECT_EQ(0u, GetErrorCount());
388 // Don't parse if name isn't a string.
390 Manifest manifest = ParseManifest("{ \"orientation\": {} }");
391 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
392 EXPECT_EQ(1u, GetErrorCount());
393 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored,"
394 " type string expected.",
395 errors()[0]);
398 // Don't parse if name isn't a string.
400 Manifest manifest = ParseManifest("{ \"orientation\": 42 }");
401 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
402 EXPECT_EQ(1u, GetErrorCount());
403 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored,"
404 " type string expected.",
405 errors()[0]);
408 // Parse fails if string isn't known.
410 Manifest manifest = ParseManifest("{ \"orientation\": \"naturalish\" }");
411 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
412 EXPECT_EQ(1u, GetErrorCount());
413 EXPECT_EQ("Manifest parsing error: unknown 'orientation' value ignored.",
414 errors()[0]);
417 // Accept 'any'.
419 Manifest manifest = ParseManifest("{ \"orientation\": \"any\" }");
420 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockAny);
421 EXPECT_EQ(0u, GetErrorCount());
424 // Accept 'natural'.
426 Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }");
427 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural);
428 EXPECT_EQ(0u, GetErrorCount());
431 // Accept 'landscape'.
433 Manifest manifest = ParseManifest("{ \"orientation\": \"landscape\" }");
434 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockLandscape);
435 EXPECT_EQ(0u, GetErrorCount());
438 // Accept 'landscape-primary'.
440 Manifest manifest =
441 ParseManifest("{ \"orientation\": \"landscape-primary\" }");
442 EXPECT_EQ(manifest.orientation,
443 blink::WebScreenOrientationLockLandscapePrimary);
444 EXPECT_EQ(0u, GetErrorCount());
447 // Accept 'landscape-secondary'.
449 Manifest manifest =
450 ParseManifest("{ \"orientation\": \"landscape-secondary\" }");
451 EXPECT_EQ(manifest.orientation,
452 blink::WebScreenOrientationLockLandscapeSecondary);
453 EXPECT_EQ(0u, GetErrorCount());
456 // Accept 'portrait'.
458 Manifest manifest = ParseManifest("{ \"orientation\": \"portrait\" }");
459 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockPortrait);
460 EXPECT_EQ(0u, GetErrorCount());
463 // Accept 'portrait-primary'.
465 Manifest manifest =
466 ParseManifest("{ \"orientation\": \"portrait-primary\" }");
467 EXPECT_EQ(manifest.orientation,
468 blink::WebScreenOrientationLockPortraitPrimary);
469 EXPECT_EQ(0u, GetErrorCount());
472 // Accept 'portrait-secondary'.
474 Manifest manifest =
475 ParseManifest("{ \"orientation\": \"portrait-secondary\" }");
476 EXPECT_EQ(manifest.orientation,
477 blink::WebScreenOrientationLockPortraitSecondary);
478 EXPECT_EQ(0u, GetErrorCount());
481 // Case insensitive.
483 Manifest manifest = ParseManifest("{ \"orientation\": \"LANDSCAPE\" }");
484 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockLandscape);
485 EXPECT_EQ(0u, GetErrorCount());
489 TEST_F(ManifestParserTest, IconsParseRules) {
490 // Smoke test: if no icon, empty list.
492 Manifest manifest = ParseManifest("{ \"icons\": [] }");
493 EXPECT_EQ(manifest.icons.size(), 0u);
494 EXPECT_TRUE(manifest.IsEmpty());
495 EXPECT_EQ(0u, GetErrorCount());
498 // Smoke test: if empty icon, empty list.
500 Manifest manifest = ParseManifest("{ \"icons\": [ {} ] }");
501 EXPECT_EQ(manifest.icons.size(), 0u);
502 EXPECT_TRUE(manifest.IsEmpty());
503 EXPECT_EQ(0u, GetErrorCount());
506 // Smoke test: icon with invalid src, empty list.
508 Manifest manifest = ParseManifest("{ \"icons\": [ { \"icons\": [] } ] }");
509 EXPECT_EQ(manifest.icons.size(), 0u);
510 EXPECT_TRUE(manifest.IsEmpty());
511 EXPECT_EQ(0u, GetErrorCount());
514 // Smoke test: if icon with empty src, it will be present in the list.
516 Manifest manifest = ParseManifest("{ \"icons\": [ { \"src\": \"\" } ] }");
517 EXPECT_EQ(manifest.icons.size(), 1u);
518 EXPECT_EQ(manifest.icons[0].src.spec(), "http://foo.com/index.html");
519 EXPECT_FALSE(manifest.IsEmpty());
520 EXPECT_EQ(0u, GetErrorCount());
523 // Smoke test: if one icons with valid src, it will be present in the list.
525 Manifest manifest =
526 ParseManifest("{ \"icons\": [{ \"src\": \"foo.jpg\" }] }");
527 EXPECT_EQ(manifest.icons.size(), 1u);
528 EXPECT_EQ(manifest.icons[0].src.spec(), "http://foo.com/foo.jpg");
529 EXPECT_FALSE(manifest.IsEmpty());
530 EXPECT_EQ(0u, GetErrorCount());
534 TEST_F(ManifestParserTest, IconSrcParseRules) {
535 // Smoke test.
537 Manifest manifest =
538 ParseManifest("{ \"icons\": [ {\"src\": \"foo.png\" } ] }");
539 EXPECT_EQ(manifest.icons[0].src.spec(),
540 default_document_url.Resolve("foo.png").spec());
541 EXPECT_EQ(0u, GetErrorCount());
544 // Whitespaces.
546 Manifest manifest =
547 ParseManifest("{ \"icons\": [ {\"src\": \" foo.png \" } ] }");
548 EXPECT_EQ(manifest.icons[0].src.spec(),
549 default_document_url.Resolve("foo.png").spec());
550 EXPECT_EQ(0u, GetErrorCount());
553 // Don't parse if property isn't a string.
555 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": {} } ] }");
556 EXPECT_TRUE(manifest.icons.empty());
557 EXPECT_EQ(1u, GetErrorCount());
558 EXPECT_EQ("Manifest parsing error: property 'src' ignored,"
559 " type string expected.",
560 errors()[0]);
563 // Don't parse if property isn't a string.
565 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": 42 } ] }");
566 EXPECT_TRUE(manifest.icons.empty());
567 EXPECT_EQ(1u, GetErrorCount());
568 EXPECT_EQ("Manifest parsing error: property 'src' ignored,"
569 " type string expected.",
570 errors()[0]);
573 // Resolving has to happen based on the document_url.
575 Manifest manifest = ParseManifestWithURLs(
576 "{ \"icons\": [ {\"src\": \"icons/foo.png\" } ] }",
577 GURL("http://foo.com/landing/index.html"),
578 default_manifest_url);
579 EXPECT_EQ(manifest.icons[0].src.spec(),
580 "http://foo.com/landing/icons/foo.png");
581 EXPECT_EQ(0u, GetErrorCount());
585 TEST_F(ManifestParserTest, IconTypeParseRules) {
586 // Smoke test.
588 Manifest manifest =
589 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": \"foo\" } ] }");
590 EXPECT_TRUE(base::EqualsASCII(manifest.icons[0].type.string(), "foo"));
591 EXPECT_EQ(0u, GetErrorCount());
594 // Trim whitespaces.
596 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
597 " \"type\": \" foo \" } ] }");
598 EXPECT_TRUE(base::EqualsASCII(manifest.icons[0].type.string(), "foo"));
599 EXPECT_EQ(0u, GetErrorCount());
602 // Don't parse if property isn't a string.
604 Manifest manifest =
605 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": {} } ] }");
606 EXPECT_TRUE(manifest.icons[0].type.is_null());
607 EXPECT_EQ(1u, GetErrorCount());
608 EXPECT_EQ("Manifest parsing error: property 'type' ignored,"
609 " type string expected.",
610 errors()[0]);
613 // Don't parse if property isn't a string.
615 Manifest manifest =
616 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": 42 } ] }");
617 EXPECT_TRUE(manifest.icons[0].type.is_null());
618 EXPECT_EQ(1u, GetErrorCount());
619 EXPECT_EQ("Manifest parsing error: property 'type' ignored,"
620 " type string expected.",
621 errors()[0]);
625 TEST_F(ManifestParserTest, IconDensityParseRules) {
626 // Smoke test.
628 Manifest manifest =
629 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 42 } ] }");
630 EXPECT_EQ(manifest.icons[0].density, 42);
631 EXPECT_EQ(0u, GetErrorCount());
634 // Decimal value.
636 Manifest manifest =
637 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 2.5 } ] }");
638 EXPECT_EQ(manifest.icons[0].density, 2.5);
639 EXPECT_EQ(0u, GetErrorCount());
642 // Parse fail if it isn't a float.
644 Manifest manifest =
645 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": {} } ] }");
646 EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
647 EXPECT_EQ(1u, GetErrorCount());
648 EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
649 "must be float greater than 0.",
650 errors()[0]);
653 // Parse fail if it isn't a float.
655 Manifest manifest =
656 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\":\"2\" } ] }");
657 EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
658 EXPECT_EQ(1u, GetErrorCount());
659 EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
660 "must be float greater than 0.",
661 errors()[0]);
664 // Edge case: 1.0.
666 Manifest manifest =
667 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 1.00 } ] }");
668 EXPECT_EQ(manifest.icons[0].density, 1);
669 EXPECT_EQ(0u, GetErrorCount());
672 // Edge case: values between 0.0 and 1.0 are allowed.
674 Manifest manifest =
675 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 0.42 } ] }");
676 EXPECT_EQ(manifest.icons[0].density, 0.42);
677 EXPECT_EQ(0u, GetErrorCount());
680 // 0 is an invalid value.
682 Manifest manifest =
683 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 0.0 } ] }");
684 EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
685 EXPECT_EQ(1u, GetErrorCount());
686 EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
687 "must be float greater than 0.",
688 errors()[0]);
691 // Negative values are invalid.
693 Manifest manifest =
694 ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": -2.5 } ] }");
695 EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
696 EXPECT_EQ(1u, GetErrorCount());
697 EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
698 "must be float greater than 0.",
699 errors()[0]);
703 TEST_F(ManifestParserTest, IconSizesParseRules) {
704 // Smoke test.
706 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
707 "\"sizes\": \"42x42\" } ] }");
708 EXPECT_EQ(manifest.icons[0].sizes.size(), 1u);
709 EXPECT_EQ(0u, GetErrorCount());
712 // Trim whitespaces.
714 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
715 "\"sizes\": \" 42x42 \" } ] }");
716 EXPECT_EQ(manifest.icons[0].sizes.size(), 1u);
717 EXPECT_EQ(0u, GetErrorCount());
720 // Ignore sizes if property isn't a string.
722 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
723 "\"sizes\": {} } ] }");
724 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
725 EXPECT_EQ(1u, GetErrorCount());
726 EXPECT_EQ("Manifest parsing error: property 'sizes' ignored,"
727 " type string expected.",
728 errors()[0]);
731 // Ignore sizes if property isn't a string.
733 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
734 "\"sizes\": 42 } ] }");
735 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
736 EXPECT_EQ(1u, GetErrorCount());
737 EXPECT_EQ("Manifest parsing error: property 'sizes' ignored,"
738 " type string expected.",
739 errors()[0]);
742 // Smoke test: value correctly parsed.
744 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
745 "\"sizes\": \"42x42 48x48\" } ] }");
746 EXPECT_EQ(manifest.icons[0].sizes[0], gfx::Size(42, 42));
747 EXPECT_EQ(manifest.icons[0].sizes[1], gfx::Size(48, 48));
748 EXPECT_EQ(0u, GetErrorCount());
751 // <WIDTH>'x'<HEIGHT> and <WIDTH>'X'<HEIGHT> are equivalent.
753 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
754 "\"sizes\": \"42X42 48X48\" } ] }");
755 EXPECT_EQ(manifest.icons[0].sizes[0], gfx::Size(42, 42));
756 EXPECT_EQ(manifest.icons[0].sizes[1], gfx::Size(48, 48));
757 EXPECT_EQ(0u, GetErrorCount());
760 // Twice the same value is parsed twice.
762 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
763 "\"sizes\": \"42X42 42x42\" } ] }");
764 EXPECT_EQ(manifest.icons[0].sizes[0], gfx::Size(42, 42));
765 EXPECT_EQ(manifest.icons[0].sizes[1], gfx::Size(42, 42));
766 EXPECT_EQ(0u, GetErrorCount());
769 // Width or height can't start with 0.
771 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
772 "\"sizes\": \"004X007 042x00\" } ] }");
773 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
774 EXPECT_EQ(1u, GetErrorCount());
775 EXPECT_EQ("Manifest parsing error: found icon with no valid size.",
776 errors()[0]);
779 // Width and height MUST contain digits.
781 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
782 "\"sizes\": \"e4X1.0 55ax1e10\" } ] }");
783 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
784 EXPECT_EQ(1u, GetErrorCount());
785 EXPECT_EQ("Manifest parsing error: found icon with no valid size.",
786 errors()[0]);
789 // 'any' is correctly parsed and transformed to gfx::Size(0,0).
791 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
792 "\"sizes\": \"any AnY ANY aNy\" } ] }");
793 gfx::Size any = gfx::Size(0, 0);
794 EXPECT_EQ(manifest.icons[0].sizes.size(), 4u);
795 EXPECT_EQ(manifest.icons[0].sizes[0], any);
796 EXPECT_EQ(manifest.icons[0].sizes[1], any);
797 EXPECT_EQ(manifest.icons[0].sizes[2], any);
798 EXPECT_EQ(manifest.icons[0].sizes[3], any);
799 EXPECT_EQ(0u, GetErrorCount());
802 // Some invalid width/height combinations.
804 Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
805 "\"sizes\": \"x 40xx 1x2x3 x42 42xx42\" } ] }");
806 gfx::Size any = gfx::Size(0, 0);
807 EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
808 EXPECT_EQ(1u, GetErrorCount());
809 EXPECT_EQ("Manifest parsing error: found icon with no valid size.",
810 errors()[0]);
814 TEST_F(ManifestParserTest, RelatedApplicationsParseRules) {
815 // If no application, empty list.
817 Manifest manifest = ParseManifest(
818 "{ \"related_applications\": []}");
819 EXPECT_EQ(manifest.related_applications.size(), 0u);
820 EXPECT_TRUE(manifest.IsEmpty());
821 EXPECT_EQ(0u, GetErrorCount());
824 // If empty application, empty list.
826 Manifest manifest = ParseManifest(
827 "{ \"related_applications\": [{}]}");
828 EXPECT_EQ(manifest.related_applications.size(), 0u);
829 EXPECT_TRUE(manifest.IsEmpty());
830 EXPECT_EQ(1u, GetErrorCount());
831 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, "
832 "related application ignored.",
833 errors()[0]);
836 // If invalid platform, application is ignored.
838 Manifest manifest = ParseManifest(
839 "{ \"related_applications\": [{\"platform\": 123}]}");
840 EXPECT_EQ(manifest.related_applications.size(), 0u);
841 EXPECT_TRUE(manifest.IsEmpty());
842 EXPECT_EQ(2u, GetErrorCount());
843 EXPECT_EQ(
844 "Manifest parsing error: property 'platform' ignored, type string "
845 "expected.",
846 errors()[0]);
847 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, "
848 "related application ignored.",
849 errors()[1]);
852 // If missing platform, application is ignored.
854 Manifest manifest = ParseManifest(
855 "{ \"related_applications\": [{\"id\": \"foo\"}]}");
856 EXPECT_EQ(manifest.related_applications.size(), 0u);
857 EXPECT_TRUE(manifest.IsEmpty());
858 EXPECT_EQ(1u, GetErrorCount());
859 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, "
860 "related application ignored.",
861 errors()[0]);
864 // If missing id and url, application is ignored.
866 Manifest manifest = ParseManifest(
867 "{ \"related_applications\": [{\"platform\": \"play\"}]}");
868 EXPECT_EQ(manifest.related_applications.size(), 0u);
869 EXPECT_TRUE(manifest.IsEmpty());
870 EXPECT_EQ(1u, GetErrorCount());
871 EXPECT_EQ("Manifest parsing error: one of 'url' or 'id' is required, "
872 "related application ignored.",
873 errors()[0]);
876 // Valid application, with url.
878 Manifest manifest = ParseManifest(
879 "{ \"related_applications\": ["
880 "{\"platform\": \"play\", \"url\": \"http://www.foo.com\"}]}");
881 EXPECT_EQ(manifest.related_applications.size(), 1u);
882 EXPECT_TRUE(base::EqualsASCII(
883 manifest.related_applications[0].platform.string(),
884 "play"));
885 EXPECT_EQ(manifest.related_applications[0].url.spec(),
886 "http://www.foo.com/");
887 EXPECT_FALSE(manifest.IsEmpty());
888 EXPECT_EQ(0u, GetErrorCount());
891 // Valid application, with id.
893 Manifest manifest = ParseManifest(
894 "{ \"related_applications\": ["
895 "{\"platform\": \"itunes\", \"id\": \"foo\"}]}");
896 EXPECT_EQ(manifest.related_applications.size(), 1u);
897 EXPECT_TRUE(base::EqualsASCII(
898 manifest.related_applications[0].platform.string(),
899 "itunes"));
900 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[0].id.string(),
901 "foo"));
902 EXPECT_FALSE(manifest.IsEmpty());
903 EXPECT_EQ(0u, GetErrorCount());
906 // All valid applications are in list.
908 Manifest manifest = ParseManifest(
909 "{ \"related_applications\": ["
910 "{\"platform\": \"play\", \"id\": \"foo\"},"
911 "{\"platform\": \"itunes\", \"id\": \"bar\"}]}");
912 EXPECT_EQ(manifest.related_applications.size(), 2u);
913 EXPECT_TRUE(base::EqualsASCII(
914 manifest.related_applications[0].platform.string(),
915 "play"));
916 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[0].id.string(),
917 "foo"));
918 EXPECT_TRUE(base::EqualsASCII(
919 manifest.related_applications[1].platform.string(),
920 "itunes"));
921 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[1].id.string(),
922 "bar"));
923 EXPECT_FALSE(manifest.IsEmpty());
924 EXPECT_EQ(0u, GetErrorCount());
927 // Two invalid applications and one valid. Only the valid application should
928 // be in the list.
930 Manifest manifest = ParseManifest(
931 "{ \"related_applications\": ["
932 "{\"platform\": \"itunes\"},"
933 "{\"platform\": \"play\", \"id\": \"foo\"},"
934 "{}]}");
935 EXPECT_EQ(manifest.related_applications.size(), 1u);
936 EXPECT_TRUE(base::EqualsASCII(
937 manifest.related_applications[0].platform.string(),
938 "play"));
939 EXPECT_TRUE(base::EqualsASCII(manifest.related_applications[0].id.string(),
940 "foo"));
941 EXPECT_FALSE(manifest.IsEmpty());
942 EXPECT_EQ(2u, GetErrorCount());
943 EXPECT_EQ("Manifest parsing error: one of 'url' or 'id' is required, "
944 "related application ignored.",
945 errors()[0]);
946 EXPECT_EQ("Manifest parsing error: 'platform' is a required field, "
947 "related application ignored.",
948 errors()[1]);
952 TEST_F(ManifestParserTest, ParsePreferRelatedApplicationsParseRules) {
953 // Smoke test.
955 Manifest manifest =
956 ParseManifest("{ \"prefer_related_applications\": true }");
957 EXPECT_TRUE(manifest.prefer_related_applications);
958 EXPECT_EQ(0u, GetErrorCount());
961 // Don't parse if the property isn't a boolean.
963 Manifest manifest =
964 ParseManifest("{ \"prefer_related_applications\": {} }");
965 EXPECT_FALSE(manifest.prefer_related_applications);
966 EXPECT_EQ(1u, GetErrorCount());
967 EXPECT_EQ(
968 "Manifest parsing error: property 'prefer_related_applications' "
969 "ignored, type boolean expected.",
970 errors()[0]);
973 Manifest manifest = ParseManifest(
974 "{ \"prefer_related_applications\": \"true\" }");
975 EXPECT_FALSE(manifest.prefer_related_applications);
976 EXPECT_EQ(1u, GetErrorCount());
977 EXPECT_EQ(
978 "Manifest parsing error: property 'prefer_related_applications' "
979 "ignored, type boolean expected.",
980 errors()[0]);
983 Manifest manifest = ParseManifest("{ \"prefer_related_applications\": 1 }");
984 EXPECT_FALSE(manifest.prefer_related_applications);
985 EXPECT_EQ(1u, GetErrorCount());
986 EXPECT_EQ(
987 "Manifest parsing error: property 'prefer_related_applications' "
988 "ignored, type boolean expected.",
989 errors()[0]);
992 // "False" should set the boolean false without throwing errors.
994 Manifest manifest =
995 ParseManifest("{ \"prefer_related_applications\": false }");
996 EXPECT_FALSE(manifest.prefer_related_applications);
997 EXPECT_EQ(0u, GetErrorCount());
1001 TEST_F(ManifestParserTest, ThemeColorParserRules) {
1002 // Smoke test.
1004 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }");
1005 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFFF0000u);
1006 EXPECT_FALSE(manifest.IsEmpty());
1007 EXPECT_EQ(0u, GetErrorCount());
1010 // Trim whitespaces.
1012 Manifest manifest = ParseManifest("{ \"theme_color\": \" blue \" }");
1013 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFF0000FFu);
1014 EXPECT_EQ(0u, GetErrorCount());
1017 // Don't parse if theme_color isn't a string.
1019 Manifest manifest = ParseManifest("{ \"theme_color\": {} }");
1020 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1021 EXPECT_EQ(1u, GetErrorCount());
1022 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
1023 " type string expected.",
1024 errors()[0]);
1027 // Don't parse if theme_color isn't a string.
1029 Manifest manifest = ParseManifest("{ \"theme_color\": false }");
1030 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1031 EXPECT_EQ(1u, GetErrorCount());
1032 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
1033 " type string expected.",
1034 errors()[0]);
1037 // Don't parse if theme_color isn't a string.
1039 Manifest manifest = ParseManifest("{ \"theme_color\": null }");
1040 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1041 EXPECT_EQ(1u, GetErrorCount());
1042 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
1043 " type string expected.",
1044 errors()[0]);
1047 // Don't parse if theme_color isn't a string.
1049 Manifest manifest = ParseManifest("{ \"theme_color\": [] }");
1050 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1051 EXPECT_EQ(1u, GetErrorCount());
1052 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
1053 " type string expected.",
1054 errors()[0]);
1057 // Don't parse if theme_color isn't a string.
1059 Manifest manifest = ParseManifest("{ \"theme_color\": 42 }");
1060 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1061 EXPECT_EQ(1u, GetErrorCount());
1062 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
1063 " type string expected.",
1064 errors()[0]);
1067 // Parse fails if string is not in a known format.
1069 Manifest manifest = ParseManifest("{ \"theme_color\": \"foo(bar)\" }");
1070 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1071 EXPECT_EQ(1u, GetErrorCount());
1072 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored,"
1073 " 'foo(bar)' is not a valid color.",
1074 errors()[0]);
1077 // Parse fails if string is not in a known format.
1079 Manifest manifest = ParseManifest("{ \"theme_color\": \"bleu\" }");
1080 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1081 EXPECT_EQ(1u, GetErrorCount());
1082 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, 'bleu'"
1083 " is not a valid color.",
1084 errors()[0]);
1087 // Parse fails if string is not in a known format.
1089 Manifest manifest = ParseManifest("{ \"theme_color\": \"FF00FF\" }");
1090 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1091 EXPECT_EQ(1u, GetErrorCount());
1092 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, 'FF00FF'"
1093 " is not a valid color.",
1094 errors()[0]);
1097 // Parse fails if multiple values for theme_color are given.
1099 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC #DEF\" }");
1100 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1101 EXPECT_EQ(1u, GetErrorCount());
1102 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, "
1103 "'#ABC #DEF' is not a valid color.",
1104 errors()[0]);
1107 // Parse fails if multiple values for theme_color are given.
1109 Manifest manifest = ParseManifest(
1110 "{ \"theme_color\": \"#AABBCC #DDEEFF\" }");
1111 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingColor);
1112 EXPECT_EQ(1u, GetErrorCount());
1113 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, "
1114 "'#AABBCC #DDEEFF' is not a valid color.",
1115 errors()[0]);
1118 // Accept CSS color keyword format.
1120 Manifest manifest = ParseManifest("{ \"theme_color\": \"blue\" }");
1121 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFF0000FFu);
1122 EXPECT_EQ(0u, GetErrorCount());
1125 // Accept CSS color keyword format.
1127 Manifest manifest = ParseManifest("{ \"theme_color\": \"chartreuse\" }");
1128 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFF7FFF00u);
1129 EXPECT_EQ(0u, GetErrorCount());
1132 // Accept CSS RGB format.
1134 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FFF\" }");
1135 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFFFFFFFu);
1136 EXPECT_EQ(0u, GetErrorCount());
1139 // Accept CSS RGB format.
1141 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC\" }");
1142 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFAABBCCu);
1143 EXPECT_EQ(0u, GetErrorCount());
1146 // Accept CSS RRGGBB format.
1148 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }");
1149 EXPECT_EQ(ExtractColor(manifest.theme_color), 0xFFFF0000u);
1150 EXPECT_EQ(0u, GetErrorCount());
1153 // Accept translucent colors.
1155 Manifest manifest = ParseManifest("{ \"theme_color\": \"rgba(255,0,0,"
1156 "0.4)\" }");
1157 EXPECT_EQ(ExtractColor(manifest.theme_color), 0x66FF0000u);
1158 EXPECT_EQ(0u, GetErrorCount());
1161 // Accept transparent colors.
1163 Manifest manifest = ParseManifest("{ \"theme_color\": \"rgba(0,0,0,0)\" }");
1164 EXPECT_EQ(ExtractColor(manifest.theme_color), 0x00000000u);
1165 EXPECT_EQ(0u, GetErrorCount());
1169 TEST_F(ManifestParserTest, BackgroundColorParserRules) {
1170 // Smoke test.
1172 Manifest manifest = ParseManifest("{ \"background_color\": \"#FF0000\" }");
1173 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFFF0000u);
1174 EXPECT_FALSE(manifest.IsEmpty());
1175 EXPECT_EQ(0u, GetErrorCount());
1178 // Trim whitespaces.
1180 Manifest manifest = ParseManifest(
1181 "{ \"background_color\": \" blue \" }");
1182 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFF0000FFu);
1183 EXPECT_EQ(0u, GetErrorCount());
1186 // Don't parse if background_color isn't a string.
1188 Manifest manifest = ParseManifest("{ \"background_color\": {} }");
1189 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1190 EXPECT_EQ(1u, GetErrorCount());
1191 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1192 " type string expected.",
1193 errors()[0]);
1196 // Don't parse if background_color isn't a string.
1198 Manifest manifest = ParseManifest("{ \"background_color\": false }");
1199 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1200 EXPECT_EQ(1u, GetErrorCount());
1201 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1202 " type string expected.",
1203 errors()[0]);
1206 // Don't parse if background_color isn't a string.
1208 Manifest manifest = ParseManifest("{ \"background_color\": null }");
1209 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1210 EXPECT_EQ(1u, GetErrorCount());
1211 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1212 " type string expected.",
1213 errors()[0]);
1216 // Don't parse if background_color isn't a string.
1218 Manifest manifest = ParseManifest("{ \"background_color\": [] }");
1219 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1220 EXPECT_EQ(1u, GetErrorCount());
1221 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1222 " type string expected.",
1223 errors()[0]);
1226 // Don't parse if background_color isn't a string.
1228 Manifest manifest = ParseManifest("{ \"background_color\": 42 }");
1229 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1230 EXPECT_EQ(1u, GetErrorCount());
1231 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1232 " type string expected.",
1233 errors()[0]);
1236 // Parse fails if string is not in a known format.
1238 Manifest manifest = ParseManifest("{ \"background_color\": \"foo(bar)\" }");
1239 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1240 EXPECT_EQ(1u, GetErrorCount());
1241 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1242 " 'foo(bar)' is not a valid color.",
1243 errors()[0]);
1246 // Parse fails if string is not in a known format.
1248 Manifest manifest = ParseManifest("{ \"background_color\": \"bleu\" }");
1249 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1250 EXPECT_EQ(1u, GetErrorCount());
1251 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1252 " 'bleu' is not a valid color.",
1253 errors()[0]);
1256 // Parse fails if string is not in a known format.
1258 Manifest manifest = ParseManifest("{ \"background_color\": \"FF00FF\" }");
1259 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1260 EXPECT_EQ(1u, GetErrorCount());
1261 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored,"
1262 " 'FF00FF' is not a valid color.",
1263 errors()[0]);
1266 // Parse fails if multiple values for background_color are given.
1268 Manifest manifest = ParseManifest(
1269 "{ \"background_color\": \"#ABC #DEF\" }");
1270 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1271 EXPECT_EQ(1u, GetErrorCount());
1272 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored, "
1273 "'#ABC #DEF' is not a valid color.",
1274 errors()[0]);
1277 // Parse fails if multiple values for background_color are given.
1279 Manifest manifest = ParseManifest(
1280 "{ \"background_color\": \"#AABBCC #DDEEFF\" }");
1281 EXPECT_EQ(manifest.background_color, Manifest::kInvalidOrMissingColor);
1282 EXPECT_EQ(1u, GetErrorCount());
1283 EXPECT_EQ("Manifest parsing error: property 'background_color' ignored, "
1284 "'#AABBCC #DDEEFF' is not a valid color.",
1285 errors()[0]);
1288 // Accept CSS color keyword format.
1290 Manifest manifest = ParseManifest("{ \"background_color\": \"blue\" }");
1291 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFF0000FFu);
1292 EXPECT_EQ(0u, GetErrorCount());
1295 // Accept CSS color keyword format.
1297 Manifest manifest = ParseManifest(
1298 "{ \"background_color\": \"chartreuse\" }");
1299 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFF7FFF00u);
1300 EXPECT_EQ(0u, GetErrorCount());
1303 // Accept CSS RGB format.
1305 Manifest manifest = ParseManifest("{ \"background_color\": \"#FFF\" }");
1306 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFFFFFFFu);
1307 EXPECT_EQ(0u, GetErrorCount());
1310 // Accept CSS RGB format.
1312 Manifest manifest = ParseManifest("{ \"background_color\": \"#ABC\" }");
1313 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFAABBCCu);
1314 EXPECT_EQ(0u, GetErrorCount());
1317 // Accept CSS RRGGBB format.
1319 Manifest manifest = ParseManifest("{ \"background_color\": \"#FF0000\" }");
1320 EXPECT_EQ(ExtractColor(manifest.background_color), 0xFFFF0000u);
1321 EXPECT_EQ(0u, GetErrorCount());
1324 // Accept translucent colors.
1326 Manifest manifest = ParseManifest("{ \"background_color\": \"rgba(255,0,0,"
1327 "0.4)\" }");
1328 EXPECT_EQ(ExtractColor(manifest.background_color), 0x66FF0000u);
1329 EXPECT_EQ(0u, GetErrorCount());
1332 // Accept transparent colors.
1334 Manifest manifest = ParseManifest("{ \"background_color\": \"rgba(0,0,0,"
1335 "0)\" }");
1336 EXPECT_EQ(ExtractColor(manifest.background_color), 0x00000000u);
1337 EXPECT_EQ(0u, GetErrorCount());
1341 TEST_F(ManifestParserTest, GCMSenderIDParseRules) {
1342 // Smoke test.
1344 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }");
1345 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo"));
1346 EXPECT_EQ(0u, GetErrorCount());
1349 // Trim whitespaces.
1351 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \" foo \" }");
1352 EXPECT_TRUE(base::EqualsASCII(manifest.gcm_sender_id.string(), "foo"));
1353 EXPECT_EQ(0u, GetErrorCount());
1356 // Don't parse if the property isn't a string.
1358 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": {} }");
1359 EXPECT_TRUE(manifest.gcm_sender_id.is_null());
1360 EXPECT_EQ(1u, GetErrorCount());
1361 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored,"
1362 " type string expected.",
1363 errors()[0]);
1366 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }");
1367 EXPECT_TRUE(manifest.gcm_sender_id.is_null());
1368 EXPECT_EQ(1u, GetErrorCount());
1369 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored,"
1370 " type string expected.",
1371 errors()[0]);
1375 } // namespace content