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 android.net.InetAddresses;
22 import android.os.Parcel;
23 import android.telephony.AccessNetworkConstants.AccessNetworkType;
24 import android.telephony.imsmedia.RtcpConfig;
25 import android.telephony.imsmedia.RtpConfig;
26 import android.telephony.imsmedia.RtpContextParams;
27 import android.telephony.imsmedia.TextConfig;
28 
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.net.InetSocketAddress;
35 
36 @RunWith(AndroidJUnit4.class)
37 public class TextConfigTest {
38     // RtcpConfig
39     private static final String CANONICAL_NAME = "name";
40     private static final int RTCP_PORT = 30001;
41     private static final int RTCP_INTERVAL = 3;
42     // TextConfig
43     private static final String REMOTE_RTP_ADDRESS = "122.22.22.22";
44     private static final int REMOTE_RTP_PORT = 30000;
45     private static final byte DSCP = 10;
46     private static final byte RX_PAYLOAD = 100;
47     private static final byte TX_PAYLOAD = 100;
48     private static final byte SAMPLING_RATE = 10;
49     private static final int CODEC_TYPE = TextConfig.TEXT_T140_RED;
50     private static final int BITRATE = 300;
51     private static final byte REDUNDANT_PAYLOAD = 101;
52     private static final byte REDUNDANT_LEVEL = 3;
53     private static final boolean KEEP_REDUNDANT_LEVEL = true;
54     private static final long SSRC = 3711990914L;
55     private static final long TIMESTAMP = 3277637148L;
56     private static final int SEQUENCE_NUMBER = 5231;
57 
58     private static final RtcpConfig sRtcp = new RtcpConfig.Builder()
59             .setCanonicalName(CANONICAL_NAME)
60             .setTransmitPort(RTCP_PORT)
61             .setIntervalSec(RTCP_INTERVAL)
62             .setRtcpXrBlockTypes(RtcpConfig.FLAG_RTCPXR_DLRR_REPORT_BLOCK)
63             .build();
64 
65     private static final RtpContextParams sRtpContextParams = new RtpContextParams.Builder()
66             .setSsrc(SSRC)
67             .setTimestamp(TIMESTAMP)
68             .setSequenceNumber(SEQUENCE_NUMBER)
69             .build();
70 
71     @Test
testConstructorAndGetters()72     public void testConstructorAndGetters() {
73         TextConfig config = createTextConfig();
74         assertThat(config.getMediaDirection()).isEqualTo(
75                 RtpConfig.MEDIA_DIRECTION_SEND_RECEIVE);
76         assertThat(config.getAccessNetwork()).isEqualTo(AccessNetworkType.EUTRAN);
77         assertThat(config.getRemoteRtpAddress()).isEqualTo(new InetSocketAddress(
78                 InetAddresses.parseNumericAddress(REMOTE_RTP_ADDRESS), REMOTE_RTP_PORT));
79         assertThat(config.getRtcpConfig()).isEqualTo(sRtcp);
80         assertThat(config.getDscp()).isEqualTo(DSCP);
81         assertThat(config.getRxPayloadTypeNumber()).isEqualTo(RX_PAYLOAD);
82         assertThat(config.getTxPayloadTypeNumber()).isEqualTo(TX_PAYLOAD);
83         assertThat(config.getSamplingRateKHz()).isEqualTo(SAMPLING_RATE);
84         assertThat(config.getRtpContextParams()).isEqualTo(sRtpContextParams);
85         assertThat(config.getCodecType()).isEqualTo(CODEC_TYPE);
86         assertThat(config.getBitrate()).isEqualTo(BITRATE);
87         assertThat(config.getRedundantPayload()).isEqualTo(REDUNDANT_PAYLOAD);
88         assertThat(config.getRedundantLevel()).isEqualTo(REDUNDANT_LEVEL);
89         assertThat(config.getKeepRedundantLevel()).isEqualTo(KEEP_REDUNDANT_LEVEL);
90     }
91 
92     @Test
testParcel()93     public void testParcel() {
94         TextConfig config = createTextConfig();
95 
96         Parcel parcel = Parcel.obtain();
97         config.writeToParcel(parcel, 0);
98         parcel.setDataPosition(0);
99 
100         TextConfig parcelConfig = TextConfig.CREATOR.createFromParcel(parcel);
101         assertThat(config).isEqualTo(parcelConfig);
102     }
103 
104     @Test
testEqual()105     public void testEqual() {
106         TextConfig config1 = createTextConfig();
107         TextConfig config2 = createTextConfig();
108         assertThat(config1).isEqualTo(config2);
109     }
110 
111     @Test
testNotEqual()112     public void testNotEqual() {
113         TextConfig config1 = createTextConfig();
114 
115         TextConfig config2 = new TextConfig.Builder()
116                 .setMediaDirection(RtpConfig.MEDIA_DIRECTION_SEND_RECEIVE)
117                 .setAccessNetwork(AccessNetworkType.EUTRAN)
118                 .setRemoteRtpAddress(new InetSocketAddress(
119                         InetAddresses.parseNumericAddress(REMOTE_RTP_ADDRESS), REMOTE_RTP_PORT))
120                 .setRtcpConfig(sRtcp)
121                 .setDscp(DSCP)
122                 .setRxPayloadTypeNumber(RX_PAYLOAD)
123                 .setTxPayloadTypeNumber(TX_PAYLOAD)
124                 .setSamplingRateKHz(SAMPLING_RATE)
125                 .setRtpContextParams(sRtpContextParams)
126                 .setCodecType(TextConfig.TEXT_T140)
127                 .setBitrate(BITRATE)
128                 .setRedundantPayload(REDUNDANT_PAYLOAD)
129                 .setRedundantLevel(REDUNDANT_LEVEL)
130                 .setKeepRedundantLevel(KEEP_REDUNDANT_LEVEL)
131                 .build();
132 
133         assertThat(config1).isNotEqualTo(config2);
134 
135         TextConfig config3 = new TextConfig.Builder()
136                 .setMediaDirection(RtpConfig.MEDIA_DIRECTION_SEND_RECEIVE)
137                 .setAccessNetwork(AccessNetworkType.EUTRAN)
138                 .setRemoteRtpAddress(new InetSocketAddress(
139                         InetAddresses.parseNumericAddress(REMOTE_RTP_ADDRESS), REMOTE_RTP_PORT))
140                 .setRtcpConfig(sRtcp)
141                 .setDscp(DSCP)
142                 .setRxPayloadTypeNumber(RX_PAYLOAD)
143                 .setTxPayloadTypeNumber(TX_PAYLOAD)
144                 .setSamplingRateKHz(SAMPLING_RATE)
145                 .setCodecType(CODEC_TYPE)
146                 .setBitrate(BITRATE)
147                 .setRedundantPayload(REDUNDANT_PAYLOAD)
148                 .setRedundantLevel(REDUNDANT_LEVEL)
149                 .setKeepRedundantLevel(KEEP_REDUNDANT_LEVEL)
150                 .build();
151 
152         assertThat(config1).isNotEqualTo(config3);
153     }
154 
createTextConfig()155     static TextConfig createTextConfig() {
156         return new TextConfig.Builder()
157                 .setMediaDirection(RtpConfig.MEDIA_DIRECTION_SEND_RECEIVE)
158                 .setAccessNetwork(AccessNetworkType.EUTRAN)
159                 .setRemoteRtpAddress(new InetSocketAddress(
160                         InetAddresses.parseNumericAddress(REMOTE_RTP_ADDRESS), REMOTE_RTP_PORT))
161                 .setRtcpConfig(sRtcp)
162                 .setDscp(DSCP)
163                 .setRxPayloadTypeNumber(RX_PAYLOAD)
164                 .setTxPayloadTypeNumber(TX_PAYLOAD)
165                 .setSamplingRateKHz(SAMPLING_RATE)
166                 .setRtpContextParams(sRtpContextParams)
167                 .setCodecType(CODEC_TYPE)
168                 .setBitrate(BITRATE)
169                 .setRedundantPayload(REDUNDANT_PAYLOAD)
170                 .setRedundantLevel(REDUNDANT_LEVEL)
171                 .setKeepRedundantLevel(KEEP_REDUNDANT_LEVEL)
172                 .build();
173     }
174 }
175