1 /* 2 * Copyright (C) 2020 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.net.wifi.cts; 18 19 import android.content.Context; 20 import android.location.LocationManager; 21 import android.os.Process; 22 import android.os.UserHandle; 23 24 import androidx.test.platform.app.InstrumentationRegistry; 25 26 import com.android.compatibility.common.util.ShellIdentityUtils; 27 28 import org.junit.AfterClass; 29 import org.junit.BeforeClass; 30 31 /** 32 * Base test for Wifi JUnit4 tests that enables/disables location 33 */ 34 public abstract class WifiJUnit4TestBase { 35 36 private static LocationManager sLocationManager; 37 private static boolean sWasLocationEnabledForTest = false; 38 39 @BeforeClass enableLocationIfNotEnabled()40 public static void enableLocationIfNotEnabled() throws Exception { 41 Context context = InstrumentationRegistry.getInstrumentation().getContext(); 42 43 sLocationManager = context.getSystemService(LocationManager.class); 44 if (!sLocationManager.isLocationEnabled()) { 45 // Turn on location if it isn't on already 46 ShellIdentityUtils.invokeWithShellPermissions( 47 () -> sLocationManager.setLocationEnabledForUser( 48 true, UserHandle.getUserHandleForUid(Process.myUid()))); 49 50 sWasLocationEnabledForTest = true; 51 } 52 } 53 54 @AfterClass disableLocationIfOriginallyDisabled()55 public static void disableLocationIfOriginallyDisabled() throws Exception { 56 if (sWasLocationEnabledForTest) { 57 ShellIdentityUtils.invokeWithShellPermissions(() -> 58 sLocationManager.setLocationEnabledForUser( 59 false, UserHandle.getUserHandleForUid(Process.myUid()))); 60 } 61 } 62 } 63