Home | History | Annotate | Download | only in mock
      1 /**
      2  * Copyright (c) 2011, Google Inc.
      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.mail.providers.protos.mock;
     18 
     19 import com.android.mail.providers.UIProvider;
     20 import com.android.mail.utils.LogTag;
     21 import com.android.mail.utils.LogUtils;
     22 
     23 import android.content.ContentResolver;
     24 import android.database.Cursor;
     25 import android.net.Uri;
     26 import android.test.AndroidTestCase;
     27 import android.test.suitebuilder.annotation.SmallTest;
     28 
     29 import java.lang.Override;
     30 import java.lang.String;
     31 import java.util.ArrayList;
     32 import java.util.HashSet;
     33 import java.util.Set;
     34 
     35 
     36 // TODO: Create a UiProviderTest, and change MockUiProviderTests to extend it.  This would allow us
     37 // to share the validation logic when we add new UI providers.
     38 
     39 public class MockUiProviderTests extends AndroidTestCase {
     40 
     41     private Set<String> mTraversedUris;
     42     private String mLogTag;
     43 
     44     @Override
     45     public void setUp() {
     46         mLogTag = LogTag.getLogTag();
     47     }
     48 
     49     @SmallTest
     50     public void testTraverseContentProvider() {
     51 
     52         // Get the starting Uri
     53         final Uri accountUri = MockUiProvider.getAccountsUri();
     54 
     55         mTraversedUris = new HashSet<String>();
     56         traverseUri(accountUri);
     57     }
     58 
     59     public void testGetFolders() {
     60         final Uri accountsUri = MockUiProvider.getAccountsUri();
     61         MockUiProvider provider = new MockUiProvider();
     62         Cursor cursor = provider.query(accountsUri, UIProvider.ACCOUNTS_PROJECTION, null, null,
     63                 null);
     64         ArrayList<Uri> folderUris = new ArrayList<Uri>();
     65         if (cursor != null) {
     66             Uri foldersUri;
     67             int folderUriIndex = cursor.getColumnIndex(UIProvider.AccountColumns.FOLDER_LIST_URI);
     68             while (cursor.moveToNext()) {
     69                 // Verify that we can get the folders URI.
     70                 foldersUri = Uri.parse(cursor.getString(folderUriIndex));
     71                 assertNotNull(foldersUri);
     72                 folderUris.add(foldersUri);
     73             }
     74         }
     75         // Now, verify that we can get folders.
     76         ArrayList<Uri> childUris = new ArrayList<Uri>();
     77         ArrayList<Uri> convUris = new ArrayList<Uri>();
     78         int count = 0;
     79         for (Uri u : folderUris) {
     80             Cursor foldersCursor = provider.query(u, UIProvider.FOLDERS_PROJECTION, null, null,
     81                     null);
     82             assertNotNull(foldersCursor);
     83             assertEquals(foldersCursor.getCount(), 2);
     84             Uri childUri;
     85             Uri convUri;
     86             int name = foldersCursor.getColumnIndex(UIProvider.FolderColumns.NAME);
     87             int childColumnIndex = foldersCursor
     88                     .getColumnIndex(UIProvider.FolderColumns.CHILD_FOLDERS_LIST_URI);
     89             int convColumnIndex = foldersCursor
     90                     .getColumnIndex(UIProvider.FolderColumns.CONVERSATION_LIST_URI);
     91             int hasChildrenIndex = foldersCursor
     92                     .getColumnIndex(UIProvider.FolderColumns.HAS_CHILDREN);
     93             while (foldersCursor.moveToNext()) {
     94                 switch (count) {
     95                     case 0:
     96                         assertEquals(foldersCursor.getString(name), "Folder zero");
     97                         childUri = Uri.parse(foldersCursor.getString(childColumnIndex));
     98                         assertNotNull(childUri);
     99                         childUris.add(childUri);
    100                         convUri = Uri.parse(foldersCursor.getString(convColumnIndex));
    101                         convUris.add(convUri);
    102                         assertEquals(foldersCursor.getInt(hasChildrenIndex), 1);
    103                         break;
    104                     case 1:
    105                         assertEquals(foldersCursor.getString(name), "Folder one");
    106                         assertEquals(foldersCursor.getInt(hasChildrenIndex), 0);
    107                         childUri = Uri.parse(foldersCursor.getString(childColumnIndex));
    108                         childUris.add(childUri);
    109                         convUri = Uri.parse(foldersCursor.getString(convColumnIndex));
    110                         convUris.add(convUri);
    111                         break;
    112                     case 2:
    113                         assertEquals(foldersCursor.getString(name), "Folder two");
    114                         assertEquals(foldersCursor.getInt(hasChildrenIndex), 0);
    115                         childUri = Uri.parse(foldersCursor.getString(childColumnIndex));
    116                         childUris.add(childUri);
    117                         convUri = Uri.parse(foldersCursor.getString(convColumnIndex));
    118                         convUris.add(convUri);
    119                         break;
    120                     case 3:
    121                         assertEquals(foldersCursor.getString(name), "Folder three");
    122                         assertEquals(foldersCursor.getInt(hasChildrenIndex), 0);
    123                         childUri = Uri.parse(foldersCursor.getString(childColumnIndex));
    124                         childUris.add(childUri);
    125                         convUri = Uri.parse(foldersCursor.getString(convColumnIndex));
    126                         convUris.add(convUri);
    127                         break;
    128                 }
    129                 count++;
    130             }
    131         }
    132         count = 0;
    133         for (Uri u : childUris) {
    134             Cursor childFoldersCursor = provider.query(u, UIProvider.FOLDERS_PROJECTION, null,
    135                     null, null);
    136             if (childFoldersCursor != null) {
    137                 int name = childFoldersCursor.getColumnIndex(UIProvider.FolderColumns.NAME);
    138                 while (childFoldersCursor.moveToNext()) {
    139                     switch (count) {
    140                         case 0:
    141                             assertEquals(childFoldersCursor.getString(name), "Folder zeroChild0");
    142                             break;
    143                         case 1:
    144                             assertEquals(childFoldersCursor.getString(name), "Folder zeroChild1");
    145                             break;
    146                     }
    147                     count++;
    148                 }
    149             }
    150         }
    151         assertEquals(2, count);
    152         count = 0;
    153         ArrayList<Uri> messageUris = new ArrayList<Uri>();
    154         for (Uri u : convUris) {
    155             Cursor convFoldersCursor = provider.query(u, UIProvider.CONVERSATION_PROJECTION, null,
    156                     null, null);
    157             if (convFoldersCursor != null) {
    158                 int subject = UIProvider.CONVERSATION_SUBJECT_COLUMN;
    159                 int messageUriCol = UIProvider.CONVERSATION_MESSAGE_LIST_URI_COLUMN;
    160                 Uri messageUri;
    161                 while (convFoldersCursor.moveToNext()) {
    162                     switch (count) {
    163                         case 0:
    164                             assertEquals(convFoldersCursor.getString(subject),
    165                                     "Conversation zeroConv0");
    166                             messageUri = Uri.parse(convFoldersCursor.getString(messageUriCol));
    167                             messageUris.add(messageUri);
    168                             break;
    169                         case 1:
    170                             assertEquals(convFoldersCursor.getString(subject),
    171                                     "Conversation zeroConv1");
    172                             messageUri = Uri.parse(convFoldersCursor.getString(messageUriCol));
    173                             messageUris.add(messageUri);
    174                             break;
    175                     }
    176                     count++;
    177                 }
    178             }
    179         }
    180         assertEquals(102, count);
    181         count = 0;
    182         ArrayList<Uri> attachmentUris = new ArrayList<Uri>();
    183         for (Uri u : messageUris) {
    184             Cursor messageCursor = provider.query(u, UIProvider.MESSAGE_PROJECTION, null, null,
    185                     null);
    186             if (messageCursor != null) {
    187                 int subject = messageCursor.getColumnIndex(UIProvider.MessageColumns.SUBJECT);
    188                 int attachmentUriCol = messageCursor
    189                         .getColumnIndex(UIProvider.MessageColumns.ATTACHMENT_LIST_URI);
    190                 int hasAttachments = messageCursor
    191                         .getColumnIndex(UIProvider.MessageColumns.HAS_ATTACHMENTS);
    192                 while (messageCursor.moveToNext()) {
    193                     switch (count) {
    194                         case 0:
    195                             assertEquals(messageCursor.getString(subject), "Message zeroConv0");
    196                             assertEquals(messageCursor.getInt(hasAttachments), 1);
    197                             attachmentUris
    198                                     .add(Uri.parse(messageCursor.getString(attachmentUriCol)));
    199                             count++;
    200                             break;
    201                         case 1:
    202                             assertEquals(messageCursor.getString(subject), "Message zeroConv1");
    203                             assertEquals(messageCursor.getInt(hasAttachments), 1);
    204                             attachmentUris
    205                                     .add(Uri.parse(messageCursor.getString(attachmentUriCol)));
    206                             count++;
    207                             break;
    208                     }
    209                 }
    210             }
    211         }
    212         assertEquals(2, count);
    213         count = 0;
    214         for (Uri u : attachmentUris) {
    215             Cursor attachmentCursor = provider.query(u, UIProvider.ATTACHMENT_PROJECTION, null,
    216                     null, null);
    217             if (attachmentCursor != null) {
    218                 int name = attachmentCursor.getColumnIndex(UIProvider.AttachmentColumns.NAME);
    219                 while (attachmentCursor.moveToNext()) {
    220                     switch (count) {
    221                         case 0:
    222                             assertEquals(attachmentCursor.getString(name), "Attachment zero");
    223                             break;
    224                         case 1:
    225                             assertEquals(attachmentCursor.getString(name), "Attachment one");
    226                             break;
    227                     }
    228                     count++;
    229                 }
    230             }
    231         }
    232         assertEquals(2, count);
    233     }
    234 
    235     private void traverseUri(Uri uri) {
    236         if (uri == null) {
    237             return;
    238         }
    239 
    240         LogUtils.i(mLogTag, "Traversing: %s", uri.toString());
    241 
    242         final ContentResolver resolver = getContext().getContentResolver();
    243 
    244         final Cursor cursor = resolver.query(uri, null, null, null, null);
    245 
    246         mTraversedUris.add(uri.toString());
    247 
    248         if (cursor != null) {
    249             try {
    250                 // get the columns
    251                 final String[] columns = cursor.getColumnNames();
    252 
    253                 // Go through each of rows
    254                 while (cursor.moveToNext()) {
    255 
    256                     // Go through each of the columns find the ones that returns uris
    257                     for (String columnName : columns) {
    258                         if (columnName.toLowerCase().contains("uri")) {
    259                             final String uriString =
    260                                     cursor.getString(cursor.getColumnIndex(columnName));
    261 
    262                             if (!mTraversedUris.contains(uriString)) {
    263                                 final Uri childUri = Uri.parse(uriString);
    264 
    265                                 traverseUri(childUri);
    266                             }
    267                         }
    268                     }
    269                 }
    270             } finally {
    271                 cursor.close();
    272             }
    273         } else {
    274             // fail("query returned null");
    275             LogUtils.e(mLogTag, "query returned null: %s", uri.toString());
    276         }
    277     }
    278 
    279 
    280     /**
    281      * Test to add
    282      * 1) Make sure that the query result columns match the UIProvider schema
    283      * 2) Make sure the data is valid
    284      */
    285 }