Add cts tests for CarrierConfigManager.

If device has SIM card, we check config isn't null. For device with no
SIM card, we check it's static value.

Bug: b/20828053
Change-Id: I72358bde630e04c5e3d45a251a087bbcf3c1bfa6
diff --git a/tests/tests/telephony/src/android/telephony/cts/CarrierConfigManagerTest.java b/tests/tests/telephony/src/android/telephony/cts/CarrierConfigManagerTest.java
new file mode 100644
index 0000000..67d75c9
--- /dev/null
+++ b/tests/tests/telephony/src/android/telephony/cts/CarrierConfigManagerTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2014 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 android.telephony.cts;
+
+import android.content.Context;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+import android.test.AndroidTestCase;
+
+public class CarrierConfigManagerTest extends AndroidTestCase {
+    private CarrierConfigManager mConfigManager;
+    private TelephonyManager mTelephonyManager;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mTelephonyManager = (TelephonyManager)
+                getContext().getSystemService(Context.TELEPHONY_SERVICE);
+        mConfigManager = (CarrierConfigManager)
+                getContext().getSystemService(Context.CARRIER_CONFIG_SERVICE);
+    }
+
+    private boolean hasValidPhone() {
+        return mTelephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE;
+    }
+
+    private boolean isSimCardPresent() {
+        return mTelephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE &&
+                mTelephonyManager.getSimState() != TelephonyManager.SIM_STATE_ABSENT;
+    }
+
+    private void checkConfig(PersistableBundle config) {
+        if (!hasValidPhone()) {
+            assertNull(config);
+            return;
+        }
+        if (isSimCardPresent()) {
+            assertNotNull(config);
+        } else {
+            // Static default in CarrierConfigManager will be returned when no sim card present.
+            assertEquals(config.getBoolean(CarrierConfigManager.KEY_ADDITIONAL_CALL_SETTING_BOOL),
+                         true);
+        }
+    }
+
+    public void testGetConfig() {
+        PersistableBundle config = mConfigManager.getConfig();
+        checkConfig(config);
+    }
+
+    public void testGetConfigForSubId() {
+        PersistableBundle config =
+                mConfigManager.getConfigForSubId(SubscriptionManager.getDefaultSubId());
+        checkConfig(config);
+    }
+
+    /**
+     * Tests the CarrierConfigManager.notifyConfigChangedForSubId() API. This makes a call to
+     * notifyConfigChangedForSubId() API and expects a SecurityException since the test apk is not signed
+     * by certificate on the SIM.
+     */
+    public void testNotifyConfigChangedForSubId() {
+        try {
+            if (isSimCardPresent()) {
+                mConfigManager.notifyConfigChangedForSubId(SubscriptionManager.getDefaultSubId());
+                fail("Expected SecurityException. App doesn't have carrier privileges.");
+            }
+        } catch (SecurityException expected) {
+        }
+    }
+
+}