1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/manifest/manifest_parser.h"
#include "base/strings/string_util.h"
#include "content/public/common/manifest.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
class ManifestParserTest : public testing::Test {
protected:
ManifestParserTest() {}
~ManifestParserTest() override {}
Manifest ParseManifestWithURLs(const base::StringPiece& data,
const GURL& document_url,
const GURL& manifest_url) {
ManifestParser parser(data, document_url, manifest_url);
parser.Parse();
errors_ = parser.errors();
return parser.manifest();
}
Manifest ParseManifest(const base::StringPiece& data) {
return ParseManifestWithURLs(
data, default_document_url, default_manifest_url);
}
const std::vector<std::string>& errors() const {
return errors_;
}
unsigned int GetErrorCount() const {
return errors_.size();
}
static const GURL default_document_url;
static const GURL default_manifest_url;
private:
std::vector<std::string> errors_;
DISALLOW_COPY_AND_ASSIGN(ManifestParserTest);
};
const GURL ManifestParserTest::default_document_url(
"http://foo.com/index.html");
const GURL ManifestParserTest::default_manifest_url(
"http://foo.com/manifest.json");
TEST_F(ManifestParserTest, CrashTest) {
// Passing temporary variables should not crash.
ManifestParser parser("{\"start_url\": \"/\"}",
GURL("http://example.com"),
GURL("http://example.com"));
parser.Parse();
// .Parse() should have been call without crashing and succeeded.
EXPECT_EQ(0u, parser.errors().size());
EXPECT_FALSE(parser.manifest().IsEmpty());
}
TEST_F(ManifestParserTest, EmptyStringNull) {
Manifest manifest = ParseManifest("");
// This Manifest is not a valid JSON object, it's a parsing error.
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: Line: 1, column: 1, Unexpected token.",
errors()[0]);
// A parsing error is equivalent to an empty manifest.
ASSERT_TRUE(manifest.IsEmpty());
ASSERT_TRUE(manifest.name.is_null());
ASSERT_TRUE(manifest.short_name.is_null());
ASSERT_TRUE(manifest.start_url.is_empty());
ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED);
ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
ASSERT_TRUE(manifest.gcm_sender_id.is_null());
ASSERT_FALSE(manifest.gcm_user_visible_only);
}
TEST_F(ManifestParserTest, ValidNoContentParses) {
Manifest manifest = ParseManifest("{}");
// Empty Manifest is not a parsing error.
EXPECT_EQ(0u, GetErrorCount());
// Check that all the fields are null in that case.
ASSERT_TRUE(manifest.IsEmpty());
ASSERT_TRUE(manifest.name.is_null());
ASSERT_TRUE(manifest.short_name.is_null());
ASSERT_TRUE(manifest.start_url.is_empty());
ASSERT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED);
ASSERT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
ASSERT_TRUE(manifest.gcm_sender_id.is_null());
ASSERT_FALSE(manifest.gcm_user_visible_only);
}
TEST_F(ManifestParserTest, MultipleErrorsReporting) {
Manifest manifest = ParseManifest("{ \"name\": 42, \"short_name\": 4,"
"\"orientation\": {}, \"display\": \"foo\", \"start_url\": null,"
"\"icons\": {}, \"gcm_user_visible_only\": 42 }");
EXPECT_EQ(7u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'name' ignored,"
" type string expected.",
errors()[0]);
EXPECT_EQ("Manifest parsing error: property 'short_name' ignored,"
" type string expected.",
errors()[1]);
EXPECT_EQ("Manifest parsing error: property 'start_url' ignored,"
" type string expected.",
errors()[2]);
EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.",
errors()[3]);
EXPECT_EQ("Manifest parsing error: property 'orientation' ignored,"
" type string expected.",
errors()[4]);
EXPECT_EQ("Manifest parsing error: property 'icons' ignored, "
"type array expected.",
errors()[5]);
EXPECT_EQ("Manifest parsing error: property 'gcm_user_visible_only' ignored, "
"type boolean expected.",
errors()[6]);
}
TEST_F(ManifestParserTest, NameParseRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"name\": \"foo\" }");
ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo"));
ASSERT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"name\": \" foo \" }");
ASSERT_TRUE(EqualsASCII(manifest.name.string(), "foo"));
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"name\": {} }");
ASSERT_TRUE(manifest.name.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'name' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"name\": 42 }");
ASSERT_TRUE(manifest.name.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'name' ignored,"
" type string expected.",
errors()[0]);
}
}
TEST_F(ManifestParserTest, ShortNameParseRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"short_name\": \"foo\" }");
ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo"));
ASSERT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"short_name\": \" foo \" }");
ASSERT_TRUE(EqualsASCII(manifest.short_name.string(), "foo"));
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"short_name\": {} }");
ASSERT_TRUE(manifest.short_name.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'short_name' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"short_name\": 42 }");
ASSERT_TRUE(manifest.short_name.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'short_name' ignored,"
" type string expected.",
errors()[0]);
}
}
TEST_F(ManifestParserTest, StartURLParseRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"start_url\": \"land.html\" }");
ASSERT_EQ(manifest.start_url.spec(),
default_document_url.Resolve("land.html").spec());
ASSERT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Whitespaces.
{
Manifest manifest = ParseManifest("{ \"start_url\": \" land.html \" }");
ASSERT_EQ(manifest.start_url.spec(),
default_document_url.Resolve("land.html").spec());
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if property isn't a string.
{
Manifest manifest = ParseManifest("{ \"start_url\": {} }");
ASSERT_TRUE(manifest.start_url.is_empty());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'start_url' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if property isn't a string.
{
Manifest manifest = ParseManifest("{ \"start_url\": 42 }");
ASSERT_TRUE(manifest.start_url.is_empty());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'start_url' ignored,"
" type string expected.",
errors()[0]);
}
// Absolute start_url, same origin with document.
{
Manifest manifest =
ParseManifestWithURLs("{ \"start_url\": \"http://foo.com/land.html\" }",
GURL("http://foo.com/manifest.json"),
GURL("http://foo.com/index.html"));
ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/land.html");
EXPECT_EQ(0u, GetErrorCount());
}
// Absolute start_url, cross origin with document.
{
Manifest manifest =
ParseManifestWithURLs("{ \"start_url\": \"http://bar.com/land.html\" }",
GURL("http://foo.com/manifest.json"),
GURL("http://foo.com/index.html"));
ASSERT_TRUE(manifest.start_url.is_empty());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'start_url' ignored, should "
"be same origin as document.",
errors()[0]);
}
// Resolving has to happen based on the manifest_url.
{
Manifest manifest =
ParseManifestWithURLs("{ \"start_url\": \"land.html\" }",
GURL("http://foo.com/landing/manifest.json"),
GURL("http://foo.com/index.html"));
ASSERT_EQ(manifest.start_url.spec(), "http://foo.com/landing/land.html");
EXPECT_EQ(0u, GetErrorCount());
}
}
TEST_F(ManifestParserTest, DisplayParserRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"display\": \"browser\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_BROWSER);
EXPECT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"display\": \" browser \" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_BROWSER);
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"display\": {} }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'display' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"display\": 42 }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'display' ignored,"
" type string expected.",
errors()[0]);
}
// Parse fails if string isn't known.
{
Manifest manifest = ParseManifest("{ \"display\": \"browser_something\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_UNSPECIFIED);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: unknown 'display' value ignored.",
errors()[0]);
}
// Accept 'fullscreen'.
{
Manifest manifest = ParseManifest("{ \"display\": \"fullscreen\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_FULLSCREEN);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'fullscreen'.
{
Manifest manifest = ParseManifest("{ \"display\": \"standalone\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_STANDALONE);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'minimal-ui'.
{
Manifest manifest = ParseManifest("{ \"display\": \"minimal-ui\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_MINIMAL_UI);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'browser'.
{
Manifest manifest = ParseManifest("{ \"display\": \"browser\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_BROWSER);
EXPECT_EQ(0u, GetErrorCount());
}
// Case insensitive.
{
Manifest manifest = ParseManifest("{ \"display\": \"BROWSER\" }");
EXPECT_EQ(manifest.display, Manifest::DISPLAY_MODE_BROWSER);
EXPECT_EQ(0u, GetErrorCount());
}
}
TEST_F(ManifestParserTest, OrientationParserRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural);
EXPECT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural);
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"orientation\": {} }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'orientation' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if name isn't a string.
{
Manifest manifest = ParseManifest("{ \"orientation\": 42 }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'orientation' ignored,"
" type string expected.",
errors()[0]);
}
// Parse fails if string isn't known.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"naturalish\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockDefault);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: unknown 'orientation' value ignored.",
errors()[0]);
}
// Accept 'any'.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"any\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockAny);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'natural'.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"natural\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockNatural);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'landscape'.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"landscape\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockLandscape);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'landscape-primary'.
{
Manifest manifest =
ParseManifest("{ \"orientation\": \"landscape-primary\" }");
EXPECT_EQ(manifest.orientation,
blink::WebScreenOrientationLockLandscapePrimary);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'landscape-secondary'.
{
Manifest manifest =
ParseManifest("{ \"orientation\": \"landscape-secondary\" }");
EXPECT_EQ(manifest.orientation,
blink::WebScreenOrientationLockLandscapeSecondary);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'portrait'.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"portrait\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockPortrait);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'portrait-primary'.
{
Manifest manifest =
ParseManifest("{ \"orientation\": \"portrait-primary\" }");
EXPECT_EQ(manifest.orientation,
blink::WebScreenOrientationLockPortraitPrimary);
EXPECT_EQ(0u, GetErrorCount());
}
// Accept 'portrait-secondary'.
{
Manifest manifest =
ParseManifest("{ \"orientation\": \"portrait-secondary\" }");
EXPECT_EQ(manifest.orientation,
blink::WebScreenOrientationLockPortraitSecondary);
EXPECT_EQ(0u, GetErrorCount());
}
// Case insensitive.
{
Manifest manifest = ParseManifest("{ \"orientation\": \"LANDSCAPE\" }");
EXPECT_EQ(manifest.orientation, blink::WebScreenOrientationLockLandscape);
EXPECT_EQ(0u, GetErrorCount());
}
}
TEST_F(ManifestParserTest, IconsParseRules) {
// Smoke test: if no icon, empty list.
{
Manifest manifest = ParseManifest("{ \"icons\": [] }");
EXPECT_EQ(manifest.icons.size(), 0u);
EXPECT_TRUE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Smoke test: if empty icon, empty list.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {} ] }");
EXPECT_EQ(manifest.icons.size(), 0u);
EXPECT_TRUE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Smoke test: icon with invalid src, empty list.
{
Manifest manifest = ParseManifest("{ \"icons\": [ { \"icons\": [] } ] }");
EXPECT_EQ(manifest.icons.size(), 0u);
EXPECT_TRUE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Smoke test: if icon with empty src, it will be present in the list.
{
Manifest manifest = ParseManifest("{ \"icons\": [ { \"src\": \"\" } ] }");
EXPECT_EQ(manifest.icons.size(), 1u);
EXPECT_EQ(manifest.icons[0].src.spec(), "http://foo.com/index.html");
EXPECT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
// Smoke test: if one icons with valid src, it will be present in the list.
{
Manifest manifest =
ParseManifest("{ \"icons\": [{ \"src\": \"foo.jpg\" }] }");
EXPECT_EQ(manifest.icons.size(), 1u);
EXPECT_EQ(manifest.icons[0].src.spec(), "http://foo.com/foo.jpg");
EXPECT_FALSE(manifest.IsEmpty());
EXPECT_EQ(0u, GetErrorCount());
}
}
TEST_F(ManifestParserTest, IconSrcParseRules) {
// Smoke test.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"foo.png\" } ] }");
EXPECT_EQ(manifest.icons[0].src.spec(),
default_document_url.Resolve("foo.png").spec());
EXPECT_EQ(0u, GetErrorCount());
}
// Whitespaces.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \" foo.png \" } ] }");
EXPECT_EQ(manifest.icons[0].src.spec(),
default_document_url.Resolve("foo.png").spec());
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if property isn't a string.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": {} } ] }");
EXPECT_TRUE(manifest.icons.empty());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'src' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if property isn't a string.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": 42 } ] }");
EXPECT_TRUE(manifest.icons.empty());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'src' ignored,"
" type string expected.",
errors()[0]);
}
// Resolving has to happen based on the document_url.
{
Manifest manifest = ParseManifestWithURLs(
"{ \"icons\": [ {\"src\": \"icons/foo.png\" } ] }",
GURL("http://foo.com/landing/index.html"),
default_manifest_url);
EXPECT_EQ(manifest.icons[0].src.spec(),
"http://foo.com/landing/icons/foo.png");
EXPECT_EQ(0u, GetErrorCount());
}
}
TEST_F(ManifestParserTest, IconTypeParseRules) {
// Smoke test.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": \"foo\" } ] }");
EXPECT_TRUE(EqualsASCII(manifest.icons[0].type.string(), "foo"));
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
" \"type\": \" foo \" } ] }");
EXPECT_TRUE(EqualsASCII(manifest.icons[0].type.string(), "foo"));
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if property isn't a string.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": {} } ] }");
EXPECT_TRUE(manifest.icons[0].type.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'type' ignored,"
" type string expected.",
errors()[0]);
}
// Don't parse if property isn't a string.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"type\": 42 } ] }");
EXPECT_TRUE(manifest.icons[0].type.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'type' ignored,"
" type string expected.",
errors()[0]);
}
}
TEST_F(ManifestParserTest, IconDensityParseRules) {
// Smoke test.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 42 } ] }");
EXPECT_EQ(manifest.icons[0].density, 42);
EXPECT_EQ(0u, GetErrorCount());
}
// Decimal value.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 2.5 } ] }");
EXPECT_EQ(manifest.icons[0].density, 2.5);
EXPECT_EQ(0u, GetErrorCount());
}
// Parse fail if it isn't a float.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": {} } ] }");
EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
"must be float greater than 0.",
errors()[0]);
}
// Parse fail if it isn't a float.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\":\"2\" } ] }");
EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
"must be float greater than 0.",
errors()[0]);
}
// Edge case: 1.0.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 1.00 } ] }");
EXPECT_EQ(manifest.icons[0].density, 1);
EXPECT_EQ(0u, GetErrorCount());
}
// Edge case: values between 0.0 and 1.0 are allowed.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 0.42 } ] }");
EXPECT_EQ(manifest.icons[0].density, 0.42);
EXPECT_EQ(0u, GetErrorCount());
}
// 0 is an invalid value.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": 0.0 } ] }");
EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
"must be float greater than 0.",
errors()[0]);
}
// Negative values are invalid.
{
Manifest manifest =
ParseManifest("{ \"icons\": [ {\"src\": \"\", \"density\": -2.5 } ] }");
EXPECT_EQ(manifest.icons[0].density, Manifest::Icon::kDefaultDensity);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: icon 'density' ignored, "
"must be float greater than 0.",
errors()[0]);
}
}
TEST_F(ManifestParserTest, IconSizesParseRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"42x42\" } ] }");
EXPECT_EQ(manifest.icons[0].sizes.size(), 1u);
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \" 42x42 \" } ] }");
EXPECT_EQ(manifest.icons[0].sizes.size(), 1u);
EXPECT_EQ(0u, GetErrorCount());
}
// Ignore sizes if property isn't a string.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": {} } ] }");
EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'sizes' ignored,"
" type string expected.",
errors()[0]);
}
// Ignore sizes if property isn't a string.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": 42 } ] }");
EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'sizes' ignored,"
" type string expected.",
errors()[0]);
}
// Smoke test: value correctly parsed.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"42x42 48x48\" } ] }");
EXPECT_EQ(manifest.icons[0].sizes[0], gfx::Size(42, 42));
EXPECT_EQ(manifest.icons[0].sizes[1], gfx::Size(48, 48));
EXPECT_EQ(0u, GetErrorCount());
}
// <WIDTH>'x'<HEIGHT> and <WIDTH>'X'<HEIGHT> are equivalent.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"42X42 48X48\" } ] }");
EXPECT_EQ(manifest.icons[0].sizes[0], gfx::Size(42, 42));
EXPECT_EQ(manifest.icons[0].sizes[1], gfx::Size(48, 48));
EXPECT_EQ(0u, GetErrorCount());
}
// Twice the same value is parsed twice.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"42X42 42x42\" } ] }");
EXPECT_EQ(manifest.icons[0].sizes[0], gfx::Size(42, 42));
EXPECT_EQ(manifest.icons[0].sizes[1], gfx::Size(42, 42));
EXPECT_EQ(0u, GetErrorCount());
}
// Width or height can't start with 0.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"004X007 042x00\" } ] }");
EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: found icon with no valid size.",
errors()[0]);
}
// Width and height MUST contain digits.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"e4X1.0 55ax1e10\" } ] }");
EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: found icon with no valid size.",
errors()[0]);
}
// 'any' is correctly parsed and transformed to gfx::Size(0,0).
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"any AnY ANY aNy\" } ] }");
gfx::Size any = gfx::Size(0, 0);
EXPECT_EQ(manifest.icons[0].sizes.size(), 4u);
EXPECT_EQ(manifest.icons[0].sizes[0], any);
EXPECT_EQ(manifest.icons[0].sizes[1], any);
EXPECT_EQ(manifest.icons[0].sizes[2], any);
EXPECT_EQ(manifest.icons[0].sizes[3], any);
EXPECT_EQ(0u, GetErrorCount());
}
// Some invalid width/height combinations.
{
Manifest manifest = ParseManifest("{ \"icons\": [ {\"src\": \"\","
"\"sizes\": \"x 40xx 1x2x3 x42 42xx42\" } ] }");
gfx::Size any = gfx::Size(0, 0);
EXPECT_EQ(manifest.icons[0].sizes.size(), 0u);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: found icon with no valid size.",
errors()[0]);
}
}
TEST_F(ManifestParserTest, GCMSenderIDParseRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \"foo\" }");
EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo"));
EXPECT_EQ(0u, GetErrorCount());
}
// Trim whitespaces.
{
Manifest manifest = ParseManifest("{ \"gcm_sender_id\": \" foo \" }");
EXPECT_TRUE(EqualsASCII(manifest.gcm_sender_id.string(), "foo"));
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if the property isn't a string.
{
Manifest manifest = ParseManifest("{ \"gcm_sender_id\": {} }");
EXPECT_TRUE(manifest.gcm_sender_id.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored,"
" type string expected.",
errors()[0]);
}
{
Manifest manifest = ParseManifest("{ \"gcm_sender_id\": 42 }");
EXPECT_TRUE(manifest.gcm_sender_id.is_null());
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ("Manifest parsing error: property 'gcm_sender_id' ignored,"
" type string expected.",
errors()[0]);
}
}
TEST_F(ManifestParserTest, GCMUserVisibleOnlyParseRules) {
// Smoke test.
{
Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": true }");
EXPECT_TRUE(manifest.gcm_user_visible_only);
EXPECT_EQ(0u, GetErrorCount());
}
// Don't parse if the property isn't a boolean.
{
Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": {} }");
EXPECT_FALSE(manifest.gcm_user_visible_only);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ(
"Manifest parsing error: property 'gcm_user_visible_only' ignored,"
" type boolean expected.",
errors()[0]);
}
{
Manifest manifest = ParseManifest(
"{ \"gcm_user_visible_only\": \"true\" }");
EXPECT_FALSE(manifest.gcm_user_visible_only);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ(
"Manifest parsing error: property 'gcm_user_visible_only' ignored,"
" type boolean expected.",
errors()[0]);
}
{
Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": 1 }");
EXPECT_FALSE(manifest.gcm_user_visible_only);
EXPECT_EQ(1u, GetErrorCount());
EXPECT_EQ(
"Manifest parsing error: property 'gcm_user_visible_only' ignored,"
" type boolean expected.",
errors()[0]);
}
// "False" should set the boolean false without throwing errors.
{
Manifest manifest = ParseManifest("{ \"gcm_user_visible_only\": false }");
EXPECT_FALSE(manifest.gcm_user_visible_only);
EXPECT_EQ(0u, GetErrorCount());
}
}
} // namespace content
|