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.rcs.uce.presence.pidfparser.capabilities;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 
23 import androidx.test.ext.junit.runners.AndroidJUnit4;
24 import androidx.test.filters.SmallTest;
25 
26 import com.android.ims.ImsTestBase;
27 import com.android.ims.rcs.uce.presence.pidfparser.ElementBase;
28 import com.android.ims.rcs.uce.presence.pidfparser.PidfParserConstant;
29 import com.android.ims.rcs.uce.presence.pidfparser.pidf.PidfConstant;
30 
31 import java.io.IOException;
32 import java.io.Reader;
33 import java.io.StringReader;
34 import java.io.StringWriter;
35 
36 import java.util.List;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.xmlpull.v1.XmlPullParser;
42 import org.xmlpull.v1.XmlPullParserException;
43 import org.xmlpull.v1.XmlPullParserFactory;
44 import org.xmlpull.v1.XmlSerializer;
45 
46 @RunWith(AndroidJUnit4.class)
47 public class ServiceCapsTest extends ImsTestBase {
48 
49     @Before
setUp()50     public void setUp() throws Exception {
51         super.setUp();
52     }
53 
54     @After
tearDown()55     public void tearDown() throws Exception {
56         super.tearDown();
57     }
58 
59     @Test
60     @SmallTest
testElementName()61     public void testElementName() throws Exception {
62         ServiceCaps serviceCaps = new ServiceCaps();
63 
64         assertEquals(CapsConstant.NAMESPACE, serviceCaps.getNamespace());
65         assertEquals(ServiceCaps.ELEMENT_NAME, serviceCaps.getElementName());
66     }
67 
68     @Test
69     @SmallTest
testSerializing()70     public void testSerializing() throws Exception {
71         Audio audio = new Audio(true);
72         Video video = new Video(true);
73         Duplex duplex = new Duplex();
74         duplex.addSupportedType(Duplex.DUPLEX_FULL);
75 
76         ServiceCaps serviceCaps = new ServiceCaps();
77         serviceCaps.addElement(audio);
78         serviceCaps.addElement(video);
79         serviceCaps.addElement(duplex);
80 
81         StringWriter writer = new StringWriter();
82         XmlSerializer serializer = getXmlSerializer(writer);
83 
84         // Serializing
85         serializer.startDocument(PidfParserConstant.ENCODING_UTF_8, true);
86         serviceCaps.serialize(serializer);
87         serializer.endDocument();
88         serializer.flush();
89 
90         String result = writer.toString();
91 
92         String verificationAudio = "<caps:audio>true</caps:audio>";
93         String verificationVideo = "<caps:video>true</caps:video>";
94         StringBuilder verificationDuplex = new StringBuilder();
95         verificationDuplex.append("<caps:duplex>")
96                 .append("<caps:supported>")
97                 .append("<caps:full />")
98                 .append("</caps:supported>")
99                 .append("</caps:duplex>");
100 
101         assertTrue(result.contains(verificationAudio));
102         assertTrue(result.contains(verificationVideo));
103         assertTrue(result.contains(verificationDuplex.toString()));
104     }
105 
106 
107     @Test
108     @SmallTest
testParsing()109     public void testParsing() throws Exception {
110         StringBuilder serviceCapsExample = new StringBuilder();
111         serviceCapsExample.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>")
112                 .append("<caps:servcaps xmlns=\"urn:ietf:params:xml:ns:pidf\"")
113                 .append(" xmlns:caps=\"urn:ietf:params:xml:ns:pidf:caps\">")
114                 .append("<caps:audio>true</caps:audio>")
115                 .append("<caps:video>true</caps:video>")
116                 .append("<caps:duplex><caps:supported>")
117                 .append("<caps:full />")
118                 .append("</caps:supported></caps:duplex>")
119                 .append("</caps:servcaps>");
120 
121         XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
122         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
123         Reader reader = new StringReader(serviceCapsExample.toString());
124         parser.setInput(reader);
125 
126         ServiceCaps serviceCaps = null;
127         int nextType = parser.next();
128 
129         // Find the start tag
130         do {
131             if (nextType == XmlPullParser.START_TAG
132                     && ServiceCaps.ELEMENT_NAME.equals(parser.getName())) {
133                 serviceCaps = new ServiceCaps();
134                 serviceCaps.parse(parser);
135                 break;
136             }
137             nextType = parser.next();
138         } while(nextType != XmlPullParser.END_DOCUMENT);
139 
140         reader.close();
141 
142         assertNotNull(serviceCaps);
143 
144         List<ElementBase> elements = serviceCaps.getElements();
145         Audio resultAudio = null;
146         Video resultVideo = null;
147         Duplex resultDuplex = null;
148         for (ElementBase element : elements) {
149             String elementName = element.getElementName();
150             if (Audio.ELEMENT_NAME.equals(elementName)) {
151                 resultAudio = (Audio) element;
152             } else if (Video.ELEMENT_NAME.equals(elementName)) {
153                 resultVideo = (Video) element;
154             } else if (Duplex.ELEMENT_NAME.equals(elementName)) {
155                 resultDuplex = (Duplex) element;
156             }
157         }
158 
159         assertNotNull(resultAudio);
160         assertTrue(resultAudio.isAudioSupported());
161 
162         assertNotNull(resultVideo);
163         assertTrue(resultVideo.isVideoSupported());
164 
165         assertNotNull(resultDuplex);
166 
167         List<String> supportedTypes = resultDuplex.getSupportedTypes();
168         assertEquals(1, supportedTypes.size());
169         assertEquals(Duplex.DUPLEX_FULL, supportedTypes.get(0));
170     }
171 
getXmlSerializer(StringWriter writer)172     private XmlSerializer getXmlSerializer(StringWriter writer)
173             throws XmlPullParserException, IOException {
174         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
175         XmlSerializer serializer = factory.newSerializer();
176         serializer.setOutput(writer);
177         serializer.setPrefix("", PidfConstant.NAMESPACE);
178         serializer.setPrefix("caps", CapsConstant.NAMESPACE);
179         return serializer;
180     }
181 }
182