From a30a1edf9f5a14ff9d87ef2a0463d4ff8f1949a1 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Tue, 15 Aug 2017 13:29:50 -0700 Subject: [PATCH] Add location permission to netpolicy test app. As part of the test, wifi ssid needs to be extracted which is guarded by ACCESS_COARSE_LOCATION permission. Bug: 64274313 Test: cts-tradefed run singleCommand cts-dev --module CtsHostsideNetworkTests -t \ com.android.cts.net.HostsideRestrictBackgroundNetworkTests Change-Id: I5c23b11dc1bf26a672bc1454ff1e2594935d43d8 --- tests/cts/hostside/app/AndroidManifest.xml | 2 ++ ...ractRestrictBackgroundNetworkTestCase.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/tests/cts/hostside/app/AndroidManifest.xml b/tests/cts/hostside/app/AndroidManifest.xml index 0598a3b02a..7466cb88dd 100644 --- a/tests/cts/hostside/app/AndroidManifest.xml +++ b/tests/cts/hostside/app/AndroidManifest.xml @@ -21,6 +21,8 @@ + + diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index f3d5d2c4b2..35e84c5f19 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -42,6 +42,7 @@ import android.os.BatteryManager; import android.os.Binder; import android.os.Bundle; import android.os.SystemClock; +import android.provider.Settings; import android.service.notification.NotificationListenerService; import android.test.InstrumentationTestCase; import android.text.TextUtils; @@ -116,6 +117,7 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation private MyServiceClient mServiceClient; private String mDeviceIdleConstantsSetting; private boolean mSupported; + private boolean mIsLocationOn; @Override protected void setUp() throws Exception { @@ -130,6 +132,10 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation mServiceClient = new MyServiceClient(mContext); mServiceClient.bind(); mDeviceIdleConstantsSetting = "device_idle_constants"; + mIsLocationOn = isLocationOn(); + if (!mIsLocationOn) { + enableLocation(); + } mSupported = setUpActiveNetworkMeteringState(); Log.i(TAG, "Apps status on " + getName() + ":\n" @@ -139,11 +145,35 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation @Override protected void tearDown() throws Exception { + if (!mIsLocationOn) { + disableLocation(); + } mServiceClient.unbind(); super.tearDown(); } + private void enableLocation() throws Exception { + Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE, + Settings.Secure.LOCATION_MODE_SENSORS_ONLY); + assertEquals(Settings.Secure.LOCATION_MODE_SENSORS_ONLY, + Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.LOCATION_MODE)); + } + + private void disableLocation() throws Exception { + Settings.Secure.putInt(mContext.getContentResolver(), Settings.Secure.LOCATION_MODE, + Settings.Secure.LOCATION_MODE_OFF); + assertEquals(Settings.Secure.LOCATION_MODE_OFF, + Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.LOCATION_MODE)); + } + + private boolean isLocationOn() throws Exception { + return Settings.Secure.getInt(mContext.getContentResolver(), + Settings.Secure.LOCATION_MODE) != Settings.Secure.LOCATION_MODE_OFF; + } + protected int getUid(String packageName) throws Exception { return mContext.getPackageManager().getPackageUid(packageName, 0); }