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