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
17package com.android.emailcommon.utility;
18
19import com.android.email.provider.EmailProvider;
20import com.android.email.provider.ProviderTestUtils;
21import com.android.emailcommon.provider.Account;
22import com.android.emailcommon.provider.EmailContent;
23import com.android.emailcommon.provider.EmailContent.Attachment;
24import com.android.emailcommon.provider.EmailContent.Message;
25import com.android.emailcommon.provider.EmailContent.MessageColumns;
26import com.android.emailcommon.provider.Mailbox;
27
28import android.content.Context;
29import android.net.Uri;
30import android.test.ProviderTestCase2;
31import android.test.suitebuilder.annotation.MediumTest;
32
33import java.io.File;
34import java.io.FileWriter;
35import java.io.IOException;
36
37/**
38 * This is a series of medium tests for the Utility class.  These tests must be locally
39 * complete - no server(s) required.
40 *
41 * You can run this entire test case with:
42 *   runtest -c com.android.emailcommon.utility.UtilityMediumTests email
43 */
44@MediumTest
45public class UtilityMediumTests extends ProviderTestCase2<EmailProvider> {
46
47    EmailProvider mProvider;
48    Context mMockContext;
49
50    public UtilityMediumTests() {
51        super(EmailProvider.class, EmailContent.AUTHORITY);
52    }
53
54    @Override
55    public void setUp() throws Exception {
56        super.setUp();
57        mMockContext = getMockContext();
58    }
59
60    public void testFindExistingAccount() {
61        // Create two accounts
62        Account account1 = ProviderTestUtils.setupAccount("account1", false, mMockContext);
63        account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("ha1", -1, false, mMockContext);
64        account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("ha1", -1, false, mMockContext);
65        account1.save(mMockContext);
66        Account account2 = ProviderTestUtils.setupAccount("account2", false, mMockContext);
67        account2.mHostAuthRecv = ProviderTestUtils.setupHostAuth("ha2", -1, false, mMockContext);
68        account2.mHostAuthSend = ProviderTestUtils.setupHostAuth("ha2", -1, false, mMockContext);
69        account2.save(mMockContext);
70        // Make sure we can find them
71        Account acct = Utility.findExistingAccount(mMockContext, -1, "address-ha1", "login-ha1");
72        assertNotNull(acct);
73        assertEquals("account1", acct.mDisplayName);
74        acct = Utility.findExistingAccount(mMockContext, -1, "address-ha2", "login-ha2");
75        assertNotNull(acct);
76        assertEquals("account2", acct.mDisplayName);
77        // We shouldn't find account
78        acct = Utility.findExistingAccount(mMockContext, -1, "address-ha3", "login-ha3");
79        assertNull(acct);
80        // Try to find account1, excluding account1
81        acct = Utility.findExistingAccount(mMockContext, account1.mId, "address-ha1", "login-ha1");
82        assertNull(acct);
83
84        // Make sure we properly handle an underscore in the login name
85        Account account3 = ProviderTestUtils.setupAccount("account3", false, mMockContext);
86        account3.mHostAuthRecv = ProviderTestUtils.setupHostAuth("foo_ba", -1, false, mMockContext);
87        account3.mHostAuthSend = ProviderTestUtils.setupHostAuth("foo_ba", -1, false, mMockContext);
88        account3.save(mMockContext);
89        acct = Utility.findExistingAccount(mMockContext, -1, "address-foo_ba", "login-foo.ba");
90        assertNull(acct);
91        acct = Utility.findExistingAccount(mMockContext, -1, "address-foo_ba", "login-foo_ba");
92        assertNotNull(acct);
93    }
94
95    public void testAttachmentExists() throws IOException {
96        Account account = ProviderTestUtils.setupAccount("account", true, mMockContext);
97        // We return false with null attachment
98        assertFalse(Utility.attachmentExists(mMockContext, null));
99
100        Mailbox mailbox =
101            ProviderTestUtils.setupMailbox("mailbox", account.mId, true, mMockContext);
102        Message message = ProviderTestUtils.setupMessage("foo", account.mId, mailbox.mId, false,
103                true, mMockContext);
104        Attachment attachment = ProviderTestUtils.setupAttachment(message.mId, "filename.ext",
105                69105, true, mMockContext);
106        attachment.mContentBytes = null;
107        // With no contentUri, we should return false
108        assertFalse(Utility.attachmentExists(mMockContext, attachment));
109
110        attachment.mContentBytes = new byte[0];
111        // With contentBytes set, we should return true
112        assertTrue(Utility.attachmentExists(mMockContext, attachment));
113
114        attachment.mContentBytes = null;
115        // Generate a file name in our data directory, and use that for contentUri
116        File file = mMockContext.getFileStreamPath("test.att");
117        // Delete the file if it already exists
118        if (file.exists()) {
119            assertTrue(file.delete());
120        }
121        // Should return false, because the file doesn't exist
122        assertFalse(Utility.attachmentExists(mMockContext, attachment));
123
124        assertTrue(file.createNewFile());
125        // Put something in the file
126        FileWriter writer = new FileWriter(file);
127        writer.write("Foo");
128        writer.flush();
129        writer.close();
130        attachment.mContentUri = "file://" + file.getAbsolutePath();
131        // Now, this should return true
132        assertTrue(Utility.attachmentExists(mMockContext, attachment));
133    }
134
135    public void testBuildLimitOneUri() {
136        // EmailProvider supports "?limit="
137        assertEquals(Uri.parse("content://com.android.email.provider?limit=1"),
138                Utility.buildLimitOneUri(Uri.parse("content://com.android.email.provider")));
139
140        // Others don't -- so don't add it.
141        assertEquals(Uri.parse("content://com.android.email.attachmentprovider"),
142                Utility.buildLimitOneUri(Uri.parse("content://com.android.email.attachmentprovider"
143                        )));
144        assertEquals(Uri.parse("content://gmail-ls/android@gmail.com"),
145                Utility.buildLimitOneUri(Uri.parse("content://gmail-ls/android@gmail.com"
146                        )));
147    }
148
149    public void testGetFirstRowLong() {
150        Account account1 = ProviderTestUtils.setupAccount("1", true, mMockContext);
151        Account account2 = ProviderTestUtils.setupAccount("X1", true, mMockContext);
152        Account account3 = ProviderTestUtils.setupAccount("X2", true, mMockContext);
153
154        // case 1. Account found
155        assertEquals((Long) account2.mId, Utility.getFirstRowLong(
156                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
157                Account.DISPLAY_NAME + " like :1", new String[] {"X%"},
158                Account.DISPLAY_NAME,
159                EmailContent.ID_PROJECTION_COLUMN));
160        // different sort order
161        assertEquals((Long) account3.mId, Utility.getFirstRowLong(
162                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
163                Account.DISPLAY_NAME + " like :1", new String[] {"X%"},
164                Account.DISPLAY_NAME + " desc",
165                EmailContent.ID_PROJECTION_COLUMN));
166
167        // case 2. no row found
168        assertEquals(null, Utility.getFirstRowLong(
169                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
170                Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"},
171                null,
172                EmailContent.ID_PROJECTION_COLUMN));
173
174        // case 3. no row found with default value
175        assertEquals((Long) (-1L), Utility.getFirstRowLong(
176                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
177                Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"},
178                null,
179                EmailContent.ID_PROJECTION_COLUMN, -1L));
180    }
181
182    public void testGetFirstRowInt() {
183        Account account1 = ProviderTestUtils.setupAccount("1", true, mMockContext);
184        Account account2 = ProviderTestUtils.setupAccount("X1", true, mMockContext);
185        Account account3 = ProviderTestUtils.setupAccount("X2", true, mMockContext);
186
187        // case 1. Account found
188        assertEquals((Integer)(int) account2.mId, Utility.getFirstRowInt(
189                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
190                Account.DISPLAY_NAME + " like :1", new String[] {"X%"},
191                Account.DISPLAY_NAME,
192                EmailContent.ID_PROJECTION_COLUMN));
193        // different sort order
194        assertEquals((Integer)(int) account3.mId, Utility.getFirstRowInt(
195                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
196                Account.DISPLAY_NAME + " like :1", new String[] {"X%"},
197                Account.DISPLAY_NAME + " desc",
198                EmailContent.ID_PROJECTION_COLUMN));
199
200        // case 2. no row found
201        assertEquals(null, Utility.getFirstRowInt(
202                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
203                Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"},
204                null,
205                EmailContent.ID_PROJECTION_COLUMN));
206
207        // case 3. no row found with default value
208        assertEquals((Integer) (-1), Utility.getFirstRowInt(
209                mMockContext, Account.CONTENT_URI, EmailContent.ID_PROJECTION,
210                Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"},
211                null,
212                EmailContent.ID_PROJECTION_COLUMN, -1));
213    }
214
215    public void testGetFirstRowString() {
216        final String[] DISPLAY_NAME_PROJECTION = new String[] {Account.DISPLAY_NAME};
217
218        Account account1 = ProviderTestUtils.setupAccount("1", true, mMockContext);
219        Account account2 = ProviderTestUtils.setupAccount("X1", true, mMockContext);
220        Account account3 = ProviderTestUtils.setupAccount("X2", true, mMockContext);
221
222        // case 1. Account found
223        assertEquals(account2.mDisplayName, Utility.getFirstRowString(
224                mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION,
225                Account.DISPLAY_NAME + " like :1", new String[] {"X%"},
226                Account.DISPLAY_NAME, 0));
227
228        // different sort order
229        assertEquals(account3.mDisplayName, Utility.getFirstRowString(
230                mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION,
231                Account.DISPLAY_NAME + " like :1", new String[] {"X%"},
232                Account.DISPLAY_NAME + " desc", 0));
233
234        // case 2. no row found
235        assertEquals(null, Utility.getFirstRowString(
236                mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION,
237                Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"},
238                null, 0));
239
240        // case 3. no row found with default value
241        assertEquals("-", Utility.getFirstRowString(
242                mMockContext, Account.CONTENT_URI, DISPLAY_NAME_PROJECTION,
243                Account.DISPLAY_NAME + " like :1", new String[] {"NO SUCH ACCOUNT"},
244                null, 0, "-"));
245    }
246}
247