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.android.providers.contacts.testutil;
18 
19 import android.content.ContentResolver;
20 import android.content.ContentValues;
21 import android.net.Uri;
22 import android.test.MoreAsserts;
23 
24 import junit.framework.Assert;
25 
26 /**
27  * Common methods for asserting database related operations.
28  */
29 public class DatabaseAsserts {
30 
assertDeleteIsUnsupported(ContentResolver resolver, Uri uri)31     public static void assertDeleteIsUnsupported(ContentResolver resolver, Uri uri) {
32         try {
33             resolver.delete(uri, null, null);
34             Assert.fail("delete operation should have failed with UnsupportedOperationException on"
35                     + uri);
36         } catch (UnsupportedOperationException e) {
37             // pass
38         }
39     }
40 
assertInsertIsUnsupported(ContentResolver resolver, Uri uri)41     public static void assertInsertIsUnsupported(ContentResolver resolver, Uri  uri) {
42         try {
43             ContentValues values = new ContentValues();
44             resolver.insert(uri, values);
45             Assert.fail("insert operation should have failed with UnsupportedOperationException on"
46                     + uri);
47         } catch (UnsupportedOperationException e) {
48             // pass
49         }
50     }
51 
52     /**
53      * Create a contact and assert that the record exists.
54      *
55      * @return The created contact id pair.
56      */
assertAndCreateContact(ContentResolver resolver)57     public static ContactIdPair assertAndCreateContact(ContentResolver resolver) {
58         long rawContactId = RawContactUtil.createRawContactWithName(resolver);
59 
60         long contactId = RawContactUtil.queryContactIdByRawContactId(resolver, rawContactId);
61         MoreAsserts.assertNotEqual(CommonDatabaseUtils.NOT_FOUND, contactId);
62 
63         return new ContactIdPair(contactId, rawContactId);
64     }
65 
66     /**
67      * Asserts that a contact id was deleted, has a delete log, and that log has a timestamp greater
68      * than the given timestamp.
69      *
70      * @param contactId The contact id to check.
71      * @param start The timestamp that the delete log should be greater than.
72      */
assertHasDeleteLogGreaterThan(ContentResolver resolver, long contactId, long start)73     public static void assertHasDeleteLogGreaterThan(ContentResolver resolver, long contactId,
74             long start) {
75         Assert.assertFalse(ContactUtil.recordExistsForContactId(resolver, contactId));
76 
77         long deletedTimestamp = DeletedContactUtil.queryDeletedTimestampForContactId(resolver,
78                 contactId);
79         MoreAsserts.assertNotEqual(CommonDatabaseUtils.NOT_FOUND, deletedTimestamp);
80         Assert.assertTrue(deletedTimestamp > start);
81     }
82 
83     /**
84      * Holds a single contact id and raw contact id relationship.
85      */
86     public static class ContactIdPair {
87         public long mContactId;
88         public long mRawContactId;
89 
ContactIdPair(long contactId, long rawContactId)90         public ContactIdPair(long contactId, long rawContactId) {
91             this.mContactId = contactId;
92             this.mRawContactId = rawContactId;
93         }
94     }
95 }
96