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