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 package org.chromium.distiller; | 5 package org.chromium.distiller; |
6 | 6 |
7 import com.google.gwt.dom.client.AnchorElement; | 7 import com.google.gwt.dom.client.AnchorElement; |
8 import com.google.gwt.dom.client.Element; | 8 import com.google.gwt.dom.client.Element; |
9 | 9 |
10 public class SchemaOrgParserAccessorTest extends DomDistillerJsTestCase { | 10 public class SchemaOrgParserAccessorTest extends DomDistillerJsTestCase { |
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
444 assertEquals(1, images.length); | 444 assertEquals(1, images.length); |
445 assertEquals(expectedImage, images[0].url); | 445 assertEquals(expectedImage, images[0].url); |
446 MarkupParser.Article article = parser.getArticle(); | 446 MarkupParser.Article article = parser.getArticle(); |
447 assertEquals(expectedDatePublished, article.publishedTime); | 447 assertEquals(expectedDatePublished, article.publishedTime); |
448 assertEquals("", article.expirationTime); | 448 assertEquals("", article.expirationTime); |
449 assertEquals(1, article.authors.length); | 449 assertEquals(1, article.authors.length); |
450 assertEquals(expectedAuthor, article.authors[0]); | 450 assertEquals(expectedAuthor, article.authors[0]); |
451 } | 451 } |
452 | 452 |
453 public void testSameItempropDifferentValues() { | 453 public void testSameItempropDifferentValues() { |
454 String expectedAuthor = "Author 1"; | 454 String expectedAuthor = "Author 1, Author 2"; |
455 String htmlStr = | 455 String htmlStr = |
456 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + | 456 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
457 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\"" + | 457 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\"" + |
458 " itemprop=\"author\">Authors: " + | 458 " itemprop=\"author\">Authors: " + |
459 "<span itemprop=\"name\">" + expectedAuthor + | 459 "<span itemprop=\"name\">Author 1" + |
460 "</span>" + | 460 "</span>" + |
461 "<span itemprop=\"name\">Author 2" + | 461 "<span itemprop=\"name\">Author 2" + |
462 "</span>" + | 462 "</span>" + |
463 "</div>" + | 463 "</div>" + |
464 "</div>"; | 464 "</div>"; |
465 | 465 |
466 Element rootDiv = TestUtil.createDiv(0); | 466 Element rootDiv = TestUtil.createDiv(0); |
467 rootDiv.setInnerHTML(htmlStr); | 467 rootDiv.setInnerHTML(htmlStr); |
468 mBody.appendChild(rootDiv); | 468 mBody.appendChild(rootDiv); |
469 | 469 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 | 534 |
535 parser = new SchemaOrgParserAccessor(mRoot); | 535 parser = new SchemaOrgParserAccessor(mRoot); |
536 assertEquals("", parser.getType()); | 536 assertEquals("", parser.getType()); |
537 assertEquals(expectedAuthor, parser.getAuthor()); | 537 assertEquals(expectedAuthor, parser.getAuthor()); |
538 assertEquals(null, parser.getArticle()); | 538 assertEquals(null, parser.getArticle()); |
539 | 539 |
540 // Remove anchor from parent, so that other testcases won't be affected. | 540 // Remove anchor from parent, so that other testcases won't be affected. |
541 link.removeFromParent(); | 541 link.removeFromParent(); |
542 } | 542 } |
543 | 543 |
544 public void testSingleRecipe() { | |
545 String htmlStr = | |
546 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Recipe\">" + | |
wychen
2016/03/14 22:58:42
Lots of duplicated text here. Maybe saving the com
| |
547 "<h1 itemprop=\"name\">Bat Rice Recipe</h1>" + | |
548 "<h2 itemprop=\"description\">" + | |
549 "Testing RECIPE with required recipe schema.org attributes" + | |
550 "</h2>" + | |
551 "<a itemprop=\"url\" " + | |
552 "href=\"http://dummy/test_article_with_embedded_items.html\" >" + | |
553 "test results" + "</a>" + | |
554 "<img itemprop=\"image\" " + | |
555 "src=\"http://dummy/test_article_with_embedded_items.jpeg\" />" + | |
556 "<div itemprop=\"author\">Whoever authored</div>" + | |
557 "<div itemprop=\"recipeYield\" content=\"4\">" + | |
558 "4 servings</div>" + | |
559 "<time itemprop=\"prepTime\" datetime=\"PT15M\">" + | |
560 "15 Minutes" + "</time>" + | |
561 "<time itemprop=\"cookTime\" datetime=\"PT45M\">" + | |
562 "45 Minutes" + "</time>" + | |
563 "<time itemprop=\"totalTime\" datetime=\"PT9H\">" + | |
564 "9 Hours</time>" + | |
565 "<span itemprop=\"ingredients\">4 bats</span>" + | |
566 "<span itemprop=\"ingredients\">1/4 milk</span>" + | |
567 "<span itemprop=\"ingredients\">1 rice</span>" + | |
568 "<span itemprop=\"ingredients\">1 salt</span>" + | |
569 "<span itemprop=\"recipeInstructions\">" + | |
570 "Mix everything and wait for cook</span>" + | |
571 "</div>"; | |
572 | |
573 Element rootDiv = TestUtil.createDiv(0); | |
574 rootDiv.setInnerHTML(htmlStr); | |
575 mBody.appendChild(rootDiv); | |
576 SchemaOrgParserAccessor parser = new SchemaOrgParserAccessor(mRoot); | |
577 String result = parser.getStructuredData(); | |
578 | |
579 String expected = | |
580 "<img src='http://dummy/test_article_with_embedded_items.jpeg' />" + | |
581 "<p>Author: Whoever authored</p><p>Description: " + | |
582 "Testing RECIPE with required recipe schema.org attributes</p>" + | |
583 "<p>Serves: 4 servings</p>" + | |
584 "<p>Prep time: 15 minute(s)</p>" + | |
585 "<p>Cook time: 45 minute(s)</p>" + | |
586 "<p>Total time: 9 hour(s)</p>" + | |
587 "Ingredients: <ul><li>4 bats</li>" + | |
588 "<li>1/4 milk</li>" + | |
589 "<li>1 rice</li>" + | |
590 "<li>1 salt</li>" + | |
591 "</ul><p>Instructions: <br />" + | |
592 "Mix everything and wait for cook</p>"; | |
593 assertEquals(expected, result); | |
594 } | |
595 | |
596 public void testSingleRecipeWithEmbeddedPerson() { | |
597 String htmlStr = | |
598 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Recipe\">" + | |
599 "<h1 itemprop=\"name\">Bat Rice Recipe</h1>" + | |
600 "<h2 itemprop=\"description\">" + | |
601 "Testing RECIPE with required recipe schema.org attributes</ h2>" + | |
602 "<a itemprop=\"url\" href=\"" + | |
603 "http://dummy/test_article_with_embedded_items.html\">" + | |
604 "test results" + "</a>" + | |
605 "<img itemprop=\"image\" " + | |
606 "src=\"http://dummy/test_article_with_embedded_items.jpeg\" />" + | |
607 "<span itemprop=\"author\" " + | |
608 "itemscope itemtype=\"http://schema.org/Person\">" + | |
609 "<span itemprop=\"givenName\">Guest</span>" + | |
610 "<span itemprop=\"familyName\">Smith</span>" + | |
611 "</span>" + | |
612 "<div itemprop=\"recipeYield\" content=\"4\">4 servings</div>" + | |
613 "<time itemprop=\"prepTime\" datetime=\"PT15M\">1" + | |
614 "5 Minutes" + "</time>" + | |
615 "<time itemprop=\"cookTime\" datetime=\"PT45M\">" + | |
616 "45 Minutes" + "</time>" + | |
617 "<time itemprop=\"totalTime\" datetime=\"PT9H\">" + | |
618 "9 Hours</time>" + | |
619 "<span itemprop=\"ingredients\">4 bats</span>" + | |
620 "<span itemprop=\"ingredients\">1/4 milk</span>" + | |
621 "<span itemprop=\"ingredients\">1 rice</span>" + | |
622 "<span itemprop=\"ingredients\">1 salt</span>" + | |
623 "<span itemprop=\"recipeInstructions\">" + | |
624 "Mix everything and wait for cook</span>" + | |
625 "</div>"; | |
626 | |
627 Element rootDiv = TestUtil.createDiv(0); | |
628 rootDiv.setInnerHTML(htmlStr); | |
629 mBody.appendChild(rootDiv); | |
630 SchemaOrgParserAccessor parser = new SchemaOrgParserAccessor(mRoot); | |
631 String result = parser.getStructuredData(); | |
632 | |
633 String expected = | |
634 "<img src='http://dummy/test_article_with_embedded_items.jpeg' />" + | |
635 "<p>Author: <span>Guest Smith</span></p>" + | |
636 "<p>Description: " + | |
637 "Testing RECIPE with required recipe schema.org attributes</p>" + | |
638 "<p>Serves: 4 servings</p>" + | |
639 "<p>Prep time: 15 minute(s)</p>" + | |
640 "<p>Cook time: 45 minute(s)</p>" + | |
641 "<p>Total time: 9 hour(s)</p>" + | |
642 "Ingredients: <ul><li>4 bats</li>" + | |
643 "<li>1/4 milk</li>" + | |
644 "<li>1 rice</li>" + | |
645 "<li>1 salt</li>" + | |
646 "</ul><p>Instructions: <br />" + | |
647 "Mix everything and wait for cook</p>"; | |
648 assertEquals(expected, result); | |
649 } | |
650 | |
651 public void testMoreThanOneSchemaOrg() { | |
652 String htmlStr = | |
653 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Recipe\">" + | |
654 "<h1 itemprop=\"name\">Bat Rice Recipe</h1>" + | |
655 "<h2 itemprop=\"description\">" + | |
656 "Testing RECIPE with required recipe schema.org attributes" + | |
657 "</h2>" + | |
658 "<a itemprop=\"url\" href=\"" + | |
659 "http://dummy/test_article_with_embedded_items.html\">" + | |
660 "test results" + "</a>" + | |
661 "<img itemprop=\"image\" src=\"" + | |
662 "http://dummy/test_article_with_embedded_items.jpeg\" />" + | |
663 "<div itemprop=\"author\">Whoever authored</div>" + | |
664 "<div itemprop=\"recipeYield\" content=\"4\">" + | |
665 "4 servings</div>" + | |
666 "<time itemprop=\"prepTime\" datetime=\"PT15M\">" + | |
667 "15 Minutes" + "</time>" + | |
668 "<time itemprop=\"cookTime\" datetime=\"PT45M\">" + | |
669 "45 Minutes" + "</time>" + | |
670 "<time itemprop=\"totalTime\" datetime=\"PT9H\">" + | |
671 "9 Hours</time>" + | |
672 "<span itemprop=\"ingredients\">4 bats</span>" + | |
673 "<span itemprop=\"ingredients\">1/4 milk</span>" + | |
674 "<span itemprop=\"ingredients\">1 rice</span>" + | |
675 "<span itemprop=\"ingredients\">1 salt</span>" + | |
676 "<span itemprop=\"recipeInstructions\">" + | |
677 "Mix everything and wait for cook</span>" + | |
678 "</div>" + | |
679 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + | |
680 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\" " + | |
681 "itemprop=\"author\">Creator: " + | |
682 "<span itemprop=\"name\"> User </span>" + | |
683 "</div>" + | |
684 "</div>"; | |
685 | |
686 Element rootDiv = TestUtil.createDiv(0); | |
687 rootDiv.setInnerHTML(htmlStr); | |
688 mBody.appendChild(rootDiv); | |
689 SchemaOrgParserAccessor parser = new SchemaOrgParserAccessor(mRoot); | |
690 String result = parser.getStructuredData(); | |
691 | |
692 assertEquals("", result); | |
693 } | |
694 | |
695 public void testMultipleSchemaOrgWithMainEntity() { | |
696 String htmlStr = | |
697 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Recipe\">" + | |
698 "<meta itemprop=\"mainEntityOfPage\" content=\"true\">" + | |
699 "<h1 itemprop=\"name\">Bat Rice Recipe</h1>" + | |
700 "<h2 itemprop=\"description\">" + | |
701 "Testing RECIPE with required recipe schema.org attributes" + | |
702 "</h2>" + | |
703 "<a itemprop=\"url\" href=\"" + | |
704 "http://dummy/test_article_with_embedded_items.html\">" + | |
705 "test results" + "</a>" + | |
706 "<img itemprop=\"image\" src=\"" + | |
707 "http://dummy/test_article_with_embedded_items.jpeg\" />" + | |
708 "<div itemprop=\"author\">Whoever authored</div>" + | |
709 "<div itemprop=\"recipeYield\" content=\"4\">" + | |
710 "4 servings</div>" + | |
711 "<time itemprop=\"prepTime\" datetime=\"PT15M\">" + | |
712 "15 Minutes" + "</time>" + | |
713 "<time itemprop=\"cookTime\" datetime=\"PT45M\">" + | |
714 "45 Minutes" + "</time>" + | |
715 "<time itemprop=\"totalTime\" datetime=\"PT9H\">" + | |
716 "9 Hours</time>" + | |
717 "<span itemprop=\"ingredients\">4 bats</span>" + | |
718 "<span itemprop=\"ingredients\">1/4 milk</span>" + | |
719 "<span itemprop=\"ingredients\">1 rice</span>" + | |
720 "<span itemprop=\"ingredients\">1 salt</span>" + | |
721 "<span itemprop=\"recipeInstructions\">" + | |
722 "Mix everything and wait for cook</span>" + | |
723 "</div>" + | |
724 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + | |
725 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\" " + | |
726 "itemprop=\"author\">Creator: " + | |
727 "<span itemprop=\"name\"> User </span>" + | |
728 "</div>" + | |
729 "</div>"; | |
730 | |
731 Element rootDiv = TestUtil.createDiv(0); | |
732 rootDiv.setInnerHTML(htmlStr); | |
733 mBody.appendChild(rootDiv); | |
734 SchemaOrgParserAccessor parser = new SchemaOrgParserAccessor(mRoot); | |
735 String result = parser.getStructuredData(); | |
736 | |
737 String expected = | |
738 "<img src='http://dummy/test_article_with_embedded_items.jpeg' />" + | |
739 "<p>Author: Whoever authored</p><p>Description: " + | |
740 "Testing RECIPE with required recipe schema.org attributes" + | |
741 "</p>" + | |
742 "<p>Serves: 4 servings</p>" + | |
743 "<p>Prep time: 15 minute(s)</p>" + | |
744 "<p>Cook time: 45 minute(s)</p>" + | |
745 "<p>Total time: 9 hour(s)</p>" + | |
746 "Ingredients: <ul><li>4 bats</li>" + | |
747 "<li>1/4 milk</li>" + | |
748 "<li>1 rice</li>" + | |
749 "<li>1 salt</li>" + | |
750 "</ul><p>Instructions: <br />" + | |
751 "Mix everything and wait for cook</p>"; | |
752 assertEquals(expected, result); | |
753 } | |
754 | |
544 private void setItemScopeAndType(Element e, String type) { | 755 private void setItemScopeAndType(Element e, String type) { |
545 e.setAttribute("ITEMSCOPE", ""); | 756 e.setAttribute("ITEMSCOPE", ""); |
546 e.setAttribute("ITEMTYPE", "http://schema.org/" + type); | 757 e.setAttribute("ITEMTYPE", "http://schema.org/" + type); |
547 } | 758 } |
548 | 759 |
549 private void setItemProp(Element e, String name) { | 760 private void setItemProp(Element e, String name) { |
550 e.setAttribute("itemprop", name); | 761 e.setAttribute("itemprop", name); |
551 } | 762 } |
552 } | 763 } |
OLD | NEW |