OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 testArePageNumsAdjacentAndConsecutive() { |
10 // Path component is not numeric i.e. contains non-digits. | 15 { |
11 String url = "http://www.foo.com/a2"; | 16 final int[] allNums = { 1, 2 }; |
12 int digitStart = url.indexOf("2"); | 17 final int[] selectedNums = { 1, 2 }; |
13 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | 18 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
14 digitStart + 1)); | 19 assertTrue(isAdjacent(result)); |
| 20 assertTrue(isConsecutive(result)); |
| 21 } |
| 22 { |
| 23 final int[] allNums = { 1, 2, 3 }; |
| 24 final int[] selectedNums = { 2, 3 }; |
| 25 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 26 assertTrue(isAdjacent(result)); |
| 27 assertTrue(isConsecutive(result)); |
| 28 } |
| 29 { |
| 30 final int[] allNums = { 1, 5, 6, 7, 10 }; |
| 31 final int[] selectedNums = { 1, 5, 7, 10 }; |
| 32 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 33 assertTrue(isAdjacent(result)); |
| 34 assertTrue(isConsecutive(result)); |
| 35 } |
| 36 { |
| 37 final int[] allNums = { 10, 25, 50 }; |
| 38 final int[] selectedNums = { 10, 25, 50 }; // No consecutive pairs. |
| 39 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 40 assertTrue(isAdjacent(result)); |
| 41 assertFalse(isConsecutive(result)); |
| 42 } |
| 43 { |
| 44 final int[] allNums = { 23, 24, 30 }; |
| 45 // This list doesn't satisfy consecutive rule. There should be "22"
on the left of "23", |
| 46 // or "25" on the right of "24", or "29" on the left of "30". |
| 47 final int[] selectedNums = { 23, 24, 30 }; |
| 48 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 49 assertTrue(isAdjacent(result)); |
| 50 assertFalse(isConsecutive(result)); |
| 51 } |
| 52 { |
| 53 final int[] allNums = { 1, 2, 3, 4, 5 }; |
| 54 final int[] selectedNums = { 1, 3, 5 }; // Two gaps. |
| 55 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 56 assertFalse(isAdjacent(result)); |
| 57 assertFalse(isConsecutive(result)); |
| 58 } |
| 59 { |
| 60 final int[] allNums = { 2, 3, 4, 5 }; |
| 61 final int[] selectedNums = { 2, 5 }; // A gap of 2 numbers. |
| 62 int result = arePageNumsAdjacentAndConsecutive(selectedNums, allNums
); |
| 63 assertFalse(isAdjacent(result)); |
| 64 assertFalse(isConsecutive(result)); |
| 65 } |
| 66 } |
15 | 67 |
16 // Numeric path component is first. | 68 private static int arePageNumsAdjacentAndConsecutive(int[] selectedNums, int
[] allNums) { |
17 url = "http://www.foo.com/2"; | 69 List<PageParamInfo.PageInfo> ascendingNumbers = new ArrayList<PageParamI
nfo.PageInfo>(); |
18 digitStart = url.indexOf("2"); | 70 Map<Integer, Integer> numberToPos = new HashMap<Integer, Integer>(); |
19 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | |
20 digitStart + 1)); | |
21 | 71 |
22 // Numeric path component follows a path component that is not a bad pag
e param name. | 72 for (int i = 0; i < allNums.length; i++) { |
23 url = "http://www.foo.com/good/2"; | 73 final int number = allNums[i]; |
24 digitStart = url.indexOf("2"); | 74 numberToPos.put(number, i); |
25 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | 75 ascendingNumbers.add(new PageParamInfo.PageInfo(number, "")); |
26 digitStart + 1)); | 76 } |
27 | 77 |
28 // Numeric path component follows a path component that is a bad page pa
ram name. | 78 List<PageParameterDetector.LinkInfo> allLinkInfo = |
29 url = "http://www.foo.com/wiki/2"; | 79 new ArrayList<PageParameterDetector.LinkInfo>(); |
30 digitStart = url.indexOf("2"); | 80 for (int i = 0; i < selectedNums.length; i++) { |
31 assertTrue(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | 81 final int number = selectedNums[i]; |
32 digitStart + 1)); | 82 allLinkInfo.add(new PageParameterDetector.LinkInfo(number, number, |
| 83 numberToPos.get(number))); |
| 84 } |
33 | 85 |
34 // (s)htm(l) extension doesn't follow digit. | 86 return PageParameterDetector.arePageNumsAdjacentAndConsecutive(allLinkIn
fo, |
35 url = "http://www.foo.com/2a"; | 87 ascendingNumbers); |
36 digitStart = url.indexOf("2"); | 88 } |
37 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | |
38 digitStart + 1)); | |
39 | 89 |
40 // .htm follows digit, previous path component is not a bad page param n
ame. | 90 private static boolean isAdjacent(int result) { |
41 url = "http://www.foo.com/good/2.htm"; | 91 return (result & PageParameterDetector.PAGE_NUM_ADJACENT_MASK) == |
42 digitStart = url.indexOf("2"); | 92 PageParameterDetector.PAGE_NUM_ADJACENT_MASK; |
43 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | 93 } |
44 digitStart + 1)); | |
45 | 94 |
46 // .html follows digit, previous path component is a bad page param name
. | 95 private static boolean isConsecutive(int result) { |
47 url = "http://www.foo.com/wiki/2.html"; | 96 return (result & PageParameterDetector.PAGE_NUM_CONSECUTIVE_MASK) == |
48 digitStart = url.indexOf("2"); | 97 PageParameterDetector.PAGE_NUM_CONSECUTIVE_MASK; |
49 assertTrue(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | |
50 digitStart + 1)); | |
51 | |
52 // .shtml follows digit, previous path component is not a bad page param
name, but the one | |
53 // before that is. | |
54 url = "http://www.foo.com/wiki/good/2.shtml"; | |
55 digitStart = url.indexOf("2"); | |
56 assertFalse(PageParameterDetector.isLastNumericPathComponentBad(url, 18,
digitStart, | |
57 digitStart + 1)); | |
58 } | 98 } |
59 | 99 |
60 } | 100 } |
OLD | NEW |