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 com.dom_distiller.client; | 5 package com.dom_distiller.client; |
6 | 6 |
7 import com.google.gwt.dom.client.AnchorElement; | 7 import com.google.gwt.dom.client.AnchorElement; |
8 import com.google.gwt.dom.client.Element; | 8 import com.google.gwt.dom.client.Element; |
9 import com.google.gwt.user.client.Window; | 9 import com.google.gwt.user.client.Window; |
10 | 10 |
11 public class PagingLinksFinderTest extends DomDistillerTestCase { | 11 public class PagingLinksFinderTest extends DomDistillerTestCase { |
12 public void testNoLink() { | 12 public void testNoLink() { |
13 Element root = TestUtil.createDiv(0); | 13 Element root = TestUtil.createDiv(0); |
14 assertEquals(null, PagingLinksFinder.findNext(root)); | 14 assertEquals(null, PagingLinksFinder.findNext(root, "")); |
15 assertEquals(null, PagingLinksFinder.findPrevious(root)); | 15 assertEquals(null, PagingLinksFinder.findPrevious(root, "")); |
16 } | 16 } |
17 | 17 |
18 public void test1NextLink() { | 18 public void test1NextLink() { |
19 Element root = TestUtil.createDiv(0); | 19 Element root = TestUtil.createDiv(0); |
20 AnchorElement anchor = TestUtil.createAnchor("next", "next page"); | 20 AnchorElement anchor = TestUtil.createAnchor("next", "next page"); |
21 root.appendChild(anchor); | 21 root.appendChild(anchor); |
22 | 22 |
23 assertEquals(anchor.getHref(), PagingLinksFinder.findNext(root)); | 23 assertEquals(anchor.getHref(), PagingLinksFinder.findNext(root, "")); |
24 assertEquals(null, PagingLinksFinder.findPrevious(root)); | 24 assertEquals(null, PagingLinksFinder.findPrevious(root, "")); |
25 } | 25 } |
26 | 26 |
27 public void test1NextLinkWithDifferentDomain() { | 27 public void test1NextLinkWithDifferentDomain() { |
28 Element root = TestUtil.createDiv(0); | 28 Element root = TestUtil.createDiv(0); |
29 AnchorElement anchor = TestUtil.createAnchor("http://testing.com/next", "next page"); | 29 AnchorElement anchor = TestUtil.createAnchor("http://testing.com/next", "next page"); |
30 root.appendChild(anchor); | 30 root.appendChild(anchor); |
31 | 31 |
32 assertEquals(null, PagingLinksFinder.findNext(root)); | 32 assertEquals(null, PagingLinksFinder.findNext(root, "")); |
33 assertEquals(null, PagingLinksFinder.findPrevious(root)); | 33 assertEquals(null, PagingLinksFinder.findPrevious(root, "")); |
34 } | 34 } |
35 | 35 |
36 public void test1PageNumberedLink() { | 36 public void test1PageNumberedLink() { |
37 Element root = TestUtil.createDiv(0); | 37 Element root = TestUtil.createDiv(0); |
38 // Prepend href with window location path so that base URL is part of fi nal href to increase | 38 // Prepend href with window location path so that base URL is part of fi nal href to increase |
39 // score. | 39 // score. |
40 AnchorElement anchor = TestUtil.createAnchor( | 40 AnchorElement anchor = TestUtil.createAnchor( |
41 TestUtil.formHrefWithWindowLocationPath("page2"), "page 2"); | 41 TestUtil.formHrefWithWindowLocationPath("page2"), "page 2"); |
42 root.appendChild(anchor); | 42 root.appendChild(anchor); |
43 | 43 |
44 // The word "page" in the link text increases its score confidently enou gh to be considered | 44 // The word "page" in the link text increases its score confidently enou gh to be considered |
45 // as a paging link. | 45 // as a paging link. |
46 assertEquals(anchor.getHref(), PagingLinksFinder.findNext(root)); | 46 assertEquals(anchor.getHref(), PagingLinksFinder.findNext(root, "")); |
47 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root)); | 47 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root, "")) ; |
48 } | 48 } |
49 | 49 |
50 public void test3NumberedLinks() { | 50 public void test3NumberedLinks() { |
51 Element root = TestUtil.createDiv(0); | 51 Element root = TestUtil.createDiv(0); |
52 // Prepend href with window location path so that base URL is part of fi nal href to increase | 52 // Prepend href with window location path so that base URL is part of fi nal href to increase |
53 // score. | 53 // score. |
54 AnchorElement anchor1 = TestUtil.createAnchor( | 54 AnchorElement anchor1 = TestUtil.createAnchor( |
55 TestUtil.formHrefWithWindowLocationPath("page1"), "1"); | 55 TestUtil.formHrefWithWindowLocationPath("page1"), "1"); |
56 AnchorElement anchor2 = TestUtil.createAnchor( | 56 AnchorElement anchor2 = TestUtil.createAnchor( |
57 TestUtil.formHrefWithWindowLocationPath("page2"), "2"); | 57 TestUtil.formHrefWithWindowLocationPath("page2"), "2"); |
58 AnchorElement anchor3 = TestUtil.createAnchor( | 58 AnchorElement anchor3 = TestUtil.createAnchor( |
59 TestUtil.formHrefWithWindowLocationPath("page3"), "3"); | 59 TestUtil.formHrefWithWindowLocationPath("page3"), "3"); |
60 root.appendChild(anchor1); | 60 root.appendChild(anchor1); |
61 root.appendChild(anchor2); | 61 root.appendChild(anchor2); |
62 root.appendChild(anchor3); | 62 root.appendChild(anchor3); |
63 | 63 |
64 // Because link text contains only digits with no paging-related words, no link has a score | 64 // Because link text contains only digits with no paging-related words, no link has a score |
65 // high enough to be confidently considered paging link. | 65 // high enough to be confidently considered paging link. |
66 assertEquals(null, PagingLinksFinder.findNext(root)); | 66 assertEquals(null, PagingLinksFinder.findNext(root, "")); |
67 assertEquals(null, PagingLinksFinder.findPrevious(root)); | 67 assertEquals(null, PagingLinksFinder.findPrevious(root, "")); |
68 } | 68 } |
69 | 69 |
70 public void test2NextLinksWithSameHref() { | 70 public void test2NextLinksWithSameHref() { |
71 Element root = TestUtil.createDiv(0); | 71 Element root = TestUtil.createDiv(0); |
72 // Prepend href with window location path so that base URL is part of fi nal href to increase | 72 // Prepend href with window location path so that base URL is part of fi nal href to increase |
73 // score. | 73 // score. |
74 AnchorElement anchor1 = TestUtil.createAnchor( | 74 AnchorElement anchor1 = TestUtil.createAnchor( |
75 TestUtil.formHrefWithWindowLocationPath("page2"), "dummy link"); | 75 TestUtil.formHrefWithWindowLocationPath("page2"), "dummy link"); |
76 AnchorElement anchor2 = TestUtil.createAnchor( | 76 AnchorElement anchor2 = TestUtil.createAnchor( |
77 TestUtil.formHrefWithWindowLocationPath("page2"), "next page"); | 77 TestUtil.formHrefWithWindowLocationPath("page2"), "next page"); |
78 root.appendChild(anchor1); | 78 root.appendChild(anchor1); |
79 root.appendChild(anchor2); | 79 root.appendChild(anchor2); |
80 | 80 |
81 // anchor1 by itself is not a confident next page link, but anchor2's li nk text helps bump | 81 // anchor1 by itself is not a confident next page link, but anchor2's li nk text helps bump |
82 // up the score for the shared href, so anchor1 is now a confident next page link. | 82 // up the score for the shared href, so anchor1 is now a confident next page link. |
83 assertEquals(anchor1.getHref(), PagingLinksFinder.findNext(root)); | 83 assertEquals(anchor1.getHref(), PagingLinksFinder.findNext(root, "")); |
84 assertEquals(null, PagingLinksFinder.findPrevious(root)); | 84 assertEquals(null, PagingLinksFinder.findPrevious(root, "")); |
85 } | 85 } |
86 | 86 |
87 public void testPagingParent() { | 87 public void testPagingParent() { |
88 Element root = TestUtil.createDiv(0); | 88 Element root = TestUtil.createDiv(0); |
89 Element div = TestUtil.createDiv(1); | 89 Element div = TestUtil.createDiv(1); |
90 div.setClassName("page"); | 90 div.setClassName("page"); |
91 root.appendChild(div); | 91 root.appendChild(div); |
92 // Prepend href with window location path so that base URL is part of fi nal href to increase | 92 // Prepend href with window location path so that base URL is part of fi nal href to increase |
93 // score. | 93 // score. |
94 AnchorElement anchor = TestUtil.createAnchor( | 94 AnchorElement anchor = TestUtil.createAnchor( |
95 TestUtil.formHrefWithWindowLocationPath("page1"), "dummy link"); | 95 TestUtil.formHrefWithWindowLocationPath("page1"), "dummy link"); |
96 div.appendChild(anchor); | 96 div.appendChild(anchor); |
97 | 97 |
98 // While it may seem strange that both previous and next links are the s ame, this test is | 98 // While it may seem strange that both previous and next links are the s ame, this test is |
99 // testing that the anchor's parents will affect its paging score even i f it has a | 99 // testing that the anchor's parents will affect its paging score even i f it has a |
100 // meaningless link text like "dummy link". | 100 // meaningless link text like "dummy link". |
101 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root)); | 101 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root, "")) ; |
102 assertEquals(anchor.getHref(), PagingLinksFinder.findNext(root)); | 102 assertEquals(anchor.getHref(), PagingLinksFinder.findNext(root, "")); |
103 } | 103 } |
104 | 104 |
105 public void test1PrevLink() { | 105 public void test1PrevLink() { |
106 Element root = TestUtil.createDiv(0); | 106 Element root = TestUtil.createDiv(0); |
107 AnchorElement anchor = TestUtil.createAnchor("prev", "prev page"); | 107 AnchorElement anchor = TestUtil.createAnchor("prev", "prev page"); |
108 root.appendChild(anchor); | 108 root.appendChild(anchor); |
109 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root)); | 109 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root, "")) ; |
110 assertEquals(null, PagingLinksFinder.findNext(root)); | 110 assertEquals(null, PagingLinksFinder.findNext(root, "")); |
111 } | 111 } |
112 | 112 |
113 public void test1PrevAnd1NextLinks() { | 113 public void test1PrevAnd1NextLinks() { |
114 Element root = TestUtil.createDiv(0); | 114 Element root = TestUtil.createDiv(0); |
115 AnchorElement prevAnchor = TestUtil.createAnchor("prev", "prev page"); | 115 AnchorElement prevAnchor = TestUtil.createAnchor("prev", "prev page"); |
116 AnchorElement nextAnchor = TestUtil.createAnchor("next", "next page"); | 116 AnchorElement nextAnchor = TestUtil.createAnchor("next", "next page"); |
117 root.appendChild(prevAnchor); | 117 root.appendChild(prevAnchor); |
118 root.appendChild(nextAnchor); | 118 root.appendChild(nextAnchor); |
119 | 119 |
120 assertEquals(prevAnchor.getHref(), PagingLinksFinder.findPrevious(root)) ; | 120 assertEquals(prevAnchor.getHref(), PagingLinksFinder.findPrevious(root, "")); |
121 assertEquals(nextAnchor.getHref(), PagingLinksFinder.findNext(root)); | 121 assertEquals(nextAnchor.getHref(), PagingLinksFinder.findNext(root, "")) ; |
122 } | 122 } |
123 | 123 |
124 public void testFirstPageLinkAsBaseUrl() { | 124 public void testFirstPageLinkAsBaseUrl() { |
125 // Some sites' first page links are the same as the base URL, previous p age link needs to | 125 // Some sites' first page links are the same as the base URL, previous p age link needs to |
126 // recognize this. | 126 // recognize this. |
127 | 127 |
128 // For testcases, Window.Location.getPath() returns a ".html" file that will be stripped | 128 // For testcases, Window.Location.getPath() returns a ".html" file that will be stripped |
129 // when determining the base URL in PagingLinksFinder.findBaseUrl(), so we need to do the | 129 // when determining the base URL in PagingLinksFinder.findBaseUrl(), so we need to do the |
130 // same to use a href identical to base URL. | 130 // same to use a href identical to base URL. |
131 String href = Window.Location.getPath(); | 131 String href = Window.Location.getPath(); |
132 String htmlExt = ".html"; | 132 String htmlExt = ".html"; |
133 if (href.indexOf(htmlExt) == href.length() - htmlExt.length()) { | 133 if (href.indexOf(htmlExt) == href.length() - htmlExt.length()) { |
134 href = StringUtil.findAndReplace(href, htmlExt, ""); | 134 href = StringUtil.findAndReplace(href, htmlExt, ""); |
135 } | 135 } |
136 | 136 |
137 Element root = TestUtil.createDiv(0); | 137 Element root = TestUtil.createDiv(0); |
138 AnchorElement anchor = TestUtil.createAnchor(href, "PREV"); | 138 AnchorElement anchor = TestUtil.createAnchor(href, "PREV"); |
139 root.appendChild(anchor); | 139 root.appendChild(anchor); |
140 | 140 |
141 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root)); | 141 assertEquals(anchor.getHref(), PagingLinksFinder.findPrevious(root, "")) ; |
142 } | 142 } |
143 | 143 |
144 public void testNonHttpOrHttpsLink() { | 144 public void testNonHttpOrHttpsLink() { |
145 Element root = TestUtil.createDiv(0); | 145 Element root = TestUtil.createDiv(0); |
146 AnchorElement anchor = TestUtil.createAnchor("javascript:void(0)", | 146 AnchorElement anchor = TestUtil.createAnchor("javascript:void(0)", |
147 "NEXT"); | 147 "NEXT"); |
148 root.appendChild(anchor); | 148 root.appendChild(anchor); |
149 assertEquals(null, PagingLinksFinder.findNext(root)); | 149 assertEquals(null, PagingLinksFinder.findNext(root, "")); |
150 | 150 |
151 anchor.setHref("file://test.html"); | 151 anchor.setHref("file://test.html"); |
152 assertEquals(null, PagingLinksFinder.findNext(root)); | 152 assertEquals(null, PagingLinksFinder.findNext(root, "")); |
153 } | 153 } |
154 | 154 |
cjhopman
2014/10/24 22:13:47
Can we add a simple test that uses a non-empty ori
kuan
2014/10/24 23:07:11
Done. i forgot :(
| |
155 } | 155 } |
OLD | NEW |