Home | History | Annotate | Download | only in vault
      1 /*
      2  * Copyright (C) 2013 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.example.android.vault;
     18 
     19 import static com.example.android.vault.VaultProvider.AUTHORITY;
     20 import static com.example.android.vault.VaultProvider.DEFAULT_DOCUMENT_ID;
     21 
     22 import android.content.ContentProviderClient;
     23 import android.database.Cursor;
     24 import android.provider.DocumentsContract.Document;
     25 import android.test.AndroidTestCase;
     26 
     27 import java.util.HashSet;
     28 
     29 /**
     30  * Tests for {@link VaultProvider}.
     31  */
     32 public class VaultProviderTest extends AndroidTestCase {
     33 
     34     private static final String MIME_TYPE_DEFAULT = "text/plain";
     35 
     36     private ContentProviderClient mClient;
     37     private VaultProvider mProvider;
     38 
     39     @Override
     40     protected void setUp() throws Exception {
     41         super.setUp();
     42 
     43         mClient = getContext().getContentResolver().acquireContentProviderClient(AUTHORITY);
     44         mProvider = (VaultProvider) mClient.getLocalContentProvider();
     45         mProvider.wipeAllContents();
     46     }
     47 
     48     @Override
     49     protected void tearDown() throws Exception {
     50         super.tearDown();
     51 
     52         mClient.release();
     53     }
     54 
     55     public void testDeleteDirectory() throws Exception {
     56         Cursor c;
     57 
     58         final String file = mProvider.createDocument(
     59                 DEFAULT_DOCUMENT_ID, MIME_TYPE_DEFAULT, "file");
     60         final String dir = mProvider.createDocument(
     61                 DEFAULT_DOCUMENT_ID, Document.MIME_TYPE_DIR, "dir");
     62 
     63         final String dirfile = mProvider.createDocument(
     64                 dir, MIME_TYPE_DEFAULT, "dirfile");
     65         final String dirdir = mProvider.createDocument(
     66                 dir, Document.MIME_TYPE_DIR, "dirdir");
     67 
     68         final String dirdirfile = mProvider.createDocument(
     69                 dirdir, MIME_TYPE_DEFAULT, "dirdirfile");
     70 
     71         // verify everything is in place
     72         c = mProvider.queryChildDocuments(DEFAULT_DOCUMENT_ID, null, null);
     73         assertContains(c, "file", "dir");
     74         c = mProvider.queryChildDocuments(dir, null, null);
     75         assertContains(c, "dirfile", "dirdir");
     76 
     77         // should remove children and parent ref
     78         mProvider.deleteDocument(dir);
     79 
     80         c = mProvider.queryChildDocuments(DEFAULT_DOCUMENT_ID, null, null);
     81         assertContains(c, "file");
     82 
     83         mProvider.queryDocument(file, null);
     84 
     85         try { mProvider.queryDocument(dir, null); } catch (Exception expected) { }
     86         try { mProvider.queryDocument(dirfile, null); } catch (Exception expected) { }
     87         try { mProvider.queryDocument(dirdir, null); } catch (Exception expected) { }
     88         try { mProvider.queryDocument(dirdirfile, null); } catch (Exception expected) { }
     89     }
     90 
     91     private static void assertContains(Cursor c, String... docs) {
     92         final HashSet<String> set = new HashSet<String>();
     93         while (c.moveToNext()) {
     94             set.add(c.getString(c.getColumnIndex(Document.COLUMN_DISPLAY_NAME)));
     95         }
     96 
     97         for (String doc : docs) {
     98             assertTrue(doc, set.contains(doc));
     99         }
    100     }
    101 }
    102