1 /* 2 * Copyright (C) 2019 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 android.telephony.ims.cts; 18 19 import static junit.framework.Assert.assertTrue; 20 21 import static org.junit.Assert.assertEquals; 22 23 import android.telephony.SmsManager; 24 import android.telephony.SmsMessage; 25 import android.telephony.ims.stub.ImsSmsImplBase; 26 import android.util.Log; 27 28 import java.util.concurrent.CountDownLatch; 29 import java.util.concurrent.TimeUnit; 30 31 public class TestImsSmsImpl extends ImsSmsImplBase { 32 private static final String TAG = "CtsTestImsService"; 33 34 private CountDownLatch mSentTriggeredLatch = new CountDownLatch(1); 35 private CountDownLatch mOnReadyLatch = new CountDownLatch(1); 36 private CountDownLatch mAckDeliveryLatch = new CountDownLatch(1); 37 private CountDownLatch mSmsAckLatch = new CountDownLatch(1); 38 // Expecting only one message at a time 39 public byte[] sentPdu; 40 private int mToken; 41 private int mMessageRef; 42 private int mResult; 43 44 @Override sendSms(int token, int messageRef, String format, String smsc, boolean isRetry, byte[] pdu)45 public void sendSms(int token, int messageRef, String format, String smsc, boolean isRetry, 46 byte[] pdu) { 47 if (ImsUtils.VDBG) { 48 Log.d(TAG, "ImsSmsImplBase.sendSms called"); 49 } 50 sentPdu = pdu; 51 mToken = token; 52 mMessageRef = messageRef; 53 54 mSentTriggeredLatch.countDown(); 55 } 56 57 @Override getSmsFormat()58 public String getSmsFormat() { 59 return SmsMessage.FORMAT_3GPP; 60 } 61 62 @Override onReady()63 public void onReady() { 64 if (ImsUtils.VDBG) { 65 Log.d(TAG, "ImsSmsImplBase.onReady called"); 66 } 67 mOnReadyLatch.countDown(); 68 69 } 70 71 @Override acknowledgeSms(int token, int messageRef, int result)72 public void acknowledgeSms(int token, int messageRef, int result) { 73 mToken = token; 74 mMessageRef = messageRef; 75 mResult = result; 76 mSmsAckLatch.countDown(); 77 } 78 79 @Override acknowledgeSmsReport(int token, int messageRef, int result)80 public void acknowledgeSmsReport(int token, int messageRef, int result) { 81 mToken = token; 82 mMessageRef = messageRef; 83 mResult = result; 84 mAckDeliveryLatch.countDown(); 85 } 86 receiveSmsWaitForAcknowledge(int token, String format, byte[] pdu)87 public void receiveSmsWaitForAcknowledge(int token, String format, byte[] pdu) { 88 onSmsReceived(token, format, pdu); 89 boolean complete = false; 90 try { 91 complete = mSmsAckLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 92 } catch (InterruptedException e) { 93 // complete == false 94 } 95 96 assertTrue("Timed out waiting for acknowledgeSms.", complete); 97 assertEquals("Token mismatch.", token, mToken); 98 assertTrue("Invalid messageRef", mMessageRef >= 0); 99 assertEquals("Invalid result in acknowledgeSms.", DELIVER_STATUS_OK, mResult); 100 } 101 sendReportWaitForAcknowledgeSmsReportR(int token, String format, byte[] pdu)102 public void sendReportWaitForAcknowledgeSmsReportR(int token, String format, byte[] pdu) { 103 onSmsStatusReportReceived(token, format, pdu); 104 boolean complete = false; 105 try { 106 complete = mAckDeliveryLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 107 } catch (InterruptedException e) { 108 // complete = false 109 } 110 assertTrue("Timed out waiting for delivery report.", complete); 111 assertEquals("Ttoken mismatch.", token, mToken); 112 assertEquals("Status mismatch.", STATUS_REPORT_STATUS_OK, mResult); 113 } 114 115 116 // Deprecated method for P and Q, where mToken is expected to be the framework token sendReportWaitForAcknowledgeSmsReportPQ(int messageRef, String format, byte[] pdu)117 public void sendReportWaitForAcknowledgeSmsReportPQ(int messageRef, String format, 118 byte[] pdu) { 119 onSmsStatusReportReceived(mToken, messageRef, format, pdu); 120 boolean complete = false; 121 try { 122 complete = mAckDeliveryLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 123 } catch (InterruptedException e) { 124 // complete = false 125 } 126 assertTrue("Timed out waiting for delivery report.", complete); 127 assertEquals("MessageRef mismatch.", messageRef, mMessageRef); 128 assertEquals("Status mismatch.", STATUS_REPORT_STATUS_OK, mResult); 129 } 130 131 // P-Q API waitForMessageSentLatch()132 public boolean waitForMessageSentLatch() { 133 boolean complete = false; 134 try { 135 complete = mSentTriggeredLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 136 onSendSmsResult(mToken, mMessageRef, ImsSmsImplBase.SEND_STATUS_OK, 137 SmsManager.RESULT_ERROR_NONE); 138 } catch (InterruptedException e) { 139 // complete = false 140 } 141 return complete; 142 } 143 144 // R+ API waitForMessageSentLatchSuccess()145 public boolean waitForMessageSentLatchSuccess() { 146 boolean complete = false; 147 try { 148 complete = mSentTriggeredLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 149 onSendSmsResultSuccess(mToken, mMessageRef); 150 } catch (InterruptedException e) { 151 // complete = false 152 } 153 return complete; 154 } 155 156 // R+ API waitForMessageSentLatchError(int resultCode, int networkErrorCode)157 public boolean waitForMessageSentLatchError(int resultCode, int networkErrorCode) { 158 boolean complete = false; 159 try { 160 complete = mSentTriggeredLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 161 onSendSmsResultError(mToken, mMessageRef, ImsSmsImplBase.SEND_STATUS_ERROR, 162 resultCode, networkErrorCode); 163 } catch (InterruptedException e) { 164 // complete = false 165 } 166 return complete; 167 } 168 waitForOnReadyLatch()169 public boolean waitForOnReadyLatch() { 170 boolean complete = false; 171 try { 172 complete = mOnReadyLatch.await(ImsUtils.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS); 173 } catch (InterruptedException e) { 174 // complete = false 175 } 176 177 return complete; 178 } 179 180 } 181