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.InjectContext; 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 @InjectContext 46 public Context mContext; 47 48 /** 49 * Verify that an SMS has been received with the correct number and body 50 */ 51 @Test testReceivedSms()52 public void testReceivedSms() throws java.lang.InterruptedException { 53 Cursor c = getSmsCursor(); 54 c.moveToFirst(); 55 56 String number = c.getString(c.getColumnIndexOrThrow("address")); 57 String body = c.getString(c.getColumnIndexOrThrow("body")); 58 59 c.close(); 60 61 assertEquals(NUMBER, number); 62 assertEquals(BODY, body); 63 } 64 getSmsCursor()65 private Cursor getSmsCursor() throws java.lang.InterruptedException { 66 ContentResolver r = mContext.getContentResolver(); 67 Uri message = Uri.parse("content://sms/"); 68 Cursor c; 69 70 for(int i = 0; i < SIXY_SECONDS_OF_LOOPS; i++) { 71 c = r.query(message,null,null,null,null); 72 73 if(c.getCount() != 0) { 74 return c; 75 } 76 77 c.close(); 78 Thread.sleep(SMS_POLL_TIME_MS); 79 } 80 Assert.fail("Did not find any SMS messages. Giving up"); 81 // necessary for compilation 82 return null; 83 } 84 85 } 86