Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(319)

Unified Diff: javatests/org/chromium/distiller/DomUtilTest.java

Issue 1411603004: Discard hidden articles when using fast path (Closed) Base URL: https://github.com/chromium/dom-distiller.git@master
Patch Set: wychen's comments addressed Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: javatests/org/chromium/distiller/DomUtilTest.java
diff --git a/javatests/org/chromium/distiller/DomUtilTest.java b/javatests/org/chromium/distiller/DomUtilTest.java
index a636a5e181f8e99e66ba9ff21716f351d42758b9..93fa2ccb2e15791ae844d5a56649eb9a96e6cefc 100644
--- a/javatests/org/chromium/distiller/DomUtilTest.java
+++ b/javatests/org/chromium/distiller/DomUtilTest.java
@@ -10,11 +10,15 @@ import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
import java.util.Map;
import java.util.List;
public class DomUtilTest extends DomDistillerJsTestCase {
+ private static final String CONTENT_TEXT = "Lorem Ipsum Lorem Ipsum Lorem Ipsum.";
+ private static final String TITLE_TEXT = "I am the document title";
+
public void testGetAttributes() {
Element e = Document.get().createDivElement();
e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\"sdf\"></div>");
@@ -110,8 +114,9 @@ public class DomUtilTest extends DomDistillerJsTestCase {
currDiv.appendChild(TestUtil.createDiv(5));
assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, currDiv.getChild(0)));
- assertEquals(div2, DomUtil.getNearestCommonAncestor(
- DomUtil.querySelectorAll(mRoot, "[id=\"3\"],[id=\"5\"]")));
+ NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot, "[id=\"3\"],[id=\"5\"]");
+ assertEquals(div2, DomUtil.getNearestCommonAncestor(TestUtil.nodeListToList(nodeList)));
+
}
/**
@@ -129,8 +134,8 @@ public class DomUtilTest extends DomDistillerJsTestCase {
div2.appendChild(div3);
assertEquals(div, DomUtil.getNearestCommonAncestor(div, div3));
- assertEquals(div, DomUtil.getNearestCommonAncestor(
- DomUtil.querySelectorAll(mRoot, "[id=\"1\"],[id=\"3\"]")));
+ NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot, "[id=\"1\"],[id=\"3\"]");
+ assertEquals(div, DomUtil.getNearestCommonAncestor(TestUtil.nodeListToList(nodeList)));
}
public void testNodeDepth() {
@@ -345,4 +350,208 @@ public class DomUtilTest extends DomDistillerJsTestCase {
DomUtil.stripStyleAttributes(mBody);
assertEquals(expected, mBody.getInnerHTML());
}
+
+ public void testIsVisibleByItsOffsetParentDisplayNone() {
+ String html =
+ "<div style=\"display: none;\">" +
+ "<div>Some Text</div>" +
+ "</div>";
+ mBody.setInnerHTML(html);
+ Element child = mBody.getFirstChildElement().getFirstChildElement();
+ assertFalse(DomUtil.isVisibleByItsOffset(child));
+ }
+
+ public void testIsVisibleByItsOffsetChildDisplayNone() {
+ String html =
+ "<div>" +
+ "<div style=\"display: none;\">Some Text</div>" +
+ "</div>";
+ mBody.setInnerHTML(html);
+ Element child = mBody.getFirstChildElement().getFirstChildElement();
+ assertFalse(DomUtil.isVisibleByItsOffset(child));
+ }
+
+ public void testIsVisibleByItsOffsetDisplayBlock() {
+ String html =
+ "<div>" +
+ "<div>Some Text</div>" +
+ "</div>";
+ mBody.setInnerHTML(html);
+ Element child = mBody.getFirstChildElement().getFirstChildElement();
+ assertTrue(DomUtil.isVisibleByItsOffset(child));
+ }
+
+ public void testOnlyProcessArticleElement() {
+ 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
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + article + "</article>";
+
+ assertNotNull(getArticleElement(htmlArticle));
wychen 2015/12/10 23:33:37 Sorry for changing my mind back and forth. It migh
+ }
+
+ public void testOnlyProcessArticleElementWithHiddenArticleElement() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + article + "</article>" +
+ "<article style=\"display:none\">" + article + "</article>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessArticleElementMultiple() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + article + "</article>" +
+ "<article>" + article + "</article>";
+
+ // The existence of multiple articles disables the fast path.
+ assertNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessArticleElementMultipleWithHiddenArticleElement() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + article + "</article>" +
+ "<article style=\"display:none\">" + article + "</article>" +
+ "<article>" + article + "</article>";
+
+ // The existence of multiple articles disables the fast path.
+ assertNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticle() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" + article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleWithHiddenArticleElement() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" + article + "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\" style=\"display:none\">" +
+ article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleNews() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" + article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleNewsWithHiddenArticleElement() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" + article + "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/NewsArticle\" style=\"display:none\">" +
+ article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleBlog() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" + article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleBlogWithHiddenArticleElement() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" + article + "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/BlogPosting\" style=\"display:none\">" +
+ article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleNested() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>";
+ final String article = paragraph + paragraph;
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" + paragraph + "</div>" +
+ "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticleNestedWithNestedHiddenArticleElement() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>";
+ final String article = paragraph + paragraph;
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" + paragraph + "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\" style=\"display:none\">" +
+ article + "</div>" +
+ "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
wychen 2015/12/10 23:33:37 Ditto. Making sure which element is picked makes e
+ }
+
+ public void testOnlyProcessOGArticleNestedWithHiddenArticleElement() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>";
+ final String article = paragraph + paragraph;
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" + paragraph + "</div>" +
+ "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\" style=\"display:none\">" +
+ article + "</div>";
+
+ assertNotNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGNonArticleMovie() {
+ final String article = "<p>" + CONTENT_TEXT + "</p><p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Movie\">" + article + "</div>";
+
+ // Non-article schema.org types should not use the fast path.
+ assertNull(getArticleElement(htmlArticle));
+ }
+
+ private Element getArticleElement(String html) {
+ mBody.setInnerHTML(html);
+ return DomUtil.getArticleElement(mRoot);
+ }
}

Powered by Google App Engine
This is Rietveld 408576698