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