summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/mail/browse/ConversationCursorTests.java
blob: fe6a07f7a35d33737364ebda4aa6b38050af9b1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*******************************************************************************
 *      Copyright (C) 2012 Google Inc.
 *      Licensed to The Android Open Source Project.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *           http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 *******************************************************************************/

package com.android.mail.browse;

import android.app.Activity;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.test.ProviderTestCase2;
import android.test.mock.MockContentResolver;

import com.android.mail.browse.ConversationCursor.ConversationProvider;

import java.util.ArrayList;

public class ConversationCursorTests extends ProviderTestCase2<TestProvider> {

    public ConversationCursorTests(Class<TestProvider> providerClass, String providerAuthority) {
        super(providerClass, providerAuthority);
    }

    Activity mActivity;
    Context mMockContext;
    MockContentResolver mMockResolver;

    private static final String CONVO_TABLE = "convo";

    public ConversationCursorTests() {
        super(TestProvider.class, TestProvider.AUTHORITY);
        mActivity = new Activity() {
            public ContentResolver getContentResolver() {
                return mMockResolver;
            }
        };
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mMockContext = getMockContext();
        mMockResolver = (MockContentResolver)mMockContext.getContentResolver();
        mMockResolver.addProvider(TestProvider.AUTHORITY, new TestProvider(mMockContext));
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
    }

    private static final String SUBJECT_COLUMN = "subject";
    private static final String FOLDER_COLUMN = "folder";
    private static final String READ_COLUMN = "read";
    private static final String STARRED_COLUMN = "starred";
    private static final String URI_COLUMN = "uri";

//    private static final int SUBJECT_INDEX = 0;
//    private static final int FOLDER_INDEX = 1;
//    private static final int READ_INDEX = 2;
//    private static final int STARRED_INDEX = 3;
    private static final int URI_INDEX = 4;

    private static final String[] CONVO_PROJECTION = new String[] {
        SUBJECT_COLUMN, FOLDER_COLUMN, READ_COLUMN, STARRED_COLUMN, URI_COLUMN};

    private ContentValues makeConvo(String subject, String folder, int read, int starred) {
        ContentValues cv = new ContentValues();
        cv.put(SUBJECT_COLUMN, subject);
        cv.put(FOLDER_COLUMN, folder);
        cv.put(READ_COLUMN, read);
        cv.put(STARRED_COLUMN, starred);
        return cv;
    }

    private Uri setupConvoList() throws RemoteException, OperationApplicationException {
        // The Uri is content://com.android.canhaz/pony
        Uri uri = new Uri.Builder().scheme("content").authority(TestProvider.AUTHORITY)
                .path(CONVO_TABLE).build();
        // Our array of CPO's to be used with applyBatch
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        // Insert 9 convos
        ContentValues values = makeConvo("Subj1", "Folder", 0, 0);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj2", "Folder", 0, 1);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj3", "Folder", 0, 0);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj4", "Folder", 0, 1);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj5", "Folder", 1, 0);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj6", "Folder", 1, 1);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj7", "Folder", 1, 0);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj8", "Folder", 1, 1);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        values = makeConvo("Subj9", "Folder", 1, 0);
        ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build());
        // Apply the batch with one insert operation
        ContentProviderResult[] results = mMockResolver.applyBatch(TestProvider.AUTHORITY, ops);
        ops.clear();
        // Set up the uri column (basis of the cache)
        for (ContentProviderResult result: results) {
            ops.add(ContentProviderOperation
                    .newUpdate(result.uri)
                    .withValue(URI_COLUMN, result.uri.toString())
                    .build());
        }
        mMockResolver.applyBatch(TestProvider.AUTHORITY, ops);
        return uri;
    }

    public void testLocalDelete() throws RemoteException, OperationApplicationException {
        Uri uri = setupConvoList();

        // Now, get our CC
        ConversationCursor cc =
                ConversationCursor.create(mActivity, URI_COLUMN, uri, CONVO_PROJECTION);
        ConversationProvider cp = ConversationCursor.sProvider;
        Cursor uc = ConversationCursor.sUnderlyingCursor;
        // First things first; cc & uc should both have 9 rows
        assertEquals(9, cc.getCount());
        assertEquals(9, uc.getCount());

        // Get the uri's of our convos (the mock provider doesn't order rows)
        String[] uris = new String[cc.getCount()];
        int i = 0;
        while (cc.moveToNext()) {
            uris[i++] = cc.getString(URI_INDEX);
        }

        // Get a random uri (first will do)
        String uriString = uris[0];
        Uri ccUri = Uri.parse(uriString);
        // It should have the authority of CP
        assertEquals(ccUri.getAuthority(), ConversationProvider.AUTHORITY);

        // Try deleting a row locally
        cp.deleteLocal(Uri.parse(uris[4]));
        assertEquals(8, cc.getCount());
        assertEquals(9, uc.getCount());
    }
}