• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.android.mail.browse;
19 
20 import android.content.ContentProviderOperation;
21 import android.content.ContentProviderResult;
22 import android.content.ContentUris;
23 import android.content.ContentValues;
24 import android.content.Context;
25 import android.content.OperationApplicationException;
26 import android.database.Cursor;
27 import android.net.Uri;
28 import android.os.RemoteException;
29 import android.test.ProviderTestCase2;
30 import android.test.mock.MockContentResolver;
31 import android.test.suitebuilder.annotation.SmallTest;
32 
33 import java.util.ArrayList;
34 
35 @SmallTest
36 public class TestProviderTests extends ProviderTestCase2<TestProvider> {
37     Context mMockContext;
38     MockContentResolver mMockResolver;
39 
40     private static final String PONY_TABLE = "pony";
41     private static final String PONY_COLUMN_NAME = "name";
42     private static final String PONY_COLUMN_TYPE = "type";
43     private static final String PONY_COLUMN_LEGS= "legs";
44     private static final String PONY_COLUMN_CAN_RIDE = "canRide";
45     private static final String[] PONY_PROJECTION = {TestProvider.ID_COLUMN, PONY_COLUMN_NAME,
46         PONY_COLUMN_TYPE, PONY_COLUMN_LEGS, PONY_COLUMN_CAN_RIDE};
47     private static final int PONY_ID = 0;
48     private static final int PONY_NAME = 1;
49     private static final int PONY_TYPE = 2;
50     private static final int PONY_LEGS = 3;
51     private static final int PONY_CAN_RIDE = 4;
52 
TestProviderTests()53     public TestProviderTests() {
54         super(TestProvider.class, TestProvider.AUTHORITY);
55     }
56 
57     @Override
setUp()58     public void setUp() throws Exception {
59         super.setUp();
60         mMockContext = getMockContext();
61         mMockResolver = (MockContentResolver)mMockContext.getContentResolver();
62     }
63 
64     @Override
tearDown()65     public void tearDown() throws Exception {
66         super.tearDown();
67     }
68 
ponyValues(String name, String type, int legs, boolean canRide)69     private static ContentValues ponyValues(String name, String type, int legs, boolean canRide) {
70         ContentValues cv = new ContentValues();
71         cv.put(PONY_COLUMN_NAME, name);
72         cv.put(PONY_COLUMN_TYPE, type);
73         cv.put(PONY_COLUMN_LEGS, legs);
74         cv.put(PONY_COLUMN_CAN_RIDE, canRide ? 1 : 0);
75         return cv;
76     }
77 
setupPonies()78     private ContentProviderResult[] setupPonies() throws RemoteException,
79             OperationApplicationException {
80         Uri uri = new Uri.Builder().scheme("content").authority(TestProvider.AUTHORITY)
81             .path(PONY_TABLE).build();
82         // Our array of CPO's to be used with applyBatch
83         ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
84 
85         // Insert two ponies
86         ContentValues pony1 = ponyValues("Flicka", "wayward", 4, true);
87         ops.add(ContentProviderOperation.newInsert(uri).withValues(pony1).build());
88         ContentValues pony2 = ponyValues("Elise", "dastardly", 3, false);
89         ops.add(ContentProviderOperation.newInsert(uri).withValues(pony2).build());
90         // Apply the batch with one insert operation
91         return mMockResolver.applyBatch(TestProvider.AUTHORITY, ops);
92     }
93 
getPonyUri()94     private static Uri getPonyUri() {
95         return new Uri.Builder().scheme("content").authority(TestProvider.AUTHORITY)
96             .path(PONY_TABLE).build();
97     }
98 
testInsertQueryandDelete()99     public void testInsertQueryandDelete() throws RemoteException, OperationApplicationException {
100         ContentProviderResult[] results = setupPonies();
101         Uri uri = getPonyUri();
102 
103         // Check the results
104         assertNotNull(results);
105         assertEquals(2, results.length);
106         // Make sure that we've created matcher entries for pony and pony/#
107         assertEquals(TestProvider.TABLE, TestProvider.sURIMatcher.match(uri));
108         assertEquals(TestProvider.RECORD, TestProvider.sURIMatcher.match(results[0].uri));
109         Cursor c = mMockResolver.query(uri, PONY_PROJECTION, null, null, null);
110         assertNotNull(c);
111         assertEquals(2, c.getCount());
112         long eliseId = -1;
113         long flickaId = -1;
114         while (c.moveToNext()) {
115             String name = c.getString(PONY_NAME);
116             if ("Flicka".equals(name)) {
117                 assertEquals("Flicka", c.getString(PONY_NAME));
118                 assertEquals("wayward", c.getString(PONY_TYPE));
119                 assertEquals(4, c.getInt(PONY_LEGS));
120                 assertEquals(1, c.getInt(PONY_CAN_RIDE));
121                 flickaId = c.getLong(PONY_ID);
122             } else if ("Elise".equals(name)) {
123                 assertEquals("dastardly", c.getString(PONY_TYPE));
124                 assertEquals(3, c.getInt(PONY_LEGS));
125                 assertEquals(0, c.getInt(PONY_CAN_RIDE));
126                 eliseId = c.getLong(PONY_ID);
127             } else {
128                 fail("Wrong record: " + name);
129             }
130         }
131 
132         // eliseId and flickaId should have been set
133         assertNotSame(-1, eliseId);
134         assertNotSame(-1, flickaId);
135         // Delete the elise record
136         assertEquals(1, mMockResolver.delete(ContentUris.withAppendedId(uri,
137                 eliseId), null, null));
138         c = mMockResolver.query(uri, PONY_PROJECTION, null, null, null);
139         assertNotNull(c);
140         // There should be one left (Flicka)
141         assertEquals(1, c.getCount());
142         assertTrue(c.moveToNext());
143         assertEquals("Flicka", c.getString(PONY_NAME));
144     }
145 
testUpdate()146     public void testUpdate() throws RemoteException, OperationApplicationException {
147         Uri uri = getPonyUri();
148         setupPonies();
149         Cursor c = mMockResolver.query(uri, PONY_PROJECTION, null, null, null);
150         assertNotNull(c);
151         assertEquals(2, c.getCount());
152         // Give all the ponies 5 legs
153         ContentValues cv = new ContentValues();
154         cv.put(PONY_COLUMN_LEGS, 5);
155         assertEquals(2, mMockResolver.update(uri, cv, null, null));
156         c = mMockResolver.query(uri, PONY_PROJECTION, null, null, null);
157         assertNotNull(c);
158         // We should still have two records, and each should have 5 legs, but otherwise be the same
159         assertEquals(2, c.getCount());
160         long eliseId = -1;
161         long flickaId = -1;
162         while (c.moveToNext()) {
163             String name = c.getString(PONY_NAME);
164             if ("Flicka".equals(name)) {
165                 assertEquals("Flicka", c.getString(PONY_NAME));
166                 assertEquals("wayward", c.getString(PONY_TYPE));
167                 assertEquals(5, c.getInt(PONY_LEGS));
168                 assertEquals(1, c.getInt(PONY_CAN_RIDE));
169                 flickaId = c.getLong(PONY_ID);
170             } else if ("Elise".equals(name)) {
171                 assertEquals("dastardly", c.getString(PONY_TYPE));
172                 assertEquals(5, c.getInt(PONY_LEGS));
173                 assertEquals(0, c.getInt(PONY_CAN_RIDE));
174                 eliseId = c.getLong(PONY_ID);
175             } else {
176                 fail("Wrong record: " + name);
177             }
178         }
179         // eliseId and flickaId should have been set
180         assertNotSame(-1, eliseId);
181         assertNotSame(-1, flickaId);
182     }
183 }
184