OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 package com.dom_distiller.client; |
| 6 |
| 7 import com.google.gwt.dom.client.Document; |
| 8 import com.google.gwt.dom.client.AnchorElement; |
| 9 import com.google.gwt.dom.client.Element; |
| 10 import com.google.gwt.dom.client.NodeList; |
| 11 |
| 12 import com.google.gwt.junit.client.GWTTestCase; |
| 13 |
| 14 public class SchemaOrgParserTest extends GWTTestCase { |
| 15 @Override |
| 16 public String getModuleName() { |
| 17 return "com.dom_distiller.DomDistillerJUnit"; |
| 18 } |
| 19 |
| 20 public void testImageWithEmbeddedPublisher() { |
| 21 String expectedTitle = "Testcase for IMAGE"; |
| 22 String expectedDescription = "Testing IMAGE with embedded publisher"; |
| 23 String expectedUrl = "http://test_image_with_embedded_item.html"; |
| 24 String expectedPublisher = "Whatever Image Incorporated"; |
| 25 String expectedCopyrightYear = "1999-2022"; |
| 26 String expectedCopyrightHolder = "Whoever Image Copyrighted"; |
| 27 String expectedFormat = "jpeg"; |
| 28 String expectedCaption = "A test for IMAGE with embedded publisher"; |
| 29 String htmlStr = |
| 30 "<div id=\"1\" itemscope itemtype=\"http://schema.org/ImageObject\">
" + |
| 31 "<h1 itemprop=\"headline\">" + expectedTitle + |
| 32 "</h1>" + |
| 33 "<h2 itemprop=\"description\">" + expectedDescription + |
| 34 "</h2>" + |
| 35 "<a itemprop=\"contentUrl\" href=\"" + expectedUrl + "\">test re
sults" + |
| 36 "</a>" + |
| 37 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Organizati
on\"" + |
| 38 " itemprop=\"publisher\">Publisher: " + |
| 39 "<span itemprop=\"name\">" + expectedPublisher + |
| 40 "</span>" + |
| 41 "</div>" + |
| 42 "<div id=\"3\">" + |
| 43 "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear
+ |
| 44 "</span>" + |
| 45 "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHol
der + |
| 46 "</span>" + |
| 47 "</div>" + |
| 48 "<span itemprop=\"encodingFormat\">" + expectedFormat + |
| 49 "</span>" + |
| 50 "<span itemprop=\"caption\">" + expectedCaption + |
| 51 "</span>" + |
| 52 "<meta itemprop=\"representativeOfPage\" content=\"true\">" + |
| 53 "<meta itemprop=\"width\" content=\"600\">" + |
| 54 "<meta itemprop=\"height\" content=\"400\">" + |
| 55 "</div>"; |
| 56 |
| 57 Element rootDiv = TestUtil.createDiv(0); |
| 58 rootDiv.setInnerHTML(htmlStr); |
| 59 mBody.appendChild(rootDiv); |
| 60 |
| 61 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 62 assertEquals("IMAGE", parser.getType()); |
| 63 assertEquals(expectedTitle, parser.getTitle()); |
| 64 assertEquals(expectedDescription, parser.getDescription()); |
| 65 assertEquals("", parser.getUrl()); |
| 66 assertEquals(expectedPublisher, parser.getPublisher()); |
| 67 assertEquals(null, parser.getArticle()); |
| 68 assertEquals("", parser.getAuthor()); |
| 69 assertEquals("Copyright " + expectedCopyrightYear + " " + expectedCopyri
ghtHolder, |
| 70 parser.getCopyright()); |
| 71 MarkupParser.Image[] images = parser.getImages(); |
| 72 assertEquals(1, images.length); |
| 73 MarkupParser.Image image = images[0]; |
| 74 assertEquals(expectedUrl, image.image); |
| 75 assertEquals(expectedUrl, image.url); |
| 76 assertEquals(null, image.secureUrl); |
| 77 assertEquals(expectedFormat, image.type); |
| 78 assertEquals(expectedCaption, image.caption); |
| 79 assertEquals(600, image.width); |
| 80 assertEquals(400, image.height); |
| 81 } |
| 82 |
| 83 public void test2Images() { |
| 84 String expectedTitle1 = "Testcase for 1st IMAGE"; |
| 85 String expectedDescription1 = "Testing 1st IMAGE"; |
| 86 String expectedUrl1 = "http://test_1st image.html"; |
| 87 String expectedPublisher1 = "Whatever 1st Image Incorporated"; |
| 88 String expectedCopyrightYear1 = "1000-1999"; |
| 89 String expectedCopyrightHolder1 = "Whoever 1st Image Copyrighted"; |
| 90 String expectedFormat1 = "jpeg"; |
| 91 String expectedCaption1 = "A test for 1st IMAGE"; |
| 92 String expectedTitle2 = "Testcase for 2nd IMAGE"; |
| 93 String expectedDescription2 = "Testing 2nd IMAGE"; |
| 94 String expectedUrl2 = "http://test_2nd image.html"; |
| 95 String expectedPublisher2 = "Whatever 2nd Image Incorporated"; |
| 96 String expectedCopyrightYear2 = "2000-2999"; |
| 97 String expectedCopyrightHolder2 = "Whoever 2nd Image Copyrighted"; |
| 98 String expectedFormat2 = "gif"; |
| 99 String expectedCaption2 = "A test for 2nd IMAGE"; |
| 100 String htmlStr = |
| 101 "<div id=\"1\" itemscope itemtype=\"http://schema.org/ImageObject\">
" + |
| 102 "<h1 itemprop=\"headline\">" + expectedTitle1 + |
| 103 "</h1>" + |
| 104 "<h2 itemprop=\"description\">" + expectedDescription1 + |
| 105 "</h2>" + |
| 106 "<a itemprop=\"contentUrl\" href=\"" + expectedUrl1 + "\">1st te
st results" + |
| 107 "</a>" + |
| 108 "<div id=\"2\" itemprop=\"publisher\">" + expectedPublisher1 + |
| 109 "</div>" + |
| 110 "<div id=\"3\">" + |
| 111 "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear1
+ |
| 112 "</span>" + |
| 113 "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHol
der1 + |
| 114 "</span>" + |
| 115 "</div>" + |
| 116 "<span itemprop=\"encodingFormat\">" + expectedFormat1 + |
| 117 "</span>" + |
| 118 "<span itemprop=\"caption\">" + expectedCaption1 + |
| 119 "</span>" + |
| 120 "<meta itemprop=\"representativeOfPage\" content=\"false\">" + |
| 121 "<meta itemprop=\"width\" content=\"400\">" + |
| 122 "<meta itemprop=\"height\" content=\"300\">" + |
| 123 "</div>" + |
| 124 "<div id=\"4\" itemscope itemtype=\"http://schema.org/ImageObject\">
" + |
| 125 "<h3 itemprop=\"headline\">" + expectedTitle2 + |
| 126 "</h3>" + |
| 127 "<h4 itemprop=\"description\">" + expectedDescription2 + |
| 128 "</h4>" + |
| 129 "<a itemprop=\"contentUrl\" href=\"" + expectedUrl2 + "\">2nd te
st results" + |
| 130 "</a>" + |
| 131 "<div id=\"5\" itemprop=\"publisher\">" + expectedPublisher2 + |
| 132 "</div>" + |
| 133 "<div id=\"6\">" + |
| 134 "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear2
+ |
| 135 "</span>" + |
| 136 "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHol
der2 + |
| 137 "</span>" + |
| 138 "</div>" + |
| 139 "<span itemprop=\"encodingFormat\">" + expectedFormat2 + |
| 140 "</span>" + |
| 141 "<span itemprop=\"caption\">" + expectedCaption2 + |
| 142 "</span>" + |
| 143 "<meta itemprop=\"representativeOfPage\" content=\"true\">" + |
| 144 "<meta itemprop=\"width\" content=\"1000\">" + |
| 145 "<meta itemprop=\"height\" content=\"600\">" + |
| 146 "</div>"; |
| 147 |
| 148 Element rootDiv = TestUtil.createDiv(0); |
| 149 rootDiv.setInnerHTML(htmlStr); |
| 150 mBody.appendChild(rootDiv); |
| 151 |
| 152 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 153 // The basic properties of Thing should be from the first image that was |
| 154 // inserted. |
| 155 assertEquals("IMAGE", parser.getType()); |
| 156 assertEquals(expectedTitle1, parser.getTitle()); |
| 157 assertEquals(expectedDescription1, parser.getDescription()); |
| 158 assertEquals("", parser.getUrl()); |
| 159 assertEquals(expectedPublisher1, parser.getPublisher()); |
| 160 assertEquals(null, parser.getArticle()); |
| 161 assertEquals("", parser.getAuthor()); |
| 162 assertEquals("Copyright " + expectedCopyrightYear1 + " " + expectedCopyr
ightHolder1, |
| 163 parser.getCopyright()); |
| 164 |
| 165 MarkupParser.Image[] images = parser.getImages(); |
| 166 assertEquals(2, images.length); |
| 167 // The 2nd image that was inserted is representative of page, so the |
| 168 // images should be swapped in |images|. |
| 169 MarkupParser.Image image = images[0]; |
| 170 assertEquals(expectedUrl2, image.image); |
| 171 assertEquals(expectedUrl2, image.url); |
| 172 assertEquals(null, image.secureUrl); |
| 173 assertEquals(expectedFormat2, image.type); |
| 174 assertEquals(expectedCaption2, image.caption); |
| 175 assertEquals(1000, image.width); |
| 176 assertEquals(600, image.height); |
| 177 image = images[1]; |
| 178 assertEquals(expectedUrl1, image.image); |
| 179 assertEquals(expectedUrl1, image.url); |
| 180 assertEquals(null, image.secureUrl); |
| 181 assertEquals(expectedFormat1, image.type); |
| 182 assertEquals(expectedCaption1, image.caption); |
| 183 assertEquals(400, image.width); |
| 184 assertEquals(300, image.height); |
| 185 } |
| 186 |
| 187 public void testArticleWithEmbeddedAuthorAndPublisher() { |
| 188 String expectedTitle = "Testcase for ARTICLE"; |
| 189 String expectedDescription = "Testing ARTICLE with embedded author and p
ublisher"; |
| 190 String expectedUrl = "http://test_article_with_embedded_items.html"; |
| 191 String expectedImage = "http://test_article_with_embedded_items.jpeg"; |
| 192 String expectedAuthor = "Whoever authored"; |
| 193 String expectedPublisher = "Whatever Article Incorporated"; |
| 194 String expectedDatePublished = "April 15, 2014"; |
| 195 String expectedTimeModified = "2014-04-16T23:59"; |
| 196 String expectedCopyrightYear = "2000-2014"; |
| 197 String expectedCopyrightHolder = "Whoever Article Copyrighted"; |
| 198 String expectedSection = "Romance thriller"; |
| 199 String htmlStr = |
| 200 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
| 201 "<h1 itemprop=\"headline\">" + expectedTitle + |
| 202 "</h1>" + |
| 203 "<h2 itemprop=\"description\">" + expectedDescription + |
| 204 "</h2>" + |
| 205 "<a itemprop=\"url\" href=\"" + expectedUrl + "\">test results"
+ |
| 206 "</a>" + |
| 207 "<img itemprop=\"image\" src=\"" + expectedImage + "\">" + |
| 208 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\""
+ |
| 209 " itemprop=\"author\">Author: " + |
| 210 "<span itemprop=\"name\">" + expectedAuthor + |
| 211 "</span>" + |
| 212 "</div>" + |
| 213 "<div id=\"3\" itemscope itemtype=\"http://schema.org/Organizati
on\"" + |
| 214 " itemprop=\"publisher\">Publisher: " + |
| 215 "<span itemprop=\"name\">" + expectedPublisher + |
| 216 "</span>" + |
| 217 "</div>" + |
| 218 "<span itemprop=\"datePublished\">" + expectedDatePublished + |
| 219 "</span>" + |
| 220 "<time itemprop=\"dateModified\" datetime=\"" + expectedTimeModi
fied + |
| 221 "\">April 16, 2014 11:59pm" + |
| 222 "</time>" + |
| 223 "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear + |
| 224 "</span>" + |
| 225 "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder
+ |
| 226 "</span>" + |
| 227 "<span itemprop=\"articleSection\">" + expectedSection + |
| 228 "</span>" + |
| 229 "</div>"; |
| 230 |
| 231 Element rootDiv = TestUtil.createDiv(0); |
| 232 rootDiv.setInnerHTML(htmlStr); |
| 233 mBody.appendChild(rootDiv); |
| 234 |
| 235 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 236 assertEquals("ARTICLE", parser.getType()); |
| 237 assertEquals(expectedTitle, parser.getTitle()); |
| 238 assertEquals(expectedDescription, parser.getDescription()); |
| 239 assertEquals(expectedUrl, parser.getUrl()); |
| 240 assertEquals(expectedAuthor, parser.getAuthor()); |
| 241 assertEquals(expectedPublisher, parser.getPublisher()); |
| 242 assertEquals( "Copyright " + expectedCopyrightYear + " " + expectedCopyr
ightHolder, |
| 243 parser.getCopyright()); |
| 244 MarkupParser.Image[] images = parser.getImages(); |
| 245 assertEquals(1, images.length); |
| 246 assertEquals(expectedImage, images[0].image); |
| 247 assertEquals(expectedImage, images[0].url); |
| 248 MarkupParser.Article article = parser.getArticle(); |
| 249 assertEquals(expectedDatePublished, article.publishedTime); |
| 250 assertEquals(expectedTimeModified, article.modifiedTime); |
| 251 assertEquals(null, article.expirationTime); |
| 252 assertEquals(expectedSection, article.section); |
| 253 assertEquals(1, article.authors.length); |
| 254 assertEquals(expectedAuthor, article.authors[0]); |
| 255 } |
| 256 |
| 257 // This verifies that properties are retrieved from the first schema.org typ
e that defines the |
| 258 // properties requested, i.e. if a property doesn't exist in the first type
but does in the |
| 259 // second, it should be available upon request. |
| 260 public void testPropertiesFromFirstAvailableType() { |
| 261 String expectedTitle = "Testcase for properties from first available typ
e"; |
| 262 String expectedImage = "http://test_properties_from_1st_available_type.j
peg"; |
| 263 String expectedAuthor = "Whoever authored"; |
| 264 String expectedPublisher = "Whatever Article Incorporated"; |
| 265 String expectedDescription = "The organization that published this artic
le"; |
| 266 String expectedUrl = "http://article_publisher.html"; |
| 267 String expectedDatePublished = "April 15, 2014"; |
| 268 String expectedTimeModified = "2014-04-16T23:59"; |
| 269 String expectedCopyrightYear = "2000-2014"; |
| 270 String expectedCopyrightHolder = "Whoever Article Copyrighted"; |
| 271 String expectedSection = "Romance thriller"; |
| 272 String htmlStr = |
| 273 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
| 274 "<h1 itemprop=\"headline\">" + expectedTitle + |
| 275 "</h1>" + |
| 276 "<img itemprop=\"image\" src=\"" + expectedImage + "\">" + |
| 277 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\""
+ |
| 278 " itemprop=\"author\">Author: " + |
| 279 "<span itemprop=\"name\">" + expectedAuthor + |
| 280 "</span>" + |
| 281 "</div>" + |
| 282 "<div id=\"3\" itemscope itemtype=\"http://schema.org/Organizati
on\"" + |
| 283 " itemprop=\"publisher\">Publisher: " + |
| 284 "<span itemprop=\"name\">" + expectedPublisher + |
| 285 "</span>" + |
| 286 "<span itemprop=\"description\">" + expectedDescription + |
| 287 "</span>" + |
| 288 "<a itemprop=\"url\" href=\"" + expectedUrl + "\">More on Pu
blisher" + |
| 289 "</a>" + |
| 290 "</div>" + |
| 291 "<span itemprop=\"datePublished\">" + expectedDatePublished + |
| 292 "</span>" + |
| 293 "<time itemprop=\"dateModified\" datetime=\"" + expectedTimeModi
fied + |
| 294 "\">April 16, 2014 11:59pm" + |
| 295 "</time>" + |
| 296 "<span itemprop=\"copyrightYear\">" + expectedCopyrightYear + |
| 297 "</span>" + |
| 298 "<span itemprop=\"copyrightHolder\">" + expectedCopyrightHolder
+ |
| 299 "</span>" + |
| 300 "<span itemprop=\"articleSection\">" + expectedSection + |
| 301 "</span>" + |
| 302 "</div>"; |
| 303 |
| 304 Element rootDiv = TestUtil.createDiv(0); |
| 305 rootDiv.setInnerHTML(htmlStr); |
| 306 mBody.appendChild(rootDiv); |
| 307 |
| 308 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 309 assertEquals("ARTICLE", parser.getType()); |
| 310 assertEquals(expectedTitle, parser.getTitle()); |
| 311 assertEquals(expectedDescription, parser.getDescription()); |
| 312 assertEquals(expectedUrl, parser.getUrl()); |
| 313 assertEquals(expectedPublisher, parser.getPublisher()); |
| 314 assertEquals(expectedAuthor, parser.getAuthor()); |
| 315 assertEquals("Copyright " + expectedCopyrightYear + " " + expectedCopyri
ghtHolder, |
| 316 parser.getCopyright()); |
| 317 MarkupParser.Article article = parser.getArticle(); |
| 318 assertEquals(expectedDatePublished, article.publishedTime); |
| 319 assertEquals(expectedTimeModified, article.modifiedTime); |
| 320 assertEquals(null, article.expirationTime); |
| 321 assertEquals(expectedSection, article.section); |
| 322 assertEquals(1, article.authors.length); |
| 323 assertEquals(expectedAuthor, article.authors[0]); |
| 324 } |
| 325 |
| 326 public void testItemscopeInHTMLTag() { |
| 327 setItemScopeAndType(mRoot, "Article"); |
| 328 |
| 329 String expectedTitle = "Testcase for ItemScope in HTML tag"; |
| 330 Element h = TestUtil.createHeading(1, expectedTitle); |
| 331 setItemProp(h, "headline"); |
| 332 mBody.appendChild(h); |
| 333 |
| 334 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 335 assertEquals("ARTICLE", parser.getType()); |
| 336 assertEquals(expectedTitle, parser.getTitle()); |
| 337 assertTrue(parser.getArticle() != null); |
| 338 |
| 339 // Remove "itemscope" and "itemtype" attributes in <html> tag, so that |
| 340 // other testcases won't be affected. |
| 341 mRoot.removeAttribute("ITEMSCOPE"); |
| 342 mRoot.removeAttribute("ITEMTYPE"); |
| 343 } |
| 344 |
| 345 public void testSupportedWithUnsupportedItemprop() { |
| 346 String expectedTitle = "Testcase for Supported With Unsupported Itemprop
"; |
| 347 String expectedSection = "Testing"; |
| 348 String htmlStr = |
| 349 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
| 350 "<span itemprop=\"headline\">" + expectedTitle + |
| 351 "</span>" + |
| 352 "<span itemprop=\"articleSection\">" + expectedSection + |
| 353 "</span>" + |
| 354 // Add unsupported AggregateRating to supported Article as itemp
rop. |
| 355 "<div id=\"2\" itemscope itemtype=\"http://schema.org/AggregateR
ating\"" + |
| 356 " itemprop=\"aggregateRating\">Ratings: " + |
| 357 "<span itemprop=\"ratingValue\">9.9" + |
| 358 "</span>" + |
| 359 // Add supported Person to unsupported AggregateRating as it
emprop. |
| 360 "<div id=\"3\" itemscope itemtype=\"http://schema.org/Person
\"" + |
| 361 " itemprop=\"author\">Author: " + |
| 362 "<span itemprop=\"name\">Whoever authored" + |
| 363 "</span>" + |
| 364 "</div>" + |
| 365 "</div>" + |
| 366 "</div>"; |
| 367 |
| 368 Element rootDiv = TestUtil.createDiv(0); |
| 369 rootDiv.setInnerHTML(htmlStr); |
| 370 mBody.appendChild(rootDiv); |
| 371 |
| 372 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 373 assertEquals("ARTICLE", parser.getType()); |
| 374 assertEquals(expectedTitle, parser.getTitle()); |
| 375 assertEquals("", parser.getDescription()); |
| 376 assertEquals("", parser.getUrl()); |
| 377 assertEquals("", parser.getAuthor()); |
| 378 assertEquals("", parser.getPublisher()); |
| 379 assertEquals("", parser.getCopyright()); |
| 380 MarkupParser.Article article = parser.getArticle(); |
| 381 assertEquals("", article.publishedTime); |
| 382 assertEquals("", article.modifiedTime); |
| 383 assertEquals(null, article.expirationTime); |
| 384 assertEquals(expectedSection, article.section); |
| 385 assertEquals(0, article.authors.length); |
| 386 } |
| 387 |
| 388 public void testUnsupportedWithSupportedItemprop() { |
| 389 String htmlStr = |
| 390 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Movie\">" + |
| 391 "<span itemprop=\"headline\">Testcase for Unsupported With Suppo
rted Itemprop" + |
| 392 "</span>" + |
| 393 // Add supported Person to unsupported Movie as itemprop. |
| 394 "<div id=\"3\" itemscope itemtype=\"http://schema.org/Person\""
+ |
| 395 " itemprop=\"publisher\">Publisher: " + |
| 396 "<span itemprop=\"name\">Whoever published" + |
| 397 "</span>" + |
| 398 "</div>" + |
| 399 "</div>"; |
| 400 Element rootDiv = TestUtil.createDiv(0); |
| 401 rootDiv.setInnerHTML(htmlStr); |
| 402 mBody.appendChild(rootDiv); |
| 403 |
| 404 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 405 assertEquals("", parser.getType()); |
| 406 assertEquals("", parser.getTitle()); |
| 407 assertEquals("", parser.getDescription()); |
| 408 assertEquals("", parser.getUrl()); |
| 409 assertEquals("", parser.getAuthor()); |
| 410 assertEquals("", parser.getPublisher()); |
| 411 assertEquals("", parser.getCopyright()); |
| 412 assertEquals(null, parser.getArticle()); |
| 413 assertEquals(null, parser.getImages()); |
| 414 } |
| 415 |
| 416 public void testUnsupportedWithNestedSupported() { |
| 417 String expectedTitle = "Testcase for ARTICLE nested in Unsupported Type"
; |
| 418 String expectedDescription = "Testing ARTICLE that is nested within unsu
pported type"; |
| 419 String expectedUrl = "http://test_article_with_embedded_items.html"; |
| 420 String expectedImage = "http://test_article_with_embedded_items.jpeg"; |
| 421 String expectedAuthor = "Whoever authored"; |
| 422 String expectedPublisher = "Whatever Article Incorporated"; |
| 423 String expectedDatePublished = "April 15, 2014"; |
| 424 String htmlStr = |
| 425 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Movie\">" + |
| 426 "<span itemprop=\"headline\">Testcase for Unsupported With Suppo
rted Itemprop" + |
| 427 "</span>" + |
| 428 // Add supported Article to unsupported Movie as a non-itemprop. |
| 429 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Article\">
" + |
| 430 "<span itemprop=\"headline\">" + expectedTitle + |
| 431 "</span>" + |
| 432 "<span itemprop=\"description\">" + expectedDescription + |
| 433 "</span>" + |
| 434 "<a itemprop=\"url\" href=\"" + expectedUrl + "\">test resul
ts" + |
| 435 "</a>" + |
| 436 "<img itemprop=\"image\" src=\"" + expectedImage + "\">" + |
| 437 "<div id=\"3\" itemscope itemtype=\"http://schema.org/Person
\"" + |
| 438 " itemprop=\"author\">Author: " + |
| 439 "<span itemprop=\"name\">" + expectedAuthor + |
| 440 "</span>" + |
| 441 "</div>" + |
| 442 "<div id=\"4\" itemscope itemtype=\"http://schema.org/Organi
zation\"" + |
| 443 " itemprop=\"publisher\">Publisher: " + |
| 444 "<span itemprop=\"name\">" + expectedPublisher + |
| 445 "</span>" + |
| 446 "</div>" + |
| 447 "<span itemprop=\"datePublished\">" + expectedDatePublished
+ |
| 448 "</span>" + |
| 449 "</div>" + |
| 450 "</div>"; |
| 451 Element rootDiv = TestUtil.createDiv(0); |
| 452 rootDiv.setInnerHTML(htmlStr); |
| 453 mBody.appendChild(rootDiv); |
| 454 |
| 455 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 456 assertEquals("ARTICLE", parser.getType()); |
| 457 assertEquals(expectedTitle, parser.getTitle()); |
| 458 assertEquals(expectedDescription, parser.getDescription()); |
| 459 assertEquals(expectedUrl, parser.getUrl()); |
| 460 assertEquals(expectedAuthor, parser.getAuthor()); |
| 461 assertEquals(expectedPublisher, parser.getPublisher()); |
| 462 assertEquals("", parser.getCopyright()); |
| 463 MarkupParser.Image[] images = parser.getImages(); |
| 464 assertEquals(1, images.length); |
| 465 assertEquals(expectedImage, images[0].image); |
| 466 assertEquals(expectedImage, images[0].url); |
| 467 MarkupParser.Article article = parser.getArticle(); |
| 468 assertEquals(expectedDatePublished, article.publishedTime); |
| 469 assertEquals(null, article.expirationTime); |
| 470 assertEquals(1, article.authors.length); |
| 471 assertEquals(expectedAuthor, article.authors[0]); |
| 472 } |
| 473 |
| 474 public void testSameItempropDifferentValues() { |
| 475 String expectedAuthor = "Author 1"; |
| 476 String htmlStr = |
| 477 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
| 478 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\""
+ |
| 479 " itemprop=\"author\">Authors: " + |
| 480 "<span itemprop=\"name\">" + expectedAuthor + |
| 481 "</span>" + |
| 482 "<span itemprop=\"name\">Author 2" + |
| 483 "</span>" + |
| 484 "</div>" + |
| 485 "</div>"; |
| 486 |
| 487 Element rootDiv = TestUtil.createDiv(0); |
| 488 rootDiv.setInnerHTML(htmlStr); |
| 489 mBody.appendChild(rootDiv); |
| 490 |
| 491 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 492 assertEquals("ARTICLE", parser.getType()); |
| 493 assertEquals(expectedAuthor, parser.getAuthor()); |
| 494 MarkupParser.Article article = parser.getArticle(); |
| 495 assertEquals(1, article.authors.length); |
| 496 assertEquals(expectedAuthor, article.authors[0]); |
| 497 } |
| 498 |
| 499 public void testItempropWithMultiProperties() { |
| 500 String expectedPerson = "Person foo"; |
| 501 String htmlStr = |
| 502 "<div id=\"1\" itemscope itemtype=\"http://schema.org/Article\">" + |
| 503 "<div id=\"2\" itemscope itemtype=\"http://schema.org/Person\""
+ |
| 504 " itemprop=\"author publisher\">" + |
| 505 "<span itemprop=\"name\">" + expectedPerson + |
| 506 "</span>" + |
| 507 "</div>" + |
| 508 "</div>"; |
| 509 |
| 510 Element rootDiv = TestUtil.createDiv(0); |
| 511 rootDiv.setInnerHTML(htmlStr); |
| 512 mBody.appendChild(rootDiv); |
| 513 |
| 514 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 515 assertEquals("ARTICLE", parser.getType()); |
| 516 assertEquals(expectedPerson, parser.getAuthor()); |
| 517 assertEquals(expectedPerson, parser.getPublisher()); |
| 518 MarkupParser.Article article = parser.getArticle(); |
| 519 assertEquals(1, article.authors.length); |
| 520 assertEquals(expectedPerson, article.authors[0]); |
| 521 } |
| 522 |
| 523 public void testAuthorFromRelAttribute() { |
| 524 String expectedAuthor = "Chromium Authors"; |
| 525 AnchorElement link = TestUtil.createAnchor("http://rel_author.html", exp
ectedAuthor); |
| 526 link.setRel("author"); |
| 527 mBody.appendChild(link); |
| 528 |
| 529 SchemaOrgParser parser = new SchemaOrgParser(mRoot); |
| 530 assertEquals("", parser.getType()); |
| 531 assertEquals(expectedAuthor, parser.getAuthor()); |
| 532 |
| 533 // Remove anchor from parent, so that other testcases won't be affected. |
| 534 link.removeFromParent(); |
| 535 } |
| 536 |
| 537 @Override |
| 538 protected void gwtSetUp() throws Exception { |
| 539 // Get root element. |
| 540 mRoot = Document.get().getDocumentElement(); |
| 541 |
| 542 // Get <body> element. |
| 543 NodeList<Element> bodies = mRoot.getElementsByTagName("BODY"); |
| 544 if (bodies.getLength() != 1) |
| 545 throw new Exception("There shouldn't be more than 1 <body> tag"); |
| 546 mBody = bodies.getItem(0); |
| 547 |
| 548 // Remove all meta tags, otherwise a testcase may run with the meta tags |
| 549 // set up in a previous testcase, resulting in unexpected results. |
| 550 NodeList<Element> allMeta = mRoot.getElementsByTagName("META"); |
| 551 for (int i = allMeta.getLength() - 1; i >= 0; i--) { |
| 552 allMeta.getItem(i).removeFromParent(); |
| 553 } |
| 554 |
| 555 // Remove all div tags, otherwise a testcase may run with the div tags |
| 556 // set up in a previous testcase, resulting in unexpected results. |
| 557 NodeList<Element> allDiv = mRoot.getElementsByTagName("DIV"); |
| 558 for (int i = allDiv.getLength() - 1; i >= 0; i--) { |
| 559 allDiv.getItem(i).removeFromParent(); |
| 560 } |
| 561 } |
| 562 |
| 563 private void setItemScopeAndType(Element e, String type) { |
| 564 e.setAttribute("ITEMSCOPE", ""); |
| 565 e.setAttribute("ITEMTYPE", "http://schema.org/" + type); |
| 566 } |
| 567 |
| 568 private void setItemProp(Element e, String name) { |
| 569 e.setAttribute("itemprop", name); |
| 570 } |
| 571 |
| 572 private Element mRoot; |
| 573 private Element mBody; |
| 574 } |
OLD | NEW |