Home | History | Annotate | Download | only in database
      1 /*
      2  * Copyright (C) 2016 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.dialer.database;
     18 
     19 import android.content.ContentUris;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.database.Cursor;
     23 import android.net.Uri;
     24 import android.test.ProviderTestCase2;
     25 import android.test.mock.MockContentResolver;
     26 import com.android.dialer.database.VoicemailArchiveContract.VoicemailArchive;
     27 
     28 import java.io.File;
     29 import java.io.OutputStream;
     30 
     31 /**
     32  * Tests for {@link VoicemailArchiveProvider}.
     33  */
     34 public class VoicemailArchiveProviderTest extends
     35         ProviderTestCase2<VoicemailArchiveProviderTest.TestVoicemailArchiveProvider> {
     36     private static final String TEST_MIME_TYPE = "audio/mp3";
     37     private static final String TEST_NUMBER = "+1412555555";
     38     private static final int TEST_ARCHIVED = 1;
     39     private static final String TEST_STRING = "TEST";
     40 
     41     private MockContentResolver mResolver;
     42     private Cursor mCursor;
     43     private Uri mVoicemailUri;
     44 
     45     public VoicemailArchiveProviderTest() {
     46         super(TestVoicemailArchiveProvider.class, VoicemailArchiveContract.AUTHORITY);
     47     }
     48 
     49     @Override
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52         mResolver = getMockContentResolver();
     53     }
     54 
     55     @Override
     56     protected void tearDown() throws Exception {
     57         if (mCursor != null) {
     58             mCursor.close();
     59         }
     60         // Need to delete test.cache folder created by {@link android.test.ProviderTestCase2}
     61         deleteRecursive(getContext().getCacheDir());
     62         getProvider().closeDb();
     63         super.tearDown();
     64     }
     65 
     66     public void testInsert() {
     67         insertVoicemail();
     68         assertNotNull(mVoicemailUri);
     69         assertTrue(doesRowExist());
     70         long id = ContentUris.parseId(mVoicemailUri);
     71         assertTrue(id > 0);
     72     }
     73 
     74     public void testQuery_createFile() throws Exception {
     75         insertVoicemail();
     76         assertTrue(doesRowExist());
     77         assertFalse(doesFileExist());
     78         createFile();
     79         assertTrue(doesFileExist());
     80     }
     81 
     82     public void testQuery_createFileMimeTypeIsAMR() throws Exception {
     83         insertVoicemailWithMimeType("audio/amr");
     84         assertTrue(doesRowExist());
     85         assertFalse(doesFileExist());
     86         createFile();
     87         assertTrue(doesFileExist());
     88         assertEquals("amr", getFileExtension(getFilePath()));
     89     }
     90 
     91     public void testQuery_createFileMimeTypeIsMP3() throws Exception {
     92         insertVoicemailWithMimeType("audio/mpeg");
     93         assertTrue(doesRowExist());
     94         assertFalse(doesFileExist());
     95         createFile();
     96         assertTrue(doesFileExist());
     97         assertEquals("mp3", getFileExtension(getFilePath()));
     98     }
     99 
    100     public void testQuery_createFileMimeTypeNotExists() throws Exception {
    101         insertVoicemailWithMimeType(TEST_STRING);
    102         assertTrue(doesRowExist());
    103         assertFalse(doesFileExist());
    104         createFile();
    105         assertTrue(doesFileExist());
    106         assertEquals("", getFileExtension(getFilePath()));
    107     }
    108 
    109     public void testQuery() {
    110         insertVoicemail();
    111         updateCursor();
    112         assertCursorCount(1);
    113         assertContentValues();
    114     }
    115 
    116     public void testQuery_correctValuesSelection() {
    117         insertVoicemail();
    118         updateCursorWithIdQuery();
    119         assertCursorCount(1);
    120         assertContentValues();
    121     }
    122 
    123     public void testQuery_illegalUri() {
    124         try {
    125             mResolver.query(Uri.withAppendedPath(VoicemailArchive.CONTENT_URI, TEST_STRING),
    126                     null, null, null, null);
    127             fail("Expecting exception but none was thrown.");
    128         } catch (IllegalArgumentException e) {}
    129     }
    130 
    131     public void testUpdate() throws Exception {
    132         insertVoicemail();
    133         assertTrue(doesRowExist());
    134         ContentValues values = new ContentValues();
    135         values.put(VoicemailArchive.MIME_TYPE, TEST_STRING);
    136         values.put(VoicemailArchive.NUMBER, TEST_STRING);
    137         values.put(VoicemailArchive.ARCHIVED, false);
    138         mResolver.update(mVoicemailUri, values, null, null);
    139         updateCursor();
    140         assertEquals(TEST_STRING, mCursor.getString(mCursor.getColumnIndex(VoicemailArchive.MIME_TYPE)));
    141         assertEquals(TEST_STRING, mCursor.getString(mCursor.getColumnIndex(VoicemailArchive.NUMBER)));
    142         assertEquals(false, mCursor.getInt(
    143                 mCursor.getColumnIndex(VoicemailArchive.ARCHIVED)) == 1);
    144     }
    145 
    146     public void testUpdate_selection() throws Exception {
    147         insertVoicemail();
    148         assertTrue(doesRowExist());
    149         ContentValues values = new ContentValues();
    150         values.put(VoicemailArchive.MIME_TYPE, TEST_STRING);
    151         values.put(VoicemailArchive.NUMBER, TEST_STRING);
    152         values.put(VoicemailArchive.ARCHIVED, false);
    153         mResolver.update(VoicemailArchive.CONTENT_URI, values, getIdQuery(), null);
    154         updateCursor();
    155         assertEquals(TEST_STRING, mCursor.getString(mCursor.getColumnIndex(VoicemailArchive.MIME_TYPE)));
    156         assertEquals(TEST_STRING, mCursor.getString(mCursor.getColumnIndex(VoicemailArchive.NUMBER)));
    157         assertEquals(false, mCursor.getInt(
    158                 mCursor.getColumnIndex(VoicemailArchive.ARCHIVED)) == 1);
    159     }
    160 
    161     public void testUpdate_illegalUri() {
    162         try {
    163             mResolver.update(Uri.withAppendedPath(VoicemailArchive.CONTENT_URI, TEST_STRING),
    164                     null, null, null);
    165             fail("Expecting exception but none was thrown.");
    166         } catch (IllegalArgumentException e) {}
    167     }
    168 
    169     public void testDelete() throws Exception {
    170         insertVoicemail();
    171         createFile();
    172         assertTrue(doesRowExist());
    173         assertTrue(doesFileExist());
    174         mResolver.delete(mVoicemailUri, null, null);
    175         assertFalse(doesRowExist());
    176         assertFalse(doesFileExist());
    177     }
    178 
    179     public void testDelete_selection() throws Exception{
    180         insertVoicemail();
    181         createFile();
    182         assertTrue(doesRowExist());
    183         assertTrue(doesFileExist());
    184         mResolver.delete(VoicemailArchive.CONTENT_URI, getIdQuery(), null);
    185         assertFalse(doesRowExist());
    186         assertFalse(doesFileExist());
    187     }
    188 
    189     public void testDelete_illegalUri() {
    190         try {
    191             mResolver.delete(Uri.withAppendedPath(VoicemailArchive.CONTENT_URI, TEST_STRING),
    192                     null, null);
    193             fail("Expecting exception but none was thrown.");
    194         } catch (IllegalArgumentException e) {}
    195     }
    196 
    197     private boolean doesRowExist() {
    198         Cursor tempCursor = mResolver.query(mVoicemailUri, null, null, null, null);
    199         boolean rowExists = tempCursor != null && tempCursor.getCount() > 0;
    200         tempCursor.close();
    201         return rowExists;
    202     }
    203 
    204     private boolean doesFileExist() {
    205         File voicemailFile = new File(getFilePath());
    206         return voicemailFile.exists();
    207     }
    208 
    209     private static String getFileExtension(String filePath) {
    210         File file = new File(filePath);
    211         String fileName = file.getName();
    212         int index = fileName.lastIndexOf(".");
    213         return index > 0 ? fileName.substring(index + 1) : "";
    214     }
    215 
    216     private void assertCursorCount(int count) {
    217         assertEquals(count, mCursor.getCount());
    218     }
    219 
    220     private void assertContentValues() {
    221         assertEquals(TEST_MIME_TYPE, mCursor
    222                 .getString(mCursor.getColumnIndex(VoicemailArchive.MIME_TYPE)));
    223         assertEquals(TEST_NUMBER, mCursor
    224                 .getString(mCursor.getColumnIndex(VoicemailArchive.NUMBER)));
    225         assertEquals(TEST_ARCHIVED, mCursor
    226                 .getInt(mCursor.getColumnIndex(VoicemailArchive.ARCHIVED)));
    227     }
    228 
    229     private void insertVoicemail() {
    230         mVoicemailUri = mResolver.insert(VoicemailArchive.CONTENT_URI, getTestValues());
    231     }
    232 
    233     private void insertVoicemailWithMimeType(String mimeType) {
    234         ContentValues values = getTestValues();
    235         values.put(VoicemailArchive.MIME_TYPE, mimeType);
    236         mVoicemailUri = mResolver.insert(VoicemailArchive.CONTENT_URI, values);
    237     }
    238 
    239     private void updateCursor() {
    240         mCursor = mResolver.query(mVoicemailUri, null, null, null, null);
    241         assertEquals(true, mCursor.getCount() > 0);
    242         mCursor.moveToFirst();
    243     }
    244 
    245     private void updateCursorWithIdQuery() {
    246         mCursor = mResolver.query(mVoicemailUri, null, getIdQuery(), null, null);
    247         assertEquals(true, mCursor.getCount() > 0);
    248         mCursor.moveToFirst();
    249     }
    250 
    251     private void createFile() throws Exception {
    252         assertFalse(doesFileExist());
    253         // Opening output stream and closing it should create the file
    254         OutputStream outputStream = mResolver.openOutputStream(mVoicemailUri);
    255         outputStream.close();
    256     }
    257 
    258     private String getIdQuery() {
    259         return VoicemailArchive._ID + "=" + ContentUris.parseId(mVoicemailUri);
    260     }
    261 
    262     private String getFilePath() {
    263         if (mCursor == null) {
    264             updateCursor();
    265         }
    266         return mCursor.getString(mCursor.getColumnIndex(VoicemailArchive._DATA));
    267     }
    268 
    269     private static ContentValues getTestValues() {
    270         ContentValues values = new ContentValues();
    271         values.put(VoicemailArchive.NUMBER, TEST_NUMBER);
    272         values.put(VoicemailArchive.MIME_TYPE, TEST_MIME_TYPE);
    273         values.put(VoicemailArchive.ARCHIVED, TEST_ARCHIVED);
    274         return values;
    275     }
    276 
    277     private void deleteRecursive(File fileOrDirectory) {
    278         if (fileOrDirectory.isDirectory()) {
    279             for (File child : fileOrDirectory.listFiles()) {
    280                 deleteRecursive(child);
    281             }
    282         }
    283         fileOrDirectory.delete();
    284     }
    285 
    286     public static class TestVoicemailArchiveProvider extends VoicemailArchiveProvider {
    287         private DialerDatabaseHelper mDialerDatabaseHelper;
    288 
    289         @Override
    290         protected File getFilesDir() {
    291             return getContext().getCacheDir();
    292         }
    293 
    294         @Override
    295         protected DialerDatabaseHelper getDatabaseHelper(Context context) {
    296             if (mDialerDatabaseHelper == null) {
    297                 mDialerDatabaseHelper = DialerDatabaseHelper.getNewInstanceForTest(context);
    298             }
    299             return mDialerDatabaseHelper;
    300         }
    301 
    302         protected void closeDb() {
    303             mDialerDatabaseHelper.close();
    304         }
    305     }
    306 }
    307