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

Side by Side Diff: javatests/org/chromium/distiller/PageParameterDetectorTest.java

Issue 1029593003: implement validations of pagination URLs (Closed) Base URL: https://github.com/chromium/dom-distiller.git@master
Patch Set: nit Created 5 years, 8 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 unified diff | Download patch
OLDNEW
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 java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
7 public class PageParameterDetectorTest extends DomDistillerJsTestCase { 12 public class PageParameterDetectorTest extends DomDistillerJsTestCase {
8 13
9 public void testIsLastNumericPathComponentBad() { 14 public void testIsLastNumericPathComponentBad() {
10 // Path component is not numeric i.e. contains non-digits. 15 // Path component is not numeric i.e. contains non-digits.
11 String url = "http://www.foo.com/a2"; 16 String url = "http://www.foo.com/a2";
12 int digitStart = url.indexOf("2"); 17 int digitStart = url.indexOf("2");
13 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18, digitStart, 18 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18, digitStart,
14 digitStart + 1)); 19 digitStart + 1));
15 20
16 // Numeric path component is first. 21 // Numeric path component is first.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 digitStart + 1)); 55 digitStart + 1));
51 56
52 // .shtml follows digit, previous path component is not a bad page param name, but the one 57 // .shtml follows digit, previous path component is not a bad page param name, but the one
53 // before that is. 58 // before that is.
54 url = "http://www.foo.com/wiki/good/2.shtml"; 59 url = "http://www.foo.com/wiki/good/2.shtml";
55 digitStart = url.indexOf("2"); 60 digitStart = url.indexOf("2");
56 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18, digitStart, 61 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18, digitStart,
57 digitStart + 1)); 62 digitStart + 1));
58 } 63 }
59 64
65 public void testArePageNumsAdjacentAndConsecutive() {
66 {
67 final int[] allNums = { 1, 2 };
68 final int[] selectedNums = { 1, 2 };
69 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
70 assertTrue(isAdjacent(result));
71 assertTrue(isConsecutive(result));
72 }
73 {
74 final int[] allNums = { 1, 2, 3 };
75 final int[] selectedNums = { 2, 3 };
76 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
77 assertTrue(isAdjacent(result));
78 assertTrue(isConsecutive(result));
79 }
80 {
81 final int[] allNums = { 1, 5, 6, 7, 10 };
82 final int[] selectedNums = { 1, 5, 7, 10 };
83 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
84 assertTrue(isAdjacent(result));
85 assertTrue(isConsecutive(result));
86 }
87 {
88 final int[] allNums = { 10, 25, 50 };
89 final int[] selectedNums = { 10, 25, 50 }; // No consecutive pairs.
90 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
91 assertTrue(isAdjacent(result));
92 assertFalse(isConsecutive(result));
93 }
94 {
95 final int[] allNums = { 23, 24, 30 };
96 // This list doesn't satisfy consecutive rule. There should be "22" on the left of "23",
97 // or "25" on the right of "24", or "29" on the left of "30".
98 final int[] selectedNums = { 23, 24, 30 };
99 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
100 assertTrue(isAdjacent(result));
101 assertFalse(isConsecutive(result));
102 }
103 {
104 final int[] allNums = { 1, 2, 3, 4, 5 };
105 final int[] selectedNums = { 1, 3, 5 }; // Two gaps.
106 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
107 assertFalse(isAdjacent(result));
108 assertFalse(isConsecutive(result));
109 }
110 {
111 final int[] allNums = { 2, 3, 4, 5 };
112 final int[] selectedNums = { 2, 5 }; // A gap of 2 numbers.
113 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums );
114 assertFalse(isAdjacent(result));
115 assertFalse(isConsecutive(result));
116 }
117 }
118
119 private static int arePageNumsAdjacentAndConsecutive(int[] selectedNums, int [] allNums) {
120 List<PageParamInfo.PageInfo> ascendingNumbers = new ArrayList<PageParamI nfo.PageInfo>();
121 Map<Integer, Integer> numberToPos = new HashMap<Integer, Integer>();
122
123 for (int i = 0; i < allNums.length; i++) {
124 final int number = allNums[i];
125 numberToPos.put(number, i);
126 ascendingNumbers.add(new PageParamInfo.PageInfo(number, ""));
127 }
128
129 List<PageParameterDetector.LinkInfo> allLinkInfo =
130 new ArrayList<PageParameterDetector.LinkInfo>();
131 for (int i = 0; i < selectedNums.length; i++) {
132 final int number = selectedNums[i];
133 allLinkInfo.add(new PageParameterDetector.LinkInfo(number, number,
134 numberToPos.get(number)));
135 }
136
137 return PageParameterDetector.arePageNumsAdjacentAndConsecutive(allLinkIn fo,
138 ascendingNumbers);
139 }
140
141 private static boolean isAdjacent(int result) {
142 return (result & PageParameterDetector.PAGE_NUM_ADJACENT_MASK) ==
143 PageParameterDetector.PAGE_NUM_ADJACENT_MASK;
144 }
145
146 private static boolean isConsecutive(int result) {
147 return (result & PageParameterDetector.PAGE_NUM_CONSECUTIVE_MASK) ==
148 PageParameterDetector.PAGE_NUM_CONSECUTIVE_MASK;
149 }
150
60 } 151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698