Merge changes Ic4eb56fc,I7ba66213,I4b4dba44 into qt-dev

* changes:
  WifiScannerTest: More unit tests
  WifiScannerTest: Add unit tests
  WifiManagerTest: Add unit tests
This commit is contained in:
TreeHugger Robot
2019-04-08 17:08:54 +00:00
committed by Android (Google) Code Review
3 changed files with 424 additions and 3 deletions

View File

@@ -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
@@ -83,4 +84,8 @@ public class BidirectionalAsyncChannelServer {
return mMessenger;
}
public Set<Messenger> getClientMessengers() {
return mClients.keySet();
}
}

View File

@@ -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;
@@ -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;
@@ -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;
@@ -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"};
@@ -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);
@@ -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);
}
}

View File

@@ -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;
@@ -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;
@@ -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;
@@ -71,6 +81,7 @@ public class WifiScannerTest {
private WifiScanner mWifiScanner;
private TestLooper mLooper;
private Handler mHandler;
private BidirectionalAsyncChannelServer mBidirectionalAsyncChannelServer;
/**
* Setup before tests.
@@ -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();
}
@@ -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);
}
}