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