diff options
Diffstat (limited to 'tests')
4 files changed, 467 insertions, 44 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java index 8835d75b8..f0758934a 100644 --- a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareDataPathStateManagerTest.java @@ -55,8 +55,10 @@ import android.net.wifi.aware.WifiAwareNetworkSpecifier; import android.net.wifi.aware.WifiAwareSession; import android.os.Handler; import android.os.INetworkManagementService; +import android.os.IPowerManager; import android.os.Message; import android.os.Messenger; +import android.os.PowerManager; import android.os.Process; import android.os.test.TestLooper; import android.test.suitebuilder.annotation.SmallTest; @@ -97,6 +99,7 @@ public class WifiAwareDataPathStateManagerTest { @Mock private IWifiAwareEventCallback mMockCallback; @Mock IWifiAwareDiscoverySessionCallback mMockSessionCallback; TestAlarmManager mAlarmManager; + private PowerManager mMockPowerManager; @Rule public ErrorCollector collector = new ErrorCollector(); @@ -112,11 +115,18 @@ public class WifiAwareDataPathStateManagerTest { when(mMockContext.getSystemService(Context.ALARM_SERVICE)) .thenReturn(mAlarmManager.getAlarmManager()); - when(mMockContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mMockCm); - mMockLooper = new TestLooper(); mMockLooperHandler = new Handler(mMockLooper.getLooper()); + IPowerManager powerManagerService = mock(IPowerManager.class); + mMockPowerManager = new PowerManager(mMockContext, powerManagerService, + new Handler(mMockLooper.getLooper())); + + when(mMockContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mMockCm); + when(mMockContext.getSystemServiceName(PowerManager.class)).thenReturn( + Context.POWER_SERVICE); + when(mMockContext.getSystemService(PowerManager.class)).thenReturn(mMockPowerManager); + mDut = new WifiAwareStateManager(); mDut.setNative(mMockNative); mDut.start(mMockContext, mMockLooper.getLooper()); @@ -124,7 +134,7 @@ public class WifiAwareDataPathStateManagerTest { when(mMockNative.getCapabilities(anyShort())).thenReturn(true); when(mMockNative.enableAndConfigure(anyShort(), any(), anyBoolean(), - anyBoolean())).thenReturn(true); + anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(true); when(mMockNative.disable(anyShort())).thenReturn(true); when(mMockNative.publish(anyShort(), anyInt(), any())).thenReturn(true); when(mMockNative.subscribe(anyShort(), anyInt(), any())) @@ -145,6 +155,9 @@ public class WifiAwareDataPathStateManagerTest { when(mMockNetworkInterface.configureAgentProperties(any(), any(), anyInt(), any(), any(), any())).thenReturn(true); + when(mMockPowerManager.isDeviceIdleMode()).thenReturn(false); + when(mMockPowerManager.isInteractive()).thenReturn(true); + installDataPathStateManagerMocks(); } @@ -884,7 +897,7 @@ public class WifiAwareDataPathStateManagerTest { false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mMockCallback).onConnectSuccess(clientId); diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareNativeApiTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareNativeApiTest.java new file mode 100644 index 000000000..956418997 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareNativeApiTest.java @@ -0,0 +1,218 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.aware; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyShort; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.hardware.wifi.V1_0.IWifiNanIface; +import android.hardware.wifi.V1_0.NanBandIndex; +import android.hardware.wifi.V1_0.NanConfigRequest; +import android.hardware.wifi.V1_0.NanEnableRequest; +import android.hardware.wifi.V1_0.WifiStatus; +import android.hardware.wifi.V1_0.WifiStatusCode; +import android.net.wifi.aware.ConfigRequest; +import android.os.RemoteException; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ErrorCollector; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.PrintWriter; + +/** + * Unit test harness for WifiAwareNativeApi + */ +public class WifiAwareNativeApiTest { + @Mock WifiAwareNativeManager mWifiAwareNativeManagerMock; + @Mock IWifiNanIface mIWifiNanIfaceMock; + + @Rule public ErrorCollector collector = new ErrorCollector(); + + private WifiAwareNativeApi mDut; + + /** + * Initializes mocks. + */ + @Before + public void setup() throws Exception { + MockitoAnnotations.initMocks(this); + + when(mWifiAwareNativeManagerMock.getWifiNanIface()).thenReturn(mIWifiNanIfaceMock); + + WifiStatus status = new WifiStatus(); + status.code = WifiStatusCode.SUCCESS; + when(mIWifiNanIfaceMock.enableRequest(anyShort(), any())).thenReturn(status); + when(mIWifiNanIfaceMock.configRequest(anyShort(), any())).thenReturn(status); + + mDut = new WifiAwareNativeApi(mWifiAwareNativeManagerMock); + } + + /** + * Test that the set parameter shell command executor works when parameters are valid. + */ + @Test + public void testSetParameterShellCommandSuccess() { + setSettableParam(WifiAwareNativeApi.PARAM_DW_ON_IDLE_5GHZ, Integer.toString(1), true); + } + + /** + * Test that the set parameter shell command executor fails on incorrect name. + */ + @Test + public void testSetParameterShellCommandInvalidParameterName() { + setSettableParam("XXX", Integer.toString(1), false); + } + + /** + * Test that the set parameter shell command executor fails on invalid value (not convertible + * to an int). + */ + @Test + public void testSetParameterShellCommandInvalidValue() { + setSettableParam(WifiAwareNativeApi.PARAM_DW_ON_IDLE_5GHZ, "garbage", false); + } + + /** + * Validate that the configuration parameters used to manage power state behavior is + * using default values at the default power state. + */ + @Test + public void testEnableAndConfigPowerSettingsDefaults() throws RemoteException { + NanConfigRequest config = validateEnableAndConfigure((short) 10, + new ConfigRequest.Builder().build(), true, true, true, false); + + collector.checkThat("validDiscoveryWindowIntervalVal-5", false, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_5GHZ] + .validDiscoveryWindowIntervalVal)); + collector.checkThat("validDiscoveryWindowIntervalVal-24", false, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_24GHZ] + .validDiscoveryWindowIntervalVal)); + } + + /** + * Validate that the configuration parameters used to manage power state behavior is + * using the specified non-interactive values when in that power state. + */ + @Test + public void testEnableAndConfigPowerSettingsNoneInteractive() throws RemoteException { + byte interactive5 = 2; + byte interactive24 = 3; + + setPowerConfigurationParams(interactive5, interactive24, (byte) -1, (byte) -1); + NanConfigRequest config = validateEnableAndConfigure((short) 10, + new ConfigRequest.Builder().build(), false, false, false, false); + + collector.checkThat("validDiscoveryWindowIntervalVal-5", true, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_5GHZ] + .validDiscoveryWindowIntervalVal)); + collector.checkThat("discoveryWindowIntervalVal-5", interactive5, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_5GHZ] + .discoveryWindowIntervalVal)); + collector.checkThat("validDiscoveryWindowIntervalVal-24", true, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_24GHZ] + .validDiscoveryWindowIntervalVal)); + collector.checkThat("discoveryWindowIntervalVal-24", interactive24, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_24GHZ] + .discoveryWindowIntervalVal)); + } + + /** + * Validate that the configuration parameters used to manage power state behavior is + * using the specified idle (doze) values when in that power state. + */ + @Test + public void testEnableAndConfigPowerSettingsIdle() throws RemoteException { + byte idle5 = 2; + byte idle24 = -1; + + setPowerConfigurationParams((byte) -1, (byte) -1, idle5, idle24); + NanConfigRequest config = validateEnableAndConfigure((short) 10, + new ConfigRequest.Builder().build(), false, true, false, true); + + collector.checkThat("validDiscoveryWindowIntervalVal-5", true, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_5GHZ] + .validDiscoveryWindowIntervalVal)); + collector.checkThat("discoveryWindowIntervalVal-5", idle5, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_5GHZ] + .discoveryWindowIntervalVal)); + collector.checkThat("validDiscoveryWindowIntervalVal-24", false, + equalTo(config.bandSpecificConfig[NanBandIndex.NAN_BAND_24GHZ] + .validDiscoveryWindowIntervalVal)); + } + + // utilities + + private void setPowerConfigurationParams(byte interactive5, byte interactive24, byte idle5, + byte idle24) { + setSettableParam(WifiAwareNativeApi.PARAM_DW_ON_INACTIVE_5GHZ, + Integer.toString(interactive5), true); + setSettableParam(WifiAwareNativeApi.PARAM_DW_ON_INACTIVE_24GHZ, + Integer.toString(interactive24), true); + setSettableParam(WifiAwareNativeApi.PARAM_DW_ON_IDLE_5GHZ, Integer.toString(idle5), true); + setSettableParam(WifiAwareNativeApi.PARAM_DW_ON_IDLE_24GHZ, Integer.toString(idle24), true); + } + + private void setSettableParam(String name, String value, boolean expectSuccess) { + PrintWriter pwMock = mock(PrintWriter.class); + WifiAwareShellCommand parentShellMock = mock(WifiAwareShellCommand.class); + when(parentShellMock.getNextArgRequired()).thenReturn("set").thenReturn(name).thenReturn( + value); + when(parentShellMock.getErrPrintWriter()).thenReturn(pwMock); + + collector.checkThat(mDut.onCommand(parentShellMock), equalTo(expectSuccess ? 0 : -1)); + } + + private NanConfigRequest validateEnableAndConfigure(short transactionId, + ConfigRequest configRequest, boolean notifyIdentityChange, boolean initialConfiguration, + boolean isInteractive, boolean isIdle) throws RemoteException { + mDut.enableAndConfigure(transactionId, configRequest, notifyIdentityChange, + initialConfiguration, isInteractive, isIdle); + + ArgumentCaptor<NanEnableRequest> enableReqCaptor = ArgumentCaptor.forClass( + NanEnableRequest.class); + ArgumentCaptor<NanConfigRequest> configReqCaptor = ArgumentCaptor.forClass( + NanConfigRequest.class); + NanConfigRequest config; + + if (initialConfiguration) { + verify(mIWifiNanIfaceMock).enableRequest(eq(transactionId), enableReqCaptor.capture()); + config = enableReqCaptor.getValue().configParams; + } else { + verify(mIWifiNanIfaceMock).configRequest(eq(transactionId), configReqCaptor.capture()); + config = configReqCaptor.getValue(); + } + + collector.checkThat("disableDiscoveryAddressChangeIndication", !notifyIdentityChange, + equalTo(config.disableDiscoveryAddressChangeIndication)); + collector.checkThat("disableStartedClusterIndication", !notifyIdentityChange, + equalTo(config.disableStartedClusterIndication)); + collector.checkThat("disableJoinedClusterIndication", !notifyIdentityChange, + equalTo(config.disableJoinedClusterIndication)); + + return config; + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java index b5e12d29c..1ab6898f5 100644 --- a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java @@ -70,6 +70,8 @@ public class WifiAwareServiceImplTest { @Mock private WifiAwareStateManager mAwareStateManagerMock; @Mock + private WifiAwareShellCommand mWifiAwareShellCommandMock; + @Mock private IBinder mBinderMock; @Mock private IWifiAwareEventCallback mCallbackMock; @@ -114,7 +116,7 @@ public class WifiAwareServiceImplTest { mDut = new WifiAwareServiceImplSpy(mContextMock); mDut.fakeUid = mDefaultUid; - mDut.start(mHandlerThreadMock, mAwareStateManagerMock); + mDut.start(mHandlerThreadMock, mAwareStateManagerMock, mWifiAwareShellCommandMock); verify(mAwareStateManagerMock).start(eq(mContextMock), any()); } diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareStateManagerTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareStateManagerTest.java index fd4d5c794..a8d310a4b 100644 --- a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareStateManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareStateManagerTest.java @@ -38,8 +38,10 @@ import android.Manifest; import android.app.AppOpsManager; import android.app.test.MockAnswerUtil; import android.app.test.TestAlarmManager; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.pm.PackageManager; import android.hardware.wifi.V1_0.NanStatusType; import android.net.ConnectivityManager; @@ -50,7 +52,10 @@ import android.net.wifi.aware.IWifiAwareEventCallback; import android.net.wifi.aware.PublishConfig; import android.net.wifi.aware.SubscribeConfig; import android.net.wifi.aware.WifiAwareManager; +import android.os.Handler; +import android.os.IPowerManager; import android.os.Message; +import android.os.PowerManager; import android.os.UserHandle; import android.os.test.TestLooper; import android.test.suitebuilder.annotation.SmallTest; @@ -86,11 +91,14 @@ public class WifiAwareStateManagerTest { private TestLooper mMockLooper; private Random mRandomNg = new Random(15687); private WifiAwareStateManager mDut; + @Mock private WifiAwareNativeManager mMockNativeManager; @Mock private WifiAwareNativeApi mMockNative; @Mock private Context mMockContext; @Mock private AppOpsManager mMockAppOpsManager; @Mock private WifiAwareRttStateManager mMockAwareRttStateManager; TestAlarmManager mAlarmManager; + private PowerManager mMockPowerManager; + private BroadcastReceiver mPowerBcastReceiver; @Mock private WifiAwareDataPathStateManager mMockAwareDataPathStatemanager; @Rule @@ -109,9 +117,18 @@ public class WifiAwareStateManagerTest { when(mMockContext.getSystemService(Context.ALARM_SERVICE)) .thenReturn(mAlarmManager.getAlarmManager()); + mMockLooper = new TestLooper(); + + IPowerManager powerManagerService = mock(IPowerManager.class); + mMockPowerManager = new PowerManager(mMockContext, powerManagerService, + new Handler(mMockLooper.getLooper())); + when(mMockContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn( mock(ConnectivityManager.class)); when(mMockContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mMockAppOpsManager); + when(mMockContext.getSystemServiceName(PowerManager.class)).thenReturn( + Context.POWER_SERVICE); + when(mMockContext.getSystemService(PowerManager.class)).thenReturn(mMockPowerManager); when(mMockContext.checkPermission(eq(android.Manifest.permission.ACCESS_FINE_LOCATION), anyInt(), anyInt())).thenReturn(PackageManager.PERMISSION_DENIED); when(mMockContext.checkPermission(eq(Manifest.permission.ACCESS_COARSE_LOCATION), @@ -120,16 +137,20 @@ public class WifiAwareStateManagerTest { any())).thenReturn(AppOpsManager.MODE_ERRORED); when(mMockAppOpsManager.noteOp(eq(AppOpsManager.OP_COARSE_LOCATION), anyInt(), any())).thenReturn(AppOpsManager.MODE_ERRORED); + when(mMockPowerManager.isDeviceIdleMode()).thenReturn(false); + when(mMockPowerManager.isInteractive()).thenReturn(true); - mMockLooper = new TestLooper(); - + ArgumentCaptor<BroadcastReceiver> bcastRxCaptor = ArgumentCaptor.forClass( + BroadcastReceiver.class); mDut = new WifiAwareStateManager(); mDut.setNative(mMockNative); mDut.start(mMockContext, mMockLooper.getLooper()); + verify(mMockContext).registerReceiver(bcastRxCaptor.capture(), any(IntentFilter.class)); + mPowerBcastReceiver = bcastRxCaptor.getValue(); installMocksInStateManager(mDut, mMockAwareRttStateManager, mMockAwareDataPathStatemanager); when(mMockNative.enableAndConfigure(anyShort(), any(), anyBoolean(), - anyBoolean())).thenReturn(true); + anyBoolean(), anyBoolean(), anyBoolean())).thenReturn(true); when(mMockNative.disable(anyShort())).thenReturn(true); when(mMockNative.publish(anyShort(), anyInt(), any())).thenReturn(true); when(mMockNative.subscribe(anyShort(), anyInt(), any())) @@ -142,6 +163,32 @@ public class WifiAwareStateManagerTest { } /** + * Test that the set parameter shell command executor works when parameters are valid. + */ + @Test + public void testSetParameterShellCommandSuccess() { + setSettableParam(WifiAwareStateManager.PARAM_ON_IDLE_DISABLE_AWARE, Integer.toString(1), + true); + } + + /** + * Test that the set parameter shell command executor fails on incorrect name. + */ + @Test + public void testSetParameterShellCommandInvalidParameterName() { + setSettableParam("XXX", Integer.toString(1), false); + } + + /** + * Test that the set parameter shell command executor fails on invalid value (not convertible + * to an int). + */ + @Test + public void testSetParameterShellCommandInvalidValue() { + setSettableParam(WifiAwareStateManager.PARAM_ON_IDLE_DISABLE_AWARE, "garbage", false); + } + + /** * Validate that Aware data-path interfaces are brought up and down correctly. */ @Test @@ -243,7 +290,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -276,7 +323,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -300,7 +347,7 @@ public class WifiAwareStateManagerTest { InOrder inOrder = inOrder(mMockContext, mMockNative, mockCallback); when(mMockNative.enableAndConfigure(anyShort(), any(), anyBoolean(), - anyBoolean())).thenReturn(false); + anyBoolean(), eq(true), eq(false))).thenReturn(false); // (1) check initial state mDut.enableUsage(); @@ -314,7 +361,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); inOrder.verify(mockCallback).onConnectFail(NanStatusType.INTERNAL_FAILURE); validateInternalClientInfoCleanedUp(clientId); @@ -358,7 +405,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId1, uid, pid, callingPackage, mockCallback1, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionIdCapture.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); short transactionId = transactionIdCapture.getValue(); mDut.onConfigSuccessResponse(transactionId); mMockLooper.dispatchAll(); @@ -367,7 +414,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId2, uid, pid, callingPackage, mockCallback2, configRequest, true); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionIdCapture.capture(), - eq(configRequest), eq(true), eq(false)); + eq(configRequest), eq(true), eq(false), eq(true), eq(false)); transactionId = transactionIdCapture.getValue(); mDut.onConfigSuccessResponse(transactionId); mMockLooper.dispatchAll(); @@ -443,7 +490,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -499,7 +546,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -563,7 +610,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -637,7 +684,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -719,7 +766,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -777,7 +824,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -842,7 +889,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -915,7 +962,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -998,7 +1045,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1070,7 +1117,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1176,7 +1223,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1270,7 +1317,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1353,7 +1400,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1420,7 +1467,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1544,7 +1591,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1629,7 +1676,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1715,7 +1762,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1811,7 +1858,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -1941,7 +1988,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2154,7 +2201,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2245,7 +2292,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId1, uid, pid, callingPackage, mockCallback1, configRequest1, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - crCapture.capture(), eq(false), eq(true)); + crCapture.capture(), eq(false), eq(true), eq(true), eq(false)); collector.checkThat("merge: stage 1", crCapture.getValue(), equalTo(configRequest1)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); @@ -2261,7 +2308,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId3, uid, pid, callingPackage, mockCallback3, configRequest3, true); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - crCapture.capture(), eq(true), eq(false)); + crCapture.capture(), eq(true), eq(false), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback3).onConnectSuccess(clientId3); @@ -2282,7 +2329,7 @@ public class WifiAwareStateManagerTest { mMockLooper.dispatchAll(); validateInternalClientInfoCleanedUp(clientId3); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - crCapture.capture(), eq(false), eq(false)); + crCapture.capture(), eq(false), eq(false), eq(true), eq(false)); collector.checkThat("configRequest1", configRequest1, equalTo(crCapture.getValue())); @@ -2337,7 +2384,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2411,7 +2458,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2454,7 +2501,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); verifyNoMoreInteractions(mMockNative, mockCallback, mockSessionCallback); } @@ -2488,7 +2535,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); short transactionIdConfig = transactionId.getValue(); mDut.onConfigSuccessResponse(transactionIdConfig); mMockLooper.dispatchAll(); @@ -2544,7 +2591,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), eq(configRequest), - eq(false), eq(true)); + eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2598,7 +2645,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2650,7 +2697,7 @@ public class WifiAwareStateManagerTest { mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); mMockLooper.dispatchAll(); inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), - eq(configRequest), eq(false), eq(true)); + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); mDut.onConfigSuccessResponse(transactionId.getValue()); mMockLooper.dispatchAll(); inOrder.verify(mockCallback).onConnectSuccess(clientId); @@ -2674,6 +2721,116 @@ public class WifiAwareStateManagerTest { } } + /** + * Validate configuration changes on power state changes when Aware is not disabled on doze. + */ + @Test + public void testConfigOnPowerStateChanges() throws Exception { + final int clientId = 188; + final int uid = 1000; + final int pid = 2000; + final String callingPackage = "com.google.somePackage"; + + ConfigRequest configRequest = new ConfigRequest.Builder().build(); + + ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class); + IWifiAwareEventCallback mockCallback = mock(IWifiAwareEventCallback.class); + InOrder inOrder = inOrder(mMockNative, mockCallback); + + mDut.enableUsage(); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).getCapabilities(transactionId.capture()); + mDut.onCapabilitiesUpdateResponse(transactionId.getValue(), getCapabilities()); + mMockLooper.dispatchAll(); + + // (1) connect + mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); + mDut.onConfigSuccessResponse(transactionId.getValue()); + mMockLooper.dispatchAll(); + inOrder.verify(mockCallback).onConnectSuccess(clientId); + + // (2) power state change: SCREEN OFF + simulatePowerStateChangeInteractive(false); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), + eq(configRequest), eq(false), eq(false), eq(false), eq(false)); + mDut.onConfigSuccessResponse(transactionId.getValue()); + mMockLooper.dispatchAll(); + + // (3) power state change: DOZE + simulatePowerStateChangeDoze(true); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), + eq(configRequest), eq(false), eq(false), eq(false), eq(true)); + mDut.onConfigSuccessResponse(transactionId.getValue()); + mMockLooper.dispatchAll(); + + // (4) restore power state to default + simulatePowerStateChangeInteractive(true); // effectively treated as no-doze + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), + eq(configRequest), eq(false), eq(false), eq(true), eq(true)); + mDut.onConfigSuccessResponse(transactionId.getValue()); + mMockLooper.dispatchAll(); + + verifyNoMoreInteractions(mMockNative, mockCallback); + } + + /** + * Validate aware enable/disable during doze transitions. + */ + @Test + public void testEnableDisableOnDoze() throws Exception { + final int clientId = 188; + final int uid = 1000; + final int pid = 2000; + final String callingPackage = "com.google.somePackage"; + + setSettableParam(WifiAwareStateManager.PARAM_ON_IDLE_DISABLE_AWARE, Integer.toString(1), + true); + + ConfigRequest configRequest = new ConfigRequest.Builder().build(); + + ArgumentCaptor<Short> transactionId = ArgumentCaptor.forClass(Short.class); + IWifiAwareEventCallback mockCallback = mock(IWifiAwareEventCallback.class); + InOrder inOrder = inOrder(mMockContext, mMockNativeManager, mMockNative, mockCallback); + + mDut.enableUsage(); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).getCapabilities(transactionId.capture()); + mDut.onCapabilitiesUpdateResponse(transactionId.getValue(), getCapabilities()); + mMockLooper.dispatchAll(); + + // (1) connect + mDut.connect(clientId, uid, pid, callingPackage, mockCallback, configRequest, false); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).enableAndConfigure(transactionId.capture(), + eq(configRequest), eq(false), eq(true), eq(true), eq(false)); + mDut.onConfigSuccessResponse(transactionId.getValue()); + mMockLooper.dispatchAll(); + inOrder.verify(mockCallback).onConnectSuccess(clientId); + + // (3) power state change: DOZE + simulatePowerStateChangeDoze(true); + mMockLooper.dispatchAll(); + inOrder.verify(mMockNative).disable((short) 0); + validateCorrectAwareStatusChangeBroadcast(inOrder, false); + + // (4) power state change: SCREEN ON (but DOZE still on - fakish but expect no changes) + simulatePowerStateChangeInteractive(false); + mMockLooper.dispatchAll(); + + // (5) power state change: DOZE OFF + simulatePowerStateChangeDoze(false); + mMockLooper.dispatchAll(); + validateCorrectAwareStatusChangeBroadcast(inOrder, true); + + verifyNoMoreInteractions(mMockNativeManager, mMockNative, mockCallback); + } + /* * Tests of internal state of WifiAwareStateManager: very limited (not usually * a good idea). However, these test that the internal state is cleaned-up @@ -2751,6 +2908,16 @@ public class WifiAwareStateManagerTest { /* * Utilities */ + private void setSettableParam(String name, String value, boolean expectSuccess) { + PrintWriter pwMock = mock(PrintWriter.class); + WifiAwareShellCommand parentShellMock = mock(WifiAwareShellCommand.class); + when(parentShellMock.getNextArgRequired()).thenReturn("set").thenReturn(name).thenReturn( + value); + when(parentShellMock.getErrPrintWriter()).thenReturn(pwMock); + + collector.checkThat(mDut.onCommand(parentShellMock), equalTo(expectSuccess ? 0 : -1)); + } + private void dumpDut(String prefix) { StringWriter sw = new StringWriter(); mDut.dump(null, new PrintWriter(sw), null); @@ -2825,6 +2992,29 @@ public class WifiAwareStateManagerTest { } } + /** + * Simulate power state change due to doze. Changes the power manager return values and + * dispatches a broadcast. + */ + private void simulatePowerStateChangeDoze(boolean isDozeOn) { + when(mMockPowerManager.isDeviceIdleMode()).thenReturn(isDozeOn); + + Intent intent = new Intent(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); + mPowerBcastReceiver.onReceive(mMockContext, intent); + } + + /** + * Simulate power state change due to interactive mode change (screen on/off). Changes the power + * manager return values and dispatches a broadcast. + */ + private void simulatePowerStateChangeInteractive(boolean isInteractive) { + when(mMockPowerManager.isInteractive()).thenReturn(isInteractive); + + Intent intent = new Intent( + isInteractive ? Intent.ACTION_SCREEN_ON : Intent.ACTION_SCREEN_OFF); + mPowerBcastReceiver.onReceive(mMockContext, intent); + } + private static Capabilities getCapabilities() { Capabilities cap = new Capabilities(); cap.maxConcurrentAwareClusters = 1; |