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.cellbroadcastreceiver; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.provider.Telephony.Sms.Intents; 22 import android.telephony.SmsCbLocation; 23 import android.telephony.SmsCbMessage; 24 import android.test.ActivityInstrumentationTestCase2; 25 import android.util.Log; 26 27 import com.android.internal.telephony.EncodeException; 28 import com.android.internal.telephony.GsmAlphabet; 29 import com.android.internal.telephony.gsm.GsmSmsCbMessage; 30 import com.android.internal.telephony.gsm.SmsCbHeader; 31 import com.android.internal.telephony.uicc.IccUtils; 32 33 import java.io.UnsupportedEncodingException; 34 35 /** 36 * Various instrumentation tests for CellBroadcastReceiver. 37 * 38 * To run this test: runtest cellbroadcastreceiver 39 * or: adb shell am instrument -w \ 40 * com.android.cellbroadcastreceiver.tests/android.test.InstrumentationTestRunner 41 * 42 * TODO: write better test cases 43 * 44 */ 45 public class DialogSmsDisplayTests 46 extends ActivityInstrumentationTestCase2<CellBroadcastListActivity> { 47 48 private static final String CB_RECEIVER_PKG = "com.android.cellbroadcastreceiver"; 49 50 public static final String ACTION_SMS_SENT = 51 "com.android.basicsmsreceiver.tests.SMS_SENT_ACTION"; 52 private static String TAG = "DialogSmsDisplayTests"; 53 54 private static final int DCS_7BIT_ENGLISH = 0x01; 55 private static final int DCS_16BIT_UCS2 = 0x48; 56 57 /* ETWS Test message including header */ 58 private static final byte[] etwsMessageNormal = IccUtils.hexStringToBytes("000011001101" + 59 // Line 1 CRLFLine 2 60 "EA307DCA602557309707901F58310D0A5BAE57CE770C531790E85C716CBF3044573065B930675730" + 61 "9707767A751F30025F37304463FA308C306B5099304830664E0B30553044FF086C178C615E81FF09"); 62 63 private static final byte[] etwsMessageCancel = IccUtils.hexStringToBytes("000011001101" + 64 // Line 1 CRLFLine 2 65 "EA307DCA602557309707901F5831002853D66D8800290D0A5148307B3069002800310030003A0035" + 66 "00320029306E7DCA602557309707901F5831309253D66D883057307E3059FF086C178C615E81FF09"); 67 68 private static final byte[] etwsMessageTest = IccUtils.hexStringToBytes("000011031101" + 69 // Line 1 CRLFLine 2 70 "EA3030108A137DF430117DCA602557309707573058310D0A5BAE57CE770C531790E85C716CBF3044" + 71 "573065B9306757309707300263FA308C306B5099304830664E0B30553044FF086C178C615E81FF09"); 72 73 private Context mContext; 74 DialogSmsDisplayTests()75 public DialogSmsDisplayTests() { 76 super(CellBroadcastListActivity.class); 77 Log.i(TAG, "DialogSmsDisplayTests"); 78 } 79 80 @Override setUp()81 protected void setUp() throws Exception { 82 super.setUp(); 83 mContext = getActivity(); 84 } 85 86 @Override tearDown()87 protected void tearDown() throws Exception { 88 super.tearDown(); 89 } 90 encodeCellBroadcast(int serialNumber, int messageId, int dcs, String message)91 byte[] encodeCellBroadcast(int serialNumber, int messageId, int dcs, String message) { 92 byte[] pdu = new byte[88]; 93 pdu[0] = (byte) ((serialNumber >> 8) & 0xff); 94 pdu[1] = (byte) (serialNumber & 0xff); 95 pdu[2] = (byte) ((messageId >> 8) & 0xff); 96 pdu[3] = (byte) (messageId & 0xff); 97 pdu[4] = (byte) (dcs & 0xff); 98 pdu[5] = 0x11; // single page message 99 try { 100 byte[] encodedString; 101 if (dcs == DCS_16BIT_UCS2) { 102 encodedString = message.getBytes("UTF-16"); 103 System.arraycopy(encodedString, 0, pdu, 6, encodedString.length); 104 } else { 105 // byte 0 of encodedString is the length in septets (don't copy) 106 encodedString = GsmAlphabet.stringToGsm7BitPacked(message); 107 System.arraycopy(encodedString, 1, pdu, 6, encodedString.length-1); 108 } 109 return pdu; 110 } catch (EncodeException e) { 111 Log.e(TAG, "Encode Exception"); 112 return null; 113 } catch (UnsupportedEncodingException e) { 114 Log.e(TAG, "Unsupported encoding exception for UTF-16"); 115 return null; 116 } 117 } 118 119 private static final SmsCbLocation sEmptyLocation = new SmsCbLocation(); 120 createFromPdu(byte[] pdu)121 private SmsCbMessage createFromPdu(byte[] pdu) { 122 try { 123 byte[][] pdus = new byte[1][]; 124 pdus[0] = pdu; 125 return GsmSmsCbMessage.createSmsCbMessage(mContext, new SmsCbHeader(pdus[0]), 126 sEmptyLocation, pdus); 127 } catch (IllegalArgumentException e) { 128 return null; 129 } 130 } 131 testSendMessage7bit()132 public void testSendMessage7bit() throws Exception { 133 Intent intent = new Intent(Intents.SMS_CB_RECEIVED_ACTION); 134 byte[] pdu = encodeCellBroadcast(0, 0, DCS_7BIT_ENGLISH, "Hello in GSM 7 bit"); 135 intent.putExtra("message", createFromPdu(pdu)); 136 intent.setPackage(CB_RECEIVER_PKG); 137 mContext.sendOrderedBroadcast(intent, "android.permission.RECEIVE_SMS"); 138 } 139 testSendMessageUCS2()140 public void testSendMessageUCS2() throws Exception { 141 Intent intent = new Intent(Intents.SMS_CB_RECEIVED_ACTION); 142 byte[] pdu = encodeCellBroadcast(0, 0, DCS_16BIT_UCS2, "Hello in UCS2"); 143 intent.putExtra("message", createFromPdu(pdu)); 144 intent.setPackage(CB_RECEIVER_PKG); 145 mContext.sendOrderedBroadcast(intent, "android.permission.RECEIVE_SMS"); 146 } 147 testSendEtwsMessageNormal()148 public void testSendEtwsMessageNormal() throws Exception { 149 Intent intent = new Intent(Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION); 150 intent.putExtra("message", createFromPdu(etwsMessageNormal)); 151 intent.setPackage(CB_RECEIVER_PKG); 152 mContext.sendOrderedBroadcast(intent, 153 "android.permission.RECEIVE_EMERGENCY_BROADCAST"); 154 } 155 testSendEtwsMessageCancel()156 public void testSendEtwsMessageCancel() throws Exception { 157 Intent intent = new Intent(Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION); 158 intent.putExtra("message", createFromPdu(etwsMessageCancel)); 159 intent.setPackage(CB_RECEIVER_PKG); 160 mContext.sendOrderedBroadcast(intent, 161 "android.permission.RECEIVE_EMERGENCY_BROADCAST"); 162 } 163 testSendEtwsMessageTest()164 public void testSendEtwsMessageTest() throws Exception { 165 Intent intent = new Intent(Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION); 166 intent.putExtra("message", createFromPdu(etwsMessageTest)); 167 intent.setPackage(CB_RECEIVER_PKG); 168 mContext.sendOrderedBroadcast(intent, 169 "android.permission.RECEIVE_EMERGENCY_BROADCAST"); 170 } 171 }