Wifi Builders: multiple calls to build() should return different instances
Previous build() implementation simply returns the object wrapped inside the builder. The expected behavior should be to return a new instance every time build() is called. Bug: 148098082 Test: atest FrameworksWifiApiTests Change-Id: If4c1cf45be155f32bea399ad0e3ec52b28f645a7
This commit is contained in:
@@ -1662,7 +1662,9 @@ public class WifiConfiguration implements Parcelable {
|
||||
*/
|
||||
@NonNull
|
||||
public NetworkSelectionStatus build() {
|
||||
return mNetworkSelectionStatus;
|
||||
NetworkSelectionStatus status = new NetworkSelectionStatus();
|
||||
status.copy(mNetworkSelectionStatus);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -435,7 +435,7 @@ public class WifiInfo implements Parcelable {
|
||||
*/
|
||||
@NonNull
|
||||
public WifiInfo build() {
|
||||
return mWifiInfo;
|
||||
return new WifiInfo(mWifiInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ 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.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.net.MacAddress;
|
||||
@@ -386,4 +387,43 @@ public class WifiConfigurationTest {
|
||||
.get(WifiConfiguration.GroupMgmtCipher.BIP_GMAC_256));
|
||||
assertTrue(config.requirePMF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the NetworkSelectionStatus Builder returns the same values that was set, and that
|
||||
* calling build multiple times returns different instances.
|
||||
*/
|
||||
@Test
|
||||
public void testNetworkSelectionStatusBuilder() throws Exception {
|
||||
NetworkSelectionStatus.Builder builder = new NetworkSelectionStatus.Builder()
|
||||
.setNetworkSelectionDisableReason(
|
||||
NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION)
|
||||
.setNetworkSelectionStatus(
|
||||
NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED);
|
||||
|
||||
NetworkSelectionStatus status1 = builder.build();
|
||||
|
||||
assertEquals(NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION,
|
||||
status1.getNetworkSelectionDisableReason());
|
||||
assertEquals(NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED,
|
||||
status1.getNetworkSelectionStatus());
|
||||
|
||||
NetworkSelectionStatus status2 = builder
|
||||
.setNetworkSelectionDisableReason(NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD)
|
||||
.build();
|
||||
|
||||
// different instances
|
||||
assertNotSame(status1, status2);
|
||||
|
||||
// assert that status1 didn't change
|
||||
assertEquals(NetworkSelectionStatus.DISABLED_ASSOCIATION_REJECTION,
|
||||
status1.getNetworkSelectionDisableReason());
|
||||
assertEquals(NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED,
|
||||
status1.getNetworkSelectionStatus());
|
||||
|
||||
// assert that status2 changed
|
||||
assertEquals(NetworkSelectionStatus.DISABLED_BY_WRONG_PASSWORD,
|
||||
status2.getNetworkSelectionDisableReason());
|
||||
assertEquals(NetworkSelectionStatus.NETWORK_SELECTION_PERMANENTLY_DISABLED,
|
||||
status2.getNetworkSelectionStatus());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package android.net.wifi;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.os.Parcel;
|
||||
@@ -26,6 +27,8 @@ import androidx.test.filters.SmallTest;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link android.net.wifi.WifiInfo}.
|
||||
*/
|
||||
@@ -41,6 +44,11 @@ public class WifiInfoTest {
|
||||
private static final int TEST_WIFI_STANDARD = ScanResult.WIFI_STANDARD_11AC;
|
||||
private static final int TEST_MAX_SUPPORTED_TX_LINK_SPEED_MBPS = 866;
|
||||
private static final int TEST_MAX_SUPPORTED_RX_LINK_SPEED_MBPS = 1200;
|
||||
private static final String TEST_SSID = "Test123";
|
||||
private static final String TEST_BSSID = "12:12:12:12:12:12";
|
||||
private static final int TEST_RSSI = -60;
|
||||
private static final int TEST_NETWORK_ID = 5;
|
||||
private static final int TEST_NETWORK_ID2 = 6;
|
||||
|
||||
/**
|
||||
* Verify parcel write/read with WifiInfo.
|
||||
@@ -101,4 +109,43 @@ public class WifiInfoTest {
|
||||
assertEquals(null, wifiInfo.getBSSID());
|
||||
assertEquals(-1, wifiInfo.getNetworkId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the WifiInfo Builder returns the same values that was set, and that
|
||||
* calling build multiple times returns different instances.
|
||||
*/
|
||||
@Test
|
||||
public void testWifiInfoBuilder() throws Exception {
|
||||
WifiInfo.Builder builder = new WifiInfo.Builder()
|
||||
.setSsid(TEST_SSID.getBytes(StandardCharsets.UTF_8))
|
||||
.setBssid(TEST_BSSID)
|
||||
.setRssi(TEST_RSSI)
|
||||
.setNetworkId(TEST_NETWORK_ID);
|
||||
|
||||
WifiInfo info1 = builder.build();
|
||||
|
||||
assertEquals("\"" + TEST_SSID + "\"", info1.getSSID());
|
||||
assertEquals(TEST_BSSID, info1.getBSSID());
|
||||
assertEquals(TEST_RSSI, info1.getRssi());
|
||||
assertEquals(TEST_NETWORK_ID, info1.getNetworkId());
|
||||
|
||||
WifiInfo info2 = builder
|
||||
.setNetworkId(TEST_NETWORK_ID2)
|
||||
.build();
|
||||
|
||||
// different instances
|
||||
assertNotSame(info1, info2);
|
||||
|
||||
// assert that info1 didn't change
|
||||
assertEquals("\"" + TEST_SSID + "\"", info1.getSSID());
|
||||
assertEquals(TEST_BSSID, info1.getBSSID());
|
||||
assertEquals(TEST_RSSI, info1.getRssi());
|
||||
assertEquals(TEST_NETWORK_ID, info1.getNetworkId());
|
||||
|
||||
// assert that info2 changed
|
||||
assertEquals("\"" + TEST_SSID + "\"", info2.getSSID());
|
||||
assertEquals(TEST_BSSID, info2.getBSSID());
|
||||
assertEquals(TEST_RSSI, info2.getRssi());
|
||||
assertEquals(TEST_NETWORK_ID2, info2.getNetworkId());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user