Loading tests/utils/testutils/java/com/android/internal/util/test/BidirectionalAsyncChannelServer.java +5 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ import com.android.internal.util.AsyncChannel; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Provides an interface for the server side implementation of a bidirectional channel as described Loading Loading @@ -83,4 +84,8 @@ public class BidirectionalAsyncChannelServer { return mMessenger; } public Set<Messenger> getClientMessengers() { return mClients.keySet(); } } wifi/tests/src/android/net/wifi/WifiManagerTest.java +201 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyList; Loading @@ -53,6 +54,7 @@ import static org.mockito.Mockito.when; import android.content.Context; import android.content.pm.ApplicationInfo; import android.net.DhcpInfo; import android.net.wifi.WifiManager.LocalOnlyHotspotCallback; import android.net.wifi.WifiManager.LocalOnlyHotspotObserver; import android.net.wifi.WifiManager.LocalOnlyHotspotReservation; Loading @@ -62,6 +64,7 @@ import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback; import android.net.wifi.WifiManager.OnWifiUsabilityStatsListener; import android.net.wifi.WifiManager.SoftApCallback; import android.net.wifi.WifiManager.TrafficStateCallback; import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.os.Message; Loading Loading @@ -92,6 +95,7 @@ public class WifiManagerTest { private static final int ERROR_NOT_SET = -1; private static final int ERROR_TEST_REASON = 5; private static final int TEST_UID = 14553; private static final int TEST_NETWORK_ID = 143; private static final String TEST_PACKAGE_NAME = "TestPackage"; private static final String TEST_COUNTRY_CODE = "US"; private static final String[] TEST_MAC_ADDRESSES = {"da:a1:19:0:0:0"}; Loading @@ -118,6 +122,7 @@ public class WifiManagerTest { MockitoAnnotations.initMocks(this); mLooper = new TestLooper(); mHandler = spy(new Handler(mLooper.getLooper())); mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.Q; when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME); Loading Loading @@ -1438,4 +1443,200 @@ i * Verify that a call to cancel WPS immediately returns a failure. .thenReturn(new Long(~WifiManager.WIFI_FEATURE_DPP)); assertFalse(mWifiManager.isEasyConnectSupported()); } /** * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)} * @throws Exception */ @Test public void testAddNetwork() throws Exception { WifiConfiguration configuration = new WifiConfiguration(); when(mWifiService.addOrUpdateNetwork(any(), anyString())) .thenReturn(TEST_NETWORK_ID); assertEquals(mWifiManager.addNetwork(configuration), TEST_NETWORK_ID); verify(mWifiService).addOrUpdateNetwork(configuration, mContext.getOpPackageName()); // send a null config assertEquals(mWifiManager.addNetwork(null), -1); } /** * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)} * @throws Exception */ @Test public void testUpdateNetwork() throws Exception { WifiConfiguration configuration = new WifiConfiguration(); when(mWifiService.addOrUpdateNetwork(any(), anyString())) .thenReturn(TEST_NETWORK_ID); configuration.networkId = TEST_NETWORK_ID; assertEquals(mWifiManager.updateNetwork(configuration), TEST_NETWORK_ID); verify(mWifiService).addOrUpdateNetwork(configuration, mContext.getOpPackageName()); // config with invalid network ID configuration.networkId = -1; assertEquals(mWifiManager.updateNetwork(configuration), -1); // send a null config assertEquals(mWifiManager.updateNetwork(null), -1); } /** * Test behavior of {@link WifiManager#enableNetwork(int, boolean)} * @throws Exception */ @Test public void testEnableNetwork() throws Exception { when(mWifiService.enableNetwork(anyInt(), anyBoolean(), anyString())) .thenReturn(true); assertTrue(mWifiManager.enableNetwork(TEST_NETWORK_ID, true)); verify(mWifiService).enableNetwork(TEST_NETWORK_ID, true, mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#disableNetwork(int)} * @throws Exception */ @Test public void testDisableNetwork() throws Exception { when(mWifiService.disableNetwork(anyInt(), anyString())) .thenReturn(true); assertTrue(mWifiManager.disableNetwork(TEST_NETWORK_ID)); verify(mWifiService).disableNetwork(TEST_NETWORK_ID, mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#disconnect()} * @throws Exception */ @Test public void testDisconnect() throws Exception { when(mWifiService.disconnect(anyString())).thenReturn(true); assertTrue(mWifiManager.disconnect()); verify(mWifiService).disconnect(mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#reconnect()} * @throws Exception */ @Test public void testReconnect() throws Exception { when(mWifiService.reconnect(anyString())).thenReturn(true); assertTrue(mWifiManager.reconnect()); verify(mWifiService).reconnect(mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#reassociate()} * @throws Exception */ @Test public void testReassociate() throws Exception { when(mWifiService.reassociate(anyString())).thenReturn(true); assertTrue(mWifiManager.reassociate()); verify(mWifiService).reassociate(mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#getSupportedFeatures()} * @throws Exception */ @Test public void testGetSupportedFeatures() throws Exception { long supportedFeatures = WifiManager.WIFI_FEATURE_SCANNER | WifiManager.WIFI_FEATURE_PASSPOINT | WifiManager.WIFI_FEATURE_P2P; when(mWifiService.getSupportedFeatures()) .thenReturn(Long.valueOf(supportedFeatures)); assertTrue(mWifiManager.isWifiScannerSupported()); assertTrue(mWifiManager.isPasspointSupported()); assertTrue(mWifiManager.isP2pSupported()); assertFalse(mWifiManager.isPortableHotspotSupported()); assertFalse(mWifiManager.is5GHzBandSupported()); assertFalse(mWifiManager.isDeviceToDeviceRttSupported()); assertFalse(mWifiManager.isDeviceToApRttSupported()); assertFalse(mWifiManager.isPreferredNetworkOffloadSupported()); assertFalse(mWifiManager.isAdditionalStaSupported()); assertFalse(mWifiManager.isTdlsSupported()); assertFalse(mWifiManager.isOffChannelTdlsSupported()); assertFalse(mWifiManager.isEnhancedPowerReportingSupported()); } /** * Test behavior of {@link WifiManager#getControllerActivityEnergyInfo()} * @throws Exception */ @Test public void testGetControllerActivityEnergyInfo() throws Exception { WifiActivityEnergyInfo activityEnergyInfo = new WifiActivityEnergyInfo(5, 3, 3, new long[]{5L, 5L, 5L}, 5, 5, 5, 5); when(mWifiService.reportActivityInfo()).thenReturn(activityEnergyInfo); assertEquals(activityEnergyInfo, mWifiManager.getControllerActivityEnergyInfo()); } /** * Test behavior of {@link WifiManager#getConnectionInfo()} * @throws Exception */ @Test public void testGetConnectionInfo() throws Exception { WifiInfo wifiInfo = new WifiInfo(); when(mWifiService.getConnectionInfo(anyString())).thenReturn(wifiInfo); assertEquals(wifiInfo, mWifiManager.getConnectionInfo()); } /** * Test behavior of {@link WifiManager#isDualModeSupported()} ()} * @throws Exception */ @Test public void testIsDualModeSupported() throws Exception { when(mWifiService.needs5GHzToAnyApBandConversion()).thenReturn(true); assertTrue(mWifiManager.isDualModeSupported()); verify(mWifiService).needs5GHzToAnyApBandConversion(); } /** * Test behavior of {@link WifiManager#isDualBandSupported()} * @throws Exception */ @Test public void testIsDualBandSupported() throws Exception { when(mWifiService.isDualBandSupported()).thenReturn(true); assertTrue(mWifiManager.isDualBandSupported()); verify(mWifiService).isDualBandSupported(); } /** * Test behavior of {@link WifiManager#getDhcpInfo()} * @throws Exception */ @Test public void testGetDhcpInfo() throws Exception { DhcpInfo dhcpInfo = new DhcpInfo(); when(mWifiService.getDhcpInfo()).thenReturn(dhcpInfo); assertEquals(dhcpInfo, mWifiManager.getDhcpInfo()); verify(mWifiService).getDhcpInfo(); } /** * Test behavior of {@link WifiManager#setWifiEnabled(boolean)} * @throws Exception */ @Test public void testSetWifiEnabled() throws Exception { when(mWifiService.setWifiEnabled(anyString(), anyBoolean())).thenReturn(true); assertTrue(mWifiManager.setWifiEnabled(true)); verify(mWifiService).setWifiEnabled(mContext.getOpPackageName(), true); assertTrue(mWifiManager.setWifiEnabled(false)); verify(mWifiService).setWifiEnabled(mContext.getOpPackageName(), false); } } wifi/tests/src/android/net/wifi/WifiScannerTest.java +218 −3 Original line number Diff line number Diff line Loading @@ -18,10 +18,16 @@ package android.net.wifi; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.validateMockitoUsage; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; Loading @@ -29,7 +35,10 @@ import android.net.wifi.WifiScanner.PnoSettings; import android.net.wifi.WifiScanner.PnoSettings.PnoNetwork; import android.net.wifi.WifiScanner.ScanData; import android.net.wifi.WifiScanner.ScanSettings; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Messenger; import android.os.Parcel; import android.os.test.TestLooper; Loading @@ -40,6 +49,7 @@ import com.android.internal.util.test.BidirectionalAsyncChannelServer; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; Loading Loading @@ -71,6 +81,7 @@ public class WifiScannerTest { private WifiScanner mWifiScanner; private TestLooper mLooper; private Handler mHandler; private BidirectionalAsyncChannelServer mBidirectionalAsyncChannelServer; /** * Setup before tests. Loading @@ -79,10 +90,10 @@ public class WifiScannerTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mLooper = new TestLooper(); mHandler = mock(Handler.class); BidirectionalAsyncChannelServer server = new BidirectionalAsyncChannelServer( mHandler = spy(new Handler(mLooper.getLooper())); mBidirectionalAsyncChannelServer = new BidirectionalAsyncChannelServer( mContext, mLooper.getLooper(), mHandler); when(mService.getMessenger()).thenReturn(server.getMessenger()); when(mService.getMessenger()).thenReturn(mBidirectionalAsyncChannelServer.getMessenger()); mWifiScanner = new WifiScanner(mContext, mService, mLooper.getLooper()); mLooper.dispatchAll(); } Loading Loading @@ -230,4 +241,208 @@ public class WifiScannerTest { parcel.setDataPosition(0); // Rewind data position back to the beginning for read. return ScanData.CREATOR.createFromParcel(parcel); } /** * Test behavior of {@link WifiScanner#startScan(ScanSettings, WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStartScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_START_SINGLE_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(scanSettings, messageBundle.getParcelable(WifiScanner.SCAN_PARAMS_SCAN_SETTINGS_KEY)); assertNull(messageBundle.getParcelable(WifiScanner.SCAN_PARAMS_WORK_SOURCE_KEY)); assertEquals(mContext.getOpPackageName(), messageBundle.getParcelable(WifiScanner.REQUEST_PACKAGE_NAME_KEY)); } /** * Test behavior of {@link WifiScanner#stopScan(WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStopScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); mWifiScanner.stopScan(scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler, times(2)).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_STOP_SINGLE_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(mContext.getOpPackageName(), messageBundle.getParcelable(WifiScanner.REQUEST_PACKAGE_NAME_KEY)); } /** * Test behavior of {@link WifiScanner#startScan(ScanSettings, WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStartScanListenerOnSuccess() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message sentMessage = messageArgumentCaptor.getValue(); assertNotNull(sentMessage); assertEquals(1, mBidirectionalAsyncChannelServer.getClientMessengers().size()); Messenger scannerMessenger = mBidirectionalAsyncChannelServer.getClientMessengers().iterator().next(); Message responseMessage = Message.obtain(); responseMessage.what = WifiScanner.CMD_OP_SUCCEEDED; responseMessage.arg2 = sentMessage.arg2; scannerMessenger.send(responseMessage); mLooper.dispatchAll(); verify(scanListener).onSuccess(); } /** * Test behavior of {@link WifiScanner#startScan(ScanSettings, WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStartScanListenerOnResults() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message sentMessage = messageArgumentCaptor.getValue(); assertNotNull(sentMessage); assertEquals(1, mBidirectionalAsyncChannelServer.getClientMessengers().size()); Messenger scannerMessenger = mBidirectionalAsyncChannelServer.getClientMessengers().iterator().next(); ScanResult scanResult = new ScanResult(); ScanData scanDatas[] = new ScanData[]{new ScanData(0, 0 , new ScanResult[] {scanResult})}; Message responseMessage = Message.obtain(); responseMessage.what = WifiScanner.CMD_SCAN_RESULT; responseMessage.arg2 = sentMessage.arg2; responseMessage.obj = new WifiScanner.ParcelableScanData(scanDatas); scannerMessenger.send(responseMessage); mLooper.dispatchAll(); verify(scanListener).onResults(scanDatas); } /** * Test behavior of {@link WifiScanner#startDisconnectedPnoScan(ScanSettings, PnoSettings, * WifiScanner.PnoScanListener)} * @throws Exception */ @Test public void testStartDisconnectedPnoScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); PnoSettings pnoSettings = new PnoSettings(); WifiScanner.PnoScanListener pnoScanListener = mock(WifiScanner.PnoScanListener.class); mWifiScanner.startDisconnectedPnoScan(scanSettings, pnoSettings, pnoScanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_START_PNO_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(scanSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_SCAN_SETTINGS_KEY)); assertTrue(scanSettings.isPnoScan); assertFalse(pnoSettings.isConnected); assertEquals(pnoSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_PNO_SETTINGS_KEY)); } /** * Test behavior of {@link WifiScanner#startConnectedPnoScan(ScanSettings, PnoSettings, * WifiScanner.PnoScanListener)} * @throws Exception */ @Test public void testStartConnectedPnoScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); PnoSettings pnoSettings = new PnoSettings(); WifiScanner.PnoScanListener pnoScanListener = mock(WifiScanner.PnoScanListener.class); mWifiScanner.startConnectedPnoScan(scanSettings, pnoSettings, pnoScanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_START_PNO_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(scanSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_SCAN_SETTINGS_KEY)); assertTrue(scanSettings.isPnoScan); assertTrue(pnoSettings.isConnected); assertEquals(pnoSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_PNO_SETTINGS_KEY)); } /** * Test behavior of {@link WifiScanner#stopPnoScan(WifiScanner.ScanListener)} * WifiScanner.PnoScanListener)} * @throws Exception */ @Test public void testStopPnoScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); PnoSettings pnoSettings = new PnoSettings(); WifiScanner.PnoScanListener pnoScanListener = mock(WifiScanner.PnoScanListener.class); mWifiScanner.startDisconnectedPnoScan(scanSettings, pnoSettings, pnoScanListener); mLooper.dispatchAll(); mWifiScanner.stopPnoScan(pnoScanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler, times(2)).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_STOP_PNO_SCAN, message.what); } } Loading
tests/utils/testutils/java/com/android/internal/util/test/BidirectionalAsyncChannelServer.java +5 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ import com.android.internal.util.AsyncChannel; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * Provides an interface for the server side implementation of a bidirectional channel as described Loading Loading @@ -83,4 +84,8 @@ public class BidirectionalAsyncChannelServer { return mMessenger; } public Set<Messenger> getClientMessengers() { return mClients.keySet(); } }
wifi/tests/src/android/net/wifi/WifiManagerTest.java +201 −0 Original line number Diff line number Diff line Loading @@ -36,6 +36,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyList; Loading @@ -53,6 +54,7 @@ import static org.mockito.Mockito.when; import android.content.Context; import android.content.pm.ApplicationInfo; import android.net.DhcpInfo; import android.net.wifi.WifiManager.LocalOnlyHotspotCallback; import android.net.wifi.WifiManager.LocalOnlyHotspotObserver; import android.net.wifi.WifiManager.LocalOnlyHotspotReservation; Loading @@ -62,6 +64,7 @@ import android.net.wifi.WifiManager.NetworkRequestUserSelectionCallback; import android.net.wifi.WifiManager.OnWifiUsabilityStatsListener; import android.net.wifi.WifiManager.SoftApCallback; import android.net.wifi.WifiManager.TrafficStateCallback; import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.os.Message; Loading Loading @@ -92,6 +95,7 @@ public class WifiManagerTest { private static final int ERROR_NOT_SET = -1; private static final int ERROR_TEST_REASON = 5; private static final int TEST_UID = 14553; private static final int TEST_NETWORK_ID = 143; private static final String TEST_PACKAGE_NAME = "TestPackage"; private static final String TEST_COUNTRY_CODE = "US"; private static final String[] TEST_MAC_ADDRESSES = {"da:a1:19:0:0:0"}; Loading @@ -118,6 +122,7 @@ public class WifiManagerTest { MockitoAnnotations.initMocks(this); mLooper = new TestLooper(); mHandler = spy(new Handler(mLooper.getLooper())); mApplicationInfo.targetSdkVersion = Build.VERSION_CODES.Q; when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo); when(mContext.getOpPackageName()).thenReturn(TEST_PACKAGE_NAME); Loading Loading @@ -1438,4 +1443,200 @@ i * Verify that a call to cancel WPS immediately returns a failure. .thenReturn(new Long(~WifiManager.WIFI_FEATURE_DPP)); assertFalse(mWifiManager.isEasyConnectSupported()); } /** * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)} * @throws Exception */ @Test public void testAddNetwork() throws Exception { WifiConfiguration configuration = new WifiConfiguration(); when(mWifiService.addOrUpdateNetwork(any(), anyString())) .thenReturn(TEST_NETWORK_ID); assertEquals(mWifiManager.addNetwork(configuration), TEST_NETWORK_ID); verify(mWifiService).addOrUpdateNetwork(configuration, mContext.getOpPackageName()); // send a null config assertEquals(mWifiManager.addNetwork(null), -1); } /** * Test behavior of {@link WifiManager#addNetwork(WifiConfiguration)} * @throws Exception */ @Test public void testUpdateNetwork() throws Exception { WifiConfiguration configuration = new WifiConfiguration(); when(mWifiService.addOrUpdateNetwork(any(), anyString())) .thenReturn(TEST_NETWORK_ID); configuration.networkId = TEST_NETWORK_ID; assertEquals(mWifiManager.updateNetwork(configuration), TEST_NETWORK_ID); verify(mWifiService).addOrUpdateNetwork(configuration, mContext.getOpPackageName()); // config with invalid network ID configuration.networkId = -1; assertEquals(mWifiManager.updateNetwork(configuration), -1); // send a null config assertEquals(mWifiManager.updateNetwork(null), -1); } /** * Test behavior of {@link WifiManager#enableNetwork(int, boolean)} * @throws Exception */ @Test public void testEnableNetwork() throws Exception { when(mWifiService.enableNetwork(anyInt(), anyBoolean(), anyString())) .thenReturn(true); assertTrue(mWifiManager.enableNetwork(TEST_NETWORK_ID, true)); verify(mWifiService).enableNetwork(TEST_NETWORK_ID, true, mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#disableNetwork(int)} * @throws Exception */ @Test public void testDisableNetwork() throws Exception { when(mWifiService.disableNetwork(anyInt(), anyString())) .thenReturn(true); assertTrue(mWifiManager.disableNetwork(TEST_NETWORK_ID)); verify(mWifiService).disableNetwork(TEST_NETWORK_ID, mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#disconnect()} * @throws Exception */ @Test public void testDisconnect() throws Exception { when(mWifiService.disconnect(anyString())).thenReturn(true); assertTrue(mWifiManager.disconnect()); verify(mWifiService).disconnect(mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#reconnect()} * @throws Exception */ @Test public void testReconnect() throws Exception { when(mWifiService.reconnect(anyString())).thenReturn(true); assertTrue(mWifiManager.reconnect()); verify(mWifiService).reconnect(mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#reassociate()} * @throws Exception */ @Test public void testReassociate() throws Exception { when(mWifiService.reassociate(anyString())).thenReturn(true); assertTrue(mWifiManager.reassociate()); verify(mWifiService).reassociate(mContext.getOpPackageName()); } /** * Test behavior of {@link WifiManager#getSupportedFeatures()} * @throws Exception */ @Test public void testGetSupportedFeatures() throws Exception { long supportedFeatures = WifiManager.WIFI_FEATURE_SCANNER | WifiManager.WIFI_FEATURE_PASSPOINT | WifiManager.WIFI_FEATURE_P2P; when(mWifiService.getSupportedFeatures()) .thenReturn(Long.valueOf(supportedFeatures)); assertTrue(mWifiManager.isWifiScannerSupported()); assertTrue(mWifiManager.isPasspointSupported()); assertTrue(mWifiManager.isP2pSupported()); assertFalse(mWifiManager.isPortableHotspotSupported()); assertFalse(mWifiManager.is5GHzBandSupported()); assertFalse(mWifiManager.isDeviceToDeviceRttSupported()); assertFalse(mWifiManager.isDeviceToApRttSupported()); assertFalse(mWifiManager.isPreferredNetworkOffloadSupported()); assertFalse(mWifiManager.isAdditionalStaSupported()); assertFalse(mWifiManager.isTdlsSupported()); assertFalse(mWifiManager.isOffChannelTdlsSupported()); assertFalse(mWifiManager.isEnhancedPowerReportingSupported()); } /** * Test behavior of {@link WifiManager#getControllerActivityEnergyInfo()} * @throws Exception */ @Test public void testGetControllerActivityEnergyInfo() throws Exception { WifiActivityEnergyInfo activityEnergyInfo = new WifiActivityEnergyInfo(5, 3, 3, new long[]{5L, 5L, 5L}, 5, 5, 5, 5); when(mWifiService.reportActivityInfo()).thenReturn(activityEnergyInfo); assertEquals(activityEnergyInfo, mWifiManager.getControllerActivityEnergyInfo()); } /** * Test behavior of {@link WifiManager#getConnectionInfo()} * @throws Exception */ @Test public void testGetConnectionInfo() throws Exception { WifiInfo wifiInfo = new WifiInfo(); when(mWifiService.getConnectionInfo(anyString())).thenReturn(wifiInfo); assertEquals(wifiInfo, mWifiManager.getConnectionInfo()); } /** * Test behavior of {@link WifiManager#isDualModeSupported()} ()} * @throws Exception */ @Test public void testIsDualModeSupported() throws Exception { when(mWifiService.needs5GHzToAnyApBandConversion()).thenReturn(true); assertTrue(mWifiManager.isDualModeSupported()); verify(mWifiService).needs5GHzToAnyApBandConversion(); } /** * Test behavior of {@link WifiManager#isDualBandSupported()} * @throws Exception */ @Test public void testIsDualBandSupported() throws Exception { when(mWifiService.isDualBandSupported()).thenReturn(true); assertTrue(mWifiManager.isDualBandSupported()); verify(mWifiService).isDualBandSupported(); } /** * Test behavior of {@link WifiManager#getDhcpInfo()} * @throws Exception */ @Test public void testGetDhcpInfo() throws Exception { DhcpInfo dhcpInfo = new DhcpInfo(); when(mWifiService.getDhcpInfo()).thenReturn(dhcpInfo); assertEquals(dhcpInfo, mWifiManager.getDhcpInfo()); verify(mWifiService).getDhcpInfo(); } /** * Test behavior of {@link WifiManager#setWifiEnabled(boolean)} * @throws Exception */ @Test public void testSetWifiEnabled() throws Exception { when(mWifiService.setWifiEnabled(anyString(), anyBoolean())).thenReturn(true); assertTrue(mWifiManager.setWifiEnabled(true)); verify(mWifiService).setWifiEnabled(mContext.getOpPackageName(), true); assertTrue(mWifiManager.setWifiEnabled(false)); verify(mWifiService).setWifiEnabled(mContext.getOpPackageName(), false); } }
wifi/tests/src/android/net/wifi/WifiScannerTest.java +218 −3 Original line number Diff line number Diff line Loading @@ -18,10 +18,16 @@ package android.net.wifi; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.validateMockitoUsage; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; Loading @@ -29,7 +35,10 @@ import android.net.wifi.WifiScanner.PnoSettings; import android.net.wifi.WifiScanner.PnoSettings.PnoNetwork; import android.net.wifi.WifiScanner.ScanData; import android.net.wifi.WifiScanner.ScanSettings; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.os.Messenger; import android.os.Parcel; import android.os.test.TestLooper; Loading @@ -40,6 +49,7 @@ import com.android.internal.util.test.BidirectionalAsyncChannelServer; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; Loading Loading @@ -71,6 +81,7 @@ public class WifiScannerTest { private WifiScanner mWifiScanner; private TestLooper mLooper; private Handler mHandler; private BidirectionalAsyncChannelServer mBidirectionalAsyncChannelServer; /** * Setup before tests. Loading @@ -79,10 +90,10 @@ public class WifiScannerTest { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); mLooper = new TestLooper(); mHandler = mock(Handler.class); BidirectionalAsyncChannelServer server = new BidirectionalAsyncChannelServer( mHandler = spy(new Handler(mLooper.getLooper())); mBidirectionalAsyncChannelServer = new BidirectionalAsyncChannelServer( mContext, mLooper.getLooper(), mHandler); when(mService.getMessenger()).thenReturn(server.getMessenger()); when(mService.getMessenger()).thenReturn(mBidirectionalAsyncChannelServer.getMessenger()); mWifiScanner = new WifiScanner(mContext, mService, mLooper.getLooper()); mLooper.dispatchAll(); } Loading Loading @@ -230,4 +241,208 @@ public class WifiScannerTest { parcel.setDataPosition(0); // Rewind data position back to the beginning for read. return ScanData.CREATOR.createFromParcel(parcel); } /** * Test behavior of {@link WifiScanner#startScan(ScanSettings, WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStartScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_START_SINGLE_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(scanSettings, messageBundle.getParcelable(WifiScanner.SCAN_PARAMS_SCAN_SETTINGS_KEY)); assertNull(messageBundle.getParcelable(WifiScanner.SCAN_PARAMS_WORK_SOURCE_KEY)); assertEquals(mContext.getOpPackageName(), messageBundle.getParcelable(WifiScanner.REQUEST_PACKAGE_NAME_KEY)); } /** * Test behavior of {@link WifiScanner#stopScan(WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStopScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); mWifiScanner.stopScan(scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler, times(2)).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_STOP_SINGLE_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(mContext.getOpPackageName(), messageBundle.getParcelable(WifiScanner.REQUEST_PACKAGE_NAME_KEY)); } /** * Test behavior of {@link WifiScanner#startScan(ScanSettings, WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStartScanListenerOnSuccess() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message sentMessage = messageArgumentCaptor.getValue(); assertNotNull(sentMessage); assertEquals(1, mBidirectionalAsyncChannelServer.getClientMessengers().size()); Messenger scannerMessenger = mBidirectionalAsyncChannelServer.getClientMessengers().iterator().next(); Message responseMessage = Message.obtain(); responseMessage.what = WifiScanner.CMD_OP_SUCCEEDED; responseMessage.arg2 = sentMessage.arg2; scannerMessenger.send(responseMessage); mLooper.dispatchAll(); verify(scanListener).onSuccess(); } /** * Test behavior of {@link WifiScanner#startScan(ScanSettings, WifiScanner.ScanListener)} * @throws Exception */ @Test public void testStartScanListenerOnResults() throws Exception { ScanSettings scanSettings = new ScanSettings(); WifiScanner.ScanListener scanListener = mock(WifiScanner.ScanListener.class); mWifiScanner.startScan(scanSettings, scanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message sentMessage = messageArgumentCaptor.getValue(); assertNotNull(sentMessage); assertEquals(1, mBidirectionalAsyncChannelServer.getClientMessengers().size()); Messenger scannerMessenger = mBidirectionalAsyncChannelServer.getClientMessengers().iterator().next(); ScanResult scanResult = new ScanResult(); ScanData scanDatas[] = new ScanData[]{new ScanData(0, 0 , new ScanResult[] {scanResult})}; Message responseMessage = Message.obtain(); responseMessage.what = WifiScanner.CMD_SCAN_RESULT; responseMessage.arg2 = sentMessage.arg2; responseMessage.obj = new WifiScanner.ParcelableScanData(scanDatas); scannerMessenger.send(responseMessage); mLooper.dispatchAll(); verify(scanListener).onResults(scanDatas); } /** * Test behavior of {@link WifiScanner#startDisconnectedPnoScan(ScanSettings, PnoSettings, * WifiScanner.PnoScanListener)} * @throws Exception */ @Test public void testStartDisconnectedPnoScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); PnoSettings pnoSettings = new PnoSettings(); WifiScanner.PnoScanListener pnoScanListener = mock(WifiScanner.PnoScanListener.class); mWifiScanner.startDisconnectedPnoScan(scanSettings, pnoSettings, pnoScanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_START_PNO_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(scanSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_SCAN_SETTINGS_KEY)); assertTrue(scanSettings.isPnoScan); assertFalse(pnoSettings.isConnected); assertEquals(pnoSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_PNO_SETTINGS_KEY)); } /** * Test behavior of {@link WifiScanner#startConnectedPnoScan(ScanSettings, PnoSettings, * WifiScanner.PnoScanListener)} * @throws Exception */ @Test public void testStartConnectedPnoScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); PnoSettings pnoSettings = new PnoSettings(); WifiScanner.PnoScanListener pnoScanListener = mock(WifiScanner.PnoScanListener.class); mWifiScanner.startConnectedPnoScan(scanSettings, pnoSettings, pnoScanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_START_PNO_SCAN, message.what); assertTrue(message.obj instanceof Bundle); Bundle messageBundle = (Bundle) message.obj; assertEquals(scanSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_SCAN_SETTINGS_KEY)); assertTrue(scanSettings.isPnoScan); assertTrue(pnoSettings.isConnected); assertEquals(pnoSettings, messageBundle.getParcelable(WifiScanner.PNO_PARAMS_PNO_SETTINGS_KEY)); } /** * Test behavior of {@link WifiScanner#stopPnoScan(WifiScanner.ScanListener)} * WifiScanner.PnoScanListener)} * @throws Exception */ @Test public void testStopPnoScan() throws Exception { ScanSettings scanSettings = new ScanSettings(); PnoSettings pnoSettings = new PnoSettings(); WifiScanner.PnoScanListener pnoScanListener = mock(WifiScanner.PnoScanListener.class); mWifiScanner.startDisconnectedPnoScan(scanSettings, pnoSettings, pnoScanListener); mLooper.dispatchAll(); mWifiScanner.stopPnoScan(pnoScanListener); mLooper.dispatchAll(); ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); verify(mHandler, times(2)).handleMessage(messageArgumentCaptor.capture()); Message message = messageArgumentCaptor.getValue(); assertNotNull(message); assertEquals(WifiScanner.CMD_STOP_PNO_SCAN, message.what); } }