1 /*
2  * Copyright (C) 2019 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.phone;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.content.Context;
22 
23 import androidx.test.InstrumentationRegistry;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.util.Collections;
31 import java.util.Vector;
32 
33 @RunWith(AndroidJUnit4.class)
34 public class CarrierXmlParserTest {
35     private CarrierXmlParser mCarrierXmlParser;
36     private Context mContext;
37 
38     @Before
setUp()39     public void setUp() throws Exception {
40         mContext = InstrumentationRegistry.getTargetContext();
41     }
42 
43     @Test
verifyParserFormat_shouldSameAsXml()44     public void verifyParserFormat_shouldSameAsXml() {
45         String expected =
46                 "((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*"
47                         + "([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)";
48 
49         mCarrierXmlParser = new CarrierXmlParser(mContext, -1);
50 
51         assertEquals(expected, mCarrierXmlParser.sParserFormat);
52     }
53 
54     @Test
verifyUssdParser_shouldMatchText()55     public void verifyUssdParser_shouldMatchText() {
56         String parserFormat =
57                 "((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*"
58                         + "([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)";
59         String text = "120*1*7*123456789*20*1*0*0#";
60         String[] expectedContent = {"120*1*7*123456789*20*1*0*0#",
61                 "120*1*7*123456789*20*1*0*0#",
62                 "120",
63                 "*1*7*123456789*20*1*0*0",
64                 "1",
65                 "*7*123456789*20*1*0*0",
66                 "7",
67                 "*123456789*20*1*0*0",
68                 "123456789",
69                 "*20*1*0*0",
70                 "20",
71                 "*1*0*0",
72                 "1",
73                 "*0*0",
74                 "0",
75                 "*0",
76                 "0"};
77         Vector<String> expected = new Vector<>();
78         Collections.addAll(expected, expectedContent);
79 
80         CarrierXmlParser.UssdParser ussdParser = new CarrierXmlParser.UssdParser(parserFormat);
81         ussdParser.newFromResponseString(text);
82 
83         assertEquals(expected, ussdParser.getResult());
84     }
85 }
86