• 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 android.provider.cts.contacts;
18 
19 import static android.provider.cts.contacts.DatabaseAsserts.ContactIdPair;
20 
21 import android.content.ContentResolver;
22 import android.content.ContentUris;
23 import android.net.Uri;
24 import android.os.SystemClock;
25 import android.provider.ContactsContract;
26 import android.test.AndroidTestCase;
27 import android.test.MoreAsserts;
28 
29 import java.util.HashSet;
30 import java.util.List;
31 
32 public class ContactsContract_DeletedContacts extends AndroidTestCase {
33 
34     private static final Uri URI = ContactsContract.DeletedContacts.CONTENT_URI;
35 
36     private ContentResolver mResolver;
37 
38     @Override
setUp()39     public void setUp() {
40         mResolver = getContext().getContentResolver();
41 
42         try {
43             Thread.sleep(5000);
44         } catch (InterruptedException e) {
45             // ignore
46         }
47     }
48 
testDelete_isUnsupported()49     public void testDelete_isUnsupported() {
50 
51         DatabaseAsserts.assertDeleteIsUnsupported(mResolver, URI);
52 
53         Uri uri = ContentUris.withAppendedId(URI, 1L);
54         DatabaseAsserts.assertDeleteIsUnsupported(mResolver, uri);
55     }
56 
testInsert_isUnsupported()57     public void testInsert_isUnsupported() {
58         DatabaseAsserts.assertInsertIsUnsupported(mResolver, URI);
59     }
60 
testQuery_returnsProperColumns()61     public void testQuery_returnsProperColumns() {
62         long start = System.currentTimeMillis();
63         ContactIdPair ids = createAndDeleteContact();
64 
65         String[] projection =
66                 new String[] {
67                     ContactsContract.DeletedContacts.CONTACT_ID,
68                     ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP
69                 };
70         List<String[]> records = DeletedContactUtil.query(mResolver, projection);
71         boolean found = false;
72         for (String[] record : records) {
73             if (Long.parseLong(record[0]) == ids.mContactId) {
74                 found = true;
75                 assertTrue(Long.parseLong(record[1]) > start);
76             }
77         }
78         assertTrue(found);
79     }
80 
testQueryByContactId()81     public void testQueryByContactId() {
82         ContactIdPair ids = createAndDeleteContact();
83 
84         MoreAsserts.assertNotEqual(CommonDatabaseUtils.NOT_FOUND,
85                 DeletedContactUtil.queryDeletedTimestampForContactId(mResolver, ids.mContactId));
86     }
87 
testQueryAll()88     public void testQueryAll() {
89         int numDeletes = 10;
90 
91         // Since we cannot clean out delete log from previous tests, we need to account for that
92         // by querying for the count first.
93         long startCount = DeletedContactUtil.getCount(mResolver);
94 
95         for (int i = 0; i < numDeletes; i++) {
96             createAndDeleteContact();
97         }
98 
99         long endCount = DeletedContactUtil.getCount(mResolver);
100 
101         assertEquals(numDeletes, endCount - startCount);
102     }
103 
testQuerySinceTimestamp()104     public void testQuerySinceTimestamp() {
105 
106         // Before
107         HashSet<Long> beforeIds = new HashSet<Long>();
108         beforeIds.add(createAndDeleteContact().mContactId);
109         beforeIds.add(createAndDeleteContact().mContactId);
110 
111         long start = System.currentTimeMillis();
112 
113         // After
114         HashSet<Long> afterIds = new HashSet<Long>();
115         afterIds.add(createAndDeleteContact().mContactId);
116         afterIds.add(createAndDeleteContact().mContactId);
117         afterIds.add(createAndDeleteContact().mContactId);
118 
119         String[] projection = new String[]{
120                 ContactsContract.DeletedContacts.CONTACT_ID,
121                 ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP
122         };
123         List<String[]> records = DeletedContactUtil.querySinceTimestamp(mResolver, projection,
124                 start);
125         for (String[] record : records) {
126             // Check ids to make sure we only have the ones that came after the time.
127             long contactId = Long.parseLong(record[0]);
128             assertFalse(beforeIds.contains(contactId));
129             assertTrue(afterIds.contains(contactId));
130 
131             // Check times to make sure they came after
132             assertTrue(Long.parseLong(record[1]) > start);
133         }
134     }
135 
createAndDeleteContact()136     private ContactIdPair createAndDeleteContact() {
137         ContactIdPair ids = DatabaseAsserts.assertAndCreateContact(mResolver);
138         assertEquals(CommonDatabaseUtils.NOT_FOUND,
139                 DeletedContactUtil.queryDeletedTimestampForContactId(mResolver, ids.mContactId));
140         SystemClock.sleep(1);
141         RawContactUtil.delete(mResolver, ids.mRawContactId, true);
142         return ids;
143     }
144 }
145