Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.distiller; | 5 package org.chromium.distiller; |
| 6 | 6 |
| 7 import org.chromium.distiller.webdocument.WebTable; | 7 import org.chromium.distiller.webdocument.WebTable; |
| 8 | 8 |
| 9 import com.google.gwt.core.client.JsArray; | 9 import com.google.gwt.core.client.JsArray; |
| 10 import com.google.gwt.dom.client.Document; | 10 import com.google.gwt.dom.client.Document; |
| 11 import com.google.gwt.dom.client.Element; | 11 import com.google.gwt.dom.client.Element; |
| 12 import com.google.gwt.dom.client.Node; | 12 import com.google.gwt.dom.client.Node; |
| 13 import com.google.gwt.dom.client.NodeList; | |
| 13 | 14 |
| 14 import java.util.Map; | 15 import java.util.Map; |
| 15 import java.util.List; | 16 import java.util.List; |
| 16 | 17 |
| 17 public class DomUtilTest extends DomDistillerJsTestCase { | 18 public class DomUtilTest extends DomDistillerJsTestCase { |
| 19 private static final String CONTENT_TEXT = "Lorem Ipsum Lorem Ipsum Lorem Ip sum."; | |
| 20 private static final String TITLE_TEXT = "I am the document title"; | |
| 21 | |
| 18 public void testGetAttributes() { | 22 public void testGetAttributes() { |
| 19 Element e = Document.get().createDivElement(); | 23 Element e = Document.get().createDivElement(); |
| 20 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\ "sdf\"></div>"); | 24 e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\ "sdf\"></div>"); |
| 21 e = Element.as(e.getChildNodes().getItem(0)); | 25 e = Element.as(e.getChildNodes().getItem(0)); |
| 22 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); | 26 JsArray<Node> jsAttrs = DomUtil.getAttributes(e); |
| 23 assertEquals(3, jsAttrs.length()); | 27 assertEquals(3, jsAttrs.length()); |
| 24 assertEquals("style", jsAttrs.get(0).getNodeName()); | 28 assertEquals("style", jsAttrs.get(0).getNodeName()); |
| 25 assertEquals("width:50px; height:100px", jsAttrs.get(0).getNodeValue()); | 29 assertEquals("width:50px; height:100px", jsAttrs.get(0).getNodeValue()); |
| 26 assertEquals("id", jsAttrs.get(1).getNodeName()); | 30 assertEquals("id", jsAttrs.get(1).getNodeName()); |
| 27 assertEquals("f", jsAttrs.get(1).getNodeValue()); | 31 assertEquals("f", jsAttrs.get(1).getNodeValue()); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 | 107 |
| 104 Element currDiv = TestUtil.createDiv(3); | 108 Element currDiv = TestUtil.createDiv(3); |
| 105 div2.appendChild(currDiv); | 109 div2.appendChild(currDiv); |
| 106 Element finalDiv1 = currDiv; | 110 Element finalDiv1 = currDiv; |
| 107 | 111 |
| 108 currDiv = TestUtil.createDiv(4); | 112 currDiv = TestUtil.createDiv(4); |
| 109 div2.appendChild(currDiv); | 113 div2.appendChild(currDiv); |
| 110 currDiv.appendChild(TestUtil.createDiv(5)); | 114 currDiv.appendChild(TestUtil.createDiv(5)); |
| 111 | 115 |
| 112 assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, currDiv.g etChild(0))); | 116 assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, currDiv.g etChild(0))); |
| 113 assertEquals(div2, DomUtil.getNearestCommonAncestor( | 117 NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot, "[id=\"3\"] ,[id=\"5\"]"); |
| 114 DomUtil.querySelectorAll(mRoot, "[id=\"3\"],[id=\"5\"]"))); | 118 assertEquals(div2, DomUtil.getNearestCommonAncestor(TestUtil.nodeListToL ist(nodeList))); |
| 119 | |
| 115 } | 120 } |
| 116 | 121 |
| 117 /** | 122 /** |
| 118 * The tree graph is: | 123 * The tree graph is: |
| 119 * 1 - 2 - 3 | 124 * 1 - 2 - 3 |
| 120 */ | 125 */ |
| 121 public void testNearestCommonAncestorIsRoot() { | 126 public void testNearestCommonAncestorIsRoot() { |
| 122 Element div = TestUtil.createDiv(1); | 127 Element div = TestUtil.createDiv(1); |
| 123 mBody.appendChild(div); | 128 mBody.appendChild(div); |
| 124 | 129 |
| 125 Element div2 = TestUtil.createDiv(2); | 130 Element div2 = TestUtil.createDiv(2); |
| 126 div.appendChild(div2); | 131 div.appendChild(div2); |
| 127 | 132 |
| 128 Element div3 = TestUtil.createDiv(3); | 133 Element div3 = TestUtil.createDiv(3); |
| 129 div2.appendChild(div3); | 134 div2.appendChild(div3); |
| 130 | 135 |
| 131 assertEquals(div, DomUtil.getNearestCommonAncestor(div, div3)); | 136 assertEquals(div, DomUtil.getNearestCommonAncestor(div, div3)); |
| 132 assertEquals(div, DomUtil.getNearestCommonAncestor( | 137 NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot, "[id=\"1\"] ,[id=\"3\"]"); |
| 133 DomUtil.querySelectorAll(mRoot, "[id=\"1\"],[id=\"3\"]"))); | 138 assertEquals(div, DomUtil.getNearestCommonAncestor(TestUtil.nodeListToLi st(nodeList))); |
| 134 } | 139 } |
| 135 | 140 |
| 136 public void testNodeDepth() { | 141 public void testNodeDepth() { |
| 137 Element div = TestUtil.createDiv(1); | 142 Element div = TestUtil.createDiv(1); |
| 138 | 143 |
| 139 Element div2 = TestUtil.createDiv(2); | 144 Element div2 = TestUtil.createDiv(2); |
| 140 div.appendChild(div2); | 145 div.appendChild(div2); |
| 141 | 146 |
| 142 Element div3 = TestUtil.createDiv(3); | 147 Element div3 = TestUtil.createDiv(3); |
| 143 div2.appendChild(div3); | 148 div2.appendChild(div3); |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 338 "<td>text</td>" + | 343 "<td>text</td>" + |
| 339 "<td>text</td>" + | 344 "<td>text</td>" + |
| 340 "</tr>" + | 345 "</tr>" + |
| 341 "</tbody>" + | 346 "</tbody>" + |
| 342 "</table>"; | 347 "</table>"; |
| 343 | 348 |
| 344 mBody.setInnerHTML(html); | 349 mBody.setInnerHTML(html); |
| 345 DomUtil.stripStyleAttributes(mBody); | 350 DomUtil.stripStyleAttributes(mBody); |
| 346 assertEquals(expected, mBody.getInnerHTML()); | 351 assertEquals(expected, mBody.getInnerHTML()); |
| 347 } | 352 } |
| 353 | |
| 354 public void testIsVisibleByItsOffsetParentDisplayNone() { | |
| 355 String html = | |
| 356 "<div style=\"display: none;\">" + | |
| 357 "<div>Some Text</div>" + | |
| 358 "</div>"; | |
| 359 mBody.setInnerHTML(html); | |
| 360 Element child = mBody.getFirstChildElement().getFirstChildElement(); | |
| 361 assertFalse(DomUtil.isVisibleByItsOffset(child)); | |
| 362 } | |
| 363 | |
| 364 public void testIsVisibleByItsOffsetChildDisplayNone() { | |
| 365 String html = | |
| 366 "<div>" + | |
| 367 "<div style=\"display: none;\">Some Text</div>" + | |
| 368 "</div>"; | |
| 369 mBody.setInnerHTML(html); | |
| 370 Element child = mBody.getFirstChildElement().getFirstChildElement(); | |
| 371 assertFalse(DomUtil.isVisibleByItsOffset(child)); | |
| 372 } | |
| 373 | |
| 374 public void testIsVisibleByItsOffsetDisplayBlock() { | |
| 375 String html = | |
| 376 "<div>" + | |
| 377 "<div>Some Text</div>" + | |
| 378 "</div>"; | |
| 379 mBody.setInnerHTML(html); | |
| 380 Element child = mBody.getFirstChildElement().getFirstChildElement(); | |
| 381 assertTrue(DomUtil.isVisibleByItsOffset(child)); | |
| 382 } | |
| 383 | |
| 384 public void testOnlyProcessArticleElement() { | |
| 385 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
|
wychen
2015/12/10 23:33:36
The reason I inserted these text was for ContentEx
| |
| 386 | |
| 387 final String htmlArticle = | |
| 388 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 389 "<article>" + article + "</article>"; | |
| 390 | |
| 391 assertNotNull(getArticleElement(htmlArticle)); | |
|
wychen
2015/12/10 23:33:37
Sorry for changing my mind back and forth. It migh
| |
| 392 } | |
| 393 | |
| 394 public void testOnlyProcessArticleElementWithHiddenArticleElement() { | |
| 395 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 396 | |
| 397 final String htmlArticle = | |
| 398 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 399 "<article>" + article + "</article>" + | |
| 400 "<article style=\"display:none\">" + article + "</article>"; | |
| 401 | |
| 402 assertNotNull(getArticleElement(htmlArticle)); | |
| 403 } | |
| 404 | |
| 405 public void testOnlyProcessArticleElementMultiple() { | |
| 406 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 407 | |
| 408 final String htmlArticle = | |
| 409 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 410 "<article>" + article + "</article>" + | |
| 411 "<article>" + article + "</article>"; | |
| 412 | |
| 413 // The existence of multiple articles disables the fast path. | |
| 414 assertNull(getArticleElement(htmlArticle)); | |
| 415 } | |
| 416 | |
| 417 public void testOnlyProcessArticleElementMultipleWithHiddenArticleElement() { | |
| 418 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 419 | |
| 420 final String htmlArticle = | |
| 421 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 422 "<article>" + article + "</article>" + | |
| 423 "<article style=\"display:none\">" + article + "</article>" + | |
| 424 "<article>" + article + "</article>"; | |
| 425 | |
| 426 // The existence of multiple articles disables the fast path. | |
| 427 assertNull(getArticleElement(htmlArticle)); | |
| 428 } | |
| 429 | |
| 430 public void testOnlyProcessOGArticle() { | |
| 431 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 432 | |
| 433 final String htmlArticle = | |
| 434 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 435 "<div itemscope itemtype=\"http://schema.org/Article\">" + article + "</div>"; | |
| 436 | |
| 437 assertNotNull(getArticleElement(htmlArticle)); | |
| 438 } | |
| 439 | |
| 440 public void testOnlyProcessOGArticleWithHiddenArticleElement() { | |
| 441 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 442 | |
| 443 final String htmlArticle = | |
| 444 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 445 "<div itemscope itemtype=\"http://schema.org/Article\">" + article + "</div>" + | |
| 446 "<div itemscope itemtype=\"http://schema.org/Article\" style=\"displ ay:none\">" + | |
| 447 article + "</div>"; | |
| 448 | |
| 449 assertNotNull(getArticleElement(htmlArticle)); | |
| 450 } | |
| 451 | |
| 452 public void testOnlyProcessOGArticleNews() { | |
| 453 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 454 | |
| 455 final String htmlArticle = | |
| 456 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 457 "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" + artic le + "</div>"; | |
| 458 | |
| 459 assertNotNull(getArticleElement(htmlArticle)); | |
| 460 } | |
| 461 | |
| 462 public void testOnlyProcessOGArticleNewsWithHiddenArticleElement() { | |
| 463 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 464 | |
| 465 final String htmlArticle = | |
| 466 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 467 "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" + artic le + "</div>" + | |
| 468 "<div itemscope itemtype=\"http://schema.org/NewsArticle\" style=\"d isplay:none\">" + | |
| 469 article + "</div>"; | |
| 470 | |
| 471 assertNotNull(getArticleElement(htmlArticle)); | |
| 472 } | |
| 473 | |
| 474 public void testOnlyProcessOGArticleBlog() { | |
| 475 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 476 | |
| 477 final String htmlArticle = | |
| 478 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 479 "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" + artic le + "</div>"; | |
| 480 | |
| 481 assertNotNull(getArticleElement(htmlArticle)); | |
| 482 } | |
| 483 | |
| 484 public void testOnlyProcessOGArticleBlogWithHiddenArticleElement() { | |
| 485 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 486 | |
| 487 final String htmlArticle = | |
| 488 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 489 "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" + artic le + "</div>" + | |
| 490 "<div itemscope itemtype=\"http://schema.org/BlogPosting\" style=\"d isplay:none\">" + | |
| 491 article + "</div>"; | |
| 492 | |
| 493 assertNotNull(getArticleElement(htmlArticle)); | |
| 494 } | |
| 495 | |
| 496 public void testOnlyProcessOGArticleNested() { | |
| 497 final String paragraph = "<p>" + CONTENT_TEXT + "</p>"; | |
| 498 final String article = paragraph + paragraph; | |
| 499 | |
| 500 final String htmlArticle = | |
| 501 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 502 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
| 503 paragraph + | |
| 504 "<div itemscope itemtype=\"http://schema.org/Article\">" + parag raph + "</div>" + | |
| 505 "</div>"; | |
| 506 | |
| 507 assertNotNull(getArticleElement(htmlArticle)); | |
| 508 } | |
| 509 | |
| 510 public void testOnlyProcessOGArticleNestedWithNestedHiddenArticleElement() { | |
| 511 final String paragraph = "<p>" + CONTENT_TEXT + "</p>"; | |
| 512 final String article = paragraph + paragraph; | |
| 513 | |
| 514 final String htmlArticle = | |
| 515 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 516 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
| 517 paragraph + | |
| 518 "<div itemscope itemtype=\"http://schema.org/Article\">" + parag raph + "</div>" + | |
| 519 "<div itemscope itemtype=\"http://schema.org/Article\" style=\"d isplay:none\">" + | |
| 520 article + "</div>" + | |
| 521 "</div>"; | |
| 522 | |
| 523 assertNotNull(getArticleElement(htmlArticle)); | |
|
wychen
2015/12/10 23:33:37
Ditto. Making sure which element is picked makes e
| |
| 524 } | |
| 525 | |
| 526 public void testOnlyProcessOGArticleNestedWithHiddenArticleElement() { | |
| 527 final String paragraph = "<p>" + CONTENT_TEXT + "</p>"; | |
| 528 final String article = paragraph + paragraph; | |
| 529 | |
| 530 final String htmlArticle = | |
| 531 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 532 "<div itemscope itemtype=\"http://schema.org/Article\">" + | |
| 533 paragraph + | |
| 534 "<div itemscope itemtype=\"http://schema.org/Article\">" + parag raph + "</div>" + | |
| 535 "</div>" + | |
| 536 "<div itemscope itemtype=\"http://schema.org/Article\" style=\"displ ay:none\">" + | |
| 537 article + "</div>"; | |
| 538 | |
| 539 assertNotNull(getArticleElement(htmlArticle)); | |
| 540 } | |
| 541 | |
| 542 public void testOnlyProcessOGNonArticleMovie() { | |
| 543 final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>"; | |
| 544 | |
| 545 final String htmlArticle = | |
| 546 "<h1>" + CONTENT_TEXT + "</h1>" + | |
| 547 "<div itemscope itemtype=\"http://schema.org/Movie\">" + article + " </div>"; | |
| 548 | |
| 549 // Non-article schema.org types should not use the fast path. | |
| 550 assertNull(getArticleElement(htmlArticle)); | |
| 551 } | |
| 552 | |
| 553 private Element getArticleElement(String html) { | |
| 554 mBody.setInnerHTML(html); | |
| 555 return DomUtil.getArticleElement(mRoot); | |
| 556 } | |
| 348 } | 557 } |
| OLD | NEW |