How can I test ContentProvider query using Robolectric

1,793 views
Skip to first unread message

DIPANJAN DAS

unread,
Feb 16, 2017, 12:43:38 PM2/16/17
to Robolectric
Hi Team,

I am developing one application which can read all inbox SMS from built in SMS database using ContentProvider URI content://sms/inbox
My snippet of code is below.

public Cursor retrieveSmsFromDevice(byte directoryType, ContentResolver resolver) {
Cursor cursor = null;
try {
if (directoryType == 0 || resolver == null) {
throw new RetrieveSmsException();
}
switch (directoryType) {
case SmsDPConstant.INBOX:
Uri inbox = Uri.parse(SmsDPConstant.INBOX_URI);
cursor = resolver.query(inbox, SMS_PROJECTION, null, null, SORT_ORDER);
return cursor;
case SmsDPConstant.SENT:
Uri send = Uri.parse(SmsDPConstant.SENT_URI);
cursor = resolver.query(send, SMS_PROJECTION, null, null, SORT_ORDER);
return cursor;
case SmsDPConstant.DRAFT:
Uri draft = Uri.parse(SmsDPConstant.DRAFT_URI);
cursor = resolver.query(draft, SMS_PROJECTION, null, null, SORT_ORDER);
return cursor;
}
} catch (RetrieveSmsException e) {
e.printStackTrace();
} finally {
return cursor;
}
}

When I am calling this function from robolectric test function I am getting Cursor object as NULL.
I am passing ContentResolver object. I am creating ContentResolver using robolectric.
Code Snippet: 
ContentResolver  mResolver =  RuntimeEnvironment.application.getContentResolver();

Please help me. How do I need to create shadow ContentProvider.

DIPANJAN DAS

unread,
Feb 16, 2017, 12:44:20 PM2/16/17
to Robolectric
I am using robolectric 3.2.2

Maks Verver

unread,
Feb 16, 2017, 12:54:21 PM2/16/17
to robol...@googlegroups.com
You probably need to create a fake/real instance of the SMS provider and register it using ShadowContentResolver.registerProvider().

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

DIPANJAN DAS

unread,
Feb 16, 2017, 2:45:39 PM2/16/17
to Robolectric
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class SmsDataPoolerTest {
private Context mContext;
private ContentResolver mResolver;
@Before
public void setUp() throws Exception {
initMocks(this);
mResolver = RuntimeEnvironment.application.getContentResolver();
}

@After
public void tearDown() throws Exception {
mContext = null;
}

@Test
public void getInstance() throws Exception{
SmsDataPooler dataPooler = SmsDataPooler.getInstance();
assertNotNull(dataPooler);
}

@Test
public void retrieveSmsFromDevice() throws Exception {
ContentValues values = new ContentValues();
values.put("address", "123456789");
values.put("body", "foo bar");
Uri uri = mResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values);
System.out.println(uri.toString());
SmsDataPooler dataPooler = SmsDataPooler.getInstance();
Cursor cursor = dataPooler.retrieveSmsFromDevice(SmsDPConstant.INBOX, mResolver);
assertNotNull(cursor);
Cursor cursor1 = dataPooler.retrieveSmsFromDevice(SmsDPConstant.SENT, mResolver);
assertNotNull(cursor1);
Cursor cursor2 = dataPooler.retrieveSmsFromDevice(SmsDPConstant.DRAFT, mResolver);
assertNotNull(cursor2);
}

@Test
public void retrieveSmsFromDeviceNegetive() throws Exception {
SmsDataPooler dataPooler = SmsDataPooler.getInstance();
ContentResolver resolver = null;
Cursor cursor = dataPooler.retrieveSmsFromDevice((byte)0, resolver);
assertNull(cursor);
}
}

This is my test class. I am getting null value for Cursor object when I am calling  retrieveSmsFromDevice(byte directoryType, ContentResolver resolver) function.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.

DIPANJAN DAS

unread,
Feb 16, 2017, 2:48:44 PM2/16/17
to Robolectric
public Cursor retrieveSmsFromDevice(byte directoryType, ContentResolver resolver) {
Cursor cursor = null;
try {
if (directoryType == 0 || resolver == null) {
throw new RetrieveSmsException();
}
switch (directoryType) {
case SmsDPConstant.INBOX:
                        cursor = resolver.query(Telephony.Sms.Inbox.CONTENT_URI,

SMS_PROJECTION, null, null, SORT_ORDER);
return cursor;
case SmsDPConstant.SENT:
                cursor = resolver.query(Telephony.Sms.Sent.CONTENT_URI,

SMS_PROJECTION, null, null, SORT_ORDER);
return cursor;
case SmsDPConstant.DRAFT:
                cursor = resolver.query(Telephony.Sms.Draft.CONTENT_URI,

SMS_PROJECTION, null, null, SORT_ORDER);
return cursor;
}
} catch (RetrieveSmsException e) {
e.printStackTrace();
} finally {
return cursor;
}
}
On Thursday, February 16, 2017 at 11:13:38 PM UTC+5:30, DIPANJAN DAS wrote:

Jonathan Gerrish

unread,
Feb 16, 2017, 5:33:14 PM2/16/17
to robol...@googlegroups.com
Maks is correct you need to register a content provider to provide this functionality.

The preferred method however is Robolectric.buildContentProvider(ContentProvider.class);

Robolectric only provides the framework classes so I suspect the real Sms Content Provider is probably not available. If I were you I would provide a mock or fake content provider to serve up fake SMS messages for your test.

--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages