• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.ims;
18 
19 import static junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertNotNull;
21 import static junit.framework.TestCase.fail;
22 
23 import static org.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.verify;
25 
26 import android.os.AsyncResult;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Looper;
30 import android.os.Message;
31 import android.telephony.ims.ImsSsInfo;
32 import android.telephony.ims.ImsUtListener;
33 
34 import androidx.test.ext.junit.runners.AndroidJUnit4;
35 import androidx.test.filters.SmallTest;
36 
37 import com.android.ims.internal.IImsUt;
38 
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.ArgumentCaptor;
44 import org.mockito.Mock;
45 
46 import java.util.concurrent.LinkedBlockingQueue;
47 import java.util.concurrent.TimeUnit;
48 
49 @RunWith(AndroidJUnit4.class)
50 public class ImsUtTest extends ImsTestBase {
51 
52     private static final int MSG_QUERY = 1;
53     private static final int TEST_TIMEOUT_MS = 5000;
54 
55     private static class TestHandler extends Handler {
56 
TestHandler(Looper looper)57         TestHandler(Looper looper) {
58             super(looper);
59         }
60 
61         private final LinkedBlockingQueue<ImsSsInfo> mPendingSsInfos = new LinkedBlockingQueue<>(1);
62         @Override
handleMessage(Message msg)63         public void handleMessage(Message msg) {
64             if (msg.what == MSG_QUERY) {
65                 AsyncResult ar = (AsyncResult) msg.obj;
66                 mPendingSsInfos.offer((ImsSsInfo) ar.result);
67             }
68         }
getPendingImsSsInfo()69         public ImsSsInfo getPendingImsSsInfo() {
70             try {
71                 return mPendingSsInfos.poll(TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
72             } catch (InterruptedException e) {
73                 fail("test interrupted!");
74             }
75             return null;
76         }
77     }
78 
79     @Mock IImsUt mImsUtBinder;
80 
81     private TestHandler mHandler;
82 
83     @Before
setUp()84     public void setUp() throws Exception {
85         super.setUp();
86         mHandler = new TestHandler(Looper.getMainLooper());
87     }
88 
89     @After
tearDown()90     public void tearDown() throws Exception {
91         waitForHandlerAction(mHandler, 1000/*ms*/);
92         super.tearDown();
93     }
94 
95     @Test
96     @SmallTest
testClirConversionCompat()97     public void testClirConversionCompat() throws Exception {
98         ArgumentCaptor<ImsUt.IImsUtListenerProxy> captor =
99                 ArgumentCaptor.forClass(ImsUt.IImsUtListenerProxy.class);
100         ImsUt mImsUt = new ImsUt(mImsUtBinder, Runnable::run);
101         verify(mImsUtBinder).setListener(captor.capture());
102         ImsUt.IImsUtListenerProxy proxy = captor.getValue();
103         assertNotNull(proxy);
104 
105         doReturn(2).when(mImsUtBinder).queryCLIR();
106         mImsUt.queryCLIR(Message.obtain(mHandler, MSG_QUERY));
107 
108         Bundle result = new Bundle();
109         result.putIntArray(ImsUtListener.BUNDLE_KEY_CLIR, new int[] {
110                 ImsSsInfo.CLIR_OUTGOING_INVOCATION, ImsSsInfo.CLIR_STATUS_PROVISIONED_PERMANENT});
111         // This is deprecated, will be converted from Bundle -> ImsSsInfo
112         proxy.utConfigurationQueried(null, 2 /*id*/, result);
113         waitForHandlerAction(mHandler, 1000/*ms*/);
114 
115 
116         ImsSsInfo info = mHandler.getPendingImsSsInfo();
117         assertNotNull(info);
118         assertEquals(ImsSsInfo.CLIR_OUTGOING_INVOCATION, info.getClirOutgoingState());
119         assertEquals(ImsSsInfo.CLIR_STATUS_PROVISIONED_PERMANENT,
120                 info.getClirInterrogationStatus());
121     }
122 
123     @Test
124     @SmallTest
testClipConversionCompat()125     public void testClipConversionCompat() throws Exception {
126         ArgumentCaptor<ImsUt.IImsUtListenerProxy> captor =
127                 ArgumentCaptor.forClass(ImsUt.IImsUtListenerProxy.class);
128         ImsUt mImsUt = new ImsUt(mImsUtBinder, Runnable::run);
129         verify(mImsUtBinder).setListener(captor.capture());
130         ImsUt.IImsUtListenerProxy proxy = captor.getValue();
131         assertNotNull(proxy);
132 
133         doReturn(2).when(mImsUtBinder).queryCLIP();
134         mImsUt.queryCLIP(Message.obtain(mHandler, MSG_QUERY));
135 
136         ImsSsInfo info = new ImsSsInfo.Builder(ImsSsInfo.ENABLED).setProvisionStatus(
137                 ImsSsInfo.CLIR_STATUS_PROVISIONED_PERMANENT).build();
138         Bundle result = new Bundle();
139         result.putParcelable(ImsUtListener.BUNDLE_KEY_SSINFO, info);
140         // This is deprecated, will be converted from Bundle -> ImsSsInfo
141         proxy.utConfigurationQueried(null, 2 /*id*/, result);
142         waitForHandlerAction(mHandler, 1000/*ms*/);
143 
144         ImsSsInfo resultInfo = mHandler.getPendingImsSsInfo();
145         assertNotNull(resultInfo);
146         assertEquals(info.getStatus(), resultInfo.getStatus());
147         assertEquals(info.getProvisionStatus(), resultInfo.getProvisionStatus());
148     }
149 }
150