1 /* 2 * Copyright (C) 2022 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; 18 19 import static android.net.wifi.ScanResult.WIFI_BAND_24_GHZ; 20 import static android.net.wifi.ScanResult.WIFI_BAND_5_GHZ; 21 import static android.net.wifi.ScanResult.WIFI_BAND_6_GHZ; 22 23 import static org.junit.Assert.assertArrayEquals; 24 import static org.junit.Assert.assertEquals; 25 import static org.junit.Assert.assertFalse; 26 import static org.junit.Assert.assertThrows; 27 import static org.junit.Assert.assertTrue; 28 import static org.junit.Assume.assumeTrue; 29 30 import android.os.Parcel; 31 import android.util.SparseArray; 32 33 import androidx.test.filters.SmallTest; 34 35 import com.android.modules.utils.build.SdkLevel; 36 37 import org.junit.Test; 38 39 /** 40 * Unit tests for {@link android.net.wifi.WifiNetworkSelectionConfig}. 41 */ 42 @SmallTest 43 public class WifiNetworkSelectionConfigTest { 44 45 /** 46 * Check that parcel marshalling/unmarshalling works 47 */ 48 @Test testWifiNetworkSelectionConfigParcel()49 public void testWifiNetworkSelectionConfigParcel() { 50 assumeTrue(SdkLevel.isAtLeastT()); 51 int[] rssi2Thresholds = {-81, -79, -73, -60}; 52 int[] rssi5Thresholds = {-80, -77, -71, -55}; 53 int[] rssi6Thresholds = {-79, -72, -65, -55}; 54 55 SparseArray<Integer> weights = new SparseArray<>(); 56 weights.put(2450, WifiNetworkSelectionConfig.FREQUENCY_WEIGHT_HIGH); 57 weights.put(5450, WifiNetworkSelectionConfig.FREQUENCY_WEIGHT_LOW); 58 59 WifiNetworkSelectionConfig config = new WifiNetworkSelectionConfig.Builder() 60 .setAssociatedNetworkSelectionOverride( 61 WifiNetworkSelectionConfig.ASSOCIATED_NETWORK_SELECTION_OVERRIDE_ENABLED) 62 .setSufficiencyCheckEnabledWhenScreenOff(false) 63 .setUserConnectChoiceOverrideEnabled(false) 64 .setLastSelectionWeightEnabled(false) 65 .setRssiThresholds(WIFI_BAND_24_GHZ, rssi2Thresholds) 66 .setRssiThresholds(WIFI_BAND_5_GHZ, rssi5Thresholds) 67 .setRssiThresholds(WIFI_BAND_6_GHZ, rssi6Thresholds) 68 .setFrequencyWeights(weights) 69 .build(); 70 71 Parcel parcelW = Parcel.obtain(); 72 config.writeToParcel(parcelW, 0); 73 byte[] bytes = parcelW.marshall(); 74 parcelW.recycle(); 75 76 Parcel parcelR = Parcel.obtain(); 77 parcelR.unmarshall(bytes, 0, bytes.length); 78 parcelR.setDataPosition(0); 79 WifiNetworkSelectionConfig parcelConfig = 80 WifiNetworkSelectionConfig.CREATOR.createFromParcel(parcelR); 81 82 assertEquals(WifiNetworkSelectionConfig.ASSOCIATED_NETWORK_SELECTION_OVERRIDE_ENABLED, 83 parcelConfig.getAssociatedNetworkSelectionOverride()); 84 assertFalse(parcelConfig.isSufficiencyCheckEnabledWhenScreenOff()); 85 assertTrue(parcelConfig.isSufficiencyCheckEnabledWhenScreenOn()); 86 assertFalse(parcelConfig.isUserConnectChoiceOverrideEnabled()); 87 assertFalse(parcelConfig.isLastSelectionWeightEnabled()); 88 assertArrayEquals(rssi2Thresholds, parcelConfig.getRssiThresholds(WIFI_BAND_24_GHZ)); 89 assertArrayEquals(rssi5Thresholds, parcelConfig.getRssiThresholds(WIFI_BAND_5_GHZ)); 90 assertArrayEquals(rssi6Thresholds, parcelConfig.getRssiThresholds(WIFI_BAND_6_GHZ)); 91 assertTrue(weights.contentEquals(parcelConfig.getFrequencyWeights())); 92 assertEquals(config, parcelConfig); 93 assertEquals(config.hashCode(), parcelConfig.hashCode()); 94 } 95 96 @Test testInvalidBuilderThrowsException()97 public void testInvalidBuilderThrowsException() { 98 assumeTrue(SdkLevel.isAtLeastT()); 99 assertThrows(IllegalArgumentException.class, 100 () -> new WifiNetworkSelectionConfig.Builder() 101 .setAssociatedNetworkSelectionOverride(-1)); 102 } 103 104 @Test testInvalidRssiThresholdArrayThrowsException()105 public void testInvalidRssiThresholdArrayThrowsException() { 106 assumeTrue(SdkLevel.isAtLeastT()); 107 assertThrows(IllegalArgumentException.class, 108 () -> new WifiNetworkSelectionConfig.Builder() 109 .setRssiThresholds(WIFI_BAND_24_GHZ, null)); 110 assertThrows(IllegalArgumentException.class, 111 () -> new WifiNetworkSelectionConfig.Builder() 112 .setRssiThresholds(WIFI_BAND_24_GHZ, new int[] {-200, -100, -50, 50})); 113 assertThrows(IllegalArgumentException.class, 114 () -> new WifiNetworkSelectionConfig.Builder() 115 .setRssiThresholds(WIFI_BAND_5_GHZ, new int[] {-100, -60, -70, -50})); 116 assertThrows(IllegalArgumentException.class, 117 () -> new WifiNetworkSelectionConfig.Builder() 118 .setRssiThresholds(WIFI_BAND_5_GHZ, new int[] {-60, -60, -50, -40})); 119 assertThrows(IllegalArgumentException.class, 120 () -> new WifiNetworkSelectionConfig.Builder() 121 .setRssiThresholds(WIFI_BAND_5_GHZ, new int[] {-127, -60, -50, -1})); 122 assertThrows(IllegalArgumentException.class, 123 () -> new WifiNetworkSelectionConfig.Builder() 124 .setRssiThresholds(WIFI_BAND_5_GHZ, new int[] {-126, -60, -50, 0})); 125 assertThrows(IllegalArgumentException.class, 126 () -> new WifiNetworkSelectionConfig.Builder() 127 .setRssiThresholds(WIFI_BAND_6_GHZ, new int[] {0, 0, 0, -50})); 128 } 129 130 @Test testInvalidFrequencyWeightsThrowsException()131 public void testInvalidFrequencyWeightsThrowsException() { 132 assumeTrue(SdkLevel.isAtLeastT()); 133 assertThrows(IllegalArgumentException.class, 134 () -> new WifiNetworkSelectionConfig.Builder().setFrequencyWeights(null)); 135 SparseArray<Integer> invalidInput = new SparseArray<>(); 136 invalidInput.put(2400, 10); 137 assertThrows(IllegalArgumentException.class, 138 () -> new WifiNetworkSelectionConfig.Builder().setFrequencyWeights(invalidInput)); 139 invalidInput.put(2400, -10); 140 assertThrows(IllegalArgumentException.class, 141 () -> new WifiNetworkSelectionConfig.Builder().setFrequencyWeights(invalidInput)); 142 } 143 144 /** 145 * Verify the default builder creates a config with the expected default values. 146 */ 147 @Test testDefaultBuilder()148 public void testDefaultBuilder() { 149 assumeTrue(SdkLevel.isAtLeastT()); 150 WifiNetworkSelectionConfig config = new WifiNetworkSelectionConfig.Builder().build(); 151 152 assertEquals(WifiNetworkSelectionConfig.ASSOCIATED_NETWORK_SELECTION_OVERRIDE_NONE, 153 config.getAssociatedNetworkSelectionOverride()); 154 assertTrue(config.isSufficiencyCheckEnabledWhenScreenOff()); 155 assertTrue(config.isSufficiencyCheckEnabledWhenScreenOn()); 156 assertTrue(config.isUserConnectChoiceOverrideEnabled()); 157 assertTrue(config.isLastSelectionWeightEnabled()); 158 assertArrayEquals(new int[4], config.getRssiThresholds(WIFI_BAND_24_GHZ)); 159 assertArrayEquals(new int[4], config.getRssiThresholds(WIFI_BAND_5_GHZ)); 160 assertArrayEquals(new int[4], config.getRssiThresholds(WIFI_BAND_6_GHZ)); 161 assertTrue(new SparseArray<Integer>().contentEquals(config.getFrequencyWeights())); 162 } 163 } 164