1d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy/*
2d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Copyright (C) 2013 The Android Open Source Project
3d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
4d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Licensed under the Apache License, Version 2.0 (the "License");
5d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * you may not use this file except in compliance with the License.
6d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * You may obtain a copy of the License at
7d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
8d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *      http://www.apache.org/licenses/LICENSE-2.0
9d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy *
10d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * Unless required by applicable law or agreed to in writing, software
11d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * distributed under the License is distributed on an "AS IS" BASIS,
12d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * See the License for the specific language governing permissions and
14d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy * limitations under the License.
15d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy */
16d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypackage com.android.mail;
17d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
18d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.test.AndroidTestCase;
19d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedyimport android.test.suitebuilder.annotation.SmallTest;
20d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
21d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy@SmallTest
22d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedypublic class EmailAddressTest extends AndroidTestCase {
23d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
24d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void testNameRegex() {
25d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        {
26d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            EmailAddress email = EmailAddress.getEmailAddress("email@gmail.com");
27d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            assertEquals("", email.getName());
28d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
29d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
30d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        {
31d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            EmailAddress nameKnown = EmailAddress.getEmailAddress("john doe <coolguy@doe.com>");
32d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            assertEquals("john doe", nameKnown.getName());
33d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
34d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
35d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        {
36d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            EmailAddress withQuotes = EmailAddress
37d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    .getEmailAddress("\"john doe\" <coolguy@doe.com>");
38d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            assertEquals("john doe", withQuotes.getName());
39d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
40d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
41d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        {
42d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            EmailAddress noSpace = EmailAddress.getEmailAddress("john doe<coolguy@doe.com>");
43d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            assertEquals("john doe", noSpace.getName());
44d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
45d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
46d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        {
47d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            EmailAddress noSpaceWithQuotes = EmailAddress
48d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy                    .getEmailAddress("\"john doe\"<coolguy@doe.com>");
49d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy            assertEquals("john doe", noSpaceWithQuotes.getName());
50d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        }
51d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
52d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
53d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
54d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    /**
55d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     * Test the parsing of email addresses
56d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy     */
57d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    public void testEmailAddressParsing() {
58d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        EmailAddress address = EmailAddress.getEmailAddress("test name <test@localhost.com>");
59d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test name", address.getName());
60d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
61d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
62d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\"test name\" <test@localhost.com>");
63d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test name", address.getName());
64d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
65d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
66d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("<test@localhost.com>");
67d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("", address.getName());
68d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
69d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
70d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("test@localhost.com");
71d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("", address.getName());
72d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
73d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
74d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("O'brian <test@localhost.com>");
75d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("O'brian", address.getName());
76d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
77d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
78d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\"O'brian\" <test@localhost.com>");
79d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("O'brian", address.getName());
80d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
81d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
82d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\"\\\"O'brian\\\"\" <test@localhost.com>");
83d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("\"O'brian\"", address.getName());
84d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test@localhost.com", address.getAddress());
85d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
86d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
87d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Ensure that white space is trimmed from the name
88d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
89d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings that will match the regular expression
90d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\" \" <test@localhost.com>");
91d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("", address.getName());
92d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
93d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\" test name \" <test@localhost.com>");
94d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("test name", address.getName());
95d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
96d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        // Strings that will fallthrough to the rfc822 tokenizer
97d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\"\\\" O'brian \\\"\" <test@localhost.com>");
98d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("\" O'brian \"", address.getName());
99d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy
100d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        address = EmailAddress.getEmailAddress("\" \\\"O'brian\\\" \" <test@localhost.com>");
101d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy        assertEquals("\"O'brian\"", address.getName());
102d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy    }
103d5edd2d02649dffb40065fdb6a16acf91552b800Scott Kennedy}
104