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

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: Comments addressed & master rebased Created 4 years, 7 months 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 2336b36e53689841d37b0fb6b6d956a2be6f20e8..73ee88d0af3bd1ef224c42e16cb2e2824cadcd0b 100644
--- a/javatests/org/chromium/distiller/DomUtilTest.java
+++ b/javatests/org/chromium/distiller/DomUtilTest.java
@@ -10,11 +10,14 @@ 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.";
+
public void testGetAttributes() {
Element e = Document.get().createDivElement();
e.setInnerHTML("<div style=\"width:50px; height:100px\" id=\"f\" class=\"sdf\"></div>");
@@ -109,9 +112,15 @@ public class DomUtilTest extends DomDistillerJsTestCase {
div2.appendChild(currDiv);
currDiv.appendChild(TestUtil.createDiv(5));
- assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, currDiv.getChild(0)));
+ assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1,
+ currDiv.getChild(0)));
+
+ NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot,
+ "[id=\"3\"],[id=\"5\"]");
+
assertEquals(div2, DomUtil.getNearestCommonAncestor(
- DomUtil.querySelectorAll(mRoot, "[id=\"3\"],[id=\"5\"]")));
+ TestUtil.nodeListToList(nodeList)));
+
}
/**
@@ -129,8 +138,12 @@ public class DomUtilTest extends DomDistillerJsTestCase {
div2.appendChild(div3);
assertEquals(div, DomUtil.getNearestCommonAncestor(div, div3));
+
+ NodeList<Element> nodeList = DomUtil.querySelectorAll(mRoot,
+ "[id=\"1\"],[id=\"3\"]");
+
assertEquals(div, DomUtil.getNearestCommonAncestor(
- DomUtil.querySelectorAll(mRoot, "[id=\"1\"],[id=\"3\"]")));
+ TestUtil.nodeListToList(nodeList)));
}
public void testNodeDepth() {
@@ -378,6 +391,308 @@ public class DomUtilTest extends DomDistillerJsTestCase {
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 htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
wychen 2016/06/02 05:56:01 All these tests should work without putting CONTEN
marcelorcorrea 2016/06/03 16:21:57 We tried using only empty elements but didn't work
wychen 2016/06/03 16:49:53 Ah. So it's about test again. Let's use the conte
marcelorcorrea 2016/06/06 13:17:57 Done.
+ "<article>" + CONTENT_TEXT + "</article>";
+
+ String expected = "<article>" + CONTENT_TEXT + "</article>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessArticleElementWithHiddenArticleElement() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + paragraph + "</article>" +
+ "<article style=\"display:none\">" + paragraph +"</article>";
+
+ String expected = "<article>" + paragraph + "</article>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessArticleElementMultiple() {
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + CONTENT_TEXT + "</article>" +
+ "<article>" + CONTENT_TEXT + "</article>";
+
+ // The existence of multiple articles disables the fast path.
+ assertNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessArticleElementMultipleWithHiddenArticleElement() {
wychen 2016/06/02 05:56:01 This seems a bit redundant.
marcelorcorrea 2016/06/03 16:21:57 Done.
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p><p>" +
+ CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<article>" + paragraph + "</article>" +
+ "<article style=\"display:none\">" + paragraph + "</article>" +
+ "<article>" + paragraph + "</article>";
+
+ // The existence of multiple articles disables the fast path.
+ assertNull(getArticleElement(htmlArticle));
+ }
+
+ public void testOnlyProcessOGArticle() {
wychen 2016/06/02 05:56:01 My bad. I mixed up open graph with schema.org back
marcelorcorrea 2016/06/03 16:21:57 Done.
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p><p>" +
+ CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "</div>";
+
+ final String expected =
+ "<div itemscope=\"\" " +
+ "itemtype=\"http://schema.org/Article\">" + paragraph +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleWithHiddenArticleElement() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\">" +
+ paragraph + "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/Article\" " +
+ "style=\"display:none\">" + paragraph +
+ "</div>";
+
+ String expected =
+ "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleNews() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" +
+ paragraph +
+ "</div>";
+
+ final String expected =
+ "<div itemscope=\"\" " +
+ "itemtype=\"http://schema.org/NewsArticle\">" + paragraph +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleNewsWithHiddenArticleElement() {
wychen 2016/06/02 05:56:01 I think we only need to test one variation of sche
marcelorcorrea 2016/06/03 16:21:57 Done.
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/NewsArticle\">" +
+ paragraph +
+ "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/NewsArticle\" " +
+ "style=\"display:none\">" + paragraph +
+ "</div>";
+
+ String expected =
+ "<div itemscope=\"\" " +
+ "itemtype=\"http://schema.org/NewsArticle\">" + paragraph +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleBlog() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" +
+ paragraph +
+ "</div>";
+
+ final String expected =
+ "<div itemscope=\"\" " +
+ "itemtype=\"http://schema.org/BlogPosting\">" +
+ paragraph +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleBlogWithHiddenArticleElement() {
wychen 2016/06/02 05:56:01 ditto
marcelorcorrea 2016/06/03 16:21:57 Done.
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/BlogPosting\">" +
+ paragraph +
+ "</div>" +
+ "<div itemscope itemtype=\"http://schema.org/BlogPosting\" " +
+ "style=\"display:none\">" + paragraph +
+ "</div>";
+
+ final String expected =
+ "<div itemscope=\"\" " +
+ "itemtype=\"http://schema.org/BlogPosting\">" +
+ paragraph +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleNested() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>";
+
+ 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>";
+
+ final String expected =
+ "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "</div>" +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleNestedWithNestedHiddenArticleElement() {
wychen 2016/06/02 05:56:01 Keep this, since this seems complicated enough.
marcelorcorrea 2016/06/03 16:21:57 Done.
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>";
+
+ 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\">" + paragraph +
+ "</div>" +
+ "</div>";
+
+ final String expected =
+ "<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\">" + paragraph + "</div>" +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGArticleNestedWithHiddenArticleElement() {
wychen 2016/06/02 05:56:01 Also keep this.
marcelorcorrea 2016/06/03 16:21:57 Done.
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>";
+
+ 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\">" + paragraph + "</div>";
+
+ final String expected =
+ "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "<div itemscope=\"\" itemtype=\"http://schema.org/Article\">" +
+ paragraph +
+ "</div>" +
+ "</div>";
+
+ Element result = getArticleElement(htmlArticle);
+ assertEquals(expected, result.getString());
+ }
+
+ public void testOnlyProcessOGNonArticleMovie() {
+ final String paragraph = "<p>" + CONTENT_TEXT + "</p>" +
+ "<p>" + CONTENT_TEXT + "</p>";
+
+ final String htmlArticle =
+ "<h1>" + CONTENT_TEXT + "</h1>" +
+ "<div itemscope itemtype=\"http://schema.org/Movie\">" +
+ paragraph +
+ "</div>";
+
+ // Non-article schema.org types should not use the fast path.
+ Element result = getArticleElement(htmlArticle);
+ assertNull(result);
+ }
+
+ private Element getArticleElement(String html) {
+ mBody.setInnerHTML(html);
+ return DomUtil.getArticleElement(mRoot);
+ }
+
public void testGetArea() {
String elements =
"<div style=\"width: 200px; height: 100px\">w</div>" +

Powered by Google App Engine
This is Rietveld 408576698