Add up to 6 10 second delays to wait for sms

Change-Id: I59a4542d70e56bf9442e2bb075c6a0a5a0454fb3
This commit is contained in:
Steve Moyer
2014-02-12 14:53:18 -08:00
parent a7524da862
commit 128ba33143

View File

@@ -21,9 +21,9 @@ import android.net.Uri;
import android.database.Cursor;
import android.os.Bundle;
import android.os.HandlerThread;
import android.support.test.InjectContext;
import org.junit.Assert;
import static junit.framework.Assert.assertEquals;
import org.junit.Test;
@@ -40,7 +40,8 @@ public class SmsTest {
*/
public final static String NUMBER = "5551212";
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
public Context mContext;
@@ -48,16 +49,35 @@ public class SmsTest {
* Verify that an SMS has been received with the correct number and body
*/
@Test
public void testReceivedSms(){
ContentResolver r = mContext.getContentResolver();
Uri message = Uri.parse("content://sms/");
Cursor c = r.query(message,null,null,null,null);
public void testReceivedSms() throws java.lang.InterruptedException {
Cursor c = getSmsCursor();
c.moveToFirst();
String number = c.getString(c.getColumnIndexOrThrow("address"));
String body = c.getString(c.getColumnIndexOrThrow("body"));
c.close();
assertEquals(NUMBER, number);
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;
}
}