wifi: override equals method for WifiSsid
Bug: 62053649 Test: frameworks/base/wifi/tests/runtests.sh Change-Id: I02309880398750a1f5039b8b07a65972fe25104a
This commit is contained in:
@@ -26,6 +26,7 @@ import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetDecoder;
|
||||
import java.nio.charset.CoderResult;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
@@ -189,6 +190,23 @@ public class WifiSsid implements Parcelable {
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object thatObject) {
|
||||
if (this == thatObject) {
|
||||
return true;
|
||||
}
|
||||
if (!(thatObject instanceof WifiSsid)) {
|
||||
return false;
|
||||
}
|
||||
WifiSsid that = (WifiSsid) thatObject;
|
||||
return Arrays.equals(octets.toByteArray(), that.octets.toByteArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(octets.toByteArray());
|
||||
}
|
||||
|
||||
private boolean isArrayAllZeroes(byte[] ssidBytes) {
|
||||
for (int i = 0; i< ssidBytes.length; i++) {
|
||||
if (ssidBytes[i] != 0) return false;
|
||||
|
||||
@@ -16,25 +16,43 @@
|
||||
|
||||
package android.net.wifi;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link android.net.wifi.WifiSsid}.
|
||||
*/
|
||||
public class WifiSsidTest {
|
||||
|
||||
private static final byte[] TEST_SSID =
|
||||
new byte[] {'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'};
|
||||
private static final String TEST_SSID = "Test SSID";
|
||||
private static final byte[] TEST_SSID_BYTES = TEST_SSID.getBytes(StandardCharsets.US_ASCII);
|
||||
|
||||
/**
|
||||
* Check that createFromByteArray() works.
|
||||
*/
|
||||
@Test
|
||||
public void testCreateFromByteArray() {
|
||||
WifiSsid wifiSsid = WifiSsid.createFromByteArray(TEST_SSID);
|
||||
WifiSsid wifiSsid = WifiSsid.createFromByteArray(TEST_SSID_BYTES);
|
||||
assertTrue(wifiSsid != null);
|
||||
assertEquals(new String(TEST_SSID), wifiSsid.toString());
|
||||
assertEquals(TEST_SSID, wifiSsid.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that SSID created from byte array and string with the same content are equal.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
@Test
|
||||
public void testEquals() throws Exception {
|
||||
WifiSsid fromBytes = WifiSsid.createFromByteArray(TEST_SSID_BYTES);
|
||||
WifiSsid fromString = WifiSsid.createFromAsciiEncoded(TEST_SSID);
|
||||
assertTrue(fromBytes != null);
|
||||
assertTrue(fromString != null);
|
||||
assertEquals(fromBytes, fromString);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user