The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 2 | * Copyright (C) 2010 The Android Open Source Project |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | */ |
Brett Chabot | 0dc59e7 | 2010-04-01 18:21:38 -0700 | [diff] [blame] | 16 | package android.util; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 17 | |
| 18 | import android.test.suitebuilder.annotation.SmallTest; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 19 | import android.util.Patterns; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | import java.util.regex.Matcher; |
Dianne Hackborn | 2269d157 | 2010-02-24 19:54:22 -0800 | [diff] [blame] | 22 | |
| 23 | import junit.framework.TestCase; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
Dan Egnor | ded0e64 | 2009-11-18 11:23:45 -0800 | [diff] [blame] | 25 | public class PatternsTest extends TestCase { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | @SmallTest |
| 28 | public void testTldPattern() throws Exception { |
| 29 | boolean t; |
| 30 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 31 | t = Patterns.TOP_LEVEL_DOMAIN.matcher("com").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | assertTrue("Missed valid TLD", t); |
| 33 | |
Shimeng (Simon) Wang | 3ed6fbd | 2010-02-05 17:57:45 -0800 | [diff] [blame] | 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 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 48 | t = Patterns.TOP_LEVEL_DOMAIN.matcher("xer").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | assertFalse("Matched invalid TLD!", t); |
| 50 | } |
| 51 | |
| 52 | @SmallTest |
| 53 | public void testUrlPattern() throws Exception { |
| 54 | boolean t; |
| 55 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 56 | t = Patterns.WEB_URL.matcher("http://www.google.com").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | assertTrue("Valid URL", t); |
| 58 | |
Shimeng (Simon) Wang | 3ed6fbd | 2010-02-05 17:57:45 -0800 | [diff] [blame] | 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 | |
Shimeng (Simon) Wang | db99075 | 2010-05-06 15:07:22 -0700 | [diff] [blame] | 71 | // 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) Wang | 51c02db | 2010-02-11 14:07:44 -0800 | [diff] [blame] | 78 | // 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) Wang | 40064d3 | 2010-04-27 11:33:15 +0100 | [diff] [blame] | 84 | 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 Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 88 | t = Patterns.WEB_URL.matcher("ftp://www.example.com").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | assertFalse("Matched invalid protocol", t); |
| 90 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 91 | t = Patterns.WEB_URL.matcher("http://www.example.com:8080").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | assertTrue("Didn't match valid URL with port", t); |
| 93 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 94 | t = Patterns.WEB_URL.matcher("http://www.example.com:8080/?foo=bar").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | assertTrue("Didn't match valid URL with port and query args", t); |
| 96 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 97 | t = Patterns.WEB_URL.matcher("http://www.example.com:8080/~user/?foo=bar").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | assertTrue("Didn't match valid URL with ~", t); |
| 99 | } |
| 100 | |
| 101 | @SmallTest |
| 102 | public void testIpPattern() throws Exception { |
| 103 | boolean t; |
| 104 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 105 | t = Patterns.IP_ADDRESS.matcher("172.29.86.3").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | assertTrue("Valid IP", t); |
| 107 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 108 | t = Patterns.IP_ADDRESS.matcher("1234.4321.9.9").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | assertFalse("Invalid IP", t); |
| 110 | } |
| 111 | |
| 112 | @SmallTest |
| 113 | public void testDomainPattern() throws Exception { |
| 114 | boolean t; |
| 115 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 116 | t = Patterns.DOMAIN_NAME.matcher("mail.example.com").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | assertTrue("Valid domain", t); |
| 118 | |
Shimeng (Simon) Wang | db99075 | 2010-05-06 15:07:22 -0700 | [diff] [blame] | 119 | t = Patterns.DOMAIN_NAME.matcher("google.me").matches(); |
Shimeng (Simon) Wang | 51c02db | 2010-02-11 14:07:44 -0800 | [diff] [blame] | 120 | 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 Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 126 | t = Patterns.DOMAIN_NAME.matcher("__+&42.xer").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 127 | assertFalse("Invalid domain", t); |
Shimeng (Simon) Wang | db99075 | 2010-05-06 15:07:22 -0700 | [diff] [blame] | 128 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | @SmallTest |
| 139 | public void testPhonePattern() throws Exception { |
| 140 | boolean t; |
| 141 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 142 | t = Patterns.PHONE.matcher("(919) 555-1212").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | assertTrue("Valid phone", t); |
| 144 | |
Dan Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 145 | t = Patterns.PHONE.matcher("2334 9323/54321").matches(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | 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 Egnor | 494a1dc | 2009-11-20 18:31:33 -0800 | [diff] [blame] | 176 | Matcher m = Patterns.PHONE.matcher(test); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | |
| 178 | assertTrue("Valid phone " + test, m.find()); |
| 179 | } |
| 180 | } |
| 181 | } |