google代码搜索 Search public source code 搜索 ProviderTestCase2 使用示例
http://www.google.com/codesearch
Search public source code
比如搜索 ProviderTestCase2
可以找到一些普通网站所没有的关于ProviderTestCase2的具体实际使用的代码 供参考
View Code
package com.pose.test;
import com.pose.models.Item;
import com.pose.providers.*;
import com.pose.service.GSServiceHelper;
import android.net.Uri;
import android.os.Handler;
import android.test.ProviderTestCase2;
import android.test.mock.MockContentResolver;
import android.util.Log;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
public class ItemContentProviderTests extends ProviderTestCase2<ItemsContentProvider>
{
private MockContentResolver mMockResolver;
private Class<ItemsContentProvider> mProviderClass;
private ItemsContentProvider mProvider;
private String mProviderAuthority;
public ItemContentProviderTests()
{
super(ItemsContentProvider.class, ItemsContentProvider.AUTHORITY);
setName("ItemsProviderTests");
mProviderClass = ItemsContentProvider.class;
mProviderAuthority = ItemsContentProvider.AUTHORITY;
}
@Override
protected void setUp() throws Exception
{
// We should not invoke super.setUp() here to avoid the UnsupportedOperationException.
super.setUp();
mMockResolver = getMockContentResolver();
mProvider = mProviderClass.newInstance();
assertNotNull(mProvider);
mProvider.attachInfo(getContext(), null);
mMockResolver.addProvider(mProviderAuthority, mProvider);
}
@Override
public ItemsContentProvider getProvider()
{
return mProvider;
}
protected void tearDown() throws Exception
{
super.tearDown();
mMockResolver.delete(Item.CONTENT_URI,"id = id",null);
}
public void testProviderSampleCreation()
{
ItemsContentProvider provider = getProvider();
assertNotNull(provider);
}
public void testItemCreateRead()
{
ContentValues values = new ContentValues();
values.put(Item.MESSAGE, "Test Pose Zebra Zulu");
Uri uri = mMockResolver.insert(Item.CONTENT_URI, values);
//Uri myItem = ContentUris.withAppendedId(Item.CONTENT_URI, 1); //Not really using URI syntax for now
// Then query for this specific record:
Cursor cur = mMockResolver.query(Item.CONTENT_URI, null, Item.MESSAGE + " = 'Test Pose Zebra Zulu'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
}
public void testItemDoesNotDuplicate()
{
// insert the first record/tuple into the db
ContentValues values = new ContentValues();
values.put(Item.MESSAGE, "Test Pose Zebra Zulu");
values.put(Item.ITEMID, "1");
Uri uri = mMockResolver.insert(Item.CONTENT_URI, values);
Cursor cur = mMockResolver.query(Item.CONTENT_URI, null, Item.MESSAGE + " = 'Test Pose Zebra Zulu'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
// Now we make sure that items can not save duplicates of the same id
values = new ContentValues();
values.put(Item.MESSAGE, "NOT SAVED");
values.put(Item.ITEMID, "1");
uri = mMockResolver.insert(Item.CONTENT_URI, values);
cur = mMockResolver.query(Item.CONTENT_URI, null, Item.MESSAGE + " = 'NOT SAVED'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
// Verify that we only have 1 item with an item id of '1'
cur = mMockResolver.query(Item.CONTENT_URI, null, Item.ITEMID + " = '1'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
}
public void testLoadUnAuthFeed()
{
GSServiceHelper gsHelper1 = GSServiceHelper.getGSServiceObject(getContext().getApplicationContext());
GSServiceHelper gsHelper2 = GSServiceHelper.getGSServiceObject(getContext().getApplicationContext());
assertNotNull(gsHelper1);
assertNotNull(gsHelper2);
assertEquals(gsHelper1, gsHelper2);
//Bind with and start GSService
gsHelper1.doBindService();
String requestID = gsHelper1.loadPosers();
assertTrue(requestID.length() > 0);
}
public void testUnAuthDataLoaded()
{
Handler handler = new Handler();
class ItemsContentObserver extends ContentObserver
{
public ItemsContentObserver( Handler h )
{
super( h );
}
public void onChange(boolean selfChange)
{
Log.i( "testUnAuthDataLoaded()", "StringsContentObserver.onChange( "+selfChange+")" );
}
}
ItemsContentObserver co = new ItemsContentObserver(handler);
mMockResolver.registerContentObserver(Item.CONTENT_URI, true, co);
}
}
import com.pose.models.Item;
import com.pose.providers.*;
import com.pose.service.GSServiceHelper;
import android.net.Uri;
import android.os.Handler;
import android.test.ProviderTestCase2;
import android.test.mock.MockContentResolver;
import android.util.Log;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
public class ItemContentProviderTests extends ProviderTestCase2<ItemsContentProvider>
{
private MockContentResolver mMockResolver;
private Class<ItemsContentProvider> mProviderClass;
private ItemsContentProvider mProvider;
private String mProviderAuthority;
public ItemContentProviderTests()
{
super(ItemsContentProvider.class, ItemsContentProvider.AUTHORITY);
setName("ItemsProviderTests");
mProviderClass = ItemsContentProvider.class;
mProviderAuthority = ItemsContentProvider.AUTHORITY;
}
@Override
protected void setUp() throws Exception
{
// We should not invoke super.setUp() here to avoid the UnsupportedOperationException.
super.setUp();
mMockResolver = getMockContentResolver();
mProvider = mProviderClass.newInstance();
assertNotNull(mProvider);
mProvider.attachInfo(getContext(), null);
mMockResolver.addProvider(mProviderAuthority, mProvider);
}
@Override
public ItemsContentProvider getProvider()
{
return mProvider;
}
protected void tearDown() throws Exception
{
super.tearDown();
mMockResolver.delete(Item.CONTENT_URI,"id = id",null);
}
public void testProviderSampleCreation()
{
ItemsContentProvider provider = getProvider();
assertNotNull(provider);
}
public void testItemCreateRead()
{
ContentValues values = new ContentValues();
values.put(Item.MESSAGE, "Test Pose Zebra Zulu");
Uri uri = mMockResolver.insert(Item.CONTENT_URI, values);
//Uri myItem = ContentUris.withAppendedId(Item.CONTENT_URI, 1); //Not really using URI syntax for now
// Then query for this specific record:
Cursor cur = mMockResolver.query(Item.CONTENT_URI, null, Item.MESSAGE + " = 'Test Pose Zebra Zulu'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
}
public void testItemDoesNotDuplicate()
{
// insert the first record/tuple into the db
ContentValues values = new ContentValues();
values.put(Item.MESSAGE, "Test Pose Zebra Zulu");
values.put(Item.ITEMID, "1");
Uri uri = mMockResolver.insert(Item.CONTENT_URI, values);
Cursor cur = mMockResolver.query(Item.CONTENT_URI, null, Item.MESSAGE + " = 'Test Pose Zebra Zulu'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
// Now we make sure that items can not save duplicates of the same id
values = new ContentValues();
values.put(Item.MESSAGE, "NOT SAVED");
values.put(Item.ITEMID, "1");
uri = mMockResolver.insert(Item.CONTENT_URI, values);
cur = mMockResolver.query(Item.CONTENT_URI, null, Item.MESSAGE + " = 'NOT SAVED'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
// Verify that we only have 1 item with an item id of '1'
cur = mMockResolver.query(Item.CONTENT_URI, null, Item.ITEMID + " = '1'", null, Item.ITEMID + " ASC");
assertNotNull(cur);
assertEquals(1,cur.getCount());
}
public void testLoadUnAuthFeed()
{
GSServiceHelper gsHelper1 = GSServiceHelper.getGSServiceObject(getContext().getApplicationContext());
GSServiceHelper gsHelper2 = GSServiceHelper.getGSServiceObject(getContext().getApplicationContext());
assertNotNull(gsHelper1);
assertNotNull(gsHelper2);
assertEquals(gsHelper1, gsHelper2);
//Bind with and start GSService
gsHelper1.doBindService();
String requestID = gsHelper1.loadPosers();
assertTrue(requestID.length() > 0);
}
public void testUnAuthDataLoaded()
{
Handler handler = new Handler();
class ItemsContentObserver extends ContentObserver
{
public ItemsContentObserver( Handler h )
{
super( h );
}
public void onChange(boolean selfChange)
{
Log.i( "testUnAuthDataLoaded()", "StringsContentObserver.onChange( "+selfChange+")" );
}
}
ItemsContentObserver co = new ItemsContentObserver(handler);
mMockResolver.registerContentObserver(Item.CONTENT_URI, true, co);
}
}
posted on 2011-08-06 09:37 freeliver54 阅读(587) 评论(1) 编辑 收藏 举报