1/*******************************************************************************
2 *      Copyright (C) 2012 Google Inc.
3 *      Licensed to The Android Open Source Project.
4 *
5 *      Licensed under the Apache License, Version 2.0 (the "License");
6 *      you may not use this file except in compliance with the License.
7 *      You may obtain a copy of the License at
8 *
9 *           http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *      Unless required by applicable law or agreed to in writing, software
12 *      distributed under the License is distributed on an "AS IS" BASIS,
13 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *      See the License for the specific language governing permissions and
15 *      limitations under the License.
16 *******************************************************************************/
17
18package com.android.mail.browse;
19
20import android.content.ContentProviderOperation;
21import android.content.ContentProviderResult;
22import android.content.ContentUris;
23import android.content.ContentValues;
24import android.content.Context;
25import android.content.OperationApplicationException;
26import android.database.Cursor;
27import android.net.Uri;
28import android.os.RemoteException;
29import android.test.ProviderTestCase2;
30import android.test.mock.MockContentResolver;
31import android.test.suitebuilder.annotation.SmallTest;
32
33import java.util.ArrayList;
34
35@SmallTest
36public class TestProviderTests extends ProviderTestCase2<TestProvider> {
37    Context mMockContext;
38    MockContentResolver mMockResolver;
39
40    private static final String CANHAZ_AUTHORITY = "com.android.canhaz";
41    private static final String PONY_TABLE = "pony";
42    private static final String PONY_COLUMN_NAME = "name";
43    private static final String PONY_COLUMN_TYPE = "type";
44    private static final String PONY_COLUMN_LEGS= "legs";
45    private static final String PONY_COLUMN_CAN_RIDE = "canRide";
46    private static final String[] PONY_PROJECTION = {TestProvider.ID_COLUMN, PONY_COLUMN_NAME,
47        PONY_COLUMN_TYPE, PONY_COLUMN_LEGS, PONY_COLUMN_CAN_RIDE};
48    private static final int PONY_ID = 0;
49    private static final int PONY_NAME = 1;
50    private static final int PONY_TYPE = 2;
51    private static final int PONY_LEGS = 3;
52    private static final int PONY_CAN_RIDE = 4;
53
54    public TestProviderTests() {
55        super(TestProvider.class, TestProvider.AUTHORITY);
56    }
57
58    @Override
59    public void setUp() throws Exception {
60        super.setUp();
61        mMockContext = getMockContext();
62        mMockResolver = (MockContentResolver)mMockContext.getContentResolver();
63        mMockResolver.addProvider(CANHAZ_AUTHORITY, new TestProvider(mMockContext));
64    }
65
66    @Override
67    public void tearDown() throws Exception {
68        super.tearDown();
69    }
70
71    private static ContentValues ponyValues(String name, String type, int legs, boolean canRide) {
72        ContentValues cv = new ContentValues();
73        cv.put(PONY_COLUMN_NAME, name);
74        cv.put(PONY_COLUMN_TYPE, type);
75        cv.put(PONY_COLUMN_LEGS, legs);
76        cv.put(PONY_COLUMN_CAN_RIDE, canRide ? 1 : 0);
77        return cv;
78    }
79
80    private ContentProviderResult[] setupPonies() throws RemoteException,
81            OperationApplicationException {
82        // The Uri is content://com.android.canhaz/pony
83        Uri uri = new Uri.Builder().scheme("content").authority(CANHAZ_AUTHORITY)
84            .path(PONY_TABLE).build();
85        // Our array of CPO's to be used with applyBatch
86        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
87
88        // Insert two ponies
89        ContentValues pony1 = ponyValues("Flicka", "wayward", 4, true);
90        ops.add(ContentProviderOperation.newInsert(uri).withValues(pony1).build());
91        ContentValues pony2 = ponyValues("Elise", "dastardly", 3, false);
92        ops.add(ContentProviderOperation.newInsert(uri).withValues(pony2).build());
93        // Apply the batch with one insert operation
94        return mMockResolver.applyBatch(TestProvider.AUTHORITY, ops);
95    }
96
97    private static Uri getPonyUri() {
98        return new Uri.Builder().scheme("content").authority(CANHAZ_AUTHORITY)
99            .path(PONY_TABLE).build();
100    }
101
102    public void testInsertQueryandDelete() throws RemoteException, OperationApplicationException {
103        // The Uri is content://com.android.canhaz/pony
104        ContentProviderResult[] results = setupPonies();
105        Uri uri = getPonyUri();
106
107        // Check the results
108        assertNotNull(results);
109        assertEquals(2, results.length);
110        // Make sure that we've created matcher entries for pony and pony/#
111        assertEquals(TestProvider.TABLE, TestProvider.sURIMatcher.match(TestProvider.uri(uri)));
112        assertEquals(TestProvider.RECORD,
113                TestProvider.sURIMatcher.match(TestProvider.uri(results[0].uri)));
114        Cursor c = mMockResolver.query(TestProvider.uri(uri), PONY_PROJECTION, null, null, null);
115        assertNotNull(c);
116        assertEquals(2, c.getCount());
117        long eliseId = -1;
118        long flickaId = -1;
119        while (c.moveToNext()) {
120            String name = c.getString(PONY_NAME);
121            if ("Flicka".equals(name)) {
122                assertEquals("Flicka", c.getString(PONY_NAME));
123                assertEquals("wayward", c.getString(PONY_TYPE));
124                assertEquals(4, c.getInt(PONY_LEGS));
125                assertEquals(1, c.getInt(PONY_CAN_RIDE));
126                flickaId = c.getLong(PONY_ID);
127            } else if ("Elise".equals(name)) {
128                assertEquals("dastardly", c.getString(PONY_TYPE));
129                assertEquals(3, c.getInt(PONY_LEGS));
130                assertEquals(0, c.getInt(PONY_CAN_RIDE));
131                eliseId = c.getLong(PONY_ID);
132            } else {
133                fail("Wrong record: " + name);
134            }
135        }
136
137        // eliseId and flickaId should have been set
138        assertNotSame(-1, eliseId);
139        assertNotSame(-1, flickaId);
140        // Delete the elise record
141        assertEquals(1, mMockResolver.delete(ContentUris.withAppendedId(TestProvider.uri(uri),
142                eliseId), null, null));
143        c = mMockResolver.query(TestProvider.uri(uri), PONY_PROJECTION, null, null, null);
144        assertNotNull(c);
145        // There should be one left (Flicka)
146        assertEquals(1, c.getCount());
147        assertTrue(c.moveToNext());
148        assertEquals("Flicka", c.getString(PONY_NAME));
149    }
150
151    public void testUpdate() throws RemoteException, OperationApplicationException {
152        // The Uri is content://com.android.canhaz/pony
153        Uri uri = getPonyUri();
154        setupPonies();
155        Cursor c = mMockResolver.query(TestProvider.uri(uri), PONY_PROJECTION, null, null, null);
156        assertNotNull(c);
157        assertEquals(2, c.getCount());
158        // Give all the ponies 5 legs
159        ContentValues cv = new ContentValues();
160        cv.put(PONY_COLUMN_LEGS, 5);
161        assertEquals(2, mMockResolver.update(TestProvider.uri(uri), cv, null, null));
162        c = mMockResolver.query(TestProvider.uri(uri), PONY_PROJECTION, null, null, null);
163        assertNotNull(c);
164        // We should still have two records, and each should have 5 legs, but otherwise be the same
165        assertEquals(2, c.getCount());
166        long eliseId = -1;
167        long flickaId = -1;
168        while (c.moveToNext()) {
169            String name = c.getString(PONY_NAME);
170            if ("Flicka".equals(name)) {
171                assertEquals("Flicka", c.getString(PONY_NAME));
172                assertEquals("wayward", c.getString(PONY_TYPE));
173                assertEquals(5, c.getInt(PONY_LEGS));
174                assertEquals(1, c.getInt(PONY_CAN_RIDE));
175                flickaId = c.getLong(PONY_ID);
176            } else if ("Elise".equals(name)) {
177                assertEquals("dastardly", c.getString(PONY_TYPE));
178                assertEquals(5, c.getInt(PONY_LEGS));
179                assertEquals(0, c.getInt(PONY_CAN_RIDE));
180                eliseId = c.getLong(PONY_ID);
181            } else {
182                fail("Wrong record: " + name);
183            }
184        }
185        // eliseId and flickaId should have been set
186        assertNotSame(-1, eliseId);
187        assertNotSame(-1, flickaId);
188    }
189}
190