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