Index: test/com/dom_distiller/client/SchemaOrgParserTest.java |
diff --git a/test/com/dom_distiller/client/SchemaOrgParserTest.java b/test/com/dom_distiller/client/SchemaOrgParserTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..169d90b253e6e2c13c0c464eca67e787d6d087f8 |
--- /dev/null |
+++ b/test/com/dom_distiller/client/SchemaOrgParserTest.java |
@@ -0,0 +1,574 @@ |
+// 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. |
+ |
+package com.dom_distiller.client; |
+ |
+import com.google.gwt.dom.client.Document; |
+import com.google.gwt.dom.client.AnchorElement; |
+import com.google.gwt.dom.client.Element; |
+import com.google.gwt.dom.client.NodeList; |
+ |
+import com.google.gwt.junit.client.GWTTestCase; |
+ |
+public class SchemaOrgParserTest extends GWTTestCase { |
+ @Override |
+ public String getModuleName() { |
+ return "com.dom_distiller.DomDistillerJUnit"; |
+ } |
+ |
+ public void testImageWithEmbeddedPublisher() { |
+ String expectedTitle = "Testcase for IMAGE"; |
+ String expectedDescription = "Testing IMAGE with embedded publisher"; |
+ String expectedUrl = "http://test_image_with_embedded_item.html"; |
+ String expectedPublisher = "Whatever Image Incorporated"; |
+ String expectedCopyrightYear = "1999-2022"; |
+ String expectedCopyrightHolder = "Whoever Image Copyrighted"; |
+ String expectedFormat = "jpeg"; |
+ String expectedCaption = "A test for IMAGE with embedded publisher"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/ImageObject\">" + |
+ "<h1 itemprop=\"headline\">" + expectedTitle + |
+ "</h1>" + |
+ "<h2 itemprop=\"description\">" + expectedDescription + |
+ "</h2>" + |
+ "<a itemprop=\"contentUrl\" href=\"" + expectedUrl + "\">test results" + |
+ "</a>" + |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/Organization\"" + |
+ " itemprop=\"publisher\">Publisher: " + |
+ "<span itemprop=\"name\">" + expectedPublisher + |
+ "</span>" + |
+ "</div>" + |
+ "<div id=\"3\">" + |
+ "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear + |
+ "</span>" + |
+ "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder + |
+ "</span>" + |
+ "</div>" + |
+ "<span itemprop=\"encodingFormat\">" + expectedFormat + |
+ "</span>" + |
+ "<span itemprop=\"caption\">" + expectedCaption + |
+ "</span>" + |
+ "<meta itemprop=\"representativeOfPage\" content=\"true\">" + |
+ "<meta itemprop=\"width\" content=\"600\">" + |
+ "<meta itemprop=\"height\" content=\"400\">" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("IMAGE", parser.getType()); |
+ assertEquals(expectedTitle, parser.getTitle()); |
+ assertEquals(expectedDescription, parser.getDescription()); |
+ assertEquals("", parser.getUrl()); |
+ assertEquals(expectedPublisher, parser.getPublisher()); |
+ assertEquals(null, parser.getArticle()); |
+ assertEquals("", parser.getAuthor()); |
+ assertEquals("Copyright " + expectedCopyrightYear + " " + expectedCopyrightHolder, |
+ parser.getCopyright()); |
+ MarkupParser.Image[] images = parser.getImages(); |
+ assertEquals(1, images.length); |
+ MarkupParser.Image image = images[0]; |
+ assertEquals(expectedUrl, image.image); |
+ assertEquals(expectedUrl, image.url); |
+ assertEquals(null, image.secureUrl); |
+ assertEquals(expectedFormat, image.type); |
+ assertEquals(expectedCaption, image.caption); |
+ assertEquals(600, image.width); |
+ assertEquals(400, image.height); |
+ } |
+ |
+ public void test2Images() { |
+ String expectedTitle1 = "Testcase for 1st IMAGE"; |
+ String expectedDescription1 = "Testing 1st IMAGE"; |
+ String expectedUrl1 = "http://test_1st image.html"; |
+ String expectedPublisher1 = "Whatever 1st Image Incorporated"; |
+ String expectedCopyrightYear1 = "1000-1999"; |
+ String expectedCopyrightHolder1 = "Whoever 1st Image Copyrighted"; |
+ String expectedFormat1 = "jpeg"; |
+ String expectedCaption1 = "A test for 1st IMAGE"; |
+ String expectedTitle2 = "Testcase for 2nd IMAGE"; |
+ String expectedDescription2 = "Testing 2nd IMAGE"; |
+ String expectedUrl2 = "http://test_2nd image.html"; |
+ String expectedPublisher2 = "Whatever 2nd Image Incorporated"; |
+ String expectedCopyrightYear2 = "2000-2999"; |
+ String expectedCopyrightHolder2 = "Whoever 2nd Image Copyrighted"; |
+ String expectedFormat2 = "gif"; |
+ String expectedCaption2 = "A test for 2nd IMAGE"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/ImageObject\">" + |
+ "<h1 itemprop=\"headline\">" + expectedTitle1 + |
+ "</h1>" + |
+ "<h2 itemprop=\"description\">" + expectedDescription1 + |
+ "</h2>" + |
+ "<a itemprop=\"contentUrl\" href=\"" + expectedUrl1 + "\">1st test results" + |
+ "</a>" + |
+ "<div id=\"2\" itemprop=\"publisher\">" + expectedPublisher1 + |
+ "</div>" + |
+ "<div id=\"3\">" + |
+ "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear1 + |
+ "</span>" + |
+ "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder1 + |
+ "</span>" + |
+ "</div>" + |
+ "<span itemprop=\"encodingFormat\">" + expectedFormat1 + |
+ "</span>" + |
+ "<span itemprop=\"caption\">" + expectedCaption1 + |
+ "</span>" + |
+ "<meta itemprop=\"representativeOfPage\" content=\"false\">" + |
+ "<meta itemprop=\"width\" content=\"400\">" + |
+ "<meta itemprop=\"height\" content=\"300\">" + |
+ "</div>" + |
+ "<div id=\"4\" itemscope itemtype=\"http://schema.org/ImageObject\">" + |
+ "<h3 itemprop=\"headline\">" + expectedTitle2 + |
+ "</h3>" + |
+ "<h4 itemprop=\"description\">" + expectedDescription2 + |
+ "</h4>" + |
+ "<a itemprop=\"contentUrl\" href=\"" + expectedUrl2 + "\">2nd test results" + |
+ "</a>" + |
+ "<div id=\"5\" itemprop=\"publisher\">" + expectedPublisher2 + |
+ "</div>" + |
+ "<div id=\"6\">" + |
+ "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear2 + |
+ "</span>" + |
+ "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder2 + |
+ "</span>" + |
+ "</div>" + |
+ "<span itemprop=\"encodingFormat\">" + expectedFormat2 + |
+ "</span>" + |
+ "<span itemprop=\"caption\">" + expectedCaption2 + |
+ "</span>" + |
+ "<meta itemprop=\"representativeOfPage\" content=\"true\">" + |
+ "<meta itemprop=\"width\" content=\"1000\">" + |
+ "<meta itemprop=\"height\" content=\"600\">" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ // The basic properties of Thing should be from the first image that was |
+ // inserted. |
+ assertEquals("IMAGE", parser.getType()); |
+ assertEquals(expectedTitle1, parser.getTitle()); |
+ assertEquals(expectedDescription1, parser.getDescription()); |
+ assertEquals("", parser.getUrl()); |
+ assertEquals(expectedPublisher1, parser.getPublisher()); |
+ assertEquals(null, parser.getArticle()); |
+ assertEquals("", parser.getAuthor()); |
+ assertEquals("Copyright " + expectedCopyrightYear1 + " " + expectedCopyrightHolder1, |
+ parser.getCopyright()); |
+ |
+ MarkupParser.Image[] images = parser.getImages(); |
+ assertEquals(2, images.length); |
+ // The 2nd image that was inserted is representative of page, so the |
+ // images should be swapped in |images|. |
+ MarkupParser.Image image = images[0]; |
+ assertEquals(expectedUrl2, image.image); |
+ assertEquals(expectedUrl2, image.url); |
+ assertEquals(null, image.secureUrl); |
+ assertEquals(expectedFormat2, image.type); |
+ assertEquals(expectedCaption2, image.caption); |
+ assertEquals(1000, image.width); |
+ assertEquals(600, image.height); |
+ image = images[1]; |
+ assertEquals(expectedUrl1, image.image); |
+ assertEquals(expectedUrl1, image.url); |
+ assertEquals(null, image.secureUrl); |
+ assertEquals(expectedFormat1, image.type); |
+ assertEquals(expectedCaption1, image.caption); |
+ assertEquals(400, image.width); |
+ assertEquals(300, image.height); |
+ } |
+ |
+ public void testArticleWithEmbeddedAuthorAndPublisher() { |
+ String expectedTitle = "Testcase for ARTICLE"; |
+ String expectedDescription = "Testing ARTICLE with embedded author and publisher"; |
+ String expectedUrl = "http://test_article_with_embedded_items.html"; |
+ String expectedImage = "http://test_article_with_embedded_items.jpeg"; |
+ String expectedAuthor = "Whoever authored"; |
+ String expectedPublisher = "Whatever Article Incorporated"; |
+ String expectedDatePublished = "April 15, 2014"; |
+ String expectedTimeModified = "2014-04-16T23:59"; |
+ String expectedCopyrightYear = "2000-2014"; |
+ String expectedCopyrightHolder = "Whoever Article Copyrighted"; |
+ String expectedSection = "Romance thriller"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
+ "<h1 itemprop=\"headline\">" + expectedTitle + |
+ "</h1>" + |
+ "<h2 itemprop=\"description\">" + expectedDescription + |
+ "</h2>" + |
+ "<a itemprop=\"url\" href=\"" + expectedUrl + "\">test results" + |
+ "</a>" + |
+ "<img itemprop=\"image\" src=\"" + expectedImage + "\">" + |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"author\">Author: " + |
+ "<span itemprop=\"name\">" + expectedAuthor + |
+ "</span>" + |
+ "</div>" + |
+ "<div id=\"3\" itemscope itemtype=\"http://schema.org/Organization\"" + |
+ " itemprop=\"publisher\">Publisher: " + |
+ "<span itemprop=\"name\">" + expectedPublisher + |
+ "</span>" + |
+ "</div>" + |
+ "<span itemprop=\"datePublished\">" + expectedDatePublished + |
+ "</span>" + |
+ "<time itemprop=\"dateModified\" datetime=\"" + expectedTimeModified + |
+ "\">April 16, 2014 11:59pm" + |
+ "</time>" + |
+ "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear + |
+ "</span>" + |
+ "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder + |
+ "</span>" + |
+ "<span itemprop=\"articleSection\">" + expectedSection + |
+ "</span>" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedTitle, parser.getTitle()); |
+ assertEquals(expectedDescription, parser.getDescription()); |
+ assertEquals(expectedUrl, parser.getUrl()); |
+ assertEquals(expectedAuthor, parser.getAuthor()); |
+ assertEquals(expectedPublisher, parser.getPublisher()); |
+ assertEquals( "Copyright " + expectedCopyrightYear + " " + expectedCopyrightHolder, |
+ parser.getCopyright()); |
+ MarkupParser.Image[] images = parser.getImages(); |
+ assertEquals(1, images.length); |
+ assertEquals(expectedImage, images[0].image); |
+ assertEquals(expectedImage, images[0].url); |
+ MarkupParser.Article article = parser.getArticle(); |
+ assertEquals(expectedDatePublished, article.publishedTime); |
+ assertEquals(expectedTimeModified, article.modifiedTime); |
+ assertEquals(null, article.expirationTime); |
+ assertEquals(expectedSection, article.section); |
+ assertEquals(1, article.authors.length); |
+ assertEquals(expectedAuthor, article.authors[0]); |
+ } |
+ |
+ // This verifies that properties are retrieved from the first schema.org type that defines the |
+ // properties requested, i.e. if a property doesn't exist in the first type but does in the |
+ // second, it should be available upon request. |
+ public void testPropertiesFromFirstAvailableType() { |
+ String expectedTitle = "Testcase for properties from first available type"; |
+ String expectedImage = "http://test_properties_from_1st_available_type.jpeg"; |
+ String expectedAuthor = "Whoever authored"; |
+ String expectedPublisher = "Whatever Article Incorporated"; |
+ String expectedDescription = "The organization that published this article"; |
+ String expectedUrl = "http://article_publisher.html"; |
+ String expectedDatePublished = "April 15, 2014"; |
+ String expectedTimeModified = "2014-04-16T23:59"; |
+ String expectedCopyrightYear = "2000-2014"; |
+ String expectedCopyrightHolder = "Whoever Article Copyrighted"; |
+ String expectedSection = "Romance thriller"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
+ "<h1 itemprop=\"headline\">" + expectedTitle + |
+ "</h1>" + |
+ "<img itemprop=\"image\" src=\"" + expectedImage + "\">" + |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"author\">Author: " + |
+ "<span itemprop=\"name\">" + expectedAuthor + |
+ "</span>" + |
+ "</div>" + |
+ "<div id=\"3\" itemscope itemtype=\"http://schema.org/Organization\"" + |
+ " itemprop=\"publisher\">Publisher: " + |
+ "<span itemprop=\"name\">" + expectedPublisher + |
+ "</span>" + |
+ "<span itemprop=\"description\">" + expectedDescription + |
+ "</span>" + |
+ "<a itemprop=\"url\" href=\"" + expectedUrl + "\">More on Publisher" + |
+ "</a>" + |
+ "</div>" + |
+ "<span itemprop=\"datePublished\">" + expectedDatePublished + |
+ "</span>" + |
+ "<time itemprop=\"dateModified\" datetime=\"" + expectedTimeModified + |
+ "\">April 16, 2014 11:59pm" + |
+ "</time>" + |
+ "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear + |
+ "</span>" + |
+ "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder + |
+ "</span>" + |
+ "<span itemprop=\"articleSection\">" + expectedSection + |
+ "</span>" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedTitle, parser.getTitle()); |
+ assertEquals(expectedDescription, parser.getDescription()); |
+ assertEquals(expectedUrl, parser.getUrl()); |
+ assertEquals(expectedPublisher, parser.getPublisher()); |
+ assertEquals(expectedAuthor, parser.getAuthor()); |
+ assertEquals("Copyright " + expectedCopyrightYear + " " + expectedCopyrightHolder, |
+ parser.getCopyright()); |
+ MarkupParser.Article article = parser.getArticle(); |
+ assertEquals(expectedDatePublished, article.publishedTime); |
+ assertEquals(expectedTimeModified, article.modifiedTime); |
+ assertEquals(null, article.expirationTime); |
+ assertEquals(expectedSection, article.section); |
+ assertEquals(1, article.authors.length); |
+ assertEquals(expectedAuthor, article.authors[0]); |
+ } |
+ |
+ public void testItemscopeInHTMLTag() { |
+ setItemScopeAndType(mRoot, "Article"); |
+ |
+ String expectedTitle = "Testcase for ItemScope in HTML tag"; |
+ Element h = TestUtil.createHeading(1, expectedTitle); |
+ setItemProp(h, "headline"); |
+ mBody.appendChild(h); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedTitle, parser.getTitle()); |
+ assertTrue(parser.getArticle() != null); |
+ |
+ // Remove "itemscope" and "itemtype" attributes in <html> tag, so that |
+ // other testcases won't be affected. |
+ mRoot.removeAttribute("ITEMSCOPE"); |
+ mRoot.removeAttribute("ITEMTYPE"); |
+ } |
+ |
+ public void testSupportedWithUnsupportedItemprop() { |
+ String expectedTitle = "Testcase for Supported With Unsupported Itemprop"; |
+ String expectedSection = "Testing"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
+ "<span itemprop=\"headline\">" + expectedTitle + |
+ "</span>" + |
+ "<span itemprop=\"articleSection\">" + expectedSection + |
+ "</span>" + |
+ // Add unsupported AggregateRating to supported Article as itemprop. |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/AggregateRating\"" + |
+ " itemprop=\"aggregateRating\">Ratings: " + |
+ "<span itemprop=\"ratingValue\">9.9" + |
+ "</span>" + |
+ // Add supported Person to unsupported AggregateRating as itemprop. |
+ "<div id=\"3\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"author\">Author: " + |
+ "<span itemprop=\"name\">Whoever authored" + |
+ "</span>" + |
+ "</div>" + |
+ "</div>" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedTitle, parser.getTitle()); |
+ assertEquals("", parser.getDescription()); |
+ assertEquals("", parser.getUrl()); |
+ assertEquals("", parser.getAuthor()); |
+ assertEquals("", parser.getPublisher()); |
+ assertEquals("", parser.getCopyright()); |
+ MarkupParser.Article article = parser.getArticle(); |
+ assertEquals("", article.publishedTime); |
+ assertEquals("", article.modifiedTime); |
+ assertEquals(null, article.expirationTime); |
+ assertEquals(expectedSection, article.section); |
+ assertEquals(0, article.authors.length); |
+ } |
+ |
+ public void testUnsupportedWithSupportedItemprop() { |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Movie\">" + |
+ "<span itemprop=\"headline\">Testcase for Unsupported With Supported Itemprop" + |
+ "</span>" + |
+ // Add supported Person to unsupported Movie as itemprop. |
+ "<div id=\"3\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"publisher\">Publisher: " + |
+ "<span itemprop=\"name\">Whoever published" + |
+ "</span>" + |
+ "</div>" + |
+ "</div>"; |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("", parser.getType()); |
+ assertEquals("", parser.getTitle()); |
+ assertEquals("", parser.getDescription()); |
+ assertEquals("", parser.getUrl()); |
+ assertEquals("", parser.getAuthor()); |
+ assertEquals("", parser.getPublisher()); |
+ assertEquals("", parser.getCopyright()); |
+ assertEquals(null, parser.getArticle()); |
+ assertEquals(null, parser.getImages()); |
+ } |
+ |
+ public void testUnsupportedWithNestedSupported() { |
+ String expectedTitle = "Testcase for ARTICLE nested in Unsupported Type"; |
+ String expectedDescription = "Testing ARTICLE that is nested within unsupported type"; |
+ String expectedUrl = "http://test_article_with_embedded_items.html"; |
+ String expectedImage = "http://test_article_with_embedded_items.jpeg"; |
+ String expectedAuthor = "Whoever authored"; |
+ String expectedPublisher = "Whatever Article Incorporated"; |
+ String expectedDatePublished = "April 15, 2014"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Movie\">" + |
+ "<span itemprop=\"headline\">Testcase for Unsupported With Supported Itemprop" + |
+ "</span>" + |
+ // Add supported Article to unsupported Movie as a non-itemprop. |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/Article\">" + |
+ "<span itemprop=\"headline\">" + expectedTitle + |
+ "</span>" + |
+ "<span itemprop=\"description\">" + expectedDescription + |
+ "</span>" + |
+ "<a itemprop=\"url\" href=\"" + expectedUrl + "\">test results" + |
+ "</a>" + |
+ "<img itemprop=\"image\" src=\"" + expectedImage + "\">" + |
+ "<div id=\"3\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"author\">Author: " + |
+ "<span itemprop=\"name\">" + expectedAuthor + |
+ "</span>" + |
+ "</div>" + |
+ "<div id=\"4\" itemscope itemtype=\"http://schema.org/Organization\"" + |
+ " itemprop=\"publisher\">Publisher: " + |
+ "<span itemprop=\"name\">" + expectedPublisher + |
+ "</span>" + |
+ "</div>" + |
+ "<span itemprop=\"datePublished\">" + expectedDatePublished + |
+ "</span>" + |
+ "</div>" + |
+ "</div>"; |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedTitle, parser.getTitle()); |
+ assertEquals(expectedDescription, parser.getDescription()); |
+ assertEquals(expectedUrl, parser.getUrl()); |
+ assertEquals(expectedAuthor, parser.getAuthor()); |
+ assertEquals(expectedPublisher, parser.getPublisher()); |
+ assertEquals("", parser.getCopyright()); |
+ MarkupParser.Image[] images = parser.getImages(); |
+ assertEquals(1, images.length); |
+ assertEquals(expectedImage, images[0].image); |
+ assertEquals(expectedImage, images[0].url); |
+ MarkupParser.Article article = parser.getArticle(); |
+ assertEquals(expectedDatePublished, article.publishedTime); |
+ assertEquals(null, article.expirationTime); |
+ assertEquals(1, article.authors.length); |
+ assertEquals(expectedAuthor, article.authors[0]); |
+ } |
+ |
+ public void testSameItempropDifferentValues() { |
+ String expectedAuthor = "Author 1"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"author\">Authors: " + |
+ "<span itemprop=\"name\">" + expectedAuthor + |
+ "</span>" + |
+ "<span itemprop=\"name\">Author 2" + |
+ "</span>" + |
+ "</div>" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedAuthor, parser.getAuthor()); |
+ MarkupParser.Article article = parser.getArticle(); |
+ assertEquals(1, article.authors.length); |
+ assertEquals(expectedAuthor, article.authors[0]); |
+ } |
+ |
+ public void testItempropWithMultiProperties() { |
+ String expectedPerson = "Person foo"; |
+ String htmlStr = |
+ "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
+ "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\"" + |
+ " itemprop=\"author publisher\">" + |
+ "<span itemprop=\"name\">" + expectedPerson + |
+ "</span>" + |
+ "</div>" + |
+ "</div>"; |
+ |
+ Element rootDiv = TestUtil.createDiv(0); |
+ rootDiv.setInnerHTML(htmlStr); |
+ mBody.appendChild(rootDiv); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("ARTICLE", parser.getType()); |
+ assertEquals(expectedPerson, parser.getAuthor()); |
+ assertEquals(expectedPerson, parser.getPublisher()); |
+ MarkupParser.Article article = parser.getArticle(); |
+ assertEquals(1, article.authors.length); |
+ assertEquals(expectedPerson, article.authors[0]); |
+ } |
+ |
+ public void testAuthorFromRelAttribute() { |
+ String expectedAuthor = "Chromium Authors"; |
+ AnchorElement link = TestUtil.createAnchor("http://rel_author.html", expectedAuthor); |
+ link.setRel("author"); |
+ mBody.appendChild(link); |
+ |
+ SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
+ assertEquals("", parser.getType()); |
+ assertEquals(expectedAuthor, parser.getAuthor()); |
+ |
+ // Remove anchor from parent, so that other testcases won't be affected. |
+ link.removeFromParent(); |
+ } |
+ |
+ @Override |
+ protected void gwtSetUp() throws Exception { |
+ // Get root element. |
+ mRoot = Document.get().getDocumentElement(); |
+ |
+ // Get <body> element. |
+ NodeList<Element> bodies = mRoot.getElementsByTagName("BODY"); |
+ if (bodies.getLength() != 1) |
+ throw new Exception("There shouldn't be more than 1 <body> tag"); |
+ mBody = bodies.getItem(0); |
+ |
+ // Remove all meta tags, otherwise a testcase may run with the meta tags |
+ // set up in a previous testcase, resulting in unexpected results. |
+ NodeList<Element> allMeta = mRoot.getElementsByTagName("META"); |
+ for (int i = allMeta.getLength() - 1; i >= 0; i--) { |
+ allMeta.getItem(i).removeFromParent(); |
+ } |
+ |
+ // Remove all div tags, otherwise a testcase may run with the div tags |
+ // set up in a previous testcase, resulting in unexpected results. |
+ NodeList<Element> allDiv = mRoot.getElementsByTagName("DIV"); |
+ for (int i = allDiv.getLength() - 1; i >= 0; i--) { |
+ allDiv.getItem(i).removeFromParent(); |
+ } |
+ } |
+ |
+ private void setItemScopeAndType(Element e, String type) { |
+ e.setAttribute("ITEMSCOPE", ""); |
+ e.setAttribute("ITEMTYPE", "http://schema.org/" + type); |
+ } |
+ |
+ private void setItemProp(Element e, String name) { |
+ e.setAttribute("itemprop", name); |
+ } |
+ |
+ private Element mRoot; |
+ private Element mBody; |
+} |