Add up to 6 10 second delays to wait for sms

Change-Id: I59a4542d70e56bf9442e2bb075c6a0a5a0454fb3
diff --git a/tools/emulator/test-apps/SmsTest/src/com/android/emulator/sms/test/SmsTest.java b/tools/emulator/test-apps/SmsTest/src/com/android/emulator/sms/test/SmsTest.java
index 4df5c51..13bcf7f 100644
--- a/tools/emulator/test-apps/SmsTest/src/com/android/emulator/sms/test/SmsTest.java
+++ b/tools/emulator/test-apps/SmsTest/src/com/android/emulator/sms/test/SmsTest.java
@@ -21,9 +21,9 @@
 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 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 @@
      * 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;
+    }
+
 }