• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
16 
17 package com.android.email.activity.setup;
18 
19 import android.content.Context;
20 import android.test.InstrumentationTestCase;
21 import android.test.suitebuilder.annotation.SmallTest;
22 
23 import com.android.email.activity.setup.AccountSettingsUtils.Provider;
24 import com.android.email.tests.R;
25 
26 /**
27  * This is a series of unit tests for the AccountSettingsUtils class.
28  *
29  * To run these tests,
30  *  runtest -c com.android.email.activity.setup.AccountSettingsUtilsTests email
31  */
32 @SmallTest
33 public class AccountSettingsUtilsTests extends InstrumentationTestCase {
34 
35     private Context mTestContext;
36 
37     @Override
setUp()38     protected void setUp() throws Exception {
39         super.setUp();
40         mTestContext = getInstrumentation().getContext();
41     }
42 
43     /**
44      * Test server name inferences
45      *
46      * Incoming: Prepend "imap" or "pop3" to domain, unless "pop", "pop3",
47      *          "imap", or "mail" are found.
48      * Outgoing: Prepend "smtp" if "pop", "pop3", "imap" are found.
49      *          Leave "mail" as-is.
50      * TBD: Are there any useful defaults for exchange?
51      */
testGuessServerName()52     public void testGuessServerName() {
53         assertEquals("foo.x.y.z", AccountSettingsUtils.inferServerName("x.y.z", "foo", null));
54         assertEquals("Pop.y.z", AccountSettingsUtils.inferServerName("Pop.y.z", "foo", null));
55         assertEquals("poP3.y.z", AccountSettingsUtils.inferServerName("poP3.y.z", "foo", null));
56         assertEquals("iMAp.y.z", AccountSettingsUtils.inferServerName("iMAp.y.z", "foo", null));
57         assertEquals("MaiL.y.z", AccountSettingsUtils.inferServerName("MaiL.y.z", "foo", null));
58 
59         assertEquals("bar.x.y.z", AccountSettingsUtils.inferServerName("x.y.z", null, "bar"));
60         assertEquals("bar.y.z", AccountSettingsUtils.inferServerName("Pop.y.z", null, "bar"));
61         assertEquals("bar.y.z", AccountSettingsUtils.inferServerName("poP3.y.z", null, "bar"));
62         assertEquals("bar.y.z", AccountSettingsUtils.inferServerName("iMAp.y.z", null, "bar"));
63         assertEquals("MaiL.y.z", AccountSettingsUtils.inferServerName("MaiL.y.z", null, "bar"));
64     }
65 
testFindProviderForDomain()66     public void testFindProviderForDomain() {
67         Provider testProvider;
68         // <provider id="gmail" label="Gmail" domain="gmail.com">
69         //   <incoming uri="imap+ssl+://imap.gmail.com" username="$email" />
70         //   <outgoing uri="smtp+ssl+://smtp.gmail.com" username="$email" />
71         // </provider>
72         testProvider = AccountSettingsUtils.findProviderForDomain(
73                 mTestContext, "gmail.com", R.xml.test_providers);
74         assertNotNull(testProvider);
75         assertEquals("imap+ssl+://imap.gmail.com", testProvider.incomingUriTemplate);
76         assertEquals("smtp+ssl+://smtp.gmail.com", testProvider.outgoingUriTemplate);
77         assertEquals("gmail.com", testProvider.domain);
78 
79         // <provider id="rr-generic" label="RoadRunner" domain="*.rr.com">
80         //   <incoming uri="pop3://pop-server.$domain" username="$email" />
81         //   <outgoing uri="smtp://mobile-smtp.roadrunner.com" username="$email" />
82         // </provider>
83         testProvider = AccountSettingsUtils.findProviderForDomain(
84                 mTestContext, "elmore.rr.com", R.xml.test_providers);
85         assertNotNull(testProvider);
86         assertEquals("pop3://pop-server.$domain", testProvider.incomingUriTemplate);
87         assertEquals("smtp://mobile-smtp.roadrunner.com", testProvider.outgoingUriTemplate);
88         assertEquals("elmore.rr.com", testProvider.domain);
89 
90         // Domain matches 2 providers; first one wins
91         testProvider = AccountSettingsUtils.findProviderForDomain(
92                 mTestContext, "leonard.rr.com", R.xml.test_providers);
93         assertNotNull(testProvider);
94         assertEquals("pop3://pop-server.firstonewins.com", testProvider.incomingUriTemplate);
95 
96         // Domains that don't exist
97         testProvider = AccountSettingsUtils.findProviderForDomain(
98                 mTestContext, "nonexist.com", R.xml.test_providers);
99         assertNull(testProvider);
100     }
101 
testMatchProvider()102     public void testMatchProvider() {
103         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "foo.com"));
104         assertFalse(AccountSettingsUtils.matchProvider("foo.co", "foo.com"));
105         assertFalse(AccountSettingsUtils.matchProvider("", "foo.com"));
106 
107         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "fo?.com"));
108         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "f??.com"));
109         assertTrue(AccountSettingsUtils.matchProvider("fzz.com", "f??.com"));
110         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "???.???"));
111         assertFalse(AccountSettingsUtils.matchProvider("foo.com", "???.????"));
112 
113         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "*.com"));
114         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "foo.*"));
115         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "*.*"));
116         assertFalse(AccountSettingsUtils.matchProvider("foo.com", "fox.*"));
117         assertTrue(AccountSettingsUtils.matchProvider("foo.com", "*.???"));
118         assertFalse(AccountSettingsUtils.matchProvider("foo.com", "*.?"));
119 
120         assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "food.barge.comb"));
121         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.com"));
122         assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.gag.com"));
123         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.*.com"));
124         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.*.*"));
125         assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.*.*"));
126         assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.*com"));
127         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "*.bar.com"));
128         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "*.*.com"));
129         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "*.*.*"));
130         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.bar.*"));
131 
132         assertTrue(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.???.*"));
133         assertFalse(AccountSettingsUtils.matchProvider("foo.bar.com", "foo.*??.*"));
134     }
135 
testExpandTemplates()136     public void testExpandTemplates() {
137         Provider testProvider;
138         // <provider id="cox" label="Cox" domain="cox.net">
139         //   <incoming uri="pop3+ssl+://pop.east.cox.net" username="$user" />
140         //   <outgoing uri="smtp+ssl+://smtp.east.cox.net" username="$user" />
141         // </provider>
142         testProvider = new Provider();
143         testProvider.domain = "cox.net";
144         testProvider.incomingUriTemplate = "pop3+ssl+://pop.east.$domain";
145         testProvider.outgoingUriTemplate = "smtp+ssl+://smtp.east.$domain";
146         testProvider.incomingUsernameTemplate = "$user";
147         testProvider.outgoingUsernameTemplate = "$user";
148         testProvider.expandTemplates("replUser@cox.net");
149         assertEquals("replUser", testProvider.incomingUsername);
150         assertEquals("replUser", testProvider.outgoingUsername);
151         assertEquals("pop3+ssl+://pop.east.cox.net", testProvider.incomingUri);
152         assertEquals("smtp+ssl+://smtp.east.cox.net", testProvider.outgoingUri);
153 
154         // <provider id="earthlink" label="Earthlink" domain="earthlink.net">
155         //   <incoming uri="pop3://pop.earthlink.net" username="$email" />
156         //   <outgoing uri="smtp://smtpauth.earthlink.net:587" username="$email" />
157         // </provider>
158         testProvider = new Provider();
159         testProvider.domain = "earthlink.net";
160         testProvider.incomingUriTemplate = "pop3://pop.earthlink.net";
161         testProvider.outgoingUriTemplate = "smtp://smtpauth.earthlink.net:587";
162         testProvider.incomingUsernameTemplate = "$email";
163         testProvider.outgoingUsernameTemplate = "$email";
164         testProvider.expandTemplates("replUser@earthlink.net");
165         assertEquals("replUser@earthlink.net", testProvider.incomingUsername);
166         assertEquals("replUser@earthlink.net", testProvider.outgoingUsername);
167         assertEquals("pop3://pop.earthlink.net", testProvider.incomingUri);
168         assertEquals("smtp://smtpauth.earthlink.net:587", testProvider.outgoingUri);
169 
170         // <provider id="tuffmail" label="Tuffmail" domain="tuffmail.com">
171         //   <incoming uri="imap+ssl+://mail.mxes.net" username="$user_$domain" />
172         //   <outgoing uri="smtp+ssl+://smtp.mxes.net" username="$user_$domain" />
173         // </provider>
174         testProvider = new Provider();
175         testProvider.domain = "tuffmail.com";
176         testProvider.incomingUriTemplate = "imap+ssl+://mail.mxes.net";
177         testProvider.outgoingUriTemplate = "smtp+ssl+://smtp.mxes.net";
178         testProvider.incomingUsernameTemplate = "$user_$domain";
179         testProvider.outgoingUsernameTemplate = "$user_$domain";
180         testProvider.expandTemplates("replUser@tuffmail.com");
181         assertEquals("replUser_tuffmail.com", testProvider.incomingUsername);
182         assertEquals("replUser_tuffmail.com", testProvider.outgoingUsername);
183         assertEquals("imap+ssl+://mail.mxes.net", testProvider.incomingUri);
184         assertEquals("smtp+ssl+://smtp.mxes.net", testProvider.outgoingUri);
185 
186         // Everything hardcoded; not effective in the wild
187         testProvider = new Provider();
188         testProvider.domain = "yahoo.com";
189         testProvider.incomingUriTemplate = "imap+ssl+://pop.yahoo.com";
190         testProvider.outgoingUriTemplate = "smtp+ssl+://smtp.yahoo.com";
191         testProvider.incomingUsernameTemplate = "joe_smith";
192         testProvider.outgoingUsernameTemplate = "joe_smith";
193         testProvider.expandTemplates("replUser@yahoo.com");
194         assertEquals("joe_smith", testProvider.incomingUsername);
195         assertEquals("joe_smith", testProvider.outgoingUsername);
196         assertEquals("imap+ssl+://pop.yahoo.com", testProvider.incomingUri);
197         assertEquals("smtp+ssl+://smtp.yahoo.com", testProvider.outgoingUri);
198     }
199 }
200