Eric Schwarzenbach | 46e3a9a | 2017-12-07 11:25:51 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 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 com.android.server.wifi; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertFalse; |
| 21 | import static org.junit.Assert.assertNull; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
| 24 | import android.net.wifi.ScanResult; |
| 25 | import android.util.ArraySet; |
| 26 | |
| 27 | import com.android.server.wifi.util.ScanResultUtil; |
| 28 | |
| 29 | import com.google.android.collect.Sets; |
| 30 | |
| 31 | import org.junit.Before; |
| 32 | import org.junit.Test; |
| 33 | |
| 34 | import java.util.Collections; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for {@link WakeupEvaluator}. |
| 41 | */ |
| 42 | public class WakeupEvaluatorTest { |
| 43 | |
| 44 | private static final String SAVED_SSID_1 = "saved ssid 1"; |
| 45 | private static final String SAVED_SSID_2 = "saved ssid 2"; |
| 46 | private static final String UNSAVED_SSID = "unsaved ssid"; |
| 47 | |
| 48 | private static final int FREQ_24 = 2402; |
| 49 | private static final int FREQ_5 = 5000; |
| 50 | |
| 51 | private static final int THRESHOLD_24 = -10; |
| 52 | private static final int THRESHOLD_5 = -1; |
| 53 | |
| 54 | private WakeupEvaluator mWakeupEvaluator; |
| 55 | |
| 56 | private ScanResult makeScanResult(String ssid, int frequency, int level) { |
| 57 | ScanResult scanResult = new ScanResult(); |
| 58 | scanResult.SSID = ssid; |
| 59 | scanResult.frequency = frequency; |
| 60 | scanResult.level = level; |
| 61 | scanResult.capabilities = "[]"; |
| 62 | |
| 63 | return scanResult; |
| 64 | } |
| 65 | |
| 66 | private Set<ScanResultMatchInfo> getSavedNetworks() { |
| 67 | Set<ScanResultMatchInfo> networks = new ArraySet<>(); |
| 68 | networks.add(ScanResultMatchInfo.fromWifiConfiguration( |
| 69 | WifiConfigurationTestUtil.createOpenNetwork( |
| 70 | ScanResultUtil.createQuotedSSID(SAVED_SSID_1)))); |
| 71 | networks.add(ScanResultMatchInfo.fromWifiConfiguration( |
| 72 | WifiConfigurationTestUtil.createOpenNetwork( |
| 73 | ScanResultUtil.createQuotedSSID(SAVED_SSID_2)))); |
| 74 | return networks; |
| 75 | } |
| 76 | |
| 77 | @Before |
| 78 | public void setUp() { |
| 79 | mWakeupEvaluator = new WakeupEvaluator(THRESHOLD_24, THRESHOLD_5); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Verify that isBelowThreshold returns true for networks below the filter threshold. |
| 84 | */ |
| 85 | @Test |
| 86 | public void isBelowThreshold_returnsTrueWhenRssiIsBelowThreshold() { |
| 87 | ScanResult scanResult24 = makeScanResult(SAVED_SSID_1, FREQ_24, THRESHOLD_24 - 1); |
| 88 | assertTrue(mWakeupEvaluator.isBelowThreshold(scanResult24)); |
| 89 | |
| 90 | ScanResult scanResult5 = makeScanResult(SAVED_SSID_1, FREQ_5, THRESHOLD_5 - 1); |
| 91 | assertTrue(mWakeupEvaluator.isBelowThreshold(scanResult5)); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Verify that isBelowThreshold returns false for networks above the filter threshold. |
| 96 | */ |
| 97 | @Test |
| 98 | public void isBelowThreshold_returnsFalseWhenRssiIsAboveThreshold() { |
| 99 | ScanResult scanResult24 = makeScanResult(SAVED_SSID_1, FREQ_24, THRESHOLD_24 + 1); |
| 100 | assertFalse(mWakeupEvaluator.isBelowThreshold(scanResult24)); |
| 101 | |
| 102 | ScanResult scanResult5 = makeScanResult(SAVED_SSID_1, FREQ_5, THRESHOLD_5 + 1); |
| 103 | assertFalse(mWakeupEvaluator.isBelowThreshold(scanResult5)); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Verify that findViableNetwork does not select ScanResult that is not present in the |
| 108 | * WifiConfigurations. |
| 109 | */ |
| 110 | @Test |
| 111 | public void findViableNetwork_returnsNullWhenScanResultIsNotInSavedNetworks() { |
| 112 | Set<ScanResult> scanResults = Collections.singleton( |
| 113 | makeScanResult(UNSAVED_SSID, FREQ_24, THRESHOLD_24 + 1)); |
| 114 | |
| 115 | ScanResult scanResult = mWakeupEvaluator.findViableNetwork(scanResults, getSavedNetworks()); |
| 116 | |
| 117 | assertNull(scanResult); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Verify that findViableNetwork does not select a scan result that is below the threshold. |
| 122 | */ |
| 123 | @Test |
| 124 | public void findViableNetwork_returnsNullWhenScanResultIsBelowThreshold() { |
| 125 | Set<ScanResult> scanResults = Collections.singleton( |
| 126 | makeScanResult(SAVED_SSID_1, FREQ_24, THRESHOLD_24 - 1)); |
| 127 | |
| 128 | ScanResult scanResult = mWakeupEvaluator.findViableNetwork(scanResults, getSavedNetworks()); |
| 129 | assertNull(scanResult); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Verify that findViableNetwork returns a viable ScanResult. |
| 134 | */ |
| 135 | @Test |
| 136 | public void findViableNetwork_returnsConnectableScanResult() { |
| 137 | ScanResult savedScanResult = makeScanResult(SAVED_SSID_1, FREQ_24, THRESHOLD_24 + 1); |
| 138 | Set<ScanResult> scanResults = Collections.singleton(savedScanResult); |
| 139 | |
| 140 | ScanResult scanResult = mWakeupEvaluator.findViableNetwork(scanResults, getSavedNetworks()); |
| 141 | assertEquals(savedScanResult, scanResult); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Verify that findViableNetwork returns the viable ScanResult with the highest RSSI. |
| 146 | */ |
| 147 | @Test |
| 148 | public void findViableNetwork_returnsConnectableScanResultWithHighestRssi() { |
| 149 | ScanResult savedScanResultLow = makeScanResult(SAVED_SSID_1, FREQ_24, THRESHOLD_24 + 1); |
| 150 | ScanResult savedScanResultHigh = makeScanResult(SAVED_SSID_1, FREQ_24, THRESHOLD_24 + 10); |
| 151 | Set<ScanResult> scanResults = Sets.newArraySet(savedScanResultLow, savedScanResultHigh); |
| 152 | |
| 153 | ScanResult scanResult = mWakeupEvaluator.findViableNetwork(scanResults, getSavedNetworks()); |
| 154 | assertEquals(savedScanResultHigh, scanResult); |
| 155 | } |
| 156 | } |