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