????3.????????ContentProvider???????????
????ProviderTestCase2<T extends ContentProvider>?Android?????????????????ContentProvider???????T??????????Target????Target?????ContentProvider????(???ContentProvider)??
????TestProvider.java????е????????ProviderToBeTest????????????????£?
package com.steven.hu.cpt.test;
import com.steven.hu.cpt.Family;
import com.steven.hu.cpt.ProviderToBeTest;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.test.ProviderTestCase2;
import android.test.mock.MockContentResolver;
import android.util.Log;
import android.webkit.WebChromeClient.CustomViewCallback;
public class TestProvider extends ProviderTestCase2<ProviderToBeTest>
{
// ??????????????ContentResolver????????ContentResolver
private MockContentResolver mMockResolver;
private SQLiteDatabase mDatabase;
private final MemberInfo[] TEST_MEMBERS =
{
new MemberInfo("name0"?? 0?? "gender0")??
new MemberInfo("name1"?? 1?? "gender1")??
new MemberInfo("name2"?? 2?? "gender2")??
new MemberInfo("name3"?? 3?? "gender3")??
new MemberInfo("name4"?? 4?? "gender4")??
new MemberInfo("name5"?? 5?? "gender5")??
new MemberInfo("name6"?? 6?? "gender6")??
new MemberInfo("name7"?? 7?? "gender7")??
new MemberInfo("name8"?? 8?? "gender8")??
new MemberInfo("name9"?? 9?? "gender9")
};
public TestProvider()
{
super(ProviderToBeTest.class?? Family.AUTHRITY);
}
@Override
protected void setUp() throws Exception
{
// TODO Auto-generated method stub
super.setUp();
mMockResolver = getMockContentResolver();
mDatabase = getProvider().getOpenHelperForTest().getWritableDatabase();
}
public void testUriAndGetType()
{
String mimeType = mMockResolver.getType(Family.Member.FAMILY_CONTENT_URI);
assertEquals("famliy/com.steven.hu.genealogy"?? mimeType);
// mimeType???????MEMBER_CONTENT_URI???????member/#
/*
* mimeType = mMockResolver.getType(Family.Member.MEMBER_CONTENT_URI);
* assertEquals("member/com.steven.hu.genealogy"?? mimeType);
*/
// ????????MEMBER_CONTENT_URI?????1???member/1????member/#??#?????????
Uri noteIdUri = ContentUris.withAppendedId(Family.Member.MEMBER_CONTENT_URI?? 1);
mimeType = mMockResolver.getType(noteIdUri);
assertEquals("member/com.steven.hu.genealogy"?? mimeType);
}
private class MemberInfo
{
String name;
int age;
String gender;
public MemberInfo(String n?? int a?? String g)
{
name = n;
age = a;
gender = g;
}
public ContentValues getContentValues()
{
ContentValues values = new ContentValues();
values.put(Family.Member.NAME?? name);
values.put(Family.Member.AGE?? age);
values.put(Family.Member.GENDER?? gender);
return values;
}
}
// ???????????????????????????????????????????????????????
public void testInsert()
{
MemberInfo memberInfo = new MemberInfo("steven"?? 100?? "man");
// ??????????????????uri
Uri rowUri = mMockResolver.insert(
Family.Member.FAMILY_CONTENT_URI??
memberInfo.getContentValues());
// ???????????????
Cursor cursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
null??
null??
null??
null);
// ????????????????????
assertEquals(1?? cursor.getCount());
// ??????е????????????????????cursor??????cursor.moveToFirst()????fase
assertTrue(cursor.moveToFirst());
// ???????ε?index
int nameIndex = cursor.getColumnIndex(Family.Member.NAME);
int ageIndex = cursor.getColumnIndex(Family.Member.AGE);
int genderIndex = cursor.getColumnIndex(Family.Member.GENDER);
// ???????????е???????????????
assertEquals(memberInfo.name?? cursor.getString(nameIndex));
assertEquals(memberInfo.age?? cursor.getInt(ageIndex));
assertEquals(memberInfo.gender?? cursor.getString(genderIndex));
}
// ????????????-->????????в?????????????в???
private void insertData()
{
for (int i = 0; i < TEST_MEMBERS.length; i++)
{
mDatabase.insertOrThrow(Family.Member.TABLE_NAME?? null?? TEST_MEMBERS[i]
.getContentValues());
}
}
// ?????????????
public void testQueryRecords()
{
Cursor cursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
null??
null??
null??
null);
// ??????????е???????????
assertEquals(0?? cursor.getCount());
// ????????????
insertData();
// ??β??
cursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
null??
null??
null??
null);
assertEquals(TEST_MEMBERS.length?? cursor.getCount());
/**
* ???????SQL?????????????????
*/
// ??????name????????SQL???
final String NAME_SELECTION = Family.Member.NAME + " = " + "?";
//???????-->???name??????????name0??name2??name7??????????
final String SELECTION_COLUMNS_NAME = NAME_SELECTION + " OR " + NAME_SELECTION + " OR "
+ NAME_SELECTION;
final String[] SELECTION_ARGS =
{
"name0"?? "name2"?? "name7"
};
// ????????????????а???name??age??gender????ζ?????(???????)
final String[] TEST_PROJECTION =
{
Family.Member.NAME?? Family.Member.AGE?? Family.Member.GENDER
};
// ?????????????(ASC?????DESC?????)
final String SORT_ORDER = Family.Member.NAME + " ASC";
// ????????
Cursor projectionCursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
TEST_PROJECTION??
null??
null??
null);
// ??????????????????
assertEquals(TEST_PROJECTION.length?? projectionCursor.getColumnCount());
// ?????????????????
assertEquals(TEST_PROJECTION[0]?? projectionCursor.getColumnName(0));
assertEquals(TEST_PROJECTION[1]?? projectionCursor.getColumnName(1));
assertEquals(TEST_PROJECTION[2]?? projectionCursor.getColumnName(2));
//????SQL?????????????
projectionCursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
TEST_PROJECTION??  //get the name?? age?? and gender columns
SELECTION_COLUMNS_NAME?? //select on the name column
SELECTION_ARGS?? // select names "name0"?? "name2"?? or "name7"
SORT_ORDER // sort ascending on the name column
);
//???????????????name?????????????
assertEquals(SELECTION_ARGS.length?? projectionCursor.getCount());
int index = 0;
while (projectionCursor.moveToNext())
{
//??????????name?????????
assertEquals(SELECTION_ARGS[index]?? projectionCursor.getString(0));
index++;
}
assertEquals(SELECTION_ARGS.length?? index);
}
// ?????????????
public void testQueryRecord()
{
//?????gender?????gender2?????????
final String SELECTION_COLUMNS_GENDER = Family.Member.GENDER + " = " + "?";
final String[] SELECTION_ARGS = { "gender2"};
//???????????
final String SORT_ORDER = Family.Member.GENDER + " ASC";
//???????????????????_id??gender??ζ?????
final String[] TEST_PROJECTION = {Family.Member._ID?? Family.Member.GENDER};
//??MEMBER_CONTENT_URI???id?1
Uri memberIdUri = ContentUris.withAppendedId(Family.Member.MEMBER_CONTENT_URI?? 1);
Cursor cursor = mMockResolver.query(
memberIdUri??
null??
null??
null??
null
);
assertEquals(0??cursor.getCount());
//????????????
insertData();
//?????????????????uri???
cursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
TEST_PROJECTION??
SELECTION_COLUMNS_GENDER??
SELECTION_ARGS??
SORT_ORDER
);
assertEquals(1?? cursor.getCount());
//????????У????????????
assertTrue(cursor.moveToFirst());
//?????????????????id
int inputMemberId = cursor.getInt(0);
//??MEMBER_CONTENT_URI???id????????????????id
memberIdUri = ContentUris.withAppendedId(Family.Member.MEMBER_CONTENT_URI?? inputMemberId);
//??????????????uri???
cursor = mMockResolver.query(
memberIdUri??
TEST_PROJECTION??
SELECTION_COLUMNS_GENDER??
SELECTION_ARGS??
SORT_ORDER
);
assertEquals(1?? cursor.getCount());
assertTrue(cursor.moveToFirst());
//????????uri?е?id??uri??????????????????id??????
assertEquals(inputMemberId?? cursor.getInt(0));
//????????ζ??????????
assertEquals("gender2"?? cursor.getString(1));
/*for (int i = 0; i < cursor.getCount(); i ++)
{
Log.d("TestProvider"?? "Id-->" + cursor.getInt(0));
Log.d("TestProvider"?? "name-->" + cursor.getString(1));
Log.d("TestProvider"?? "age-->" + cursor.getInt(2));
Log.d("TestProvider"?? "gender-->" + cursor.getString(3));
cursor.moveToNext();
}   */
}
//???????
public void testDeletes()
{
final String SELECTION_COLUMNS_AGE = Family.Member.AGE + " = " + "?";
final String[] SELECTION_ARGS = { "0" };
int rowsDeleted = mMockResolver.delete(
Family.Member.FAMILY_CONTENT_URI??
SELECTION_COLUMNS_AGE??
SELECTION_ARGS
);
//???????????????δ????в????????????????????0
assertEquals(0?? rowsDeleted);
//????????
insertData();
//??????
rowsDeleted = mMockResolver.delete(
Family.Member.FAMILY_CONTENT_URI??
SELECTION_COLUMNS_AGE??
SELECTION_ARGS
);
//?????????????????
assertEquals(1?? rowsDeleted);
//???????????????????
Cursor cursor = mMockResolver.query(
Family.Member.FAMILY_CONTENT_URI??
null??
SELECTION_COLUMNS_AGE??
SELECTION_ARGS??
null
);
//?????????????????
assertEquals(0?? cursor.getCount());
}
// ??????????????????????е?????????????????????insertData??????????????????
@Override
protected void tearDown() throws Exception
{
// TODO Auto-generated method stub
super.tearDown();
insertData();
}
}
???????????????????????TestProvider.java--->??????--->Run As--->Android Junit Test???????????