Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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 android.telephony2.cts;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 import android.os.ParcelFileDescriptor;
     22 import android.telephony.TelephonyManager;
     23 import android.test.InstrumentationTestCase;
     24 import android.util.Log;
     25 
     26 import java.io.FileInputStream;
     27 import java.io.IOException;
     28 import java.io.InputStream;
     29 
     30 public class PhoneNumberTest extends InstrumentationTestCase {
     31     private static final String TAG = "PhoneNumberTest";
     32 
     33     private void setDefaultSmsApp(boolean setToSmsApp) {
     34         StringBuilder command = new StringBuilder();
     35         command.append("appops set ");
     36         command.append(getInstrumentation().getContext().getPackageName());
     37         command.append(" WRITE_SMS ");
     38         command.append(setToSmsApp ? "allow" : "default");
     39 
     40         ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation()
     41                 .executeShellCommand(command.toString());
     42 
     43         InputStream is = new FileInputStream(pfd.getFileDescriptor());
     44         try {
     45             final byte[] buffer = new byte[8192];
     46             while ((is.read(buffer)) != -1);
     47         } catch (IOException e) {
     48             Log.e(TAG, "Error managing default SMS app", e);
     49         }
     50     }
     51 
     52     public void testGetLine1Number() {
     53         Context context = getInstrumentation().getContext();
     54         PackageManager packageManager = context.getPackageManager();
     55         if (!packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
     56             return;
     57         }
     58 
     59         TelephonyManager tm = context.getSystemService(TelephonyManager.class);
     60 
     61         setDefaultSmsApp(true);
     62 
     63         // This shouldn't crash.
     64         tm.getLine1Number();
     65 
     66         setDefaultSmsApp(false);
     67 
     68         // This should throw now.
     69         try {
     70             tm.getLine1Number();
     71             fail("Should throw SecurityException");
     72         } catch (SecurityException expected) {
     73         }
     74     }
     75 }
     76