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