diff options
Diffstat (limited to 'tests/FrameworkTest')
| -rw-r--r-- | tests/FrameworkTest/tests/src/android/content/AbstractTableMergerTest.java | 35 | ||||
| -rw-r--r-- | tests/FrameworkTest/tests/src/android/content/ContentProviderOperationTest.java | 468 |
2 files changed, 490 insertions, 13 deletions
diff --git a/tests/FrameworkTest/tests/src/android/content/AbstractTableMergerTest.java b/tests/FrameworkTest/tests/src/android/content/AbstractTableMergerTest.java index aa3d186..42c1e78 100644 --- a/tests/FrameworkTest/tests/src/android/content/AbstractTableMergerTest.java +++ b/tests/FrameworkTest/tests/src/android/content/AbstractTableMergerTest.java @@ -8,6 +8,7 @@ import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.test.AndroidTestCase; import android.text.TextUtils; +import android.accounts.Account; import java.util.ArrayList; import java.util.Map; @@ -26,7 +27,7 @@ public class AbstractTableMergerTest extends AndroidTestCase { static final Uri TABLE_URI = Uri.withAppendedPath(CONTENT_URI, TABLE_NAME); static final Uri DELETED_TABLE_URI = Uri.withAppendedPath(CONTENT_URI, DELETED_TABLE_NAME); - private final String ACCOUNT = "account@goo.com"; + private final Account ACCOUNT = new Account("account@goo.com", "example.type"); private final ArrayList<Expectation> mExpectations = Lists.newArrayList(); @@ -65,25 +66,31 @@ public class AbstractTableMergerTest extends AndroidTestCase { mExpectations.clear(); } - ContentValues newValues(String data, String syncId, String syncAccount, + ContentValues newValues(String data, String syncId, Account syncAccount, String syncTime, String syncVersion, Long syncLocalId) { ContentValues values = new ContentValues(); if (data != null) values.put("data", data); if (syncTime != null) values.put("_sync_time", syncTime); if (syncVersion != null) values.put("_sync_version", syncVersion); if (syncId != null) values.put("_sync_id", syncId); - if (syncAccount != null) values.put("_sync_account", syncAccount); + if (syncAccount != null) { + values.put("_sync_account", syncAccount.mName); + values.put("_sync_account_type", syncAccount.mType); + } values.put("_sync_local_id", syncLocalId); values.put("_sync_dirty", 0); return values; } - ContentValues newDeletedValues(String syncId, String syncAccount, String syncVersion, + ContentValues newDeletedValues(String syncId, Account syncAccount, String syncVersion, Long syncLocalId) { ContentValues values = new ContentValues(); if (syncVersion != null) values.put("_sync_version", syncVersion); if (syncId != null) values.put("_sync_id", syncId); - if (syncAccount != null) values.put("_sync_account", syncAccount); + if (syncAccount != null) { + values.put("_sync_account", syncAccount.mName); + values.put("_sync_account_type", syncAccount.mType); + } if (syncLocalId != null) values.put("_sync_local_id", syncLocalId); return values; } @@ -380,6 +387,7 @@ public class AbstractTableMergerTest extends AndroidTestCase { + "_sync_local_id INTEGER, " + "_sync_dirty INTEGER NOT NULL DEFAULT 0, " + "_sync_account TEXT, " + + "_sync_account_type TEXT, " + "_sync_mark INTEGER)"); mDb.execSQL("CREATE TABLE deleted_items (" @@ -388,6 +396,7 @@ public class AbstractTableMergerTest extends AndroidTestCase { + "_sync_id TEXT, " + "_sync_local_id INTEGER, " + "_sync_account TEXT, " + + "_sync_account_type TEXT, " + "_sync_mark INTEGER)"); } @@ -501,7 +510,7 @@ public class AbstractTableMergerTest extends AndroidTestCase { throw new UnsupportedOperationException(); } - public void onSyncStart(SyncContext context, String account) { + public void onSyncStart(SyncContext context, Account account) { throw new UnsupportedOperationException(); } @@ -509,7 +518,7 @@ public class AbstractTableMergerTest extends AndroidTestCase { throw new UnsupportedOperationException(); } - public String getSyncingAccount() { + public Account getSyncingAccount() { throw new UnsupportedOperationException(); } @@ -544,24 +553,24 @@ public class AbstractTableMergerTest extends AndroidTestCase { throw new UnsupportedOperationException(); } - protected void onAccountsChanged(String[] accountsArray) { + protected void onAccountsChanged(Account[] accountsArray) { throw new UnsupportedOperationException(); } - protected void deleteRowsForRemovedAccounts(Map<String, Boolean> accounts, String table, - String accountColumnName) { + protected void deleteRowsForRemovedAccounts(Map<Account, Boolean> accounts, String table + ) { throw new UnsupportedOperationException(); } - public void wipeAccount(String account) { + public void wipeAccount(Account account) { throw new UnsupportedOperationException(); } - public byte[] readSyncDataBytes(String account) { + public byte[] readSyncDataBytes(Account account) { throw new UnsupportedOperationException(); } - public void writeSyncDataBytes(String account, byte[] data) { + public void writeSyncDataBytes(Account account, byte[] data) { throw new UnsupportedOperationException(); } } diff --git a/tests/FrameworkTest/tests/src/android/content/ContentProviderOperationTest.java b/tests/FrameworkTest/tests/src/android/content/ContentProviderOperationTest.java new file mode 100644 index 0000000..e9a43d7 --- /dev/null +++ b/tests/FrameworkTest/tests/src/android/content/ContentProviderOperationTest.java @@ -0,0 +1,468 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.content; + +import android.database.Cursor; +import android.net.Uri; +import android.os.Parcel; +import android.test.suitebuilder.annotation.SmallTest; +import android.text.TextUtils; +import junit.framework.TestCase; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; + +@SmallTest +public class ContentProviderOperationTest extends TestCase { + private final static Uri sTestUri1 = Uri.parse("content://authority/blah"); + private final static ContentValues sTestValues1; + + private final static Class<ContentProviderOperation.Builder> CLASS_BUILDER = + ContentProviderOperation.Builder.class; + private final static Class<ContentProviderOperation> CLASS_OPERATION = + ContentProviderOperation.class; + + static { + sTestValues1 = new ContentValues(); + sTestValues1.put("a", 1); + sTestValues1.put("b", "two"); + } + + public void testInsert() throws OperationApplicationException { + ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1) + .withValues(sTestValues1) + .build(); + ContentProviderResult result = op1.apply(new TestContentProvider() { + public Uri insert(Uri uri, ContentValues values) { + assertEquals(sTestUri1.toString(), uri.toString()); + assertEquals(sTestValues1.toString(), values.toString()); + return uri.buildUpon().appendPath("19").build(); + } + }, null, 0); + assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString()); + } + + public void testInsertNoValues() throws OperationApplicationException { + ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1) + .build(); + ContentProviderResult result = op1.apply(new TestContentProvider() { + public Uri insert(Uri uri, ContentValues values) { + assertEquals(sTestUri1.toString(), uri.toString()); + assertNull(values); + return uri.buildUpon().appendPath("19").build(); + } + }, null, 0); + assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString()); + } + + public void testInsertFailed() { + ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1) + .withValues(sTestValues1) + .build(); + try { + op1.apply(new TestContentProvider() { + public Uri insert(Uri uri, ContentValues values) { + assertEquals(sTestUri1.toString(), uri.toString()); + assertEquals(sTestValues1.toString(), values.toString()); + return null; + } + }, null, 0); + fail("the apply should have thrown an OperationApplicationException"); + } catch (OperationApplicationException e) { + // this is the expected case + } + } + + public void testInsertWithBackRefs() throws OperationApplicationException { + ContentProviderResult[] previousResults = new ContentProviderResult[4]; + previousResults[0] = new ContentProviderResult(100); + previousResults[1] = new ContentProviderResult(101); + previousResults[2] = new ContentProviderResult(102); + previousResults[3] = new ContentProviderResult(103); + ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1) + .withValues(sTestValues1) + .withValueBackReference("a1", 3) + .withValueBackReference("a2", 1) + .build(); + ContentProviderResult result = op1.apply(new TestContentProvider() { + public Uri insert(Uri uri, ContentValues values) { + assertEquals(sTestUri1.toString(), uri.toString()); + ContentValues expected = new ContentValues(sTestValues1); + expected.put("a1", 103); + expected.put("a2", 101); + assertEquals(expected.toString(), values.toString()); + return uri.buildUpon().appendPath("19").build(); + } + }, previousResults, previousResults.length); + assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString()); + } + + public void testUpdate() throws OperationApplicationException { + ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1) + .withValues(sTestValues1) + .build(); + ContentProviderResult[] backRefs = new ContentProviderResult[2]; + ContentProviderResult result = op1.apply(new TestContentProvider() { + public Uri insert(Uri uri, ContentValues values) { + assertEquals(sTestUri1.toString(), uri.toString()); + assertEquals(sTestValues1.toString(), values.toString()); + return uri.buildUpon().appendPath("19").build(); + } + }, backRefs, 1); + assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString()); + } + + public void testValueBackRefs() { + ContentValues values = new ContentValues(); + values.put("a", "in1"); + values.put("a2", "in2"); + values.put("b", "in3"); + values.put("c", "in4"); + + ContentProviderResult[] previousResults = new ContentProviderResult[4]; + previousResults[0] = new ContentProviderResult(100); + previousResults[1] = new ContentProviderResult(101); + previousResults[2] = new ContentProviderResult(102); + previousResults[3] = new ContentProviderResult(103); + + ContentValues expectedValues = new ContentValues(values); + expectedValues.put("a1", "103"); + expectedValues.put("a2", "101"); + expectedValues.put("a3", "102"); + + ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1) + .withValues(values) + .withValueBackReference("a1", 3) + .withValueBackReference("a2", 1) + .withValueBackReference("a3", 2) + .build(); + ContentValues v2 = op1.resolveValueBackReferences(previousResults, previousResults.length); + assertEquals(expectedValues, v2); + } + + public void testSelectionBackRefs() { + ContentProviderResult[] previousResults = new ContentProviderResult[4]; + previousResults[0] = new ContentProviderResult(100); + previousResults[1] = new ContentProviderResult(101); + previousResults[2] = new ContentProviderResult(102); + previousResults[3] = new ContentProviderResult(103); + + String[] selectionArgs = new String[]{"a", null, null, "b", null}; + + ContentProviderOperation op1 = ContentProviderOperation.newUpdate(sTestUri1) + .withSelectionBackReference(1, 3) + .withSelectionBackReference(2, 1) + .withSelectionBackReference(4, 2) + .withSelection("unused", selectionArgs) + .build(); + String[] s2 = op1.resolveSelectionArgsBackReferences( + previousResults, previousResults.length); + assertEquals("a,103,101,b,102", TextUtils.join(",", s2)); + } + + public void testParcelingOperation() throws NoSuchFieldException, IllegalAccessException, + NoSuchMethodException, InvocationTargetException, InstantiationException { + Parcel parcel = Parcel.obtain(); + ContentProviderOperation op1; + ContentProviderOperation op2; + + HashMap<Integer, Integer> selArgsBackRef = new HashMap<Integer, Integer>(); + selArgsBackRef.put(1, 2); + selArgsBackRef.put(3, 4); + + ContentValues values = new ContentValues(); + values.put("v1", "val1"); + values.put("v2", "43"); + + ContentValues valuesBackRef = new ContentValues(); + values.put("v3", "val3"); + values.put("v4", "44"); + + try { + ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert( + Uri.parse("content://goo/bar")); + + builderSetExpectedCount(builder, 42); + builderSetSelection(builder, "selection"); + builderSetSelectionArgs(builder, new String[]{"a", "b"}); + builderSetSelectionArgsBackReferences(builder, selArgsBackRef); + builderSetValues(builder, values); + builderSetValuesBackReferences(builder, valuesBackRef); + + op1 = newOperationFromBuilder(builder); + op1.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + op2 = ContentProviderOperation.CREATOR.createFromParcel(parcel); + + assertEquals(1 /* ContentProviderOperation.TYPE_INSERT */, operationGetType(op2)); + assertEquals("content://goo/bar", operationGetUri(op2).toString()); + assertEquals(Integer.valueOf(42), operationGetExpectedCount(op2)); + assertEquals("selection", operationGetSelection(op2)); + assertEquals(2, operationGetSelectionArgs(op2).length); + assertEquals("a", operationGetSelectionArgs(op2)[0]); + assertEquals("b", operationGetSelectionArgs(op2)[1]); + assertEquals(values, operationGetValues(op2)); + assertEquals(valuesBackRef, operationGetValuesBackReferences(op2)); + assertEquals(2, operationGetSelectionArgsBackReferences(op2).size()); + assertEquals(Integer.valueOf(2), operationGetSelectionArgsBackReferences(op2).get(1)); + assertEquals(Integer.valueOf(4), operationGetSelectionArgsBackReferences(op2).get(3)); + } finally { + parcel.recycle(); + } + + try { + ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate( + Uri.parse("content://goo/bar")); + + builderSetSelectionArgsBackReferences(builder, selArgsBackRef); + + op1 = newOperationFromBuilder(builder); + op1.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + op2 = ContentProviderOperation.CREATOR.createFromParcel(parcel); + assertEquals(2 /* ContentProviderOperation.TYPE_UPDATE */, operationGetType(op2)); + assertEquals("content://goo/bar", operationGetUri(op2).toString()); + assertNull(operationGetEntity(op2)); + assertNull(operationGetExpectedCount(op2)); + assertNull(operationGetSelection(op2)); + assertNull(operationGetSelectionArgs(op2)); + assertNull(operationGetValues(op2)); + assertNull(operationGetValuesBackReferences(op2)); + assertEquals(2, operationGetSelectionArgsBackReferences(op2).size()); + assertEquals(Integer.valueOf(2), operationGetSelectionArgsBackReferences(op2).get(1)); + assertEquals(Integer.valueOf(4), operationGetSelectionArgsBackReferences(op2).get(3)); + } finally { + parcel.recycle(); + } + + try { + ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete( + Uri.parse("content://goo/bar")); + + op1 = newOperationFromBuilder(builder); + op1.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + op2 = ContentProviderOperation.CREATOR.createFromParcel(parcel); + assertEquals(3 /* ContentProviderOperation.TYPE_DELETE */, operationGetType(op2)); + assertEquals("content://goo/bar", operationGetUri(op2).toString()); + assertNull(operationGetEntity(op2)); + assertNull(operationGetExpectedCount(op2)); + assertNull(operationGetSelection(op2)); + assertNull(operationGetSelectionArgs(op2)); + assertNull(operationGetValues(op2)); + assertNull(operationGetValuesBackReferences(op2)); + assertNull(operationGetSelectionArgsBackReferences(op2)); + } finally { + parcel.recycle(); + } + } + + private static ContentProviderOperation newOperationFromBuilder( + ContentProviderOperation.Builder builder) + throws NoSuchMethodException, InstantiationException, IllegalAccessException, + InvocationTargetException { + final Constructor constructor = CLASS_OPERATION.getDeclaredConstructor(CLASS_BUILDER); + constructor.setAccessible(true); + return (ContentProviderOperation) constructor.newInstance(builder); + } + + private void builderSetSelectionArgsBackReferences( + ContentProviderOperation.Builder builder, HashMap<Integer, Integer> selArgsBackRef) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mSelectionArgsBackReferences"); + field.setAccessible(true); + field.set(builder, selArgsBackRef); + } + + private void builderSetValuesBackReferences( + ContentProviderOperation.Builder builder, ContentValues valuesBackReferences) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mValuesBackReferences"); + field.setAccessible(true); + field.set(builder, valuesBackReferences); + } + + private void builderSetSelection( + ContentProviderOperation.Builder builder, String selection) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mSelection"); + field.setAccessible(true); + field.set(builder, selection); + } + + private void builderSetSelectionArgs( + ContentProviderOperation.Builder builder, String[] selArgs) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mSelectionArgs"); + field.setAccessible(true); + field.set(builder, selArgs); + } + + private void builderSetValues( + ContentProviderOperation.Builder builder, ContentValues values) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mValues"); + field.setAccessible(true); + field.set(builder, values); + } + + private void builderSetEntity( + ContentProviderOperation.Builder builder, Entity entity) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mEntity"); + field.setAccessible(true); + field.set(builder, entity); + } + + private void builderSetExpectedCount( + ContentProviderOperation.Builder builder, Integer expectedCount) + throws NoSuchFieldException, IllegalAccessException { + Field field; + field = CLASS_BUILDER.getDeclaredField("mExpectedCount"); + field.setAccessible(true); + field.set(builder, expectedCount); + } + + private int operationGetType(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mType"); + field.setAccessible(true); + return field.getInt(operation); + } + + private Uri operationGetUri(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mUri"); + field.setAccessible(true); + return (Uri) field.get(operation); + } + + private String operationGetSelection(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mSelection"); + field.setAccessible(true); + return (String) field.get(operation); + } + + private String[] operationGetSelectionArgs(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mSelectionArgs"); + field.setAccessible(true); + return (String[]) field.get(operation); + } + + private ContentValues operationGetValues(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mValues"); + field.setAccessible(true); + return (ContentValues) field.get(operation); + } + + private Entity operationGetEntity(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mEntity"); + field.setAccessible(true); + return (Entity) field.get(operation); + } + + private Integer operationGetExpectedCount(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mExpectedCount"); + field.setAccessible(true); + return (Integer) field.get(operation); + } + + private ContentValues operationGetValuesBackReferences(ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mValuesBackReferences"); + field.setAccessible(true); + return (ContentValues) field.get(operation); + } + + private Map<Integer, Integer> operationGetSelectionArgsBackReferences( + ContentProviderOperation operation) + throws NoSuchFieldException, IllegalAccessException { + final Field field = CLASS_OPERATION.getDeclaredField("mSelectionArgsBackReferences"); + field.setAccessible(true); + return (Map<Integer, Integer>) field.get(operation); + } + + public void testParcelingResult() { + Parcel parcel = Parcel.obtain(); + ContentProviderResult result1; + ContentProviderResult result2; + try { + result1 = new ContentProviderResult(Uri.parse("content://goo/bar")); + result1.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + result2 = ContentProviderResult.CREATOR.createFromParcel(parcel); + assertEquals("content://goo/bar", result2.uri.toString()); + assertNull(result2.count); + } finally { + parcel.recycle(); + } + + parcel = Parcel.obtain(); + try { + result1 = new ContentProviderResult(42); + result1.writeToParcel(parcel, 0); + parcel.setDataPosition(0); + result2 = ContentProviderResult.CREATOR.createFromParcel(parcel); + assertEquals(Integer.valueOf(42), result2.count); + assertNull(result2.uri); + } finally { + parcel.recycle(); + } + } + + static class TestContentProvider extends ContentProvider { + public boolean onCreate() { + throw new UnsupportedOperationException(); + } + + public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, + String sortOrder) { + throw new UnsupportedOperationException(); + } + + public String getType(Uri uri) { + throw new UnsupportedOperationException(); + } + + public Uri insert(Uri uri, ContentValues values) { + throw new UnsupportedOperationException(); + } + + public int delete(Uri uri, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + throw new UnsupportedOperationException(); + } + } +}
\ No newline at end of file |
