PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cts/tests/tests/text/src/android/text/util/cts/LinkifyTest.java

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk
Java | 340 lines | 261 code | 51 blank | 28 comment | 10 complexity | 631bd0d20ac9a052ec12e334f783794e MD5 | raw file
  1. /*
  2. * Copyright (C) 2008 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.text.util.cts;
  17. import android.test.AndroidTestCase;
  18. import android.text.Spannable;
  19. import android.text.SpannableString;
  20. import android.text.style.URLSpan;
  21. import android.text.util.Linkify;
  22. import android.text.util.Linkify.MatchFilter;
  23. import android.text.util.Linkify.TransformFilter;
  24. import android.widget.TextView;
  25. import java.util.Locale;
  26. import java.util.regex.Matcher;
  27. import java.util.regex.Pattern;
  28. /**
  29. * Test {@link Linkify}.
  30. */
  31. public class LinkifyTest extends AndroidTestCase {
  32. private static final Pattern LINKIFY_TEST_PATTERN = Pattern.compile(
  33. "(test:)?[a-zA-Z0-9]+(\\.pattern)?");
  34. private MatchFilter mMatchFilterStartWithDot = new MatchFilter() {
  35. public final boolean acceptMatch(final CharSequence s, final int start, final int end) {
  36. if (start == 0) {
  37. return true;
  38. }
  39. if (s.charAt(start - 1) == '.') {
  40. return false;
  41. }
  42. return true;
  43. }
  44. };
  45. private TransformFilter mTransformFilterUpperChar = new TransformFilter() {
  46. public final String transformUrl(final Matcher match, String url) {
  47. StringBuilder buffer = new StringBuilder();
  48. String matchingRegion = match.group();
  49. for (int i = 0, size = matchingRegion.length(); i < size; i++) {
  50. char character = matchingRegion.charAt(i);
  51. if (character == '.' || Character.isLowerCase(character)
  52. || Character.isDigit(character)) {
  53. buffer.append(character);
  54. }
  55. }
  56. return buffer.toString();
  57. }
  58. };
  59. public void testConstructor() {
  60. new Linkify();
  61. }
  62. public void testAddLinks1() {
  63. SpannableString spannable = new SpannableString("name@gmail.com, "
  64. + "www.google.com, http://www.google.com/language_tools?hl=en, ");
  65. assertTrue(Linkify.addLinks(spannable, Linkify.WEB_URLS));
  66. URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
  67. assertEquals(2, spans.length);
  68. assertEquals("http://www.google.com", spans[0].getURL());
  69. assertEquals("http://www.google.com/language_tools?hl=en", spans[1].getURL());
  70. assertTrue(Linkify.addLinks(spannable, Linkify.EMAIL_ADDRESSES));
  71. spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
  72. assertEquals(1, spans.length);
  73. assertEquals("mailto:name@gmail.com", spans[0].getURL());
  74. try {
  75. Linkify.addLinks((Spannable) null, Linkify.WEB_URLS);
  76. fail("Should throw NullPointerException!");
  77. } catch (NullPointerException e) {
  78. // expect
  79. }
  80. assertFalse(Linkify.addLinks((Spannable) null, 0));
  81. }
  82. public void testAddLinks2() {
  83. String text = "www.google.com, name@gmail.com";
  84. TextView tv = new TextView(mContext);
  85. tv.setText(text);
  86. assertTrue(Linkify.addLinks(tv, Linkify.WEB_URLS));
  87. URLSpan[] spans = ((Spannable)tv.getText()).getSpans(0, text.length(), URLSpan.class);
  88. assertEquals(1, spans.length);
  89. assertEquals("http://www.google.com", spans[0].getURL());
  90. SpannableString spannable = SpannableString.valueOf(text);
  91. tv.setText(spannable);
  92. assertTrue(Linkify.addLinks(tv, Linkify.EMAIL_ADDRESSES));
  93. spans = ((Spannable)tv.getText()).getSpans(0, text.length(), URLSpan.class);
  94. assertEquals(1, spans.length);
  95. assertEquals("mailto:name@gmail.com", spans[0].getURL());
  96. try {
  97. Linkify.addLinks((TextView)null, Linkify.WEB_URLS);
  98. fail("Should throw NullPointerException!");
  99. } catch (NullPointerException e) {
  100. // expect
  101. }
  102. assertFalse(Linkify.addLinks((TextView)null, 0));
  103. }
  104. public void testAddLinks3() {
  105. String text = "Alan, Charlie";
  106. TextView tv = new TextView(mContext);
  107. tv.setText(text);
  108. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, "Test:");
  109. URLSpan[] spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  110. assertEquals(2, spans.length);
  111. assertEquals("test:Alan", spans[0].getURL());
  112. assertEquals("test:Charlie", spans[1].getURL());
  113. text = "google.pattern, test:AZ0101.pattern";
  114. tv.setText(text);
  115. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, "Test:");
  116. spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  117. assertEquals(2, spans.length);
  118. assertEquals("test:google.pattern", spans[0].getURL());
  119. assertEquals("test:AZ0101.pattern", spans[1].getURL());
  120. try {
  121. Linkify.addLinks((TextView) null, LINKIFY_TEST_PATTERN, "Test:");
  122. fail("Should throw NullPointerException!");
  123. } catch (NullPointerException e) {
  124. // expect
  125. }
  126. try {
  127. Linkify.addLinks(tv, null, "Test:");
  128. fail("Should throw NullPointerException!");
  129. } catch (NullPointerException e) {
  130. // expect
  131. }
  132. tv = new TextView(mContext);
  133. tv.setText(text);
  134. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, null);
  135. spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  136. assertEquals(2, spans.length);
  137. assertEquals("google.pattern", spans[0].getURL());
  138. assertEquals("test:AZ0101.pattern", spans[1].getURL());
  139. }
  140. public void testAddLinks4() {
  141. TextView tv = new TextView(mContext);
  142. String text = "FilterUpperCase.pattern, 12.345.pattern";
  143. tv.setText(text);
  144. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, "Test:",
  145. mMatchFilterStartWithDot, mTransformFilterUpperChar);
  146. URLSpan[] spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  147. assertEquals(2, spans.length);
  148. assertEquals("test:ilterpperase.pattern", spans[0].getURL());
  149. assertEquals("test:12", spans[1].getURL());
  150. try {
  151. Linkify.addLinks((TextView) null, LINKIFY_TEST_PATTERN, "Test:",
  152. mMatchFilterStartWithDot, mTransformFilterUpperChar);
  153. fail("Should throw NullPointerException!");
  154. } catch (NullPointerException e) {
  155. // expect
  156. }
  157. try {
  158. Linkify.addLinks(tv, null, "Test:",
  159. mMatchFilterStartWithDot, mTransformFilterUpperChar);
  160. fail("Should throw NullPointerException!");
  161. } catch (NullPointerException e) {
  162. // expect
  163. }
  164. tv.setText(text);
  165. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, null,
  166. mMatchFilterStartWithDot, mTransformFilterUpperChar);
  167. spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  168. assertEquals(2, spans.length);
  169. assertEquals("ilterpperase.pattern", spans[0].getURL());
  170. assertEquals("12", spans[1].getURL());
  171. tv.setText(text);
  172. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, "Test:", null, mTransformFilterUpperChar);
  173. spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  174. assertEquals(3, spans.length);
  175. assertEquals("test:ilterpperase.pattern", spans[0].getURL());
  176. assertEquals("test:12", spans[1].getURL());
  177. assertEquals("test:345.pattern", spans[2].getURL());
  178. tv.setText(text);
  179. Linkify.addLinks(tv, LINKIFY_TEST_PATTERN, "Test:", mMatchFilterStartWithDot, null);
  180. spans = ((Spannable) tv.getText()).getSpans(0, text.length(), URLSpan.class);
  181. assertEquals(2, spans.length);
  182. assertEquals("test:FilterUpperCase.pattern", spans[0].getURL());
  183. assertEquals("test:12", spans[1].getURL());
  184. }
  185. public void testAddLinks5() {
  186. String text = "google.pattern, test:AZ0101.pattern";
  187. SpannableString spannable = new SpannableString(text);
  188. Linkify.addLinks(spannable, LINKIFY_TEST_PATTERN, "Test:");
  189. URLSpan[] spans = (spannable.getSpans(0, spannable.length(), URLSpan.class));
  190. assertEquals(2, spans.length);
  191. assertEquals("test:google.pattern", spans[0].getURL());
  192. assertEquals("test:AZ0101.pattern", spans[1].getURL());
  193. try {
  194. Linkify.addLinks((Spannable)null, LINKIFY_TEST_PATTERN, "Test:");
  195. fail("Should throw NullPointerException!");
  196. } catch (NullPointerException e) {
  197. }
  198. try {
  199. Linkify.addLinks(spannable, null, "Test:");
  200. fail("Should throw NullPointerException!");
  201. } catch (NullPointerException e) {
  202. }
  203. spannable = new SpannableString(text);
  204. Linkify.addLinks(spannable, LINKIFY_TEST_PATTERN, null);
  205. spans = (spannable.getSpans(0, spannable.length(), URLSpan.class));
  206. assertEquals(2, spans.length);
  207. assertEquals("google.pattern", spans[0].getURL());
  208. assertEquals("test:AZ0101.pattern", spans[1].getURL());
  209. }
  210. public void testAddLinks6() {
  211. String text = "FilterUpperCase.pattern, 12.345.pattern";
  212. SpannableString spannable = new SpannableString(text);
  213. Linkify.addLinks(spannable, LINKIFY_TEST_PATTERN, "Test:",
  214. mMatchFilterStartWithDot, mTransformFilterUpperChar);
  215. URLSpan[] spans = (spannable.getSpans(0, spannable.length(), URLSpan.class));
  216. assertEquals(2, spans.length);
  217. assertEquals("test:ilterpperase.pattern", spans[0].getURL());
  218. assertEquals("test:12", spans[1].getURL());
  219. try {
  220. Linkify.addLinks((Spannable)null, LINKIFY_TEST_PATTERN, "Test:",
  221. mMatchFilterStartWithDot, mTransformFilterUpperChar);
  222. fail("Should throw NullPointerException!");
  223. } catch (NullPointerException e) {
  224. // expect
  225. }
  226. try {
  227. Linkify.addLinks(spannable, null, "Test:", mMatchFilterStartWithDot,
  228. mTransformFilterUpperChar);
  229. fail("Should throw NullPointerException!");
  230. } catch (NullPointerException e) {
  231. // expect
  232. }
  233. spannable = new SpannableString(text);
  234. Linkify.addLinks(spannable, LINKIFY_TEST_PATTERN, null, mMatchFilterStartWithDot,
  235. mTransformFilterUpperChar);
  236. spans = (spannable.getSpans(0, spannable.length(), URLSpan.class));
  237. assertEquals(2, spans.length);
  238. assertEquals("ilterpperase.pattern", spans[0].getURL());
  239. assertEquals("12", spans[1].getURL());
  240. spannable = new SpannableString(text);
  241. Linkify.addLinks(spannable, LINKIFY_TEST_PATTERN, "Test:", null, mTransformFilterUpperChar);
  242. spans = (spannable.getSpans(0, spannable.length(), URLSpan.class));
  243. assertEquals(3, spans.length);
  244. assertEquals("test:ilterpperase.pattern", spans[0].getURL());
  245. assertEquals("test:12", spans[1].getURL());
  246. assertEquals("test:345.pattern", spans[2].getURL());
  247. spannable = new SpannableString(text);
  248. Linkify.addLinks(spannable, LINKIFY_TEST_PATTERN, "Test:", mMatchFilterStartWithDot, null);
  249. spans = (spannable.getSpans(0, spannable.length(), URLSpan.class));
  250. assertEquals(2, spans.length);
  251. assertEquals("test:FilterUpperCase.pattern", spans[0].getURL());
  252. assertEquals("test:12", spans[1].getURL());
  253. }
  254. public void testAddLinks7() {
  255. String numbersInvalid = "123456789 not a phone number";
  256. String numbersUKLocal = "tel:(0812)1234560 (0812)1234561";
  257. String numbersUSLocal = "tel:(812)1234562 (812)123.4563 "
  258. + " tel:(800)5551210 (800) 555-1211 555-1212";
  259. String numbersIntl = "tel:+4408121234564 +44-0812-123-4565"
  260. + " tel:+18005551213 +1-800-555-1214";
  261. SpannableString spannable = new SpannableString(
  262. numbersInvalid
  263. + " " + numbersUKLocal
  264. + " " + numbersUSLocal
  265. + " " + numbersIntl);
  266. // phonenumber linkify is locale-dependent
  267. if (Locale.US.equals(Locale.getDefault())) {
  268. assertTrue(Linkify.addLinks(spannable, Linkify.PHONE_NUMBERS));
  269. URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
  270. assertEquals(9, spans.length);
  271. assertEquals("tel:8121234562", spans[0].getURL());
  272. assertEquals("tel:8121234563", spans[1].getURL());
  273. assertEquals("tel:8005551210", spans[2].getURL());
  274. assertEquals("tel:8005551211", spans[3].getURL());
  275. assertEquals("tel:5551212", spans[4].getURL());
  276. assertEquals("tel:+4408121234564", spans[5].getURL());
  277. assertEquals("tel:+4408121234565", spans[6].getURL());
  278. assertEquals("tel:+18005551213", spans[7].getURL());
  279. assertEquals("tel:+18005551214", spans[8].getURL());
  280. }
  281. try {
  282. Linkify.addLinks((Spannable) null, Linkify.WEB_URLS);
  283. fail("Should throw NullPointerException!");
  284. } catch (NullPointerException e) {
  285. // expect
  286. }
  287. assertFalse(Linkify.addLinks((Spannable) null, 0));
  288. }
  289. }