blob: aad3fe1570e479eb97f33f2479bd92558b6d99a5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
Dianne Hackborn2269d1572010-02-24 19:54:22 -08002 * Copyright (C) 2010 The Android Open Source Project
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003 *
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 */
Brett Chabot0dc59e72010-04-01 18:21:38 -070016package android.util;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017
18import android.test.suitebuilder.annotation.SmallTest;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080019import android.util.Patterns;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21import java.util.regex.Matcher;
Dianne Hackborn2269d1572010-02-24 19:54:22 -080022
23import junit.framework.TestCase;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
Dan Egnorded0e642009-11-18 11:23:45 -080025public class PatternsTest extends TestCase {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
27 @SmallTest
28 public void testTldPattern() throws Exception {
29 boolean t;
30
Dan Egnor494a1dc2009-11-20 18:31:33 -080031 t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 assertTrue("Missed valid TLD", t);
33
Shimeng (Simon) Wang3ed6fbd2010-02-05 17:57:45 -080034 // One of the new top level domain.
35 t = Patterns.TOP_LEVEL_DOMAIN.matcher("me").matches();
36 assertTrue("Missed valid TLD", t);
37
38 // One of the new top level test domain.
39 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn--0zwm56d").matches();
40 assertTrue("Missed valid TLD", t);
41
42 t = Patterns.TOP_LEVEL_DOMAIN.matcher("mem").matches();
43 assertFalse("Matched invalid TLD!", t);
44
45 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xn").matches();
46 assertFalse("Matched invalid TLD!", t);
47
Dan Egnor494a1dc2009-11-20 18:31:33 -080048 t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 assertFalse("Matched invalid TLD!", t);
50 }
51
52 @SmallTest
53 public void testUrlPattern() throws Exception {
54 boolean t;
55
Dan Egnor494a1dc2009-11-20 18:31:33 -080056 t = Patterns.WEB_URL.matcher("http://www.google.com").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 assertTrue("Valid URL", t);
58
Shimeng (Simon) Wang3ed6fbd2010-02-05 17:57:45 -080059 // Google in one of the new top level domain.
60 t = Patterns.WEB_URL.matcher("http://www.google.me").matches();
61 assertTrue("Valid URL", t);
62 t = Patterns.WEB_URL.matcher("google.me").matches();
63 assertTrue("Valid URL", t);
64
65 // Test url in Chinese: http://xn--fsqu00a.xn--0zwm56d
66 t = Patterns.WEB_URL.matcher("http://xn--fsqu00a.xn--0zwm56d").matches();
67 assertTrue("Valid URL", t);
68 t = Patterns.WEB_URL.matcher("xn--fsqu00a.xn--0zwm56d").matches();
69 assertTrue("Valid URL", t);
70
Shimeng (Simon) Wangdb990752010-05-06 15:07:22 -070071 // Url for testing top level Arabic country code domain in Punycode:
72 // http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx
73 t = Patterns.WEB_URL.matcher("http://xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx").matches();
74 assertTrue("Valid URL", t);
75 t = Patterns.WEB_URL.matcher("xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c/ar/default.aspx").matches();
76 assertTrue("Valid URL", t);
77
Shimeng (Simon) Wang51c02db2010-02-11 14:07:44 -080078 // Internationalized URL.
79 t = Patterns.WEB_URL.matcher("http://\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
80 assertTrue("Valid URL", t);
81 t = Patterns.WEB_URL.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
82 assertTrue("Valid URL", t);
83
Shimeng (Simon) Wang40064d32010-04-27 11:33:15 +010084 t = Patterns.WEB_URL.matcher("http://brainstormtech.blogs.fortune.cnn.com/2010/03/11/" +
85 "top-five-moments-from-eric-schmidt\u2019s-talk-in-abu-dhabi/").matches();
86 assertTrue("Valid URL", t);
87
Dan Egnor494a1dc2009-11-20 18:31:33 -080088 t = Patterns.WEB_URL.matcher("ftp://www.example.com").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 assertFalse("Matched invalid protocol", t);
90
Dan Egnor494a1dc2009-11-20 18:31:33 -080091 t = Patterns.WEB_URL.matcher("http://www.example.com:8080").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 assertTrue("Didn't match valid URL with port", t);
93
Dan Egnor494a1dc2009-11-20 18:31:33 -080094 t = Patterns.WEB_URL.matcher("http://www.example.com:8080/?foo=bar").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 assertTrue("Didn't match valid URL with port and query args", t);
96
Dan Egnor494a1dc2009-11-20 18:31:33 -080097 t = Patterns.WEB_URL.matcher("http://www.example.com:8080/~user/?foo=bar").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 assertTrue("Didn't match valid URL with ~", t);
99 }
100
101 @SmallTest
102 public void testIpPattern() throws Exception {
103 boolean t;
104
Dan Egnor494a1dc2009-11-20 18:31:33 -0800105 t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 assertTrue("Valid IP", t);
107
Dan Egnor494a1dc2009-11-20 18:31:33 -0800108 t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 assertFalse("Invalid IP", t);
110 }
111
112 @SmallTest
113 public void testDomainPattern() throws Exception {
114 boolean t;
115
Dan Egnor494a1dc2009-11-20 18:31:33 -0800116 t = Patterns.DOMAIN_NAME.matcher("mail.example.com").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 assertTrue("Valid domain", t);
118
Shimeng (Simon) Wangdb990752010-05-06 15:07:22 -0700119 t = Patterns.DOMAIN_NAME.matcher("google.me").matches();
Shimeng (Simon) Wang51c02db2010-02-11 14:07:44 -0800120 assertTrue("Valid domain", t);
121
122 // Internationalized domains.
123 t = Patterns.DOMAIN_NAME.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
124 assertTrue("Valid domain", t);
125
Dan Egnor494a1dc2009-11-20 18:31:33 -0800126 t = Patterns.DOMAIN_NAME.matcher("__+&42.xer").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 assertFalse("Invalid domain", t);
Shimeng (Simon) Wangdb990752010-05-06 15:07:22 -0700128
129 // Obsolete domain .yu
130 t = Patterns.DOMAIN_NAME.matcher("test.yu").matches();
131 assertFalse("Obsolete country code top level domain", t);
132
133 // Testing top level Arabic country code domain in Punycode:
134 t = Patterns.DOMAIN_NAME.matcher("xn--4gbrim.xn----rmckbbajlc6dj7bxne2c.xn--wgbh1c").matches();
135 assertTrue("Valid domain", t);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 }
137
138 @SmallTest
139 public void testPhonePattern() throws Exception {
140 boolean t;
141
Dan Egnor494a1dc2009-11-20 18:31:33 -0800142 t = Patterns.PHONE.matcher("(919) 555-1212").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 assertTrue("Valid phone", t);
144
Dan Egnor494a1dc2009-11-20 18:31:33 -0800145 t = Patterns.PHONE.matcher("2334 9323/54321").matches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 assertFalse("Invalid phone", t);
147
148 String[] tests = {
149 "Me: 16505551212 this\n",
150 "Me: 6505551212 this\n",
151 "Me: 5551212 this\n",
152
153 "Me: 1-650-555-1212 this\n",
154 "Me: (650) 555-1212 this\n",
155 "Me: +1 (650) 555-1212 this\n",
156 "Me: +1-650-555-1212 this\n",
157 "Me: 650-555-1212 this\n",
158 "Me: 555-1212 this\n",
159
160 "Me: 1.650.555.1212 this\n",
161 "Me: (650) 555.1212 this\n",
162 "Me: +1 (650) 555.1212 this\n",
163 "Me: +1.650.555.1212 this\n",
164 "Me: 650.555.1212 this\n",
165 "Me: 555.1212 this\n",
166
167 "Me: 1 650 555 1212 this\n",
168 "Me: (650) 555 1212 this\n",
169 "Me: +1 (650) 555 1212 this\n",
170 "Me: +1 650 555 1212 this\n",
171 "Me: 650 555 1212 this\n",
172 "Me: 555 1212 this\n",
173 };
174
175 for (String test : tests) {
Dan Egnor494a1dc2009-11-20 18:31:33 -0800176 Matcher m = Patterns.PHONE.matcher(test);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177
178 assertTrue("Valid phone " + test, m.find());
179 }
180 }
181}