OLD | NEW |
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 "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "content/public/common/manifest.h" | 8 #include "content/public/common/manifest.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.", | 71 EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.", |
72 errors()[0]); | 72 errors()[0]); |
73 | 73 |
74 // A parsing error is equivalent to an empty manifest. | 74 // A parsing error is equivalent to an empty manifest. |
75 ASSERT_TRUE(manifest.IsEmpty()); | 75 ASSERT_TRUE(manifest.IsEmpty()); |
76 ASSERT_TRUE(manifest.name.is_null()); | 76 ASSERT_TRUE(manifest.name.is_null()); |
77 ASSERT_TRUE(manifest.short_name.is_null()); | 77 ASSERT_TRUE(manifest.short_name.is_null()); |
78 ASSERT_TRUE(manifest.start_url.is_empty()); | 78 ASSERT_TRUE(manifest.start_url.is_empty()); |
79 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); | 79 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); |
80 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); | 80 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); |
| 81 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
81 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); | 82 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); |
82 } | 83 } |
83 | 84 |
84 TEST_F(ManifestParserTest, ValidNoContentParses) { | 85 TEST_F(ManifestParserTest, ValidNoContentParses) { |
85 Manifest manifest = ParseManifest("{}"); | 86 Manifest manifest = ParseManifest("{}"); |
86 | 87 |
87 // Empty Manifest is not a parsing error. | 88 // Empty Manifest is not a parsing error. |
88 EXPECT_EQ(0u, GetErrorCount()); | 89 EXPECT_EQ(0u, GetErrorCount()); |
89 | 90 |
90 // Check that all the fields are null in that case. | 91 // Check that all the fields are null in that case. |
91 ASSERT_TRUE(manifest.IsEmpty()); | 92 ASSERT_TRUE(manifest.IsEmpty()); |
92 ASSERT_TRUE(manifest.name.is_null()); | 93 ASSERT_TRUE(manifest.name.is_null()); |
93 ASSERT_TRUE(manifest.short_name.is_null()); | 94 ASSERT_TRUE(manifest.short_name.is_null()); |
94 ASSERT_TRUE(manifest.start_url.is_empty()); | 95 ASSERT_TRUE(manifest.start_url.is_empty()); |
95 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); | 96 ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED); |
96 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); | 97 ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault); |
| 98 ASSERT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
97 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); | 99 ASSERT_TRUE(manifest.gcm_sender_id.is_null()); |
98 } | 100 } |
99 | 101 |
100 TEST_F(ManifestParserTest, MultipleErrorsReporting) { | 102 TEST_F(ManifestParserTest, MultipleErrorsReporting) { |
101 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4," | 103 Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4," |
102 "\"orientation\": {}, \"display\": \"foo\", \"start_url\": null," | 104 "\"orientation\": {}, \"display\": \"foo\"," |
103 "\"icons\": {} }"); | 105 "\"start_url\": null, \"icons\": {}, \"theme_color\": 42 }"); |
104 | 106 |
105 EXPECT_EQ(6u, GetErrorCount()); | 107 EXPECT_EQ(7u, GetErrorCount()); |
106 | 108 |
107 EXPECT_EQ("Manifest parsing error: property 'name' ignored," | 109 EXPECT_EQ("Manifest parsing error: property 'name' ignored," |
108 " type string expected.", | 110 " type string expected.", |
109 errors()[0]); | 111 errors()[0]); |
110 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored," | 112 EXPECT_EQ("Manifest parsing error: property 'short_name' ignored," |
111 " type string expected.", | 113 " type string expected.", |
112 errors()[1]); | 114 errors()[1]); |
113 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored," | 115 EXPECT_EQ("Manifest parsing error: property 'start_url' ignored," |
114 " type string expected.", | 116 " type string expected.", |
115 errors()[2]); | 117 errors()[2]); |
116 EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.", | 118 EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.", |
117 errors()[3]); | 119 errors()[3]); |
118 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored," | 120 EXPECT_EQ("Manifest parsing error: property 'orientation' ignored," |
119 " type string expected.", | 121 " type string expected.", |
120 errors()[4]); | 122 errors()[4]); |
121 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, " | 123 EXPECT_EQ("Manifest parsing error: property 'icons' ignored, " |
122 "type array expected.", | 124 "type array expected.", |
123 errors()[5]); | 125 errors()[5]); |
| 126 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 127 " type string expected.", |
| 128 errors()[6]); |
124 } | 129 } |
125 | 130 |
126 TEST_F(ManifestParserTest, NameParseRules) { | 131 TEST_F(ManifestParserTest, NameParseRules) { |
127 // Smoke test. | 132 // Smoke test. |
128 { | 133 { |
129 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); | 134 Manifest manifest = ParseManifest("{ \"name\": \"foo\" }"); |
130 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo")); | 135 ASSERT_TRUE(base::EqualsASCII(manifest.name.string(), "foo")); |
131 ASSERT_FALSE(manifest.IsEmpty()); | 136 ASSERT_FALSE(manifest.IsEmpty()); |
132 EXPECT_EQ(0u, GetErrorCount()); | 137 EXPECT_EQ(0u, GetErrorCount()); |
133 } | 138 } |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 } | 348 } |
344 | 349 |
345 // Case insensitive. | 350 // Case insensitive. |
346 { | 351 { |
347 Manifest manifest = ParseManifest("{ \"display\": \"BROWSER\" }"); | 352 Manifest manifest = ParseManifest("{ \"display\": \"BROWSER\" }"); |
348 EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_BROWSER); | 353 EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_BROWSER); |
349 EXPECT_EQ(0u, GetErrorCount()); | 354 EXPECT_EQ(0u, GetErrorCount()); |
350 } | 355 } |
351 } | 356 } |
352 | 357 |
| 358 TEST_F(ManifestParserTest, ThemeColorParserRules) { |
| 359 // Smoke test. |
| 360 { |
| 361 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }"); |
| 362 EXPECT_EQ(manifest.theme_color, 0xFFFF0000); |
| 363 EXPECT_FALSE(manifest.IsEmpty()); |
| 364 EXPECT_EQ(0u, GetErrorCount()); |
| 365 } |
| 366 |
| 367 // Trim whitespaces. |
| 368 { |
| 369 Manifest manifest = ParseManifest("{ \"theme_color\": \" blue \" }"); |
| 370 EXPECT_EQ(manifest.theme_color, 0xFF0000FF); |
| 371 EXPECT_EQ(0u, GetErrorCount()); |
| 372 } |
| 373 |
| 374 // Don't parse if theme_color isn't a string. |
| 375 { |
| 376 Manifest manifest = ParseManifest("{ \"theme_color\": {} }"); |
| 377 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 378 EXPECT_EQ(1u, GetErrorCount()); |
| 379 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 380 " type string expected.", |
| 381 errors()[0]); |
| 382 } |
| 383 |
| 384 // Don't parse if theme_color isn't a string. |
| 385 { |
| 386 Manifest manifest = ParseManifest("{ \"theme_color\": 42 }"); |
| 387 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 388 EXPECT_EQ(1u, GetErrorCount()); |
| 389 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored," |
| 390 " type string expected.", |
| 391 errors()[0]); |
| 392 } |
| 393 |
| 394 // Parse fails if string is not in a known format. |
| 395 { |
| 396 Manifest manifest = ParseManifest("{ \"theme_color\": \"foo(bar)\" }"); |
| 397 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 398 EXPECT_EQ(1u, GetErrorCount()); |
| 399 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, foo(bar)" |
| 400 " is not a valid color.", |
| 401 errors()[0]); |
| 402 } |
| 403 |
| 404 // Parse fails if string is not in a known format. |
| 405 { |
| 406 Manifest manifest = ParseManifest("{ \"theme_color\": \"bleu\" }"); |
| 407 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 408 EXPECT_EQ(1u, GetErrorCount()); |
| 409 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, bleu" |
| 410 " is not a valid color.", |
| 411 errors()[0]); |
| 412 } |
| 413 |
| 414 // Parse fails if string is not in a known format. |
| 415 { |
| 416 Manifest manifest = ParseManifest("{ \"theme_color\": \"FF00FF\" }"); |
| 417 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 418 EXPECT_EQ(1u, GetErrorCount()); |
| 419 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, FF00FF" |
| 420 " is not a valid color.", |
| 421 errors()[0]); |
| 422 } |
| 423 |
| 424 // Parse fails if multiple values for theme_color are given. |
| 425 { |
| 426 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC #DEF\" }"); |
| 427 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 428 EXPECT_EQ(1u, GetErrorCount()); |
| 429 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, " |
| 430 "#ABC #DEF is not a valid color.", |
| 431 errors()[0]); |
| 432 } |
| 433 |
| 434 // Parse fails if multiple values for theme_color are given. |
| 435 { |
| 436 Manifest manifest = ParseManifest( |
| 437 "{ \"theme_color\": \"#AABBCC #DDEEFF\" }"); |
| 438 EXPECT_EQ(manifest.theme_color, Manifest::kInvalidOrMissingThemeColor); |
| 439 EXPECT_EQ(1u, GetErrorCount()); |
| 440 EXPECT_EQ("Manifest parsing error: property 'theme_color' ignored, " |
| 441 "#AABBCC #DDEEFF is not a valid color.", |
| 442 errors()[0]); |
| 443 } |
| 444 |
| 445 // Accept CSS color keyword format. |
| 446 { |
| 447 Manifest manifest = ParseManifest("{ \"theme_color\": \"blue\" }"); |
| 448 EXPECT_EQ(manifest.theme_color, 0xFF0000FF); |
| 449 EXPECT_EQ(0u, GetErrorCount()); |
| 450 } |
| 451 |
| 452 // Accept CSS color keyword format. |
| 453 { |
| 454 Manifest manifest = ParseManifest("{ \"theme_color\": \"chartreuse\" }"); |
| 455 EXPECT_EQ(manifest.theme_color, 0xFF7FFF00); |
| 456 EXPECT_EQ(0u, GetErrorCount()); |
| 457 } |
| 458 |
| 459 // Accept CSS RGB format. |
| 460 { |
| 461 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FFF\" }"); |
| 462 EXPECT_EQ(manifest.theme_color, 0xFFFFFFFF); |
| 463 EXPECT_EQ(0u, GetErrorCount()); |
| 464 } |
| 465 |
| 466 // Accept CSS RGB format. |
| 467 { |
| 468 Manifest manifest = ParseManifest("{ \"theme_color\": \"#ABC\" }"); |
| 469 EXPECT_EQ(manifest.theme_color, 0xFFAABBCC); |
| 470 EXPECT_EQ(0u, GetErrorCount()); |
| 471 } |
| 472 |
| 473 // Accept CSS RRGGBB format. |
| 474 { |
| 475 Manifest manifest = ParseManifest("{ \"theme_color\": \"#FF0000\" }"); |
| 476 EXPECT_EQ(manifest.theme_color, 0xFFFF0000); |
| 477 EXPECT_EQ(0u, GetErrorCount()); |
| 478 } |
| 479 } |
| 480 |
353 TEST_F(ManifestParserTest, OrientationParserRules) { | 481 TEST_F(ManifestParserTest, OrientationParserRules) { |
354 // Smoke test. | 482 // Smoke test. |
355 { | 483 { |
356 Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }"); | 484 Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }"); |
357 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural); | 485 EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural); |
358 EXPECT_FALSE(manifest.IsEmpty()); | 486 EXPECT_FALSE(manifest.IsEmpty()); |
359 EXPECT_EQ(0u, GetErrorCount()); | 487 EXPECT_EQ(0u, GetErrorCount()); |
360 } | 488 } |
361 | 489 |
362 // Trim whitespaces. | 490 // Trim whitespaces. |
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); | 1135 Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }"); |
1008 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); | 1136 EXPECT_TRUE(manifest.gcm_sender_id.is_null()); |
1009 EXPECT_EQ(1u, GetErrorCount()); | 1137 EXPECT_EQ(1u, GetErrorCount()); |
1010 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," | 1138 EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored," |
1011 " type string expected.", | 1139 " type string expected.", |
1012 errors()[0]); | 1140 errors()[0]); |
1013 } | 1141 } |
1014 } | 1142 } |
1015 | 1143 |
1016 } // namespace content | 1144 } // namespace content |
OLD | NEW |