MockContentProvider.java revision 74a2dc5dd353aa724f3b4bcfc4694f15c4d0ff73
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 17package com.android.contacts.tests.mocks; 18 19import android.content.ContentProvider; 20import android.content.ContentValues; 21import android.database.Cursor; 22import android.database.MatrixCursor; 23import android.net.Uri; 24import android.text.TextUtils; 25 26import java.util.ArrayList; 27import java.util.Arrays; 28import java.util.LinkedList; 29 30import junit.framework.Assert; 31 32/** 33 * A programmable mock content provider. 34 */ 35public class MockContentProvider extends ContentProvider { 36 37 public static class Query { 38 39 private final Uri mUri; 40 private String[] mProjection; 41 private String[] mDefaultProjection; 42 private String mSelection; 43 private String[] mSelectionArgs; 44 private String mSortOrder; 45 private ArrayList<Object[]> mRows = new ArrayList<Object[]>(); 46 47 public Query(Uri uri) { 48 mUri = uri; 49 } 50 51 @Override 52 public String toString() { 53 return queryToString(mUri, mProjection, mSelection, mSelectionArgs, mSortOrder); 54 } 55 56 public Query withProjection(String... projection) { 57 mProjection = projection; 58 return this; 59 } 60 61 public Query withDefaultProjection(String... projection) { 62 mDefaultProjection = projection; 63 return this; 64 } 65 66 public Query withSelection(String selection, String... selectionArgs) { 67 mSelection = selection; 68 mSelectionArgs = selectionArgs; 69 return this; 70 } 71 72 public Query withSortOrder(String sortOrder) { 73 mSortOrder = sortOrder; 74 return this; 75 } 76 77 public Query returnRow(Object... row) { 78 mRows.add(row); 79 return this; 80 } 81 82 public boolean equals(Uri uri, String[] projection, String selection, 83 String[] selectionArgs, String sortOrder) { 84 if (!uri.equals(mUri)) { 85 return false; 86 } 87 88 if (!equals(projection, mProjection)) { 89 return false; 90 } 91 92 if (!TextUtils.equals(selection, mSelection)) { 93 return false; 94 } 95 96 if (!equals(selectionArgs, mSelectionArgs)) { 97 return false; 98 } 99 100 if (!TextUtils.equals(sortOrder, mSortOrder)) { 101 return false; 102 } 103 104 return true; 105 } 106 107 private boolean equals(String[] array1, String[] array2) { 108 boolean empty1 = array1 == null || array1.length == 0; 109 boolean empty2 = array2 == null || array2.length == 0; 110 if (empty1 && empty2) { 111 return true; 112 } 113 if (empty1) { 114 return false; 115 } 116 117 for (int i = 0; i < array1.length; i++) { 118 if (!array1[i].equals(array2[i])) { 119 return false; 120 } 121 } 122 return true; 123 } 124 125 public Cursor getResult() { 126 String[] columnNames = mProjection != null ? mProjection : mDefaultProjection; 127 MatrixCursor cursor = new MatrixCursor(columnNames); 128 for (Object[] row : mRows) { 129 cursor.addRow(row); 130 } 131 return cursor; 132 } 133 } 134 135 private LinkedList<Query> mExpectedQueries = new LinkedList<Query>(); 136 137 @Override 138 public boolean onCreate() { 139 return true; 140 } 141 142 public Query expectQuery(Uri contentUri) { 143 Query query = new Query(contentUri); 144 mExpectedQueries.offer(query); 145 return query; 146 } 147 148 @Override 149 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 150 String sortOrder) { 151 if (mExpectedQueries.isEmpty()) { 152 Assert.fail("Unexpected query: " 153 + queryToString(uri, projection, selection, selectionArgs, sortOrder)); 154 } 155 156 Query query = mExpectedQueries.remove(); 157 if (!query.equals(uri, projection, selection, selectionArgs, sortOrder)) { 158 Assert.fail("Incorrect query.\n Expected: " + query + "\n Actual: " + 159 queryToString(uri, projection, selection, selectionArgs, sortOrder)); 160 } 161 162 return query.getResult(); 163 } 164 165 @Override 166 public int delete(Uri uri, String selection, String[] selectionArgs) { 167 throw new UnsupportedOperationException(); 168 } 169 170 @Override 171 public String getType(Uri uri) { 172 throw new UnsupportedOperationException(); 173 } 174 175 @Override 176 public Uri insert(Uri uri, ContentValues values) { 177 throw new UnsupportedOperationException(); 178 } 179 180 @Override 181 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 182 throw new UnsupportedOperationException(); 183 } 184 185 private static String queryToString(Uri uri, String[] projection, String selection, 186 String[] selectionArgs, String sortOrder) { 187 StringBuilder sb = new StringBuilder(); 188 sb.append(uri).append(" "); 189 if (projection != null) { 190 sb.append(Arrays.toString(projection)); 191 } else { 192 sb.append("[]"); 193 } 194 if (selection != null) { 195 sb.append(" selection: '").append(selection).append("'"); 196 if (selectionArgs != null) { 197 sb.append(Arrays.toString(selectionArgs)); 198 } else { 199 sb.append("[]"); 200 } 201 } 202 if (sortOrder != null) { 203 sb.append(" sort: '").append(sortOrder).append("'"); 204 } 205 return sb.toString(); 206 } 207 208 public void verify() { 209 Assert.assertTrue("Not all expected queries have been called: " + 210 mExpectedQueries, mExpectedQueries.isEmpty()); 211 } 212} 213