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
17package com.android.providers.contacts.testutil;
18
19import android.content.ContentResolver;
20import android.content.ContentValues;
21import android.net.Uri;
22import android.test.MoreAsserts;
23
24import junit.framework.Assert;
25
26/**
27 * Common methods for asserting database related operations.
28 */
29public class DatabaseAsserts {
30
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
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     */
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     * Create a contact with name and assert that the record exists.
68     *
69     * @return The created contact id pair.
70     */
71    public static ContactIdPair assertAndCreateContactWithName(ContentResolver resolver,
72            String firstName, String lastName) {
73        long rawContactId = RawContactUtil.createRawContactWithName(resolver, firstName, lastName);
74
75        long contactId = RawContactUtil.queryContactIdByRawContactId(resolver, rawContactId);
76        MoreAsserts.assertNotEqual(CommonDatabaseUtils.NOT_FOUND, contactId);
77
78        return new ContactIdPair(contactId, rawContactId);
79    }
80
81    /**
82     * Asserts that a contact id was deleted, has a delete log, and that log has a timestamp greater
83     * than the given timestamp.
84     *
85     * @param contactId The contact id to check.
86     * @param start The timestamp that the delete log should be greater than.
87     */
88    public static void assertHasDeleteLogGreaterThan(ContentResolver resolver, long contactId,
89            long start) {
90        Assert.assertFalse(ContactUtil.recordExistsForContactId(resolver, contactId));
91
92        long deletedTimestamp = DeletedContactUtil.queryDeletedTimestampForContactId(resolver,
93                contactId);
94        MoreAsserts.assertNotEqual(CommonDatabaseUtils.NOT_FOUND, deletedTimestamp);
95        Assert.assertTrue(deletedTimestamp > start);
96    }
97
98    /**
99     * Holds a single contact id and raw contact id relationship.
100     */
101    public static class ContactIdPair {
102        public long mContactId;
103        public long mRawContactId;
104
105        public ContactIdPair(long contactId, long rawContactId) {
106            this.mContactId = contactId;
107            this.mRawContactId = rawContactId;
108        }
109    }
110}
111