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.app.Activity; 21import android.content.ContentProviderOperation; 22import android.content.ContentProviderResult; 23import android.content.ContentResolver; 24import android.content.ContentValues; 25import android.content.Context; 26import android.content.OperationApplicationException; 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 ConversationCursorTests extends ProviderTestCase2<TestProvider> { 37 38 public ConversationCursorTests(Class<TestProvider> providerClass, String providerAuthority) { 39 super(providerClass, providerAuthority); 40 } 41 42 Activity mActivity; 43 Context mMockContext; 44 MockContentResolver mMockResolver; 45 46 private static final String CONVO_TABLE = "convo"; 47 48 public ConversationCursorTests() { 49 super(TestProvider.class, TestProvider.AUTHORITY); 50 mActivity = new Activity() { 51 @Override 52 public ContentResolver getContentResolver() { 53 return mMockResolver; 54 } 55 }; 56 } 57 58 @Override 59 public void setUp() throws Exception { 60 super.setUp(); 61 mMockContext = getMockContext(); 62 mMockResolver = (MockContentResolver)mMockContext.getContentResolver(); 63 mMockResolver.addProvider(TestProvider.AUTHORITY, new TestProvider(mMockContext)); 64 } 65 66 @Override 67 public void tearDown() throws Exception { 68 super.tearDown(); 69 } 70 71 private static final String SUBJECT_COLUMN = "subject"; 72 private static final String FOLDER_COLUMN = "folder"; 73 private static final String READ_COLUMN = "read"; 74 private static final String STARRED_COLUMN = "starred"; 75 private static final String URI_COLUMN = "uri"; 76 77// private static final int SUBJECT_INDEX = 0; 78// private static final int FOLDER_INDEX = 1; 79// private static final int READ_INDEX = 2; 80// private static final int STARRED_INDEX = 3; 81 private static final int URI_INDEX = 4; 82 83 private static ContentValues makeConvo(String subject, String folder, int read, int starred) { 84 ContentValues cv = new ContentValues(); 85 cv.put(SUBJECT_COLUMN, subject); 86 cv.put(FOLDER_COLUMN, folder); 87 cv.put(READ_COLUMN, read); 88 cv.put(STARRED_COLUMN, starred); 89 return cv; 90 } 91 92 private Uri setupConvoList() throws RemoteException, OperationApplicationException { 93 // The Uri is content://com.android.canhaz/pony 94 Uri uri = new Uri.Builder().scheme("content").authority(TestProvider.AUTHORITY) 95 .path(CONVO_TABLE).build(); 96 // Our array of CPO's to be used with applyBatch 97 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 98 // Insert 9 convos 99 ContentValues values = makeConvo("Subj1", "Folder", 0, 0); 100 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 101 values = makeConvo("Subj2", "Folder", 0, 1); 102 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 103 values = makeConvo("Subj3", "Folder", 0, 0); 104 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 105 values = makeConvo("Subj4", "Folder", 0, 1); 106 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 107 values = makeConvo("Subj5", "Folder", 1, 0); 108 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 109 values = makeConvo("Subj6", "Folder", 1, 1); 110 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 111 values = makeConvo("Subj7", "Folder", 1, 0); 112 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 113 values = makeConvo("Subj8", "Folder", 1, 1); 114 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 115 values = makeConvo("Subj9", "Folder", 1, 0); 116 ops.add(ContentProviderOperation.newInsert(uri).withValues(values).build()); 117 // Apply the batch with one insert operation 118 ContentProviderResult[] results = mMockResolver.applyBatch(TestProvider.AUTHORITY, ops); 119 ops.clear(); 120 // Set up the uri column (basis of the cache) 121 for (ContentProviderResult result: results) { 122 ops.add(ContentProviderOperation 123 .newUpdate(result.uri) 124 .withValue(URI_COLUMN, result.uri.toString()) 125 .build()); 126 } 127 mMockResolver.applyBatch(TestProvider.AUTHORITY, ops); 128 return uri; 129 } 130 131 public void brokentestLocalDelete() throws RemoteException, OperationApplicationException { 132 Uri uri = setupConvoList(); 133 134// // Now, get our CC 135// ConversationCursor cc = 136// ConversationCursor.create(mActivity, URI_COLUMN, uri, CONVO_PROJECTION); 137// ConversationProvider cp = ConversationCursor.sProvider; 138// Cursor uc = ConversationCursor.sUnderlyingCursor; 139// // First things first; cc & uc should both have 9 rows 140// assertEquals(9, cc.getCount()); 141// assertEquals(9, uc.getCount()); 142// 143// // Get the uri's of our convos (the mock provider doesn't order rows) 144// String[] uris = new String[cc.getCount()]; 145// int i = 0; 146// while (cc.moveToNext()) { 147// uris[i++] = cc.getString(URI_INDEX); 148// } 149// 150// // Get a random uri (first will do) 151// String uriString = uris[0]; 152// Uri ccUri = Uri.parse(uriString); 153// // It should have the authority of CP 154// assertEquals(ccUri.getAuthority(), ConversationProvider.AUTHORITY); 155// 156// // Try deleting a row locally 157// cp.deleteLocal(Uri.parse(uris[4])); 158// assertEquals(8, cc.getCount()); 159// assertEquals(9, uc.getCount()); 160 } 161} 162