From 52a1c457bebd6392a2514148c49d0e47284f8f41 Mon Sep 17 00:00:00 2001 From: David Su Date: Fri, 29 Nov 2019 00:15:11 -0800 Subject: Formalize WifiManager#requestActivityInfo as @SystemApi Rename WifiManager#requestActivityInfo to getWifiActivityEnergyInfoAsync and convert ResultReceiver to a formal listener interface. Bug: 145244073 Test: atest FrameworksWifiTests Change-Id: Ib08aa1133fbe857eb12a3a433ac94814b717443d --- .../android/server/wifi/WifiServiceImplTest.java | 114 ++++++++++++++++++++- 1 file changed, 109 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index 38060843d..338e1c0a8 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static android.Manifest.permission.ACCESS_WIFI_STATE; import static android.net.wifi.WifiManager.DEVICE_MOBILITY_STATE_STATIONARY; import static android.net.wifi.WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR; import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; @@ -94,6 +95,7 @@ import android.net.wifi.IActionListener; import android.net.wifi.IDppCallback; import android.net.wifi.ILocalOnlyHotspotCallback; import android.net.wifi.INetworkRequestMatchCallback; +import android.net.wifi.IOnWifiActivityEnergyInfoListener; import android.net.wifi.IOnWifiUsabilityStatsListener; import android.net.wifi.IScanResultsCallback; import android.net.wifi.ISoftApCallback; @@ -103,6 +105,7 @@ import android.net.wifi.ITxPacketCountListener; import android.net.wifi.ScanResult; import android.net.wifi.SoftApConfiguration; import android.net.wifi.SoftApInfo; +import android.net.wifi.WifiActivityEnergyInfo; import android.net.wifi.WifiClient; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration.KeyMgmt; @@ -282,6 +285,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock ILocalOnlyHotspotCallback mLohsCallback; @Mock IScanResultsCallback mScanResultsCallback; @Mock ISuggestionConnectionStatusListener mSuggestionConnectionStatusListener; + @Mock IOnWifiActivityEnergyInfoListener mOnWifiActivityEnergyInfoListener; WifiLog mLog = new LogcatLog(TAG); @@ -1146,7 +1150,7 @@ public class WifiServiceImplTest extends WifiBaseTest { public void testGetWifiApEnabledPermissionDenied() { // we should not be able to get the state doThrow(new SecurityException()).when(mContext) - .enforceCallingOrSelfPermission(eq(android.Manifest.permission.ACCESS_WIFI_STATE), + .enforceCallingOrSelfPermission(eq(ACCESS_WIFI_STATE), eq("WifiService")); try { @@ -4556,7 +4560,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testRegisterScanResultCallbackWithMissingPermission() throws Exception { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.registerScanResultsCallback(mScanResultsCallback); } @@ -4566,7 +4570,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testUnregisterScanResultCallbackWithMissingPermission() throws Exception { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.unregisterScanResultsCallback(mScanResultsCallback); } @@ -4597,7 +4601,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testRegisterSuggestionNetworkCallbackWithMissingPermission() { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.registerSuggestionConnectionStatusListener(mAppBinder, mSuggestionConnectionStatusListener, NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME, TEST_FEATURE_ID); @@ -4618,7 +4622,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Test(expected = SecurityException.class) public void testUnregisterSuggestionNetworkCallbackWithMissingPermission() { doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( - eq(android.Manifest.permission.ACCESS_WIFI_STATE), eq("WifiService")); + eq(ACCESS_WIFI_STATE), eq("WifiService")); mWifiServiceImpl.unregisterSuggestionConnectionStatusListener( NETWORK_CALLBACK_ID, TEST_PACKAGE_NAME); } @@ -4655,4 +4659,104 @@ public class WifiServiceImplTest extends WifiBaseTest { mWifiServiceImpl.acquireWifiLock(mAppBinder, wifiLockModeInvalid, "", null); } + + /** + * Tests that {@link WifiServiceImpl#reportActivityInfo()} throws {@link SecurityException} if + * the caller doesn't have the necessary permissions. + */ + @Test(expected = SecurityException.class) + public void reportActivityInfoNoPermission() throws Exception { + doThrow(SecurityException.class) + .when(mContext).enforceCallingOrSelfPermission(eq(ACCESS_WIFI_STATE), any()); + mWifiServiceImpl.reportActivityInfo(); + } + + /** + * Tests that {@link WifiServiceImpl#reportActivityInfo()} returns null if link layer stats is + * unsupported. + */ + @Test + public void reportActivityInfoFeatureUnsupported() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(0L); + assertNull(mWifiServiceImpl.reportActivityInfo()); + } + + private void setupReportActivityInfo() { + WifiLinkLayerStats stats = new WifiLinkLayerStats(); + stats.on_time = 1000; + stats.tx_time = 1; + stats.rx_time = 2; + stats.tx_time_per_level = new int[] {3, 4, 5}; + stats.on_time_scan = 6; + when(mClientModeImpl.syncGetLinkLayerStats(any())).thenReturn(stats); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_IDLE)) + .thenReturn(7.0); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_RX)) + .thenReturn(8.0); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_TX)) + .thenReturn(9.0); + when(mPowerProfile.getAveragePower(PowerProfile.POWER_WIFI_CONTROLLER_OPERATING_VOLTAGE)) + .thenReturn(10000.0); + when(mClock.getElapsedSinceBootMillis()).thenReturn(9999L); + } + + private void validateWifiActivityEnergyInfo(WifiActivityEnergyInfo info) { + assertNotNull(info); + assertEquals(9999L, info.getTimeSinceBootMillis()); + assertEquals(WifiActivityEnergyInfo.STACK_STATE_STATE_IDLE, info.getStackState()); + assertEquals(1, info.getControllerTxDurationMillis()); + assertEquals(2, info.getControllerRxDurationMillis()); + assertEquals(6, info.getControllerScanDurationMillis()); + assertEquals(997, info.getControllerIdleDurationMillis()); + assertEquals(70040, info.getControllerEnergyUsedMicroJoules()); + } + + /** + * Tests that {@link WifiServiceImpl#reportActivityInfo()} returns the expected values on + * success. + */ + @Test + public void reportActivityInfoSuccess() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(Long.MAX_VALUE); + setupReportActivityInfo(); + WifiActivityEnergyInfo info = mWifiServiceImpl.reportActivityInfo(); + validateWifiActivityEnergyInfo(info); + } + + /** + * Tests that {@link WifiServiceImpl#getWifiActivityEnergyInfoAsync} throws + * {@link SecurityException} if the caller doesn't have the necessary permissions. + */ + @Test(expected = SecurityException.class) + public void getWifiActivityEnergyInfoAsyncNoPermission() throws Exception { + doThrow(SecurityException.class) + .when(mContext).enforceCallingOrSelfPermission(eq(ACCESS_WIFI_STATE), any()); + mWifiServiceImpl.getWifiActivityEnergyInfoAsync(mOnWifiActivityEnergyInfoListener); + } + + /** + * Tests that {@link WifiServiceImpl#getWifiActivityEnergyInfoAsync} passes null to the listener + * if link layer stats is unsupported. + */ + @Test + public void getWifiActivityEnergyInfoAsyncFeatureUnsupported() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(0L); + mWifiServiceImpl.getWifiActivityEnergyInfoAsync(mOnWifiActivityEnergyInfoListener); + verify(mOnWifiActivityEnergyInfoListener).onWifiActivityEnergyInfo(null); + } + + /** + * Tests that {@link WifiServiceImpl#getWifiActivityEnergyInfoAsync} passes the expected values + * to the listener on success. + */ + @Test + public void getWifiActivityEnergyInfoAsyncSuccess() throws Exception { + when(mClientModeImpl.syncGetSupportedFeatures(any())).thenReturn(Long.MAX_VALUE); + setupReportActivityInfo(); + mWifiServiceImpl.getWifiActivityEnergyInfoAsync(mOnWifiActivityEnergyInfoListener); + ArgumentCaptor infoCaptor = + ArgumentCaptor.forClass(WifiActivityEnergyInfo.class); + verify(mOnWifiActivityEnergyInfoListener).onWifiActivityEnergyInfo(infoCaptor.capture()); + validateWifiActivityEnergyInfo(infoCaptor.getValue()); + } } -- cgit v1.2.3