Add test for Ussd Parser

Behavior of xml reader was changed.
- xml renew.
- add testcase.

Bug: 132216780
Test: atest CarrierXmlParserTest
Change-Id: I8688147d1c94a382c2761eb77e29c7a06162ff4a
diff --git a/Android.mk b/Android.mk
index 775d795..8aff191 100644
--- a/Android.mk
+++ b/Android.mk
@@ -25,7 +25,8 @@
 
 LOCAL_STATIC_JAVA_LIBRARIES := \
         guava \
-        volley
+        volley \
+        android-support-annotations
 
 LOCAL_SRC_FILES := $(call all-java-files-under, $(src_dirs))
 LOCAL_SRC_FILES += $(call all-proto-files-under, ecc/proto)
diff --git a/res/xml/carrier_ss_string.xml b/res/xml/carrier_ss_string.xml
index 96bcfa4..826f0ea 100644
--- a/res/xml/carrier_ss_string.xml
+++ b/res/xml/carrier_ss_string.xml
@@ -74,10 +74,10 @@
 <resources>
     <!-- This is a parser format by Regular Expression.
          "response_format"'s position follow below index.
-         ((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)
-         1 2        3   4       5   6       7   8       9   10      11  12      13  14      15  16
+         ((\d{2,3})(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*))?)?)?)?)?)?)?#)
+         1 2       3  4       5  6       7  8       9  10      11 12     13  14      15 16
     -->
-    <regular_parser>((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)</regular_parser>
+    <regular_parser>((\d{2,3})(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*))?)?)?)?)?)?)?#)</regular_parser>
     <feature name="callforwarding">
         <command name="query"><!--For example: *#120*1#-->
             <service_code>120</service_code>
diff --git a/res/xml/carrier_ss_string_850.xml b/res/xml/carrier_ss_string_850.xml
index 53611d4..86d5743 100644
--- a/res/xml/carrier_ss_string_850.xml
+++ b/res/xml/carrier_ss_string_850.xml
@@ -22,10 +22,10 @@
 <resources>
     <!-- This is a parser format by Regular Expression.
          "response_format"'s position follow below index.
-         ((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)
-         1 2        3   4       5   6       7   8       9   10      11  12      13  14      15  16
+         ((\d{2,3})(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*))?)?)?)?)?)?)?#)
+         1 2       3  4       5  6       7  8       9  10      11 12     13  14      15 16
     -->
-    <regular_parser>((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)</regular_parser>
+    <regular_parser>((\d{2,3})(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*)(\*([^*#]*))?)?)?)?)?)?)?#)</regular_parser>
     <feature name="callforwarding">
         <command name="query"><!--For example: *#120*1#-->
             <service_code>120</service_code>
diff --git a/src/com/android/phone/CarrierXmlParser.java b/src/com/android/phone/CarrierXmlParser.java
index 4cf7e35..18602c9 100644
--- a/src/com/android/phone/CarrierXmlParser.java
+++ b/src/com/android/phone/CarrierXmlParser.java
@@ -18,6 +18,7 @@
 
 import android.content.Context;
 import android.content.res.XmlResourceParser;
+import android.support.annotation.VisibleForTesting;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
@@ -95,7 +96,9 @@
     private static final String ATTR_DEFINITION_KEY = "definition";
 
     HashMap<String, SsFeature> mFeatureMaps;
-    private static String sParserFormat = "";
+
+    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
+    static String sParserFormat = "";
 
     // TAG_ENTRY_NUMBER and TAG_ENTRY_TIME is xml's entry value.
     // This is mapping user's input value. For example: number,time ...
@@ -155,6 +158,8 @@
                         mParserStr.add("");
                     }
                 }
+            } else {
+                Log.d(LOG_TAG, "no match");
             }
         }
 
diff --git a/tests/src/com/android/phone/CarrierXmlParserTest.java b/tests/src/com/android/phone/CarrierXmlParserTest.java
new file mode 100644
index 0000000..92a8ba7
--- /dev/null
+++ b/tests/src/com/android/phone/CarrierXmlParserTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.phone;
+
+import static org.junit.Assert.assertEquals;
+
+import android.content.Context;
+
+import androidx.test.InstrumentationRegistry;
+import androidx.test.runner.AndroidJUnit4;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.Collections;
+import java.util.Vector;
+
+@RunWith(AndroidJUnit4.class)
+public class CarrierXmlParserTest {
+    private CarrierXmlParser mCarrierXmlParser;
+    private Context mContext;
+
+    @Before
+    public void setUp() throws Exception {
+        mContext = InstrumentationRegistry.getTargetContext();
+    }
+
+    @Test
+    public void verifyParserFormat_shouldSameAsXml() {
+        String expected =
+                "((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*"
+                        + "([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)";
+
+        mCarrierXmlParser = new CarrierXmlParser(mContext, -1);
+
+        assertEquals(expected, mCarrierXmlParser.sParserFormat);
+    }
+
+    @Test
+    public void verifyUssdParser_shouldMatchText() {
+        String parserFormat =
+                "((\\d{2,3})(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*([^*#]*)(\\*"
+                        + "([^*#]*)(\\*([^*#]*))?)?)?)?)?)?)?#)";
+        String text = "120*1*7*123456789*20*1*0*0#";
+        String[] expectedContent = {"120*1*7*123456789*20*1*0*0#",
+                "120*1*7*123456789*20*1*0*0#",
+                "120",
+                "*1*7*123456789*20*1*0*0",
+                "1",
+                "*7*123456789*20*1*0*0",
+                "7",
+                "*123456789*20*1*0*0",
+                "123456789",
+                "*20*1*0*0",
+                "20",
+                "*1*0*0",
+                "1",
+                "*0*0",
+                "0",
+                "*0",
+                "0"};
+        Vector<String> expected = new Vector<>();
+        Collections.addAll(expected, expectedContent);
+
+        CarrierXmlParser.UssdParser ussdParser = new CarrierXmlParser.UssdParser(parserFormat);
+        ussdParser.newFromResponseString(text);
+
+        assertEquals(expected, ussdParser.getResult());
+    }
+}