Merge "Add up to 6 10 second delays to wait for sms"

This commit is contained in:
Steve Moyer
2014-02-19 01:12:54 +00:00
committed by Android (Google) Code Review

View File

@@ -21,9 +21,9 @@ import android.net.Uri;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.support.test.InjectContext; import android.support.test.InjectContext;
import org.junit.Assert;
import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertEquals;
import org.junit.Test; import org.junit.Test;
@@ -40,7 +40,8 @@ public class SmsTest {
*/ */
public final static String NUMBER = "5551212"; public final static String NUMBER = "5551212";
public final static String BODY = "test sms"; public final static String BODY = "test sms";
private final static int SMS_POLL_TIME_MS = 10 * 1000;
private final static int SIXY_SECONDS_OF_LOOPS = 6;
@InjectContext @InjectContext
public Context mContext; public Context mContext;
@@ -48,16 +49,35 @@ public class SmsTest {
* Verify that an SMS has been received with the correct number and body * Verify that an SMS has been received with the correct number and body
*/ */
@Test @Test
public void testReceivedSms(){ public void testReceivedSms() throws java.lang.InterruptedException {
ContentResolver r = mContext.getContentResolver(); Cursor c = getSmsCursor();
Uri message = Uri.parse("content://sms/");
Cursor c = r.query(message,null,null,null,null);
c.moveToFirst(); c.moveToFirst();
String number = c.getString(c.getColumnIndexOrThrow("address")); String number = c.getString(c.getColumnIndexOrThrow("address"));
String body = c.getString(c.getColumnIndexOrThrow("body")); String body = c.getString(c.getColumnIndexOrThrow("body"));
c.close(); c.close();
assertEquals(NUMBER, number); assertEquals(NUMBER, number);
assertEquals(BODY, body); assertEquals(BODY, body);
} }
private Cursor getSmsCursor() throws java.lang.InterruptedException {
ContentResolver r = mContext.getContentResolver();
Uri message = Uri.parse("content://sms/");
Cursor c;
for(int i = 0; i < SIXY_SECONDS_OF_LOOPS; i++) {
c = r.query(message,null,null,null,null);
if(c.getCount() != 0) {
return c;
}
c.close();
Thread.sleep(SMS_POLL_TIME_MS);
}
Assert.fail("Did not find any SMS messages. Giving up");
// necessary for compilation
return null;
}
} }