From 73cf4e7594368d0bb2688867a7c055a58bf078db Mon Sep 17 00:00:00 2001 From: Roshan Pius Date: Tue, 24 Jul 2018 14:49:25 -0700 Subject: ScanRequestProxy: Add a setting to toggle wifi scan throttling Scan throttling affects apps which are used by testers for survey like application. Add a hidden setting which can be toggled via adb commands to turn off all scan throttling. Bug: 111763749 Test: Unit tests Test: `adb shell settings put global wifi_scan_throttle_enabled 0` on user build. Change-Id: Ie43550d7fad40d27e2440916b0efab19db4ff6a6 --- .../android/server/wifi/ScanRequestProxyTest.java | 45 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java b/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java index 4b262b07f..865501685 100644 --- a/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java @@ -28,11 +28,14 @@ import android.app.ActivityManager; import android.app.AppOpsManager; import android.content.Context; import android.content.Intent; +import android.database.ContentObserver; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.net.wifi.WifiScanner; +import android.os.Handler; import android.os.UserHandle; import android.os.WorkSource; +import android.provider.Settings; import android.support.test.filters.SmallTest; import com.android.server.wifi.util.WifiPermissionsUtil; @@ -72,12 +75,15 @@ public class ScanRequestProxyTest { @Mock private WifiPermissionsUtil mWifiPermissionsUtil; @Mock private WifiMetrics mWifiMetrics; @Mock private Clock mClock; + @Mock private FrameworkFacade mFrameworkFacade; private ArgumentCaptor mWorkSourceArgumentCaptor = ArgumentCaptor.forClass(WorkSource.class); private ArgumentCaptor mScanSettingsArgumentCaptor = ArgumentCaptor.forClass(WifiScanner.ScanSettings.class); private ArgumentCaptor mScanListenerArgumentCaptor = ArgumentCaptor.forClass(WifiScanner.ScanListener.class); + private ArgumentCaptor mThrottleEnabledSettingObservorCaptor = + ArgumentCaptor.forClass(ContentObserver.class); private WifiScanner.ScanData[] mTestScanDatas1; private WifiScanner.ScanData[] mTestScanDatas2; private InOrder mInOrder; @@ -99,9 +105,14 @@ public class ScanRequestProxyTest { mTestScanDatas1 = ScanTestUtil.createScanDatas(new int[][]{ { 2417, 2427, 5180, 5170 } }); mTestScanDatas2 = ScanTestUtil.createScanDatas(new int[][]{ { 2412, 2422, 5200, 5210 } }); + // Scan throttling is enabled by default. + when(mFrameworkFacade.getIntegerSetting( + eq(mContext), eq(Settings.Global.WIFI_SCAN_THROTTLE_ENABLED), anyInt())) + .thenReturn(1); mScanRequestProxy = new ScanRequestProxy(mContext, mAppOps, mActivityManager, mWifiInjector, - mWifiConfigManager, mWifiPermissionsUtil, mWifiMetrics, mClock); + mWifiConfigManager, mWifiPermissionsUtil, mWifiMetrics, mClock, + mFrameworkFacade, mock(Handler.class)); } @After @@ -115,7 +126,7 @@ public class ScanRequestProxyTest { */ @Test public void testEnableScanning() { - mScanRequestProxy.enableScanning(true, true); + mScanRequestProxy.enableScanning(true, false); verify(mWifiScanner).setScanningEnabled(true); validateScanAvailableBroadcastSent(true); } @@ -595,6 +606,36 @@ public class ScanRequestProxyTest { mInOrder.verify(mWifiScanner).startScan(any(), any(), any()); } + /** + * Ensure new scan requests from the same app are not throttled when the user turns + * off scan throttling. + */ + @Test + public void testSuccessiveScanRequestFromSameAppWhenThrottlingIsDisabledNotThrottled() { + // Triggers the scan throttle setting registration. + testEnableScanning(); + verify(mFrameworkFacade).registerContentObserver(any(), + eq(Settings.Global.getUriFor(Settings.Global.WIFI_SCAN_THROTTLE_ENABLED)), + anyBoolean(), mThrottleEnabledSettingObservorCaptor.capture()); + assertNotNull(mThrottleEnabledSettingObservorCaptor); + // Disable scan throttling & invoke the content observer callback. + when(mFrameworkFacade.getIntegerSetting( + eq(mContext), eq(Settings.Global.WIFI_SCAN_THROTTLE_ENABLED), anyInt())) + .thenReturn(0); + mThrottleEnabledSettingObservorCaptor.getValue().onChange(false); + + long firstRequestMs = 782; + when(mClock.getElapsedSinceBootMillis()).thenReturn(firstRequestMs); + for (int i = 0; i < SCAN_REQUEST_THROTTLE_MAX_IN_TIME_WINDOW_FG_APPS; i++) { + when(mClock.getElapsedSinceBootMillis()).thenReturn(firstRequestMs + i); + assertTrue(mScanRequestProxy.startScan(TEST_UID, TEST_PACKAGE_NAME_1)); + mInOrder.verify(mWifiScanner).startScan(any(), any(), any()); + } + // Make next scan request from the same package name & ensure that it is not throttled. + assertTrue(mScanRequestProxy.startScan(TEST_UID, TEST_PACKAGE_NAME_1)); + mInOrder.verify(mWifiScanner).startScan(any(), any(), any()); + } + /** * Ensure new scan requests from different apps are not throttled. */ -- cgit v1.2.3