1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.wifi.nl80211; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.os.Parcel; 22 23 import androidx.test.filters.SmallTest; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.HashMap; 31 32 /** 33 * Unit tests for {@link android.net.wifi.nl80211.PnoSettings}. 34 */ 35 @SmallTest 36 public class PnoSettingsTest { 37 38 private static final byte[] TEST_SSID_1 = 39 new byte[] {'G', 'o', 'o', 'g', 'l', 'e', 'G', 'u', 'e', 's', 't'}; 40 private static final byte[] TEST_SSID_2 = 41 new byte[] {'A', 'n', 'd', 'r', 'o', 'i', 'd', 'T', 'e', 's', 't'}; 42 private static final int[] TEST_FREQUENCIES_1 = {}; 43 private static final int[] TEST_FREQUENCIES_2 = {2500, 5124}; 44 private static final int TEST_INTERVAL_MS = 30000; 45 private static final int TEST_MIN_2G_RSSI = -60; 46 private static final int TEST_MIN_5G_RSSI = -65; 47 private static final int TEST_VALUE = 42; 48 49 private PnoNetwork mPnoNetwork1; 50 private PnoNetwork mPnoNetwork2; 51 52 @Before setUp()53 public void setUp() { 54 mPnoNetwork1 = new PnoNetwork(); 55 mPnoNetwork1.setSsid(TEST_SSID_1); 56 mPnoNetwork1.setHidden(true); 57 mPnoNetwork1.setFrequenciesMhz(TEST_FREQUENCIES_1); 58 59 mPnoNetwork2 = new PnoNetwork(); 60 mPnoNetwork2.setSsid(TEST_SSID_2); 61 mPnoNetwork2.setHidden(false); 62 mPnoNetwork2.setFrequenciesMhz(TEST_FREQUENCIES_2); 63 } 64 65 /** 66 * PnoSettings object can be serialized and deserialized, while keeping the 67 * values unchanged. 68 */ 69 @Test canSerializeAndDeserialize()70 public void canSerializeAndDeserialize() { 71 PnoSettings pnoSettings = new PnoSettings(); 72 pnoSettings.setIntervalMillis(TEST_INTERVAL_MS); 73 pnoSettings.setMin2gRssiDbm(TEST_MIN_2G_RSSI); 74 pnoSettings.setMin5gRssiDbm(TEST_MIN_5G_RSSI); 75 pnoSettings.setPnoNetworks(new ArrayList<>(Arrays.asList(mPnoNetwork1, mPnoNetwork2))); 76 77 Parcel parcel = Parcel.obtain(); 78 pnoSettings.writeToParcel(parcel, 0); 79 // Rewind the pointer to the head of the parcel. 80 parcel.setDataPosition(0); 81 PnoSettings pnoSettingsDeserialized = PnoSettings.CREATOR.createFromParcel(parcel); 82 83 assertEquals(pnoSettings, pnoSettingsDeserialized); 84 assertEquals(pnoSettings.hashCode(), pnoSettingsDeserialized.hashCode()); 85 } 86 87 /** 88 * Tests usage of {@link PnoSettings} as a HashMap key type. 89 */ 90 @Test testAsHashMapKey()91 public void testAsHashMapKey() { 92 PnoSettings pnoSettings1 = new PnoSettings(); 93 pnoSettings1.setIntervalMillis(TEST_INTERVAL_MS); 94 pnoSettings1.setMin2gRssiDbm(TEST_MIN_2G_RSSI); 95 pnoSettings1.setMin5gRssiDbm(TEST_MIN_5G_RSSI); 96 pnoSettings1.setPnoNetworks(new ArrayList<>(Arrays.asList(mPnoNetwork1, mPnoNetwork2))); 97 98 PnoSettings pnoSettings2 = new PnoSettings(); 99 pnoSettings2.setIntervalMillis(TEST_INTERVAL_MS); 100 pnoSettings2.setMin2gRssiDbm(TEST_MIN_2G_RSSI); 101 pnoSettings2.setMin5gRssiDbm(TEST_MIN_5G_RSSI); 102 pnoSettings2.setPnoNetworks(new ArrayList<>(Arrays.asList(mPnoNetwork1, mPnoNetwork2))); 103 104 assertEquals(pnoSettings1, pnoSettings2); 105 assertEquals(pnoSettings1.hashCode(), pnoSettings2.hashCode()); 106 107 HashMap<PnoSettings, Integer> map = new HashMap<>(); 108 map.put(pnoSettings1, TEST_VALUE); 109 110 assertEquals(TEST_VALUE, map.get(pnoSettings2).intValue()); 111 } 112 } 113