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