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.PidfParserConstant;
28 
29 import com.android.ims.rcs.uce.presence.pidfparser.pidf.PidfConstant;
30 import java.io.IOException;
31 import java.io.Reader;
32 import java.io.StringReader;
33 import java.io.StringWriter;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.xmlpull.v1.XmlPullParser;
40 import org.xmlpull.v1.XmlPullParserException;
41 import org.xmlpull.v1.XmlPullParserFactory;
42 import org.xmlpull.v1.XmlSerializer;
43 
44 @RunWith(AndroidJUnit4.class)
45 public class AudioTest extends ImsTestBase {
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         super.setUp();
50     }
51 
52     @After
tearDown()53     public void tearDown() throws Exception {
54         super.tearDown();
55     }
56 
57     @Test
58     @SmallTest
testElementName()59     public void testElementName() throws Exception {
60         Audio audio = new Audio();
61 
62         assertEquals(CapsConstant.NAMESPACE, audio.getNamespace());
63         assertEquals(Audio.ELEMENT_NAME, audio.getElementName());
64     }
65 
66     @Test
67     @SmallTest
testSerializing()68     public void testSerializing() throws Exception {
69         Audio audio = new Audio(true);
70         StringWriter writer = new StringWriter();
71         XmlSerializer serializer = getXmlSerializer(writer);
72 
73         // Serializing
74         serializer.startDocument(PidfParserConstant.ENCODING_UTF_8, true);
75         audio.serialize(serializer);
76         serializer.endDocument();
77         serializer.flush();
78 
79         String result = writer.toString();
80         StringBuilder verificationBuilder = new StringBuilder();
81         verificationBuilder.append("<caps:audio")
82                 .append(" xmlns=\"").append(PidfConstant.NAMESPACE).append("\"")
83                 .append(" xmlns:caps=\"").append(CapsConstant.NAMESPACE).append("\">")
84                 .append("true")
85                 .append("</caps:audio>");
86 
87         assertTrue(result.contains(verificationBuilder.toString()));
88     }
89 
90 
91     @Test
92     @SmallTest
testParsing()93     public void testParsing() throws Exception {
94         StringBuilder audioExample = new StringBuilder();
95         audioExample.append("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>")
96                 .append("<caps:audio xmlns=\"urn:ietf:params:xml:ns:pidf\"")
97                 .append(" xmlns:caps=\"urn:ietf:params:xml:ns:pidf:caps\">")
98                 .append(true)
99                 .append("</caps:audio>");
100 
101         XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
102         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
103         Reader reader = new StringReader(audioExample.toString());
104         parser.setInput(reader);
105 
106         Audio audio = null;
107         int nextType = parser.next();
108 
109         // Find the start tag
110         do {
111             if (nextType == XmlPullParser.START_TAG
112                     && Audio.ELEMENT_NAME.equals(parser.getName())) {
113                 audio = new Audio();
114                 audio.parse(parser);
115                 break;
116             }
117             nextType = parser.next();
118         } while(nextType != XmlPullParser.END_DOCUMENT);
119 
120         reader.close();
121 
122         assertNotNull(audio);
123         assertTrue(audio.isAudioSupported());
124     }
125 
getXmlSerializer(StringWriter writer)126     private XmlSerializer getXmlSerializer(StringWriter writer)
127             throws XmlPullParserException, IOException {
128         XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
129         XmlSerializer serializer = factory.newSerializer();
130         serializer.setOutput(writer);
131         serializer.setPrefix("", PidfConstant.NAMESPACE);
132         serializer.setPrefix("caps", CapsConstant.NAMESPACE);
133         return serializer;
134     }
135 }
136