1/*
2 * Copyright (C) 2013 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 com.android.mail;
17
18import android.test.AndroidTestCase;
19import android.test.suitebuilder.annotation.SmallTest;
20
21@SmallTest
22public class EmailAddressTest extends AndroidTestCase {
23
24    public void testNameRegex() {
25        {
26            EmailAddress email = EmailAddress.getEmailAddress("email@gmail.com");
27            assertEquals("", email.getName());
28        }
29
30        {
31            EmailAddress nameKnown = EmailAddress.getEmailAddress("john doe <coolguy@doe.com>");
32            assertEquals("john doe", nameKnown.getName());
33        }
34
35        {
36            EmailAddress withQuotes = EmailAddress
37                    .getEmailAddress("\"john doe\" <coolguy@doe.com>");
38            assertEquals("john doe", withQuotes.getName());
39        }
40
41        {
42            EmailAddress noSpace = EmailAddress.getEmailAddress("john doe<coolguy@doe.com>");
43            assertEquals("john doe", noSpace.getName());
44        }
45
46        {
47            EmailAddress noSpaceWithQuotes = EmailAddress
48                    .getEmailAddress("\"john doe\"<coolguy@doe.com>");
49            assertEquals("john doe", noSpaceWithQuotes.getName());
50        }
51
52    }
53
54    /**
55     * Test the parsing of email addresses
56     */
57    public void testEmailAddressParsing() {
58        EmailAddress address = EmailAddress.getEmailAddress("test name <test@localhost.com>");
59        assertEquals("test name", address.getName());
60        assertEquals("test@localhost.com", address.getAddress());
61
62        address = EmailAddress.getEmailAddress("\"test name\" <test@localhost.com>");
63        assertEquals("test name", address.getName());
64        assertEquals("test@localhost.com", address.getAddress());
65
66        address = EmailAddress.getEmailAddress("<test@localhost.com>");
67        assertEquals("", address.getName());
68        assertEquals("test@localhost.com", address.getAddress());
69
70        address = EmailAddress.getEmailAddress("test@localhost.com");
71        assertEquals("", address.getName());
72        assertEquals("test@localhost.com", address.getAddress());
73
74        address = EmailAddress.getEmailAddress("O'brian <test@localhost.com>");
75        assertEquals("O'brian", address.getName());
76        assertEquals("test@localhost.com", address.getAddress());
77
78        address = EmailAddress.getEmailAddress("\"O'brian\" <test@localhost.com>");
79        assertEquals("O'brian", address.getName());
80        assertEquals("test@localhost.com", address.getAddress());
81
82        address = EmailAddress.getEmailAddress("\"\\\"O'brian\\\"\" <test@localhost.com>");
83        assertEquals("\"O'brian\"", address.getName());
84        assertEquals("test@localhost.com", address.getAddress());
85
86
87        // Ensure that white space is trimmed from the name
88
89        // Strings that will match the regular expression
90        address = EmailAddress.getEmailAddress("\" \" <test@localhost.com>");
91        assertEquals("", address.getName());
92
93        address = EmailAddress.getEmailAddress("\" test name \" <test@localhost.com>");
94        assertEquals("test name", address.getName());
95
96        // Strings that will fallthrough to the rfc822 tokenizer
97        address = EmailAddress.getEmailAddress("\"\\\" O'brian \\\"\" <test@localhost.com>");
98        assertEquals("\" O'brian \"", address.getName());
99
100        address = EmailAddress.getEmailAddress("\" \\\"O'brian\\\" \" <test@localhost.com>");
101        assertEquals("\"O'brian\"", address.getName());
102    }
103}
104