• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2022 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.telephony.imsmedia;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.fail;
22 import static org.mockito.Mockito.eq;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 
26 import android.os.Looper;
27 import android.os.Parcel;
28 import android.os.ParcelFileDescriptor;
29 import android.os.RemoteException;
30 import android.telephony.imsmedia.IImsTextSessionCallback;
31 import android.telephony.imsmedia.ImsMediaSession;
32 import android.telephony.imsmedia.MediaQualityThreshold;
33 import android.telephony.imsmedia.TextConfig;
34 import android.testing.AndroidTestingRunner;
35 import android.testing.TestableLooper;
36 
37 import com.android.telephony.imsmedia.Utils.OpenSessionParams;
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.Mock;
44 import org.mockito.MockitoAnnotations;
45 
46 import java.net.DatagramSocket;
47 import java.net.SocketException;
48 
49 @RunWith(AndroidTestingRunner.class)
50 @TestableLooper.RunWithLooper
51 public class TextSessionTest extends ImsMediaTest {
52     private static final int SESSION_ID = 1;
53     private static final int SUCCESS = ImsMediaSession.RESULT_SUCCESS;
54     private static final int NO_RESOURCES = ImsMediaSession.RESULT_NO_RESOURCES;
55     private static final int RTP = ImsMediaSession.PACKET_TYPE_RTP;
56     private static final int RTCP = ImsMediaSession.PACKET_TYPE_RTCP;
57     private static final String TEXT_STREAM = "Hello";
58     private TextSession mTextSession;
59     private TextSession.TextSessionHandler mHandler;
60     private WakeLockManager mWakeLockManager;
61     @Mock
62     private TextService mTextService;
63     private TextListener mTextListener;
64     @Mock
65     private TextLocalSession mTextLocalSession;
66     @Mock
67     private IImsTextSessionCallback mCallback;
68 
69     @Before
setUp()70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72         mTextSession = new TextSession(SESSION_ID, mCallback, mTextService, mTextLocalSession,
73                 Looper.myLooper());
74         mTextListener = mTextSession.getTextListener();
75         mHandler = mTextSession.getTextSessionHandler();
76         mTestClass = TextSessionTest.this;
77         mWakeLockManager = WakeLockManager.getInstance();
78         super.setUp();
79     }
80 
81     @After
tearDown()82     public void tearDown() throws Exception {
83         super.tearDown();
84         mWakeLockManager.cleanup();
85     }
86 
createParcel(int message, int result, TextConfig config)87     private Parcel createParcel(int message, int result, TextConfig config) {
88         Parcel parcel = Parcel.obtain();
89         parcel.writeInt(message);
90         parcel.writeInt(result);
91         if (config != null) {
92             config.writeToParcel(parcel, 0);
93         }
94         parcel.setDataPosition(0);
95         return parcel;
96     }
97 
98     @Test
testOpenSession()99     public void testOpenSession() {
100         DatagramSocket rtpSocket = null;
101         DatagramSocket rtcpSocket = null;
102 
103         try {
104             rtpSocket = new DatagramSocket();
105             rtcpSocket = new DatagramSocket();
106         } catch (SocketException e) {
107             fail("SocketException:" + e);
108         }
109 
110         OpenSessionParams params = new OpenSessionParams(
111                 ParcelFileDescriptor.fromDatagramSocket(rtpSocket),
112                 ParcelFileDescriptor.fromDatagramSocket(rtcpSocket),
113                 null, null);
114 
115         mTextSession.openSession(params);
116         processAllMessages();
117         verify(mTextService, times(1)).openSession(eq(SESSION_ID), eq(params));
118         assertThat(mWakeLockManager.mWakeLock.isHeld()).isEqualTo(false);
119     }
120 
121     @Test
testCloseSession()122     public void testCloseSession() {
123         mTextSession.closeSession();
124         processAllMessages();
125         verify(mTextService, times(1)).closeSession(eq(SESSION_ID));
126         assertThat(mWakeLockManager.mWakeLock.isHeld()).isEqualTo(false);
127     }
128 
129     @Test
testModifySession()130     public void testModifySession() {
131         // Modify Session Request
132         TextConfig config = TextConfigTest.createTextConfig();
133         mTextSession.modifySession(config);
134         processAllMessages();
135         verify(mTextLocalSession, times(1)).modifySession(eq(config));
136         assertThat(mWakeLockManager.mWakeLock.isHeld()).isEqualTo(true);
137 
138         // Modify Session Response - Success
139         mTextListener.onMessage(
140                 createParcel(TextSession.EVENT_MODIFY_SESSION_RESPONSE, SUCCESS, config));
141         processAllMessages();
142         try {
143             verify(mCallback, times(1)).onModifySessionResponse(eq(config), eq(SUCCESS));
144         } catch (RemoteException e) {
145             fail("Failed to notify modifySessionResponse: " + e);
146         }
147 
148         // Modify Session Response - Failure (NO_RESOURCES)
149         mTextListener.onMessage(
150                 createParcel(TextSession.EVENT_MODIFY_SESSION_RESPONSE, NO_RESOURCES, config));
151         processAllMessages();
152         try {
153             verify(mCallback, times(1)).onModifySessionResponse(eq(config), eq(NO_RESOURCES));
154         } catch (RemoteException e) {
155             fail("Failed to notify modifySessionResponse: " + e);
156         }
157     }
158 
159     @Test
testSendRtt()160     public void testSendRtt() {
161         mTextSession.sendRtt(TEXT_STREAM);
162         processAllMessages();
163         verify(mTextLocalSession, times(1)).sendRtt(eq(TEXT_STREAM));
164     }
165 
166     @Test
testSetMediaQualityThreshold()167     public void testSetMediaQualityThreshold() {
168         // Set Media Quality Threshold
169         MediaQualityThreshold threshold = MediaQualityThresholdTest.createMediaQualityThreshold();
170         mTextSession.setMediaQualityThreshold(threshold);
171         processAllMessages();
172         verify(mTextLocalSession, times(1)).setMediaQualityThreshold(eq(threshold));
173     }
174 
175     @Test
testMediaInactivityInd()176     public void testMediaInactivityInd() {
177         // Receive Inactivity - RTP
178         Parcel parcel = Parcel.obtain();
179         parcel.writeInt(TextSession.EVENT_MEDIA_INACTIVITY_IND);
180         parcel.writeInt(RTP);
181         parcel.setDataPosition(0);
182         mTextListener.onMessage(parcel);
183         processAllMessages();
184         try {
185             verify(mCallback, times(1)).notifyMediaInactivity(eq(RTP));
186         } catch (RemoteException e) {
187             fail("Failed to notify notifyMediaInactivity: " + e);
188         }
189 
190         // Receive Inactivity - RTCP
191         Parcel parcel2 = Parcel.obtain();
192         parcel2.writeInt(TextSession.EVENT_MEDIA_INACTIVITY_IND);
193         parcel2.writeInt(RTCP);
194         parcel2.setDataPosition(0);
195         mTextListener.onMessage(parcel2);
196         processAllMessages();
197         try {
198             verify(mCallback, times(1)).notifyMediaInactivity(eq(RTCP));
199         } catch (RemoteException e) {
200             fail("Failed to notify notifyMediaInactivity: " + e);
201         }
202     }
203 
204     @Test
testRttReceived()205     public void testRttReceived() {
206         // Receive onRttReceived
207         Utils.sendMessage(mHandler, TextSession.EVENT_RTT_RECEIVED, TEXT_STREAM);
208         processAllMessages();
209         try {
210             verify(mCallback, times(1)).onRttReceived(eq(TEXT_STREAM));
211         } catch (RemoteException e) {
212             fail("Failed to notify onRttReceived: " + e);
213         }
214     }
215 
216     @Test
testOpenSessionSuccess()217     public void testOpenSessionSuccess() {
218         mTextSession.onOpenSessionSuccess(mTextLocalSession);
219         processAllMessages();
220         try {
221             verify(mCallback, times(1)).onOpenSessionSuccess(mTextSession);
222             assertThat(mWakeLockManager.mWakeLock.isHeld()).isEqualTo(false);
223         } catch (RemoteException e) {
224             fail("Failed to notify onOpenSessionSuccess: " + e);
225         }
226     }
227 
228     @Test
testOpenSessionFailure()229     public void testOpenSessionFailure() {
230         mTextSession.onOpenSessionFailure(ImsMediaSession.RESULT_INVALID_PARAM);
231         processAllMessages();
232         try {
233             verify(mCallback, times(1)).onOpenSessionFailure(ImsMediaSession.RESULT_INVALID_PARAM);
234             assertThat(mWakeLockManager.mWakeLock.isHeld()).isEqualTo(false);
235         } catch (RemoteException e) {
236             fail("Failed to notify onOpenSessionFailure: " + e);
237         }
238     }
239 
240     @Test
testSessionClosed()241     public void testSessionClosed() {
242         mTextSession.onSessionClosed();
243         processAllMessages();
244         try {
245             verify(mCallback, times(1)).onSessionClosed();
246         } catch (RemoteException e) {
247             fail("Failed to notify onSessionClosed: " + e);
248         }
249     }
250 }
251