1 /* 2 * Copyright (C) 2008 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 17 package com.android.basicsmsreceiver; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 import android.app.Activity; 23 import android.app.PendingIntent; 24 import android.content.BroadcastReceiver; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.IntentFilter; 28 import android.database.Cursor; 29 import android.graphics.Color; 30 import android.os.Bundle; 31 import android.provider.Telephony; 32 import android.provider.Telephony.Sms.Intents; 33 import android.telephony.SmsManager; 34 import android.telephony.SmsMessage; 35 import android.telephony.TelephonyManager; 36 import android.test.ActivityInstrumentationTestCase2; 37 import android.test.suitebuilder.annotation.MediumTest; 38 import android.test.suitebuilder.annotation.SmallTest; 39 import android.test.suitebuilder.annotation.LargeTest; 40 import android.util.Log; 41 42 /** 43 * Various instrumentation tests for BasicSmsReceiver. 44 * 45 * To run this test: runtest basicsmsreceiver 46 * 47 * TODO: write tests that verify that notifications get created. As of now, I don't see a way 48 * to query the NotificationManager for what notifications are there. I only see methods to 49 * post and cancel notifications. 50 * 51 */ 52 public class DialogSmsDisplayTests 53 extends ActivityInstrumentationTestCase2<DialogSmsDisplay> { 54 55 private boolean mHasSms; 56 private String mMyNumber; // phone number of this device 57 public static final String ACTION_SMS_SENT = 58 "com.android.basicsmsreceiver.tests.SMS_SENT_ACTION"; 59 private static String TAG = "DialogSmsDisplayTests"; 60 private List<String> mReceivedMessages = new ArrayList<String>(); 61 private String mReceivedSender; 62 private DialogSmsDisplay dialogSmsDisplayActivity; 63 private int mMessageReceivedCount; 64 private BroadcastReceiver mSmsSenderReceiver; 65 private BroadcastReceiver mSmsReceiverReceiver; 66 67 public DialogSmsDisplayTests() { 68 super("com.android.basicsmsreceiver", DialogSmsDisplay.class); 69 Log.i(TAG, "DialogSmsDisplayTests"); 70 } 71 72 @Override 73 protected void setUp() throws Exception { 74 super.setUp(); 75 76 dialogSmsDisplayActivity = (DialogSmsDisplay)getActivity(); 77 78 TelephonyManager telephonyManager = ((TelephonyManager)dialogSmsDisplayActivity 79 .getSystemService(Context.TELEPHONY_SERVICE)); 80 mHasSms = true; //telephonyManager.isSmsCapable(); 81 mMyNumber = telephonyManager.getLine1Number(); 82 83 Log.i(TAG, "hasSms: " + mHasSms + " my number: " + mMyNumber); 84 85 assertTrue("SMS must be enabled on the device", mHasSms); 86 assertNotNull("Device does not have a phone number", mMyNumber); 87 88 if (mHasSms) { 89 // Register broadcast receivers for SMS sent and delivered intents 90 mSmsSenderReceiver = new BroadcastReceiver() { 91 @Override 92 public void onReceive(Context context, Intent intent) { 93 String message = null; 94 boolean error = true; 95 switch (getResultCode()) { 96 case Activity.RESULT_OK: 97 message = "Message sent!"; 98 error = false; 99 break; 100 case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 101 message = "Error."; 102 break; 103 case SmsManager.RESULT_ERROR_NO_SERVICE: 104 message = "Error: No SMS service."; 105 break; 106 case SmsManager.RESULT_ERROR_NULL_PDU: 107 message = "Error: Null PDU."; 108 break; 109 case SmsManager.RESULT_ERROR_RADIO_OFF: 110 message = "Error: Radio off."; 111 break; 112 } 113 assertFalse(message, error); 114 } 115 }; 116 dialogSmsDisplayActivity.registerReceiver(mSmsSenderReceiver, 117 new IntentFilter(ACTION_SMS_SENT)); 118 119 // Register broadcast receivers for received SMS 120 mSmsReceiverReceiver = new BroadcastReceiver() { 121 @Override 122 public void onReceive(Context context, Intent intent) { 123 Bundle extras = intent.getExtras(); 124 Log.i(TAG, "onReceive"); 125 if (extras == null) 126 return; 127 128 Object[] pdus = (Object[]) extras.get("pdus"); 129 130 for (int i = 0; i < pdus.length; i++) { 131 SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]); 132 String sender = message.getOriginatingAddress(); 133 if (mReceivedSender != null) { 134 assertEquals(mReceivedSender, sender); 135 } else { 136 mReceivedSender = sender; 137 } 138 mReceivedMessages.add(message.getMessageBody().toString()); 139 140 Log.i(TAG, "From: " + mReceivedSender + " message: " + 141 mReceivedMessages.get(mReceivedMessages.size() - 1)); 142 } 143 } 144 }; 145 dialogSmsDisplayActivity.registerReceiver(mSmsReceiverReceiver, 146 new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)); 147 } 148 } 149 150 @Override 151 protected void tearDown() throws Exception { 152 if (mSmsSenderReceiver != null) { 153 dialogSmsDisplayActivity.unregisterReceiver(mSmsSenderReceiver); 154 mSmsSenderReceiver = null; 155 } 156 if (mSmsReceiverReceiver != null) { 157 dialogSmsDisplayActivity.unregisterReceiver(mSmsReceiverReceiver); 158 mSmsReceiverReceiver = null; 159 } 160 super.tearDown(); 161 } 162 163 // Returns the broken up list of messages for long messages or the original message in 164 // element 0. 165 private List<String> sendSmsMessageInternal(String messageOut) { 166 if (!mHasSms) { 167 fail("no sms on device"); 168 return null; 169 } 170 SmsManager sms = SmsManager.getDefault(); 171 172 List<String> messages = sms.divideMessage(messageOut); 173 mMessageReceivedCount = 0; 174 mReceivedSender = null; 175 mReceivedMessages.clear(); 176 177 for (String message : messages) { 178 Log.i(TAG, "sendSmsMessage: " + messageOut + " to: " + mMyNumber); 179 sms.sendTextMessage(mMyNumber, null, message, PendingIntent.getBroadcast( 180 dialogSmsDisplayActivity, 0, new Intent(ACTION_SMS_SENT), 0), null); 181 } 182 return messages; 183 } 184 185 // returns true if "messageCount" sms messages are received, false if timeout 186 private boolean waitForSms(int messageCount) { 187 // wait up to two minutes for the sent message to be received 188 long now = System.currentTimeMillis(); 189 boolean success = true; 190 Log.i(TAG, "waitForSms -- waiting for: " + messageCount + " parts"); 191 while (mReceivedMessages.size() < messageCount) { 192 try { 193 Thread.sleep(1000); 194 } catch (Exception e) { 195 } 196 if (System.currentTimeMillis() - now > 1000 * 2 * 60) { 197 // Give up after two minutes 198 success = false; 199 break; 200 } 201 } 202 if (success) { 203 // Wait 5 seconds for the dialog to launch and update 204 try { 205 Thread.sleep(5000); 206 } catch (Exception e) { 207 } 208 } 209 return success; 210 } 211 212 private void sendMessageTest(String message) { 213 List<String> messages = sendSmsMessageInternal(message); 214 Log.i(TAG, "sendMessageTest -- message broken into " + messages.size() + "parts"); 215 216 boolean receivedSms = waitForSms(messages.size()); 217 assertTrue("sms not received after two minutes", receivedSms); 218 219 // Check to see if message/# matches 220 assertEquals(mMyNumber, mReceivedSender); 221 assertEquals(messages.size(), mReceivedMessages.size()); 222 int i = 0; 223 for (String messageFrag : messages) { 224 assertEquals(messageFrag, mReceivedMessages.get(i++)); 225 } 226 } 227 228 public void testSendingSmallSmsMessage() { 229 sendMessageTest("This is a regular size message that might be sent"); 230 } 231 232 public void testSendingLargeSmsMessage() { 233 sendMessageTest("This is a long long message. " + 234 "This is a long long message. " + 235 "This is a long long message. " + 236 "This is a long long message. " + 237 "This is a long long message. " + 238 "This is a long long message. " + 239 "This is a long long message. " + 240 "This is a long long message. " + 241 "This is a long long message. " + 242 "This is a long long message. " + 243 "This is a long long message. " + 244 "This is a long long message. " + 245 "This is a long long message. " + 246 "This is a long long message. " + 247 "This is a long long message. " + 248 "This is a long long message. " + 249 "This is a long long message. " + 250 "This is a long long message. " + 251 "This is a long long message. " + 252 "This is a long long message. " + 253 "This is a long long message. " + 254 "This is a long long message. " + 255 "This is a long long message. " + 256 "This is a long long message. " + 257 "This is a long long message. " + 258 "This is a long long message. " + 259 "This is a long long message. " + 260 "This is a long long message. "); 261 } 262 263 public void testOnNewIntentSmall() { 264 // Test a small message and a non-numeric phone number 265 sendOnNewIntent("this is a big fat test", "xyzzy", 2000); 266 } 267 268 public void testOnNewIntentLarge() { 269 // A long message like this should never really happen because SMS messages are limited 270 // to about 140 characters. 271 sendOnNewIntent("now is the time for all good men to come to the aid of their country1" + 272 "now is the time for all good men to come to the aid of their country2" + 273 "now is the time for all good men to come to the aid of their country3", 274 "513-891-7823", 2001); 275 } 276 277 public void sendOnNewIntent(String message, String dest, int notificationId) { 278 Intent di = new Intent(); 279 di.setClass(dialogSmsDisplayActivity, DialogSmsDisplay.class); 280 di.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | 281 Intent.FLAG_ACTIVITY_CLEAR_TOP); 282 di.putExtra(DialogSmsDisplay.SMS_FROM_ADDRESS_EXTRA, dest); 283 di.putExtra(DialogSmsDisplay.SMS_MESSAGE_EXTRA, message); 284 di.putExtra(DialogSmsDisplay.SMS_NOTIFICATION_ID_EXTRA, notificationId); 285 dialogSmsDisplayActivity.onNewIntent(di); 286 287 // Check to see if message/# matches 288 assertEquals(dest, dialogSmsDisplayActivity.mFromAddress); 289 assertEquals(message, dialogSmsDisplayActivity.mMessage); 290 } 291 } 292