PatternsTest.java revision cbeaf2d0aef5c8de9ce86b8f1dde17459f9227e3
1/*
2 * Copyright (C) 2010 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 */
16package android.util;
17
18import android.test.suitebuilder.annotation.SmallTest;
19import android.util.Patterns;
20
21import java.util.regex.Matcher;
22
23import junit.framework.TestCase;
24
25public class PatternsTest extends TestCase {
26
27    @SmallTest
28    public void testTldPattern() throws Exception {
29        boolean t;
30
31        t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches();
32        assertTrue("Missed valid TLD", t);
33
34        // 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
48        t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches();
49        assertFalse("Matched invalid TLD!", t);
50    }
51
52    @SmallTest
53    public void testUrlPattern() throws Exception {
54        boolean t;
55
56        t = Patterns.WEB_URL.matcher("http://www.google.com").matches();
57        assertTrue("Valid URL", t);
58
59        // 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
71        // Internationalized URL.
72        t = Patterns.WEB_URL.matcher("http://\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
73        assertTrue("Valid URL", t);
74        t = Patterns.WEB_URL.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
75        assertTrue("Valid URL", t);
76
77        t = Patterns.WEB_URL.matcher("http://brainstormtech.blogs.fortune.cnn.com/2010/03/11/" +
78            "top-five-moments-from-eric-schmidt\u2019s-talk-in-abu-dhabi/").matches();
79        assertTrue("Valid URL", t);
80
81        t = Patterns.WEB_URL.matcher("ftp://www.example.com").matches();
82        assertFalse("Matched invalid protocol", t);
83
84        t = Patterns.WEB_URL.matcher("http://www.example.com:8080").matches();
85        assertTrue("Didn't match valid URL with port", t);
86
87        t = Patterns.WEB_URL.matcher("http://www.example.com:8080/?foo=bar").matches();
88        assertTrue("Didn't match valid URL with port and query args", t);
89
90        t = Patterns.WEB_URL.matcher("http://www.example.com:8080/~user/?foo=bar").matches();
91        assertTrue("Didn't match valid URL with ~", t);
92    }
93
94    @SmallTest
95    public void testIpPattern() throws Exception {
96        boolean t;
97
98        t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches();
99        assertTrue("Valid IP", t);
100
101        t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches();
102        assertFalse("Invalid IP", t);
103    }
104
105    @SmallTest
106    public void testDomainPattern() throws Exception {
107        boolean t;
108
109        t = Patterns.DOMAIN_NAME.matcher("mail.example.com").matches();
110        assertTrue("Valid domain", t);
111
112        t = Patterns.WEB_URL.matcher("google.me").matches();
113        assertTrue("Valid domain", t);
114
115        // Internationalized domains.
116        t = Patterns.DOMAIN_NAME.matcher("\uD604\uAE08\uC601\uC218\uC99D.kr").matches();
117        assertTrue("Valid domain", t);
118
119        t = Patterns.DOMAIN_NAME.matcher("__+&42.xer").matches();
120        assertFalse("Invalid domain", t);
121    }
122
123    @SmallTest
124    public void testPhonePattern() throws Exception {
125        boolean t;
126
127        t = Patterns.PHONE.matcher("(919) 555-1212").matches();
128        assertTrue("Valid phone", t);
129
130        t = Patterns.PHONE.matcher("2334 9323/54321").matches();
131        assertFalse("Invalid phone", t);
132
133        String[] tests = {
134                "Me: 16505551212 this\n",
135                "Me: 6505551212 this\n",
136                "Me: 5551212 this\n",
137
138                "Me: 1-650-555-1212 this\n",
139                "Me: (650) 555-1212 this\n",
140                "Me: +1 (650) 555-1212 this\n",
141                "Me: +1-650-555-1212 this\n",
142                "Me: 650-555-1212 this\n",
143                "Me: 555-1212 this\n",
144
145                "Me: 1.650.555.1212 this\n",
146                "Me: (650) 555.1212 this\n",
147                "Me: +1 (650) 555.1212 this\n",
148                "Me: +1.650.555.1212 this\n",
149                "Me: 650.555.1212 this\n",
150                "Me: 555.1212 this\n",
151
152                "Me: 1 650 555 1212 this\n",
153                "Me: (650) 555 1212 this\n",
154                "Me: +1 (650) 555 1212 this\n",
155                "Me: +1 650 555 1212 this\n",
156                "Me: 650 555 1212 this\n",
157                "Me: 555 1212 this\n",
158        };
159
160        for (String test : tests) {
161            Matcher m = Patterns.PHONE.matcher(test);
162
163            assertTrue("Valid phone " + test, m.find());
164        }
165    }
166}
167