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