Home | History | Annotate | Download | only in sms
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.emulator.smoketests.sms;
     17 
     18 import android.content.Context;
     19 import android.content.ContentResolver;
     20 import android.net.Uri;
     21 import android.database.Cursor;
     22 import android.os.Bundle;
     23 import android.os.HandlerThread;
     24 import android.support.test.InstrumentationRegistry;
     25 
     26 import org.junit.Assert;
     27 import static junit.framework.Assert.assertEquals;
     28 
     29 import org.junit.Test;
     30 /**
     31  * Sms Test
     32  *
     33  * Test that an SMS message has been received
     34  */
     35 public class SmsTest {
     36 
     37     /**
     38      * Prior to running this test an sms must be sent
     39      * via DDMS
     40      */
     41     public final static String NUMBER = "5551212";
     42     public final static String BODY = "test sms";
     43     private final static int SMS_POLL_TIME_MS = 10 * 1000;
     44     private final static int SIXY_SECONDS_OF_LOOPS = 6;
     45 
     46     /**
     47      * Verify that an SMS has been received with the correct number and body
     48      */
     49     @Test
     50     public void testReceivedSms() throws java.lang.InterruptedException {
     51         Cursor c = getSmsCursor();
     52         c.moveToFirst();
     53 
     54         String number = c.getString(c.getColumnIndexOrThrow("address"));
     55         String body = c.getString(c.getColumnIndexOrThrow("body"));
     56 
     57         c.close();
     58 
     59         assertEquals(NUMBER, number);
     60         assertEquals(BODY, body);
     61     }
     62 
     63     private Cursor getSmsCursor() throws java.lang.InterruptedException {
     64         ContentResolver r = InstrumentationRegistry.getTargetContext().getContentResolver();
     65         Uri message = Uri.parse("content://sms/");
     66         Cursor c;
     67 
     68         for(int i = 0; i < SIXY_SECONDS_OF_LOOPS; i++) {
     69             c = r.query(message,null,null,null,null);
     70 
     71             if(c.getCount() != 0) {
     72                 return c;
     73             }
     74 
     75             c.close();
     76             Thread.sleep(SMS_POLL_TIME_MS);
     77         }
     78         Assert.fail("Did not find any SMS messages. Giving up");
     79         // necessary for compilation
     80         return null;
     81     }
     82 
     83 }
     84