1/*
2 * Copyright (C) 2016 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.internal.telephony.cdma;
18
19import android.os.HandlerThread;
20import android.os.Message;
21import android.test.suitebuilder.annotation.SmallTest;
22
23import com.android.internal.telephony.ImsSMSDispatcher;
24import com.android.internal.telephony.SMSDispatcher;
25import com.android.internal.telephony.TelephonyTest;
26
27import static org.mockito.Mockito.*;
28
29import org.junit.After;
30import org.junit.Before;
31import org.junit.Test;
32import org.mockito.Mock;
33
34public class CdmaSmsDispatcherTest extends TelephonyTest {
35    @Mock
36    private android.telephony.SmsMessage mSmsMessage;
37    @Mock
38    private SmsMessage mCdmaSmsMessage;
39    @Mock
40    private ImsSMSDispatcher mImsSmsDispatcher;
41    @Mock
42    private SMSDispatcher.SmsTracker mSmsTracker;
43
44    private CdmaSMSDispatcher mCdmaSmsDispatcher;
45    private CdmaSmsDispatcherTestHandler mCdmaSmsDispatcherTestHandler;
46
47    private class CdmaSmsDispatcherTestHandler extends HandlerThread {
48
49        private CdmaSmsDispatcherTestHandler(String name) {
50            super(name);
51        }
52
53        @Override
54        public void onLooperPrepared() {
55            mCdmaSmsDispatcher = new CdmaSMSDispatcher(mPhone, mSmsUsageMonitor,
56                    mImsSmsDispatcher);
57            setReady(true);
58        }
59    }
60
61    @Before
62    public void setUp() throws Exception {
63        super.setUp(this.getClass().getSimpleName());
64
65        setupMockPackagePermissionChecks();
66
67        mCdmaSmsDispatcherTestHandler = new CdmaSmsDispatcherTestHandler(TAG);
68        mCdmaSmsDispatcherTestHandler.start();
69        waitUntilReady();
70    }
71
72    @After
73    public void tearDown() throws Exception {
74        mCdmaSmsDispatcher = null;
75        mCdmaSmsDispatcherTestHandler.quit();
76        super.tearDown();
77    }
78
79    @Test @SmallTest
80    public void testSendSms() {
81        doReturn(mServiceState).when(mPhone).getServiceState();
82        mCdmaSmsDispatcher.sendSms(mSmsTracker);
83        verify(mSimulatedCommandsVerifier).sendCdmaSms(nullable(byte[].class), any(Message.class));
84    }
85
86    @Test @SmallTest
87    public void testSendText() {
88        mCdmaSmsDispatcher.sendText("111"/* desAddr*/, "222" /*scAddr*/, TAG,
89                null, null, null, null, false);
90        verify(mSimulatedCommandsVerifier).sendCdmaSms(any(byte[].class), any(Message.class));
91    }
92
93    @Test @SmallTest
94    public void testSendTextWithOutDesAddr() {
95        mCdmaSmsDispatcher.sendText(null, "222" /*scAddr*/, TAG,
96                null, null, null, null, false);
97        verify(mSimulatedCommandsVerifier, times(0)).sendImsGsmSms(anyString(), anyString(),
98                anyInt(), anyInt(), any(Message.class));
99    }
100}
101