1 /* 2 * Copyright (C) 2018 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.permission.cts.sdk28; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.fail; 21 22 import android.content.Context; 23 import android.content.pm.PackageManager; 24 import android.telephony.NeighboringCellInfo; 25 import android.telephony.TelephonyManager; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.util.List; 35 36 @RunWith(AndroidJUnit4.class) 37 public class RequestLocation { 38 39 private TelephonyManager mTelephonyManager; 40 private boolean mHasTelephony; 41 42 @Before setUp()43 public void setUp() throws Exception { 44 mHasTelephony = getContext().getPackageManager().hasSystemFeature( 45 PackageManager.FEATURE_TELEPHONY); 46 mTelephonyManager = (TelephonyManager) getContext().getSystemService( 47 Context.TELEPHONY_SERVICE); 48 assertNotNull(mTelephonyManager); 49 } 50 51 /** 52 * Verify that a SecurityException is thrown when an app targeting SDK 28 53 * lacks the coarse location permission. 54 */ 55 @Test testGetNeighboringCellInfo()56 public void testGetNeighboringCellInfo() { 57 if (!mHasTelephony) return; 58 try { 59 List<NeighboringCellInfo> cellInfos = mTelephonyManager.getNeighboringCellInfo(); 60 if (cellInfos != null && !cellInfos.isEmpty()) { 61 fail("Meaningful information returned from getNeighboringCellInfo!"); 62 } 63 } catch (SecurityException expected) { 64 } 65 } 66 getContext()67 private static Context getContext() { 68 return InstrumentationRegistry.getContext(); 69 } 70 } 71