diff options
author | Nate(Qiang) Jiang <qiangjiang@google.com> | 2019-11-15 10:58:28 -0800 |
---|---|---|
committer | Nate(Qiang) Jiang <qiangjiang@google.com> | 2019-12-09 10:23:17 -0800 |
commit | 86a674e2d4fa10e36d3897c61ea827338ef9903b (patch) | |
tree | 29acaee52f2e56a60535baddf74011a1552f85cf | |
parent | a669e034decb682f145e089cec6c204b09e0c07c (diff) |
[Evaluator] evaluator will only nominate candidates
As all score will be handles by scorer, remove the evaluate function
from each evaluator. evaluator will only nominate candidates to the
scorer.
Bug: 141019662
Test: atest android.net.wifi
Test: atest com.android.server.wifi
Change-Id: I996dd19aa0e67051fe8003d7c621e4f75f73db38
13 files changed, 219 insertions, 1457 deletions
diff --git a/service/java/com/android/server/wifi/CarrierNetworkEvaluator.java b/service/java/com/android/server/wifi/CarrierNetworkEvaluator.java index c9f567e89..e9f6a48ca 100644 --- a/service/java/com/android/server/wifi/CarrierNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/CarrierNetworkEvaluator.java @@ -37,10 +37,7 @@ import javax.annotation.concurrent.NotThreadSafe; * * 1. Filtering: figure out which of the networks is a Carrier Wi-Fi network (using the * {@link CarrierNetworkConfig} APIs). - * 2. Evaluation: current evaluator API has 2 outputs (effectively): - * - Connectable networks: all networks which match #1 will be fed to this API - * - Selected network: a single network 'selected' by the evaluator. A simple max(RSSI) will be - * used to pick one network from among the connectable networks. + * 2. Nominating: Connectable networks: all networks which match #1 will be fed to this API * * Note: This class is not thread safe and meant to be used only from {@link WifiNetworkSelector}. */ @@ -86,15 +83,13 @@ public class CarrierNetworkEvaluator implements NetworkEvaluator { } @Override - public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + public void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, OnConnectableListener onConnectableListener) { if (!mCarrierNetworkConfig.isCarrierEncryptionInfoAvailable()) { - return null; + return; } - int currentMaxRssi = Integer.MIN_VALUE; - WifiConfiguration configWithMaxRssi = null; for (ScanDetail scanDetail : scanDetails) { ScanResult scanResult = scanDetail.getScanResult(); @@ -168,13 +163,7 @@ public class CarrierNetworkEvaluator implements NetworkEvaluator { mWifiConfigManager.updateScanDetailForNetwork(result.getNetworkId(), scanDetail); } - onConnectableListener.onConnectable(scanDetail, config, 0); - if (scanResult.level > currentMaxRssi) { - configWithMaxRssi = config; - currentMaxRssi = scanResult.level; - } + onConnectableListener.onConnectable(scanDetail, config); } - - return configWithMaxRssi; } } diff --git a/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java b/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java index c8f370bcc..9d13e3c56 100644 --- a/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java +++ b/service/java/com/android/server/wifi/NetworkSuggestionEvaluator.java @@ -28,7 +28,6 @@ import com.android.server.wifi.util.ScanResultUtil; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -39,20 +38,11 @@ import javax.annotation.Nullable; import javax.annotation.concurrent.NotThreadSafe; /** - * Evaluator to pick the best network to connect to from the list of active network suggestions - * provided by apps. + * Evaluator nominate the highest available suggestion candidates. * Note: * <li> This class is not thread safe and meant to be used only from {@link WifiNetworkSelector}. * </li> * - * This is a non-optimal implementation which picks any network suggestion which matches - * the scan result with the highest RSSI. - * TODO: More advanced implementation will follow! - * Params to consider for evaluating network suggestions: - * - Regular network evaluator params like security, band, RSSI, etc. - * - Priority of suggestions provided by a single app. - * - Whether the network suggestions requires user/app interaction or if it is metered. - * - Historical quality of suggestions provided by the corresponding app. */ @NotThreadSafe public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEvaluator { @@ -75,7 +65,7 @@ public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEv } @Override - public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + public void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { @@ -133,17 +123,9 @@ public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEv // Return early on no match. if (matchMetaInfo.isEmpty()) { mLocalLog.log("did not see any matching network suggestions."); - return null; - } - // Note: These matched sets should be very small & hence these additional manipulations that - // follow should not be very expensive. - PerNetworkSuggestionMatchMetaInfo candidate = - matchMetaInfo.findConnectableNetworksAndPickBest(onConnectableListener); - if (candidate == null) { // should never happen. - Log.wtf(TAG, "Unexepectedly got null"); - return null; + return; } - return candidate.wCmConfiguredNetwork; + matchMetaInfo.findConnectableNetworksAndHighestPriority(onConnectableListener); } // Add and enable this network to the central database (i.e WifiConfigManager). @@ -263,16 +245,11 @@ public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEv } /** - * Find all the connectable networks and pick the best network among the current match info - * candidates. - * - * Among the highest priority suggestions from different packages, choose the suggestion - * with the highest RSSI. - * Note: This should need to be replaced by a more sophisticated algorithm. + * Run through all connectable suggestions and nominate highest priority networks from each + * app as candidates to {@link WifiNetworkSelector}. */ - public PerNetworkSuggestionMatchMetaInfo findConnectableNetworksAndPickBest( + public void findConnectableNetworksAndHighestPriority( @NonNull OnConnectableListener onConnectableListener) { - List<PerNetworkSuggestionMatchMetaInfo> allMatchedNetworkInfos = new ArrayList<>(); for (PerAppMatchMetaInfo appInfo : mAppInfos.values()) { List<PerNetworkSuggestionMatchMetaInfo> matchedNetworkInfos = appInfo.getHighestPriorityNetworks(); @@ -292,23 +269,11 @@ public class NetworkSuggestionEvaluator implements WifiNetworkSelector.NetworkEv WifiNetworkSelector.toNetworkString( matchedNetworkInfo.wCmConfiguredNetwork))); } - allMatchedNetworkInfos.add(matchedNetworkInfo); - // Invoke onConnectable for the best networks from each app. onConnectableListener.onConnectable( matchedNetworkInfo.matchingScanDetail, - matchedNetworkInfo.wCmConfiguredNetwork, - 0); + matchedNetworkInfo.wCmConfiguredNetwork); } } - PerNetworkSuggestionMatchMetaInfo networkInfo = allMatchedNetworkInfos - .stream() - .max(Comparator.comparing(e -> e.matchingScanDetail.getScanResult().level)) - .orElse(null); - if (networkInfo == null) { // should never happen. - Log.wtf(TAG, "Unexepectedly got null"); - return null; - } - return networkInfo; } } diff --git a/service/java/com/android/server/wifi/SavedNetworkEvaluator.java b/service/java/com/android/server/wifi/SavedNetworkEvaluator.java index d3254790b..3ceafc8e6 100644 --- a/service/java/com/android/server/wifi/SavedNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/SavedNetworkEvaluator.java @@ -17,14 +17,11 @@ package com.android.server.wifi; import android.annotation.NonNull; -import android.content.Context; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.util.LocalLog; -import com.android.internal.annotations.VisibleForTesting; import com.android.server.wifi.util.TelephonyUtil; -import com.android.wifi.resources.R; import java.util.List; @@ -35,32 +32,14 @@ import java.util.List; public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluator { private static final String NAME = "SavedNetworkEvaluator"; private final WifiConfigManager mWifiConfigManager; - private final Context mContext; - private final Clock mClock; private final LocalLog mLocalLog; - private final WifiConnectivityHelper mConnectivityHelper; private final TelephonyUtil mTelephonyUtil; - private final ScoringParams mScoringParams; - /** - * Time it takes for the lastSelectionAward to decay by one point, in milliseconds - */ - @VisibleForTesting - public static final int LAST_SELECTION_AWARD_DECAY_MSEC = 60 * 1000; - - - SavedNetworkEvaluator(final Context context, ScoringParams scoringParams, - WifiConfigManager configManager, Clock clock, - LocalLog localLog, WifiConnectivityHelper connectivityHelper, + SavedNetworkEvaluator(WifiConfigManager configManager, LocalLog localLog, TelephonyUtil telephonyUtil) { - mContext = context; - mScoringParams = scoringParams; mWifiConfigManager = configManager; - mClock = clock; mLocalLog = localLog; - mConnectivityHelper = connectivityHelper; mTelephonyUtil = telephonyUtil; - } private void localLog(String log) { @@ -89,109 +68,15 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat @Override public void update(List<ScanDetail> scanDetails) { } - private int calculateBssidScore(ScanResult scanResult, WifiConfiguration network, - WifiConfiguration currentNetwork, String currentBssid, - StringBuffer sbuf) { - int score = 0; - boolean is5GHz = scanResult.is5GHz(); - boolean is6GHz = scanResult.is6GHz(); - - final int rssiScoreSlope = mContext.getResources().getInteger( - R.integer.config_wifi_framework_RSSI_SCORE_SLOPE); - final int rssiScoreOffset = mContext.getResources().getInteger( - R.integer.config_wifi_framework_RSSI_SCORE_OFFSET); - final int sameBssidAward = mContext.getResources().getInteger( - R.integer.config_wifi_framework_SAME_BSSID_AWARD); - final int sameNetworkAward = mContext.getResources().getInteger( - R.integer.config_wifi_framework_current_network_boost); - final int lastSelectionAward = mContext.getResources().getInteger( - R.integer.config_wifi_framework_LAST_SELECTION_AWARD); - final int securityAward = mContext.getResources().getInteger( - R.integer.config_wifi_framework_SECURITY_AWARD); - final int band5GHzAward = mContext.getResources().getInteger( - R.integer.config_wifi_framework_5GHz_preference_boost_factor); - final int band6GHzAward = mContext.getResources().getInteger( - R.integer.config_wifiFramework6ghzPreferenceBoostFactor); - - sbuf.append("[ ").append(scanResult.SSID).append(" ").append(scanResult.BSSID) - .append(" RSSI:").append(scanResult.level).append(" ] "); - // Calculate the RSSI score. - int rssiSaturationThreshold = mScoringParams.getGoodRssi(scanResult.frequency); - int rssi = Math.min(scanResult.level, rssiSaturationThreshold); - score += (rssi + rssiScoreOffset) * rssiScoreSlope; - sbuf.append(" RSSI score: ").append(score).append(","); - - // 5GHz band bonus. - if (is5GHz) { - score += band5GHzAward; - sbuf.append(" 5GHz bonus: ").append(band5GHzAward).append(","); - } else if (is6GHz) { - score += band6GHzAward; - sbuf.append(" 6GHz bonus: ").append(band6GHzAward).append(","); - } - - // Last user selection award. - int lastUserSelectedNetworkId = mWifiConfigManager.getLastSelectedNetwork(); - if (lastUserSelectedNetworkId != WifiConfiguration.INVALID_NETWORK_ID - && lastUserSelectedNetworkId == network.networkId) { - long timeDifference = mClock.getElapsedSinceBootMillis() - - mWifiConfigManager.getLastSelectedTimeStamp(); - if (timeDifference > 0) { - int decay = (int) (timeDifference / LAST_SELECTION_AWARD_DECAY_MSEC); - int bonus = Math.max(lastSelectionAward - decay, 0); - score += bonus; - sbuf.append(" User selection ").append(timeDifference) - .append(" ms ago, bonus: ").append(bonus).append(","); - } - } - - // Same network award. - if (currentNetwork != null && network.networkId == currentNetwork.networkId) { - score += sameNetworkAward; - sbuf.append(" Same network bonus: ").append(sameNetworkAward).append(","); - - // When firmware roaming is supported, equivalent BSSIDs (the ones under the - // same network as the currently connected one) get the same BSSID award. - if (mConnectivityHelper.isFirmwareRoamingSupported() - && currentBssid != null && !currentBssid.equals(scanResult.BSSID)) { - score += sameBssidAward; - sbuf.append(" Equivalent BSSID bonus: ").append(sameBssidAward).append(","); - } - } - - // Same BSSID award. - if (currentBssid != null && currentBssid.equals(scanResult.BSSID)) { - score += sameBssidAward; - sbuf.append(" Same BSSID bonus: ").append(sameBssidAward).append(","); - } - - // Security award. - if (!WifiConfigurationUtil.isConfigForOpenNetwork(network)) { - score += securityAward; - sbuf.append(" Secure network bonus: ").append(securityAward).append(","); - } - - sbuf.append(" ## Total score: ").append(score).append("\n"); - - return score; - } - /** - * Evaluate all the networks from the scan results and return - * the WifiConfiguration of the network chosen for connection. + * Run through all scanDetails and nominate all connectable network as candidates. * - * @return configuration of the chosen network; - * null if no network in this category is available. */ @Override - public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + public void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { - int highestScore = Integer.MIN_VALUE; - ScanResult scanResultCandidate = null; - WifiConfiguration candidate = null; - StringBuffer scoreHistory = new StringBuffer(); for (ScanDetail scanDetail : scanDetails) { ScanResult scanResult = scanDetail.getScanResult(); @@ -247,19 +132,6 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat } } - int score = calculateBssidScore(scanResult, network, currentNetwork, currentBssid, - scoreHistory); - - // Set candidate ScanResult for all saved networks to ensure that users can - // override network selection. See WifiNetworkSelector#setUserConnectChoice. - if (score > status.getCandidateScore() - || (score == status.getCandidateScore() - && status.getCandidate() != null - && scanResult.level > status.getCandidate().level)) { - mWifiConfigManager.setNetworkCandidateScanResult( - network.networkId, scanResult, score); - } - // If the network is marked to use external scores, or is an open network with // curate saved open networks enabled, do not consider it for network selection. if (network.useExternalScores) { @@ -269,29 +141,7 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat } onConnectableListener.onConnectable(scanDetail, - mWifiConfigManager.getConfiguredNetwork(network.networkId), score); - - // TODO(b/112196799) - pull into common code - if (score > highestScore - || (score == highestScore - && scanResultCandidate != null - && scanResult.level > scanResultCandidate.level)) { - highestScore = score; - scanResultCandidate = scanResult; - mWifiConfigManager.setNetworkCandidateScanResult( - network.networkId, scanResultCandidate, highestScore); - // Reload the network config with the updated info. - candidate = mWifiConfigManager.getConfiguredNetwork(network.networkId); - } - } - - if (scoreHistory.length() > 0) { - localLog("\n" + scoreHistory.toString()); - } - - if (scanResultCandidate == null) { - localLog("did not see any good candidates."); + mWifiConfigManager.getConfiguredNetwork(network.networkId)); } - return candidate; } } diff --git a/service/java/com/android/server/wifi/ScoredNetworkEvaluator.java b/service/java/com/android/server/wifi/ScoredNetworkEvaluator.java index 000d1f69e..60bbd5b82 100644 --- a/service/java/com/android/server/wifi/ScoredNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/ScoredNetworkEvaluator.java @@ -131,13 +131,13 @@ public class ScoredNetworkEvaluator implements WifiNetworkSelector.NetworkEvalua } @Override - public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + public void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { if (!mNetworkRecommendationsEnabled) { mLocalLog.log("Skipping evaluateNetworks; Network recommendations disabled."); - return null; + return; } final ScoreTracker scoreTracker = new ScoreTracker(); @@ -188,11 +188,9 @@ public class ScoredNetworkEvaluator implements WifiNetworkSelector.NetworkEvalua scoreTracker.trackExternallyScoredCandidate( scanResult, configuredNetwork, isCurrentNetwork); } - onConnectableListener.onConnectable(scanDetail, configuredNetwork, 0); + onConnectableListener.onConnectable(scanDetail, configuredNetwork); } - - - return scoreTracker.getCandidateConfiguration(onConnectableListener); + scoreTracker.getCandidateConfiguration(onConnectableListener); } /** Used to track the network with the highest score. */ @@ -347,7 +345,7 @@ public class ScoredNetworkEvaluator implements WifiNetworkSelector.NetworkEvalua candidateNetworkId); if (ans != null && mScanDetailCandidate != null) { // This is a newly created config, so we need to call onConnectable. - onConnectableListener.onConnectable(mScanDetailCandidate, ans, 0); + onConnectableListener.onConnectable(mScanDetailCandidate, ans); } return ans; } diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index bd2a435d1..aab7905a2 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -288,9 +288,8 @@ public class WifiInjector { ThroughputScorer throughputScorer = new ThroughputScorer(mScoringParams); mWifiNetworkSelector.registerCandidateScorer(throughputScorer); mWifiMetrics.setWifiNetworkSelector(mWifiNetworkSelector); - mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mScoringParams, - mWifiConfigManager, mClock, mConnectivityLocalLog, mWifiConnectivityHelper, - mTelephonyUtil); + mSavedNetworkEvaluator = new SavedNetworkEvaluator( + mWifiConfigManager, mConnectivityLocalLog, mTelephonyUtil); mWifiNetworkSuggestionsManager = new WifiNetworkSuggestionsManager(mContext, wifiHandler, this, mWifiPermissionsUtil, mWifiConfigManager, mWifiConfigStore, mWifiMetrics, mTelephonyUtil); diff --git a/service/java/com/android/server/wifi/WifiNetworkSelector.java b/service/java/com/android/server/wifi/WifiNetworkSelector.java index 433fd0b01..27e874a85 100644 --- a/service/java/com/android/server/wifi/WifiNetworkSelector.java +++ b/service/java/com/android/server/wifi/WifiNetworkSelector.java @@ -29,7 +29,6 @@ import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.text.TextUtils; import android.util.ArrayMap; -import android.util.ArraySet; import android.util.LocalLog; import android.util.Log; import android.util.Pair; @@ -125,8 +124,7 @@ public class WifiNetworkSelector { /** * WiFi Network Selector supports various categories of networks. Each category - * has an evaluator to choose the best WiFi network to connect to. Evaluators - * should be registered in order, by decreasing importance. + * has an evaluator to choose the best WiFi network to connect to. * Wifi Network Selector iterates through the registered scorers in registration order * before making a final selection from among the candidates. */ @@ -134,8 +132,7 @@ public class WifiNetworkSelector { /** * Interface for WiFi Network Evaluator * - * A network evaluator examines the scan results and recommends the - * best network in its category to connect to; it also reports the + * A network evaluator examines the scan results reports the * connectable candidates in its category for further consideration. */ public interface NetworkEvaluator { @@ -189,11 +186,8 @@ public class WifiNetworkSelector { * ephemeral networks are allowed * @param onConnectableListener callback to record all of the connectable networks * - * @return configuration of the chosen network; - * null if no network in this category is available. */ - @Nullable - WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + @Nullable void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, OnConnectableListener onConnectableListener); @@ -207,9 +201,8 @@ public class WifiNetworkSelector { * * @param scanDetail describes the specific access point * @param config is the WifiConfiguration for the network - * @param score is the score assigned by the evaluator */ - void onConnectable(ScanDetail scanDetail, WifiConfiguration config, int score); + void onConnectable(ScanDetail scanDetail, WifiConfiguration config); } } @@ -380,8 +373,8 @@ public class WifiNetworkSelector { private List<ScanDetail> filterScanResults(List<ScanDetail> scanDetails, Set<String> bssidBlacklist, boolean isConnected, String currentBssid) { - ArrayList<NetworkKey> unscoredNetworks = new ArrayList<NetworkKey>(); - List<ScanDetail> validScanDetails = new ArrayList<ScanDetail>(); + ArrayList<NetworkKey> unscoredNetworks = new ArrayList<>(); + List<ScanDetail> validScanDetails = new ArrayList<>(); StringBuffer noValidSsid = new StringBuffer(); StringBuffer blacklistedBssid = new StringBuffer(); StringBuffer lowRssi = new StringBuffer(); @@ -730,26 +723,22 @@ public class WifiNetworkSelector { // Determine the weight for the last user selection final int lastUserSelectedNetworkId = mWifiConfigManager.getLastSelectedNetwork(); final double lastSelectionWeight = calculateLastSelectionWeight(); - final ArraySet<Integer> mNetworkIds = new ArraySet<>(); - // Go through the registered network evaluators in order - WifiConfiguration selectedNetwork = null; WifiCandidates wifiCandidates = new WifiCandidates(mWifiScoreCard); if (currentNetwork != null) { wifiCandidates.setCurrent(currentNetwork.networkId, currentBssid); } for (NetworkEvaluator registeredEvaluator : mEvaluators) { localLog("About to run " + registeredEvaluator.getName() + " :"); - WifiConfiguration choice = registeredEvaluator.evaluateNetworks( + registeredEvaluator.evaluateNetworks( new ArrayList<>(mFilteredNetworks), currentNetwork, currentBssid, connected, untrustedNetworkAllowed, - (scanDetail, config, score) -> { + (scanDetail, config) -> { if (config != null) { mConnectableNetworks.add(Pair.create(scanDetail, config)); - mNetworkIds.add(config.networkId); wifiCandidates.add(scanDetail, config, registeredEvaluator.getId(), - score, + 0, (config.networkId == lastUserSelectedNetworkId) ? lastSelectionWeight : 0.0, WifiConfiguration.isMetered(config, wifiInfo), @@ -758,15 +747,6 @@ public class WifiNetworkSelector { evaluatorIdToNominatorId(registeredEvaluator.getId())); } }); - if (choice != null && !mNetworkIds.contains(choice.networkId)) { - Log.wtf(TAG, registeredEvaluator.getName() - + " failed to report choice with noConnectibleListener"); - } - if (selectedNetwork == null && choice != null) { - selectedNetwork = choice; // First one wins - localLog(registeredEvaluator.getName() + " selects " - + WifiNetworkSelector.toNetworkString(selectedNetwork)); - } } if (mConnectableNetworks.size() != wifiCandidates.size()) { @@ -802,11 +782,7 @@ public class WifiNetworkSelector { ArrayMap<Integer, Integer> experimentNetworkSelections = new ArrayMap<>(); // for metrics - final int legacySelectedNetworkId = selectedNetwork == null - ? WifiConfiguration.INVALID_NETWORK_ID - : selectedNetwork.networkId; - - int selectedNetworkId = legacySelectedNetworkId; + int selectedNetworkId = WifiConfiguration.INVALID_NETWORK_ID; // Run all the CandidateScorers boolean legacyOverrideWanted = true; @@ -837,9 +813,7 @@ public class WifiNetworkSelector { } // Update metrics about differences in the selections made by various methods - final int activeExperimentId = activeScorer == null ? LEGACY_CANDIDATE_SCORER_EXP_ID - : experimentIdFromIdentifier(activeScorer.getIdentifier()); - experimentNetworkSelections.put(LEGACY_CANDIDATE_SCORER_EXP_ID, legacySelectedNetworkId); + final int activeExperimentId = experimentIdFromIdentifier(activeScorer.getIdentifier()); for (Map.Entry<Integer, Integer> entry : experimentNetworkSelections.entrySet()) { int experimentId = entry.getKey(); @@ -851,7 +825,8 @@ public class WifiNetworkSelector { } // Get a fresh copy of WifiConfiguration reflecting any scan result updates - selectedNetwork = mWifiConfigManager.getConfiguredNetwork(selectedNetworkId); + WifiConfiguration selectedNetwork = + mWifiConfigManager.getConfiguredNetwork(selectedNetworkId); if (selectedNetwork != null && legacyOverrideWanted) { selectedNetwork = overrideCandidateWithUserConnectChoice(selectedNetwork); mLastNetworkSelectionTimeStamp = mClock.getElapsedSinceBootMillis(); diff --git a/service/java/com/android/server/wifi/hotspot2/PasspointNetworkEvaluator.java b/service/java/com/android/server/wifi/hotspot2/PasspointNetworkEvaluator.java index 6396f2c7c..1a0a7096f 100644 --- a/service/java/com/android/server/wifi/hotspot2/PasspointNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/hotspot2/PasspointNetworkEvaluator.java @@ -21,7 +21,6 @@ import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.os.Process; import android.telephony.SubscriptionManager; -import android.text.TextUtils; import android.util.LocalLog; import android.util.Pair; @@ -32,7 +31,6 @@ import com.android.server.wifi.WifiInjector; import com.android.server.wifi.WifiNetworkSelector; import com.android.server.wifi.util.ScanResultUtil; -import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -88,7 +86,7 @@ public class PasspointNetworkEvaluator implements WifiNetworkSelector.NetworkEva public void update(List<ScanDetail> scanDetails) {} @Override - public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + public void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { @@ -109,7 +107,6 @@ public class PasspointNetworkEvaluator implements WifiNetworkSelector.NetworkEva }).collect(Collectors.toList()); // Go through each ScanDetail and find the best provider for each ScanDetail. - List<PasspointNetworkCandidate> candidateList = new ArrayList<>(); for (ScanDetail scanDetail : filteredScanDetails) { ScanResult scanResult = scanDetail.getScanResult(); @@ -117,41 +114,13 @@ public class PasspointNetworkEvaluator implements WifiNetworkSelector.NetworkEva Pair<PasspointProvider, PasspointMatch> bestProvider = mPasspointManager.matchProvider(scanResult); if (bestProvider != null) { - candidateList.add(new PasspointNetworkCandidate( - bestProvider.first, bestProvider.second, scanDetail)); + WifiConfiguration candidate = createWifiConfigForProvider(bestProvider, scanDetail); + if (candidate == null) { + continue; + } + onConnectableListener.onConnectable(scanDetail, candidate); } } - - // Done if no candidate is found. - if (candidateList.isEmpty()) { - localLog("No suitable Passpoint network found"); - return null; - } - - // Find the best Passpoint network among all candidates. - PasspointNetworkCandidate bestNetwork = - findBestNetwork(candidateList, currentNetwork == null ? null : currentNetwork.SSID); - - // Return the configuration for the current connected network if it is the best network. - if (currentNetwork != null && TextUtils.equals(currentNetwork.SSID, - ScanResultUtil.createQuotedSSID(bestNetwork.mScanDetail.getSSID()))) { - localLog("Staying with current Passpoint network " + currentNetwork.SSID); - - // Update current network with the latest scan info. TODO - pull into common code - mWifiConfigManager.setNetworkCandidateScanResult(currentNetwork.networkId, - bestNetwork.mScanDetail.getScanResult(), 0); - mWifiConfigManager.updateScanDetailForNetwork(currentNetwork.networkId, - bestNetwork.mScanDetail); - onConnectableListener.onConnectable(bestNetwork.mScanDetail, currentNetwork, 0); - return currentNetwork; - } - - WifiConfiguration config = createWifiConfigForProvider(bestNetwork); - if (config != null) { - onConnectableListener.onConnectable(bestNetwork.mScanDetail, config, 0); - localLog("Passpoint network to connect to: " + config.SSID); - } - return config; } /** @@ -161,10 +130,11 @@ public class PasspointNetworkEvaluator implements WifiNetworkSelector.NetworkEva * @param networkInfo Contained information for the Passpoint network to connect to * @return {@link WifiConfiguration} */ - private WifiConfiguration createWifiConfigForProvider(PasspointNetworkCandidate networkInfo) { - WifiConfiguration config = networkInfo.mProvider.getWifiConfig(); - config.SSID = ScanResultUtil.createQuotedSSID(networkInfo.mScanDetail.getSSID()); - if (networkInfo.mMatchStatus == PasspointMatch.HomeProvider) { + private WifiConfiguration createWifiConfigForProvider( + Pair<PasspointProvider, PasspointMatch> bestProvider, ScanDetail scanDetail) { + WifiConfiguration config = bestProvider.first.getWifiConfig(); + config.SSID = ScanResultUtil.createQuotedSSID(scanDetail.getSSID()); + if (bestProvider.second == PasspointMatch.HomeProvider) { config.isHomeProviderNetwork = true; } @@ -196,45 +166,12 @@ public class PasspointNetworkEvaluator implements WifiNetworkSelector.NetworkEva mWifiConfigManager.enableNetwork(result.getNetworkId(), false, Process.WIFI_UID, null); mWifiConfigManager.setNetworkCandidateScanResult(result.getNetworkId(), - networkInfo.mScanDetail.getScanResult(), 0); + scanDetail.getScanResult(), 0); mWifiConfigManager.updateScanDetailForNetwork( - result.getNetworkId(), networkInfo.mScanDetail); + result.getNetworkId(), scanDetail); return mWifiConfigManager.getConfiguredNetwork(result.getNetworkId()); } - /** - * Given a list of Passpoint networks (with both provider and scan info), find and return - * the one with highest score. The score is calculated using - * {@link PasspointNetworkScore#calculateScore}. - * - * @param networkList List of Passpoint networks - * @param currentNetworkSsid The SSID of the currently connected network, null if not connected - * @return {@link PasspointNetworkCandidate} - */ - private PasspointNetworkCandidate findBestNetwork( - List<PasspointNetworkCandidate> networkList, String currentNetworkSsid) { - PasspointNetworkCandidate bestCandidate = null; - int bestScore = Integer.MIN_VALUE; - for (PasspointNetworkCandidate candidate : networkList) { - ScanDetail scanDetail = candidate.mScanDetail; - PasspointMatch match = candidate.mMatchStatus; - - boolean isActiveNetwork = TextUtils.equals(currentNetworkSsid, - ScanResultUtil.createQuotedSSID(scanDetail.getSSID())); - int score = PasspointNetworkScore.calculateScore(match == PasspointMatch.HomeProvider, - scanDetail, mPasspointManager.getANQPElements(scanDetail.getScanResult()), - isActiveNetwork); - - if (score > bestScore) { - bestCandidate = candidate; - bestScore = score; - } - } - localLog("Best Passpoint network " + bestCandidate.mScanDetail.getSSID() + " provided by " - + bestCandidate.mProvider.getConfig().getHomeSp().getFqdn()); - return bestCandidate; - } - private void localLog(String log) { mLocalLog.log(log); } diff --git a/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java index 447cdc2d8..f9b53a17a 100644 --- a/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/CarrierNetworkEvaluatorTest.java @@ -17,7 +17,6 @@ package com.android.server.wifi; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; @@ -199,13 +198,8 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { } /** - * Baseline positive test case: carrier Wi-Fi enabled (have cert), present >1 Carrier networks - * of varying RSSI, include some none carrier networks with even better RSSI and some saved - * carrier networks (one of which is ephemeral). - * * Desired behavior: * - all Carrier Wi-Fi (including all saved networks) as connectable - * - best Carrier Wi-Fi (highest RSSI) as return value */ @Test public void testSelectOneFromMultiple() { @@ -226,11 +220,11 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { configureNewSsid(CARRIER_SAVED_EPH_NET_ID, scanDetails.get(3), true, false); configureNewSsid(NON_CARRIER_NET_ID, scanDetails.get(4), false, true); - WifiConfiguration selected = mDut.evaluateNetworks(scanDetails, null, null, false, false, + mDut.evaluateNetworks(scanDetails, null, null, false, false, mConnectableListener); verify(mConnectableListener, times(4)).onConnectable(mScanDetailCaptor.capture(), - mWifiConfigCaptor.capture(), anyInt()); + mWifiConfigCaptor.capture()); assertEquals(4, mScanDetailCaptor.getAllValues().size()); assertEquals(CARRIER1_SSID.replace("\"", ""), @@ -260,16 +254,12 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { assertEquals(CARRIER_SAVED_EPH_SSID, config4.SSID); assertTrue(config4.isEphemeral()); assertTrue(config4.allowedKeyManagement.get(WifiConfiguration.KeyMgmt.WPA_EAP)); - - assertEquals(config2.getKey(), selected.getKey()); // SSID2 has the highest RSSI - assertEquals("", selected.enterpriseConfig.getAnonymousIdentity()); - assertTrue(TelephonyUtil.isSimEapMethod(selected.enterpriseConfig.getEapMethod())); } /** * Cert installed and no Carrier Wi-Fi visible * - * Desired behavior: no networks connectable or selected + * Desired behavior: no networks connectable. */ @Test public void testSelectFromNoneAvailable() { @@ -283,17 +273,16 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { freqs, caps, levels, mClock); configureNewSsid(NON_CARRIER_NET_ID, scanDetails.get(0), false, true); - WifiConfiguration selected = mDut.evaluateNetworks(scanDetails, null, null, false, false, + mDut.evaluateNetworks(scanDetails, null, null, false, false, mConnectableListener); - verify(mConnectableListener, never()).onConnectable(any(), any(), anyInt()); - assertNull(selected); + verify(mConnectableListener, never()).onConnectable(any(), any()); } /** * Multiple carrier Wi-Fi networks visible but no cert installed. * - * Desired behavior: no networks connectable or selected + * Desired behavior: no networks connectable. */ @Test public void testNoCarrierCert() { @@ -316,18 +305,17 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { configureNewSsid(CARRIER_SAVED_EPH_NET_ID, scanDetails.get(3), true, false); configureNewSsid(NON_CARRIER_NET_ID, scanDetails.get(4), false, true); - WifiConfiguration selected = mDut.evaluateNetworks(scanDetails, null, null, false, false, + mDut.evaluateNetworks(scanDetails, null, null, false, false, mConnectableListener); - verify(mConnectableListener, never()).onConnectable(any(), any(), anyInt()); - assertNull(selected); + verify(mConnectableListener, never()).onConnectable(any(), any()); } /** * One carrier Wi-Fi networks visible and cert installed but user has previously forgotten the * network. * - * Desired behavior: no networks connectable or selected + * Desired behavior: no networks connectable. */ @Test public void testAvailableButPreviouslyUserDeleted() { @@ -345,17 +333,16 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { freqs, caps, levels, mClock); configureNewSsid(CARRIER1_NET_ID, scanDetails.get(0), true, false); - WifiConfiguration selected = mDut.evaluateNetworks(scanDetails, null, null, false, false, + mDut.evaluateNetworks(scanDetails, null, null, false, false, mConnectableListener); - verify(mConnectableListener, never()).onConnectable(any(), any(), anyInt()); - assertNull(selected); + verify(mConnectableListener, never()).onConnectable(any(), any()); } /** * One carrier Wi-Fi networks visible and cert installed but ssid is blacklisted. * - * Desired behavior: no networks connectable or selected + * Desired behavior: no networks connectable. */ @Test public void testAvailableButBlacklisted() { @@ -380,18 +367,17 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { when(mWifiConfigManager.tryEnableNetwork(CARRIER1_NET_ID)) .thenReturn(false); - WifiConfiguration selected = mDut.evaluateNetworks(scanDetails, null, null, false, false, + mDut.evaluateNetworks(scanDetails, null, null, false, false, mConnectableListener); verify(mWifiConfigManager).getConfiguredNetwork(eq(blacklisted.getKey())); - verify(mConnectableListener, never()).onConnectable(any(), any(), anyInt()); - assertNull(selected); + verify(mConnectableListener, never()).onConnectable(any(), any()); } /** * One carrier Wi-Fi network that is visible and supports encrypted IMSI. * - * Desired behavior: selected network supports encrypted IMSI by using EAP-SIM/AKA/AKA' + * Desired behavior: nominated network supports encrypted IMSI by using EAP-SIM/AKA/AKA' * and has an empty anonymous identity. The anonymous identity will be populated with * {@code anonymous@<realm>} by ClientModeImpl's handling of the * {@link ClientModeImpl#CMD_START_CONNECT} event. @@ -409,11 +395,13 @@ public class CarrierNetworkEvaluatorTest extends WifiBaseTest { WifiConfiguration carrierConfig = configureNewSsid(CARRIER1_NET_ID, scanDetails.get(0), true, false); - WifiConfiguration selected = mDut.evaluateNetworks(scanDetails, null, null, false, false, + mDut.evaluateNetworks(scanDetails, null, null, false, false, mConnectableListener); - assertEquals(carrierConfig.getKey(), selected.getKey()); - assertEquals("", selected.enterpriseConfig.getAnonymousIdentity()); - assertTrue(TelephonyUtil.isSimEapMethod(selected.enterpriseConfig.getEapMethod())); + verify(mConnectableListener).onConnectable(any(), mWifiConfigCaptor.capture()); + assertEquals(carrierConfig.getKey(), mWifiConfigCaptor.getValue().getKey()); + assertEquals("", mWifiConfigCaptor.getValue().enterpriseConfig.getAnonymousIdentity()); + assertTrue(TelephonyUtil + .isSimEapMethod(mWifiConfigCaptor.getValue().enterpriseConfig.getEapMethod())); } } diff --git a/tests/wifitests/src/com/android/server/wifi/NetworkSuggestionEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/NetworkSuggestionEvaluatorTest.java index b130ca794..16fbd09d2 100644 --- a/tests/wifitests/src/com/android/server/wifi/NetworkSuggestionEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/NetworkSuggestionEvaluatorTest.java @@ -16,6 +16,7 @@ package com.android.server.wifi; +import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID; import static android.net.wifi.WifiConfiguration.NetworkSelectionStatus .NETWORK_SELECTION_TEMPORARY_DISABLED; @@ -76,7 +77,6 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { /** * Ensure that we ignore all scan results not matching the network suggestion. - * Expected candidate: null * Expected connectable Networks: {} */ @Test @@ -102,19 +102,17 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { linkScanDetailsWithNetworkSuggestions(scanDetails, suggestions); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNull(candidate); assertTrue(connectableNetworks.isEmpty()); } /** - * Ensure that we select the only matching network suggestion. - * Expected candidate: suggestionSsids[0] + * Ensure that we nominate the only matching network suggestion. * Expected connectable Networks: {suggestionSsids[0]} */ @Test @@ -142,14 +140,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { setupAddToWifiConfigManager(suggestions[0].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[0] , candidate.SSID); validateConnectableNetworks(connectableNetworks, scanSsids[0]); @@ -157,9 +153,7 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we select the network suggestion corresponding to the scan result with - * highest RSSI. - * Expected candidate: suggestionSsids[1] + * Ensure that we nominate the all network suggestion corresponding to the scan results * Expected connectable Networks: {suggestionSsids[0], suggestionSsids[1]} */ @Test @@ -188,15 +182,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { suggestions[1].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[1] , candidate.SSID); - validateConnectableNetworks(connectableNetworks, scanSsids[0], scanSsids[1]); verifyAddToWifiConfigManager(suggestions[1].wns.wifiConfiguration, @@ -204,10 +195,9 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we select the network suggestion corresponding to the scan result with + * Ensure that we nominate the network suggestion corresponding to the scan result with * higest priority. - * Expected candidate: suggestionSsids[0] - * Expected connectable Networks: {suggestionSsids[0], suggestionSsids[1]} + * Expected connectable Networks: {suggestionSsids[0]} */ @Test public void testSelectNetworkSuggestionForMultipleMatchHighPriorityWins() { @@ -235,26 +225,20 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { suggestions[1].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[0] , candidate.SSID); - validateConnectableNetworks(connectableNetworks, scanSsids[0]); verifyAddToWifiConfigManager(suggestions[0].wns.wifiConfiguration); } /** - * Ensure that we select the network suggestion corresponding to the scan result with - * highest RSSI. The lower RSSI scan result has multiple matching suggestions - * (should pick any one in the connectable networks). + * Ensure that we nominate one network when multiple suggestor suggested same network. * - * Expected candidate: suggestionSsids[0] * Expected connectable Networks: {suggestionSsids[0], * (suggestionSsids[1] || suggestionSsids[2]} */ @@ -285,15 +269,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { suggestions[1].wns.wifiConfiguration, suggestions[2].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[0] , candidate.SSID); - validateConnectableNetworks(connectableNetworks, scanSsids); verifyAddToWifiConfigManager(suggestions[0].wns.wifiConfiguration, @@ -301,14 +282,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we select the network suggestion with the higest priority among network - * suggestions from the same package. Among different packages, pick the suggestion - * corresponding to the scan result with highest RSSI. + * Ensure that we nominate the network suggestion with the higest priority among network + * suggestions from the same package. Among different packages, nominate all the suggestion + * corresponding to the scan result. * - * The suggestion[1] has higher priority than suggestion[0] even though it has lower RSSI than - * suggestion[0]. + * The suggestion[1] has higher priority than suggestion[0]. * - * Expected candidate: suggestionSsids[1] * Expected connectable Networks: {suggestionSsids[1], * (suggestionSsids[2], * suggestionSsids[3]} @@ -345,15 +324,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { suggestions[3].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[1] , candidate.SSID); - validateConnectableNetworks(connectableNetworks, scanSsids[1], scanSsids[2], scanSsids[3]); verifyAddToWifiConfigManager(suggestions[1].wns.wifiConfiguration, @@ -361,10 +337,10 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we select the only matching network suggestion, but return null because - * we failed the {@link WifiConfigManager} interactions. - * Expected candidate: null. - * Expected connectable Networks: {suggestionSsids[0], suggestionSsids[1]} + * Ensure that we nominate no candidate if the only matching network suggestion, but we failed + * the {@link WifiConfigManager} interactions. + * + * Expected connectable Networks: {} */ @Test public void testSelectNetworkSuggestionForOneMatchButFailToAddToWifiConfigManager() { @@ -389,16 +365,15 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { linkScanDetailsWithNetworkSuggestions(scanDetails, suggestions); // Fail add to WifiConfigManager when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt(), anyString())) - .thenReturn(new NetworkUpdateResult(WifiConfiguration.INVALID_NETWORK_ID)); + .thenReturn(new NetworkUpdateResult(INVALID_NETWORK_ID)); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNull(candidate); assertTrue(connectableNetworks.isEmpty()); verify(mWifiConfigManager, times(scanSsids.length)) @@ -412,9 +387,9 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we select the only matching network suggestion, but that matches an existing + * Ensure that we nominate the only matching network suggestion, but that matches an existing * saved network (maybe saved or maybe it exists from a previous connection attempt) . - * Expected candidate: suggestionSsids[0] + * * Expected connectable Networks: {suggestionSsids[0]} */ @Test @@ -447,21 +422,19 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { .thenReturn(suggestions[0].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[0] , candidate.SSID); - validateConnectableNetworks(connectableNetworks, new String[] {scanSsids[0]}); // check for any saved networks. verify(mWifiConfigManager, times(scanSsids.length)) .wasEphemeralNetworkDeleted(anyString()); - verify(mWifiConfigManager).getConfiguredNetwork(candidate.getKey()); + verify(mWifiConfigManager) + .getConfiguredNetwork(suggestions[0].wns.wifiConfiguration.getKey()); verify(mWifiConfigManager).addOrUpdateNetwork(eq(suggestions[0].wns.wifiConfiguration), eq(suggestions[0].perAppInfo.uid), eq(suggestions[0].perAppInfo.packageName)); verify(mWifiConfigManager).getConfiguredNetwork( @@ -472,9 +445,9 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we don't select the only matching network suggestion if it was previously + * Ensure that we don't nominate the only matching network suggestion if it was previously * disabled by the user. - * Expected candidate: null + * * Expected connectable Networks: {} */ @Test @@ -504,27 +477,24 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { when(mWifiConfigManager.wasEphemeralNetworkDeleted(suggestionSsids[0])).thenReturn(true); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { - connectableNetworks.add(Pair.create(scanDetail, configuration)); - }); + (ScanDetail scanDetail, WifiConfiguration configuration) -> + connectableNetworks.add(Pair.create(scanDetail, configuration))); - assertNull(candidate); assertTrue(connectableNetworks.isEmpty()); - verify(mWifiConfigManager, times(scanSsids.length)) - .wasEphemeralNetworkDeleted(anyString()); + verify(mWifiConfigManager, times(scanSsids.length)).wasEphemeralNetworkDeleted(anyString()); // Verify we did not try to add any new networks or other interactions with // WifiConfigManager. verifyNoMoreInteractions(mWifiConfigManager); } /** - * Ensure that we don't select the only matching network suggestion if the network configuration - * already exists (maybe saved or maybe it exists from a previous connection attempt) and - * blacklisted. - * Expected candidate: null + * Ensure that we don't nominate the only matching network suggestion if the network + * configuration already exists (maybe saved or maybe it exists from a previous connection + * attempt) and blacklisted. + * * Expected connectable Networks: {} */ @Test @@ -560,13 +530,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { .thenReturn(suggestions[0].wns.wifiConfiguration); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNull(candidate); assertTrue(connectableNetworks.isEmpty()); verify(mWifiConfigManager, times(scanSsids.length)) @@ -585,10 +554,10 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { } /** - * Ensure that we do select the only matching network suggestion if the network configuration + * Ensure that we do nominate the only matching network suggestion if the network configuration * already exists (maybe saved or maybe it exists from a previous connection attempt) and a * temporary blacklist expired. - * Expected candidate: suggestionSsids[0] + * * Expected connectable Networks: {suggestionSsids[0]} */ @Test @@ -626,15 +595,12 @@ public class NetworkSuggestionEvaluatorTest extends WifiBaseTest { .thenReturn(true); List<Pair<ScanDetail, WifiConfiguration>> connectableNetworks = new ArrayList<>(); - WifiConfiguration candidate = mNetworkSuggestionEvaluator.evaluateNetworks( + mNetworkSuggestionEvaluator.evaluateNetworks( Arrays.asList(scanDetails), null, null, true, false, - (ScanDetail scanDetail, WifiConfiguration configuration, int score) -> { + (ScanDetail scanDetail, WifiConfiguration configuration) -> { connectableNetworks.add(Pair.create(scanDetail, configuration)); }); - assertNotNull(candidate); - assertEquals(suggestionSsids[0] , candidate.SSID); - validateConnectableNetworks(connectableNetworks, new String[] {scanSsids[0]}); verify(mWifiConfigManager, times(scanSsids.length)) diff --git a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java index 4f8170d0a..e34786997 100644 --- a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java @@ -19,25 +19,20 @@ package com.android.server.wifi; import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_NONE; import static com.android.server.wifi.WifiConfigurationTestUtil.SECURITY_PSK; -import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import android.content.Context; -import android.content.res.Resources; -import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; -import android.os.SystemClock; import android.test.suitebuilder.annotation.SmallTest; import android.util.LocalLog; import com.android.server.wifi.WifiNetworkSelector.NetworkEvaluator.OnConnectableListener; import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs; import com.android.server.wifi.util.TelephonyUtil; -import com.android.wifi.resources.R; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -53,30 +48,10 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - setupContext(); - setupResource(); - setupWifiConfigManager(); mLocalLog = new LocalLog(512); - when(mWifiConnectivityHelper.isFirmwareRoamingSupported()).thenReturn(false); - when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime()); - - mThresholdMinimumRssi2G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz); - mThresholdMinimumRssi5G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz); - mThresholdQualifiedRssi2G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz); - mThresholdQualifiedRssi5G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz); - mThresholdSaturatedRssi2G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz); - mThresholdSaturatedRssi5G = mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz); - - mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, - new ScoringParams(mContext), mWifiConfigManager, - mClock, mLocalLog, mWifiConnectivityHelper, mTelephonyUtil); + mSavedNetworkEvaluator = new SavedNetworkEvaluator(mWifiConfigManager, mLocalLog, + mTelephonyUtil); } /** Cleans up test. */ @@ -85,75 +60,18 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { validateMockitoUsage(); } + private ArgumentCaptor<WifiConfiguration> mWifiConfigurationArgumentCaptor = + ArgumentCaptor.forClass(WifiConfiguration.class); private static final int INVALID_SUBID = 1; private static final int TEST_CARRIER_ID = 100; + private static final int RSSI_LEVEL = -50; private SavedNetworkEvaluator mSavedNetworkEvaluator; @Mock private WifiConfigManager mWifiConfigManager; - @Mock private WifiConnectivityHelper mWifiConnectivityHelper; - @Mock private Context mContext; - @Mock private Resources mResource; @Mock private Clock mClock; @Mock private OnConnectableListener mOnConnectableListener; @Mock private TelephonyUtil mTelephonyUtil; private LocalLog mLocalLog; - private int mThresholdMinimumRssi2G; - private int mThresholdMinimumRssi5G; - private int mThresholdQualifiedRssi2G; - private int mThresholdQualifiedRssi5G; - private int mThresholdSaturatedRssi2G; - private int mThresholdSaturatedRssi5G; - - private void setupContext() { - when(mContext.getResources()).thenReturn(mResource); - } - - private void setupResource() { - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz)) - .thenReturn(-57); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz)) - .thenReturn(-60); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz)) - .thenReturn(-70); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz)) - .thenReturn(-73); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz)) - .thenReturn(-82); - when(mResource.getInteger( - R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz)) - .thenReturn(-85); - when(mResource.getInteger( - R.integer.config_wifi_framework_RSSI_SCORE_SLOPE)) - .thenReturn(4); - when(mResource.getInteger( - R.integer.config_wifi_framework_RSSI_SCORE_OFFSET)) - .thenReturn(85); - when(mResource.getInteger( - R.integer.config_wifi_framework_SAME_BSSID_AWARD)) - .thenReturn(24); - when(mResource.getInteger( - R.integer.config_wifi_framework_SECURITY_AWARD)) - .thenReturn(80); - when(mResource.getInteger( - R.integer.config_wifi_framework_5GHz_preference_boost_factor)) - .thenReturn(16); - when(mResource.getInteger( - R.integer.config_wifiFramework6ghzPreferenceBoostFactor)) - .thenReturn(16); - when(mResource.getInteger( - R.integer.config_wifi_framework_current_network_boost)) - .thenReturn(16); - } - - private void setupWifiConfigManager() { - when(mWifiConfigManager.getLastSelectedNetwork()) - .thenReturn(WifiConfiguration.INVALID_NETWORK_ID); - } /** * Do not evaluate networks that {@link WifiConfiguration#useExternalScores}. @@ -164,7 +82,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2470, 2437}; String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + int[] levels = {RSSI_LEVEL, RSSI_LEVEL}; int[] securities = {SECURITY_PSK, SECURITY_PSK}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = @@ -176,10 +94,10 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { wifiConfiguration.useExternalScores = true; } - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, true, false, mOnConnectableListener); - assertNull(candidate); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } /** @@ -190,7 +108,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { String[] ssids = {"\"test1\""}; String[] bssids = {"6c:f3:7f:ae:8c:f3"}; int[] freqs = {2470}; - int[] levels = {mThresholdQualifiedRssi2G + 8}; + int[] levels = {RSSI_LEVEL}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigForEapSimNetwork(ssids, bssids, @@ -203,10 +121,10 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { .thenReturn(INVALID_SUBID); when(mTelephonyUtil.isSimPresent(eq(INVALID_SUBID))).thenReturn(false); - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, true, false, mOnConnectableListener); - assertNull(candidate); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } /** @@ -218,7 +136,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2470, 2437}; String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + int[] levels = {RSSI_LEVEL, RSSI_LEVEL}; int[] securities = {SECURITY_NONE, SECURITY_NONE}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = @@ -230,10 +148,10 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { wifiConfiguration.ephemeral = true; } - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, true, false, mOnConnectableListener); - assertNull(candidate); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } /** @@ -246,7 +164,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2470, 2437}; String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + int[] levels = {RSSI_LEVEL, RSSI_LEVEL}; int[] securities = {SECURITY_NONE, SECURITY_NONE}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = @@ -255,22 +173,18 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, true, false, mOnConnectableListener); - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - + verify(mOnConnectableListener, times(2)).onConnectable(any(), any()); + reset(mOnConnectableListener); savedConfigs[1].allowAutojoin = false; - candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, + mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, true, false, mOnConnectableListener); - - chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); + verify(mOnConnectableListener).onConnectable(any(), + mWifiConfigurationArgumentCaptor.capture()); + WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], + mWifiConfigurationArgumentCaptor.getValue()); } /** @@ -282,7 +196,7 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; int[] freqs = {2470, 2437}; String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; + int[] levels = {RSSI_LEVEL, RSSI_LEVEL}; int[] securities = {SECURITY_NONE, SECURITY_NONE}; ScanDetailsAndWifiConfigs scanDetailsAndConfigs = @@ -294,358 +208,9 @@ public class SavedNetworkEvaluatorTest extends WifiBaseTest { wifiConfiguration.allowAutojoin = false; } - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, true, false, mOnConnectableListener); - - assertNull(candidate); - } - - /** - * Set the candidate {@link ScanResult} for all {@link WifiConfiguration}s regardless of - * whether they are secure saved, open saved, or {@link WifiConfiguration#useExternalScores}. - */ - @Test - public void setCandidateScanResultsForAllSavedNetworks() { - String[] ssids = {"\"test1\"", "\"test2\"", "\"test3\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "6c:f3:7f:ae:8c:f5"}; - int[] freqs = {5200, 5220, 5240}; - String[] caps = {"[WPA2-PSK][ESS]", "[ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = - {mThresholdQualifiedRssi5G, mThresholdQualifiedRssi5G, mThresholdQualifiedRssi5G}; - int[] securities = {SECURITY_PSK, SECURITY_NONE, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration useExternalScoresConfig = scanDetailsAndConfigs.getWifiConfigs()[0]; - useExternalScoresConfig.useExternalScores = true; - WifiConfiguration openNetworkConfig = scanDetailsAndConfigs.getWifiConfigs()[1]; - WifiConfiguration secureNetworkConfig = scanDetailsAndConfigs.getWifiConfigs()[2]; - mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, true, false, mOnConnectableListener); - verify(mWifiConfigManager, atLeastOnce()).setNetworkCandidateScanResult( - eq(useExternalScoresConfig.networkId), - eq(scanDetails.get(0).getScanResult()), - anyInt()); - verify(mWifiConfigManager, atLeastOnce()).setNetworkCandidateScanResult( - eq(openNetworkConfig.networkId), - eq(scanDetails.get(1).getScanResult()), - anyInt()); - verify(mWifiConfigManager, atLeastOnce()).setNetworkCandidateScanResult( - eq(secureNetworkConfig.networkId), - eq(scanDetails.get(2).getScanResult()), - anyInt()); - } - - /** - * Between two 2G networks, choose the one with stronger RSSI value if other conditions - * are the same and the RSSI values are not saturated. - */ - @Test - public void chooseStrongerRssi2GNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, true, false, mOnConnectableListener); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - } - - /** - * Between two 5G networks, choose the one with stronger RSSI value if other conditions - * are the same and the RSSI values are not saturated. - */ - @Test - public void chooseStrongerRssi5GNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {5200, 5240}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = {mThresholdQualifiedRssi5G + 8, mThresholdQualifiedRssi5G + 10}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, true, false, mOnConnectableListener); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - } - - /** - * Choose secure network over open network if other conditions are the same. - */ - @Test - public void chooseSecureNetworkOverOpenNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {5200, 5240}; - String[] caps = {"[ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = {mThresholdQualifiedRssi5G, mThresholdQualifiedRssi5G}; - int[] securities = {SECURITY_NONE, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, true, false, mOnConnectableListener); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - } - - /** - * Choose 5G network over 2G network if other conditions are the same. - */ - @Test - public void choose5GNetworkOver2GNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2437, 5240}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G, mThresholdQualifiedRssi5G}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, true, false, mOnConnectableListener); - - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - } - - /** - * Verify that we stick to the currently connected network if the other one is - * just slightly better scored. - */ - @Test - public void stickToCurrentNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {5200, 5240}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - // test2 has slightly stronger RSSI value than test1 - int[] levels = {mThresholdMinimumRssi5G + 2, mThresholdMinimumRssi5G + 4}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - // Simuluate we are connected to SSID test1 already. - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - savedConfigs[0], null, true, false, mOnConnectableListener); - - // Even though test2 has higher RSSI value, test1 is chosen because of the - // currently connected network bonus. - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - } - - /** - * Verify that we stick to the currently connected BSSID if the other one is - * just slightly better scored. - */ - @Test - public void stickToCurrentBSSID() { - // Same SSID - String[] ssids = {"\"test1\"", "\"test1\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {5200, 5240}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - // test2 has slightly stronger RSSI value than test1 - int[] levels = {mThresholdMinimumRssi5G + 2, mThresholdMinimumRssi5G + 6}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - // Simuluate we are connected to BSSID "6c:f3:7f:ae:8c:f3" already - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, bssids[0], true, false, mOnConnectableListener); - - // Even though test2 has higher RSSI value, test1 is chosen because of the - // currently connected BSSID bonus. - ScanResult chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - } - - /** - * Verify that the same BSSID award is applied to all the BSSIDs which are under the same - * network as the currently connected BSSID. - */ - @Test - public void currentBssidAwardForAllBssidsWithinTheSameNetworkWhenFirmwareRoamingSupported() { - // Three BSSIDs are carefully setup there: - // BSSID_0 and BSSID_1 have the same SSID and security type, so they are considered under - // the same 2.4 GHz network. BSSID_1 RSSI is stronger than BSSID_0. - // BSSID_2 is under a 5GHz network different from BSSID_0 and BSSID_1. Its RSSI is - // slightly stronger than BSSID_1. - // - // When firmware roaming is not supported, BSSID_2 has higher score than BSSID_0 and - // BSSID_1. - // When firmware roaming is suported, BSSID_1 has higher score than BSSID_2 because the - // same BSSID award is now applied to both BSSID_0 and BSSID_1. - String[] ssids = {"\"test1\"", "\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4", "6c:f3:7f:ae:8c:f5"}; - int[] freqs = {2470, 2437, 5200}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] levels = {mThresholdMinimumRssi2G + 2, mThresholdMinimumRssi2G + 5, - mThresholdMinimumRssi5G + 7}; - int[] securities = {SECURITY_PSK, SECURITY_PSK, SECURITY_PSK}; - - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - - // Firmware roaming is not supported. - when(mWifiConnectivityHelper.isFirmwareRoamingSupported()).thenReturn(false); - // Simuluate we are connected to BSSID_0 already. - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - savedConfigs[0], bssids[0], true, false, mOnConnectableListener); - // Verify that BSSID_2 is chosen. - ScanResult chosenScanResult = scanDetails.get(2).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[2], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - - // Firmware roaming is supported. - when(mWifiConnectivityHelper.isFirmwareRoamingSupported()).thenReturn(true); - // Simuluate we are connected to BSSID_0 already. - candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - savedConfigs[0], bssids[0], true, false, mOnConnectableListener); - // Verify that BSSID_1 is chosen. - chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - } - - /** - * One 2.4GHz network and one 5GHz network have the same security type. Perform - * the following tests to verify that once across the RSSI saturation threshold - * stronger RSSI value doesn't increase network score. - * - * 1) Both 2.4GHz network and 5GHz network have the same RSSI value, - * mThresholdQualifiedRssi2G, which is below the saturation threshold. 5GHz - * network is chosen because of the 5G band award. - * 2) Bump up 2.4GHz network RSSI 20dBm higher. Verify that it helps the 2.4GHz network - * score and it gets chosen over the 5GHz network. - * 3) Bring both 2.4GHz network and 5GHz network RSSI value to mThresholdSaturatedRssi2G. - * Verify that 5GHz network is chosen because of the 5G band award. - * 4) Bump up 2.4GHz network RSSI to be 20dBm higher than mThresholdSaturatedRssi2G. - * Verify that the incresed RSSI doesn't help 2.4GHz network score and 5GHz network - * is still chosen. - */ - @Test - public void saturatedRssiAddsNoWeightToNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2437, 5400}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - - // 1) The RSSI of both networks is mThresholdQualifiedRssi2G - int[] levels = {mThresholdQualifiedRssi2G, mThresholdQualifiedRssi2G}; - ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - WifiConfiguration candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, false, false, mOnConnectableListener); - // Verify that 5GHz network is chosen because of 5G band award - ScanResult chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - - // 2) Bump up 2.4GHz network RSSI by 20dBm. - levels[0] = mThresholdQualifiedRssi2G + 20; - scanDetailsAndConfigs = WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, - bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); - scanDetails = scanDetailsAndConfigs.getScanDetails(); - savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener); - // Verify that 2.4GHz network is chosen because of much higher RSSI value - chosenScanResult = scanDetails.get(0).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - - // 3) Bring both 2.4GHz network and 5GHz network RSSI to mThresholdSaturatedRssi2G - levels[0] = levels[1] = mThresholdSaturatedRssi2G; - scanDetailsAndConfigs = WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, - bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); - scanDetails = scanDetailsAndConfigs.getScanDetails(); - savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener); - // Verify that 5GHz network is chosen because of 5G band award - chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); - - // 4) Bump 2.4GHz network RSSI to be 20dBm higher than mThresholdSaturatedRssi2G - levels[0] = mThresholdSaturatedRssi2G + 20; - scanDetailsAndConfigs = WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, - bssids, freqs, caps, levels, securities, mWifiConfigManager, mClock); - scanDetails = scanDetailsAndConfigs.getScanDetails(); - savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - candidate = mSavedNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener); - // Verify that the increased RSSI doesn't help 2.4GHz network and 5GHz network - // is still chosen - chosenScanResult = scanDetails.get(1).getScanResult(); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - chosenScanResult, candidate); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } } diff --git a/tests/wifitests/src/com/android/server/wifi/ScoredNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/ScoredNetworkEvaluatorTest.java index 6a2718ef2..2414b684c 100644 --- a/tests/wifitests/src/com/android/server/wifi/ScoredNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ScoredNetworkEvaluatorTest.java @@ -293,121 +293,26 @@ public class ScoredNetworkEvaluatorTest extends WifiBaseTest { int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; Integer[] scores = {null, 120}; boolean[] meteredHints = {false, false}; - List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( ssids, bssids, freqs, caps, levels, mClock); WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, scanDetails, scores, meteredHints); - ScanResult scanResult = scanDetails.get(1).getScanResult(); WifiConfiguration ephemeralNetworkConfig = WifiNetworkSelectorTestUtil .setupEphemeralNetwork(mWifiConfigManager, 1, scanDetails.get(1), meteredHints[1]); - // No saved networks. when(mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(any(ScanDetail.class))) .thenReturn(null); - // But when we create one, this is should be it. when(mWifiConfigManager.addOrUpdateNetwork(any(), anyInt())) .thenReturn(new NetworkUpdateResult(1)); - // Untrusted networks allowed. - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, false, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfig, candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanResult, candidate); - assertEquals(meteredHints[1], candidate.meteredHint); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - } - - /** - * When no saved networks available, choose the available ephemeral networks - * if untrusted networks are allowed. - */ - @Test - public void testEvaluateNetworks_chooseEphemeralNetworkBecauseOfNoSavedNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[WPA2-PSK][ESS]", "[ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 10}; - Integer[] scores = {null, 120}; - boolean[] meteredHints = {false, true}; - - List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( - ssids, bssids, freqs, caps, levels, mClock); - WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, - scanDetails, scores, meteredHints); - - // No saved networks. - when(mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(any(ScanDetail.class))) - .thenReturn(null); - - ScanResult scanResult = scanDetails.get(1).getScanResult(); - WifiConfiguration ephemeralNetworkConfig = WifiNetworkSelectorTestUtil - .setupEphemeralNetwork(mWifiConfigManager, 1, scanDetails.get(1), meteredHints[1]); - - // Untrusted networks allowed. - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, false, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfig, candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanResult, candidate); - assertEquals(meteredHints[1], candidate.meteredHint); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - } - - /** - * When no saved networks available, choose the highest scored ephemeral networks - * if untrusted networks are allowed. - */ - @Test - public void testEvaluateNetworks_chooseHigherScoredEphemeralNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; - Integer[] scores = {100, 120}; - boolean[] meteredHints = {true, true}; - ScanResult[] scanResults = new ScanResult[2]; - WifiConfiguration[] ephemeralNetworkConfigs = new WifiConfiguration[2]; - - List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil.buildScanDetails( - ssids, bssids, freqs, caps, levels, mClock); - WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, - scanDetails, scores, meteredHints); - - // No saved networks. - when(mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(any(ScanDetail.class))) - .thenReturn(null); - - for (int i = 0; i < 2; i++) { - scanResults[i] = scanDetails.get(i).getScanResult(); - ephemeralNetworkConfigs[i] = WifiNetworkSelectorTestUtil.setupEphemeralNetwork( - mWifiConfigManager, i, scanDetails.get(i), meteredHints[i]); - } - - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, + mScoredNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanResults[1], candidate); - assertEquals(meteredHints[1], candidate.meteredHint); verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); + .onConnectable(any(), mWifiConfigCaptor.capture()); assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); + .anyMatch(c -> c.networkId == ephemeralNetworkConfig.networkId)); } /** @@ -437,10 +342,10 @@ public class ScoredNetworkEvaluatorTest extends WifiBaseTest { mWifiConfigManager, 1, scanDetails.get(1), meteredHints[1]); // Untrusted networks not allowed. - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, + mScoredNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, false, mOnConnectableListener); - assertEquals("Expect null configuration", null, candidate); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } /** @@ -467,146 +372,11 @@ public class ScoredNetworkEvaluatorTest extends WifiBaseTest { WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, scanDetails, scores, meteredHints); - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, + mScoredNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, true, mOnConnectableListener); - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanDetails.get(0).getScanResult(), candidate); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - } - - /** - * Choose externally scored saved network with higher score. - */ - @Test - public void testEvaluateNetworks_chooseSavedNetworkWithHigherExternalScore() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; - Integer[] scores = {100, 120}; - boolean[] meteredHints = {false, false}; - - WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - savedConfigs[0].useExternalScores = savedConfigs[1].useExternalScores = true; - - WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, - scanDetails, scores, meteredHints); - - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, false, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanDetails.get(1).getScanResult(), candidate); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - } - - /** - * Prefer externally scored saved network over untrusted network when they have - * the same score. - */ - @Test - public void testEvaluateNetworks_chooseExternallyScoredOverUntrustedNetworksWithSameScore() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[WPA2-PSK][ESS]", "[ESS]"}; - int[] securities = {SECURITY_PSK, SECURITY_NONE}; - int[] levels = {mThresholdQualifiedRssi2G + 8, mThresholdQualifiedRssi2G + 8}; - Integer[] scores = {120, 120}; - boolean[] meteredHints = {false, true}; - - WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - savedConfigs[0].useExternalScores = true; - - WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, - scanDetails, scores, meteredHints); - - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, false, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[0], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanDetails.get(0).getScanResult(), candidate); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - } - - /** - * Choose untrusted network when it has higher score than the externally scored - * saved network. - */ - @Test - public void testEvaluateNetworks_chooseUntrustedWithHigherScoreThanExternallyScoredNetwork() { - // Saved network. - String[] savedSsids = {"\"test1\""}; - String[] savedBssids = {"6c:f3:7f:ae:8c:f3"}; - int[] savedFreqs = {2470}; - String[] savedCaps = {"[WPA2-PSK][ESS]"}; - int[] savedSecurities = {SECURITY_PSK}; - int[] savedLevels = {mThresholdQualifiedRssi2G + 8}; - // Ephemeral network. - String[] ephemeralSsids = {"\"test2\""}; - String[] ephemeralBssids = {"6c:f3:7f:ae:8c:f4"}; - int[] ephemeralFreqs = {2437}; - String[] ephemeralCaps = {"[ESS]"}; - int[] ephemeralLevels = {mThresholdQualifiedRssi2G + 8}; - // Ephemeral network has higher score than the saved network. - Integer[] scores = {100, 120}; - boolean[] meteredHints = {false, true}; - - // Set up the saved network. - WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(savedSsids, - savedBssids, savedFreqs, savedCaps, savedLevels, savedSecurities, - mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - savedConfigs[0].useExternalScores = true; - - // Set up the ephemeral network. - scanDetails.addAll(WifiNetworkSelectorTestUtil.buildScanDetails( - ephemeralSsids, ephemeralBssids, ephemeralFreqs, - ephemeralCaps, ephemeralLevels, mClock)); - ScanResult ephemeralScanResult = scanDetails.get(1).getScanResult(); - WifiConfiguration ephemeralNetworkConfig = WifiNetworkSelectorTestUtil - .setupEphemeralNetwork(mWifiConfigManager, 1, scanDetails.get(1), - meteredHints[1]); - - // Set up score cache for both the saved network and the ephemeral network. - WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, - scanDetails, scores, meteredHints); - - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, - null, null, false, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfig, candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - ephemeralScanResult, candidate); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); + verify(mOnConnectableListener).onConnectable(any(), mWifiConfigCaptor.capture()); + assertEquals(mWifiConfigCaptor.getValue().networkId, savedConfigs[0].networkId); } /** @@ -634,91 +404,10 @@ public class ScoredNetworkEvaluatorTest extends WifiBaseTest { WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, scanDetails, scores, meteredHints); - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, + mScoredNetworkEvaluator.evaluateNetworks(scanDetails, null, null, false, true, mOnConnectableListener); - assertEquals("Expect null configuration", null, candidate); - } - - /** - * Between two ephemeral networks with the same RSSI, choose - * the currently connected one. - */ - @Test - public void testEvaluateNetworks_chooseActiveEphemeralNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[ESS]", "[ESS]"}; - int[] levels = {mThresholdQualifiedRssi2G + 28, mThresholdQualifiedRssi2G + 28}; - boolean[] meteredHints = {true, true}; - ScanResult[] scanResults = new ScanResult[2]; - WifiConfiguration[] ephemeralNetworkConfigs = new WifiConfiguration[2]; - - List<ScanDetail> scanDetails = WifiNetworkSelectorTestUtil - .buildScanDetails(ssids, bssids, freqs, caps, levels, mClock); - - WifiNetworkSelectorTestUtil.configureScoreCache( - mScoreCache, scanDetails, null, meteredHints); - - // No saved networks. - when(mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache(any(ScanDetail.class))) - .thenReturn(null); - - for (int i = 0; i < 2; i++) { - scanResults[i] = scanDetails.get(i).getScanResult(); - ephemeralNetworkConfigs[i] = WifiNetworkSelectorTestUtil.setupEphemeralNetwork( - mWifiConfigManager, i, scanDetails.get(i), meteredHints[i]); - } - - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks( - scanDetails, ephemeralNetworkConfigs[1], - bssids[1], true, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(ephemeralNetworkConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanResults[1], candidate); - assertEquals(meteredHints[1], candidate.meteredHint); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - } - - /** - * Between two externally scored saved networks with the same RSSI, choose - * the currently connected one. - */ - @Test - public void testEvaluateNetworks_chooseActiveSavedNetwork() { - String[] ssids = {"\"test1\"", "\"test2\""}; - String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; - int[] freqs = {2470, 2437}; - String[] caps = {"[WPA2-PSK][ESS]", "[WPA2-PSK][ESS]"}; - int[] securities = {SECURITY_PSK, SECURITY_PSK}; - int[] levels = {mThresholdQualifiedRssi2G + 28, mThresholdQualifiedRssi2G + 28}; - boolean[] meteredHints = {false, false}; - - WifiNetworkSelectorTestUtil.ScanDetailsAndWifiConfigs scanDetailsAndConfigs = - WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, - freqs, caps, levels, securities, mWifiConfigManager, mClock); - List<ScanDetail> scanDetails = scanDetailsAndConfigs.getScanDetails(); - WifiConfiguration[] savedConfigs = scanDetailsAndConfigs.getWifiConfigs(); - savedConfigs[0].useExternalScores = savedConfigs[1].useExternalScores = true; - - WifiNetworkSelectorTestUtil.configureScoreCache(mScoreCache, - scanDetails, null, meteredHints); - - WifiConfiguration candidate = mScoredNetworkEvaluator.evaluateNetworks(scanDetails, - savedConfigs[1], bssids[1], true, true, mOnConnectableListener); - - WifiConfigurationTestUtil.assertConfigurationEqual(savedConfigs[1], candidate); - WifiNetworkSelectorTestUtil.verifySelectedScanResult(mWifiConfigManager, - scanDetails.get(1).getScanResult(), candidate); - verify(mOnConnectableListener, atLeastOnce()) - .onConnectable(any(), mWifiConfigCaptor.capture(), anyInt()); - assertTrue(mWifiConfigCaptor.getAllValues().stream() - .anyMatch(c -> c.networkId == candidate.networkId)); - + verify(mOnConnectableListener).onConnectable(any(), mWifiConfigCaptor.capture()); + assertEquals(mWifiConfigCaptor.getValue().networkId, savedConfigs[0].networkId); } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java index bddf3f946..6ef7c08b3 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java @@ -101,6 +101,7 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { mScoreCardBasedScorer = new ScoreCardBasedScorer(mScoringParams); mThroughputScorer = new ThroughputScorer(mScoringParams); when(mWifiNative.getClientInterfaceName()).thenReturn("wlan0"); + mWifiNetworkSelector.registerCandidateScorer(mCompatibilityScorer); } /** Cleans up test. */ @@ -165,12 +166,12 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { * {@link WifiNetworkSelectorTestUtil#setupScanDetailsAndConfigStore}. */ @Override - public WifiConfiguration evaluateNetworks(List<ScanDetail> scanDetails, + public void evaluateNetworks(List<ScanDetail> scanDetails, WifiConfiguration currentNetwork, String currentBssid, boolean connected, boolean untrustedNetworkAllowed, @NonNull OnConnectableListener onConnectableListener) { if (!mEvaluatorShouldSelectCandidate) { - return null; + return; } for (ScanDetail scanDetail : scanDetails) { WifiConfiguration config = @@ -183,8 +184,7 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { mWifiConfigManager.getConfiguredNetworkForScanDetailAndCache( scanDetailToReturn); assertNotNull("Saved network must not be null", configToReturn); - onConnectableListener.onConnectable(scanDetailToReturn, configToReturn, 100); - return configToReturn; + onConnectableListener.onConnectable(scanDetailToReturn, configToReturn); } } @@ -1391,18 +1391,6 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { } /** - * Test registerCandidateScorer. - * - * Just make sure it does not crash, for now. - */ - @Test - public void testRegisterCandidateScorer() { - mWifiNetworkSelector.registerCandidateScorer(mCompatibilityScorer); - - test2GhzHighQuality5GhzAvailable(); - } - - /** * Test that registering a new CandidateScorer causes it to be used */ @Test @@ -1437,51 +1425,6 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { anyInt(), anyInt(), anyBoolean(), anyInt()); } - /** - * Tests that metrics are recorded for 2 scorers (legacy and another). - */ - @Test - public void testCandidateScorerMetrics_twoScorers() { - mWifiNetworkSelector.registerCandidateScorer(mScoreCardBasedScorer); - - // add a second NetworkEvaluator that returns the second network in the scan list - mWifiNetworkSelector.registerNetworkEvaluator( - new DummyNetworkEvaluator(1, DUMMY_EVALUATOR_ID_2)); - - test2GhzHighQuality5GhzAvailable(); - - int registeredExpId = experimentIdFromIdentifier(mScoreCardBasedScorer.getIdentifier()); - - // Wanted 2 times since test2GhzHighQuality5GhzAvailable() calls - // WifiNetworkSelector.selectNetwork() twice - verify(mWifiMetrics, times(2)).logNetworkSelectionDecision(registeredExpId, - WifiNetworkSelector.LEGACY_CANDIDATE_SCORER_EXP_ID, true, 2); - } - - /** - * Tests that metrics are recorded for 2 scorers (legacy and legacy compatibility), when - * legacy compatibility experiment is active. - */ - @Test - public void testCandidateScorerMetrics_twoScorers_experimentActive() { - mWifiNetworkSelector.registerCandidateScorer(mCompatibilityScorer); - - // add a second NetworkEvaluator that returns the second network in the scan list - mWifiNetworkSelector.registerNetworkEvaluator( - new DummyNetworkEvaluator(1, DUMMY_EVALUATOR_ID_2)); - - int compatibilityExpId = experimentIdFromIdentifier(mCompatibilityScorer.getIdentifier()); - mScoringParams.update("expid=" + compatibilityExpId); - assertEquals(compatibilityExpId, mScoringParams.getExperimentIdentifier()); - - test2GhzHighQuality5GhzAvailable(); - - // Wanted 2 times since test2GhzHighQuality5GhzAvailable() calls - // WifiNetworkSelector.selectNetwork() twice - verify(mWifiMetrics, times(2)).logNetworkSelectionDecision( - WifiNetworkSelector.LEGACY_CANDIDATE_SCORER_EXP_ID, compatibilityExpId, true, 2); - } - private static final WifiCandidates.CandidateScorer NULL_SCORER = new WifiCandidates.CandidateScorer() { @Override @@ -1496,60 +1439,6 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { } }; - /** - * Tests that metrics are recorded for 2 scorers (legacy and null) when one - * candidate scorer returns null. - */ - @Test - public void testCandidateScorerMetrics_twoScorers_oneNull() { - // add null scorer - mWifiNetworkSelector.registerCandidateScorer(NULL_SCORER); - - // add a second NetworkEvaluator that returns the second network in the scan list - mWifiNetworkSelector.registerNetworkEvaluator( - new DummyNetworkEvaluator(1, DUMMY_EVALUATOR_ID_2)); - - test2GhzHighQuality5GhzAvailable(); - - int nullScorerId = experimentIdFromIdentifier(NULL_SCORER.getIdentifier()); - - // Wanted 2 times since test2GhzHighQuality5GhzAvailable() calls - // WifiNetworkSelector.selectNetwork() twice - verify(mWifiMetrics, times(2)).logNetworkSelectionDecision(nullScorerId, - WifiNetworkSelector.LEGACY_CANDIDATE_SCORER_EXP_ID, false, 2); - } - - /** - * Tests that metrics are recorded for 2 scorers (legacy and null) when the active - * candidate scorer returns NONE. - */ - @Test - public void testCandidateScorerMetrics_twoScorers_nullActive() { - int nullScorerId = experimentIdFromIdentifier(NULL_SCORER.getIdentifier()); - - mScoringParams.update("expid=" + nullScorerId); - assertEquals(nullScorerId, mScoringParams.getExperimentIdentifier()); - - // add null scorer - mWifiNetworkSelector.registerCandidateScorer(NULL_SCORER); - - // add a second NetworkEvaluator that returns the second network in the scan list - mWifiNetworkSelector.registerNetworkEvaluator( - new DummyNetworkEvaluator(1, DUMMY_EVALUATOR_ID_2)); - - WifiConfiguration selected = mWifiNetworkSelector.selectNetwork( - setUpTwoNetworks(-35, -40), - EMPTY_BLACKLIST, mWifiInfo, false, true, true); - - assertNull(selected); - - verify(mWifiMetrics).logNetworkSelectionDecision( - WifiNetworkSelector.LEGACY_CANDIDATE_SCORER_EXP_ID, nullScorerId, false, 2); - verify(mWifiMetrics, atLeastOnce()).setNominatorForNetwork(anyInt(), anyInt()); - verify(mWifiMetrics, atLeastOnce()).setNetworkSelectorExperimentId(anyInt()); - verifyNoMoreInteractions(mWifiMetrics); - } - private List<ScanDetail> setUpTwoNetworks(int rssiNetwork1, int rssiNetwork2) { String[] ssids = {"\"test1\"", "\"test2\""}; String[] bssids = {"6c:f3:7f:ae:8c:f3", "6c:f3:7f:ae:8c:f4"}; @@ -1588,9 +1477,6 @@ public class WifiNetworkSelectorTest extends WifiBaseTest { verify(mWifiMetrics, times(2)).logNetworkSelectionDecision(nullScorerId, compatibilityExpId, false, 2); - verify(mWifiMetrics, times(2)).logNetworkSelectionDecision( - WifiNetworkSelector.LEGACY_CANDIDATE_SCORER_EXP_ID, compatibilityExpId, true, 2); - int expid = CompatibilityScorer.COMPATIBILITY_SCORER_DEFAULT_EXPID; verify(mWifiMetrics, atLeastOnce()).setNetworkSelectorExperimentId(eq(expid)); } diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java index 96a8aac3d..a11c05b8c 100644 --- a/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/PasspointNetworkEvaluatorTest.java @@ -19,7 +19,6 @@ package com.android.server.wifi.hotspot2; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.any; @@ -27,6 +26,7 @@ import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -79,6 +79,8 @@ public class PasspointNetworkEvaluatorTest { private static final WifiConfiguration TEST_CONFIG2 = generateWifiConfig(TEST_FQDN2); private static final PasspointProvider TEST_PROVIDER1 = generateProvider(TEST_CONFIG1); private static final PasspointProvider TEST_PROVIDER2 = generateProvider(TEST_CONFIG2); + private ArgumentCaptor<WifiConfiguration> mWifiConfigurationArgumentCaptor = + ArgumentCaptor.forClass(WifiConfiguration.class); @Mock PasspointManager mPasspointManager; @Mock PasspointConfiguration mPasspointConfiguration; @@ -157,24 +159,23 @@ public class PasspointNetworkEvaluatorTest { } /** - * Verify that null will be returned when evaluating scans without any matching providers. - * - * @throws Exception + * Verify that no candidate will be nominated when evaluating scans without any matching + * providers. */ @Test - public void evaluateScansWithNoMatch() throws Exception { + public void evaluateScansWithNoMatch() { List<ScanDetail> scanDetails = Arrays.asList(new ScanDetail[] { generateScanDetail(TEST_SSID1, TEST_BSSID1), generateScanDetail(TEST_SSID2, TEST_BSSID2)}); when(mPasspointManager.matchProvider(any(ScanResult.class))).thenReturn(null); - assertEquals(null, mEvaluator.evaluateNetworks( - scanDetails, null, null, false, false, mOnConnectableListener)); - verify(mOnConnectableListener, never()).onConnectable(any(), any(), anyInt()); + mEvaluator.evaluateNetworks( + scanDetails, null, null, false, false, mOnConnectableListener); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } /** * Verify that provider matching will not be performed when evaluating scans with no - * interworking support, and null will be returned. + * interworking support, verify that no candidate will be nominated. * * @throws Exception */ @@ -186,16 +187,16 @@ public class PasspointNetworkEvaluatorTest { when(scanDetail.getNetworkDetail()).thenReturn(networkDetail); List<ScanDetail> scanDetails = Arrays.asList(new ScanDetail[] {scanDetail}); - assertEquals(null, mEvaluator.evaluateNetworks( - scanDetails, null, null, false, false, mOnConnectableListener)); - verify(mOnConnectableListener, never()).onConnectable(any(), any(), anyInt()); + mEvaluator.evaluateNetworks( + scanDetails, null, null, false, false, mOnConnectableListener); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); // Verify that no provider matching is performed. verify(mPasspointManager, never()).matchProvider(any(ScanResult.class)); } /** * Verify that when a network matches a home provider is found, the correct network - * information (WifiConfiguration) is setup and returned. + * information (WifiConfiguration) is setup and nominated. * * @throws Exception */ @@ -216,9 +217,9 @@ public class PasspointNetworkEvaluatorTest { when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID)).thenReturn(TEST_CONFIG1); - assertNotNull(mEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener)); - verify(mOnConnectableListener).onConnectable(any(), any(), anyInt()); + mEvaluator.evaluateNetworks(scanDetails, null, null, false, + false, mOnConnectableListener); + verify(mOnConnectableListener).onConnectable(any(), any()); // Verify the content of the WifiConfiguration that was added to WifiConfigManager. ArgumentCaptor<WifiConfiguration> addedConfig = @@ -239,12 +240,10 @@ public class PasspointNetworkEvaluatorTest { /** * Verify that when a network matches a roaming provider is found, the correct network - * information (WifiConfiguration) is setup and returned. - * - * @throws Exception + * information (WifiConfiguration) is setup and nominated. */ @Test - public void evaluateScansWithNetworkMatchingRoamingProvider() throws Exception { + public void evaluateScansWithNetworkMatchingRoamingProvider() { List<ScanDetail> scanDetails = Arrays.asList(new ScanDetail[] { generateScanDetail(TEST_SSID1, TEST_BSSID1), generateScanDetail(TEST_SSID2, TEST_BSSID2)}); @@ -260,9 +259,9 @@ public class PasspointNetworkEvaluatorTest { when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID)).thenReturn(TEST_CONFIG1); - assertNotNull(mEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener)); - verify(mOnConnectableListener).onConnectable(any(), any(), anyInt()); + mEvaluator.evaluateNetworks(scanDetails, null, null, false, + false, mOnConnectableListener); + verify(mOnConnectableListener).onConnectable(any(), any()); // Verify the content of the WifiConfiguration that was added to WifiConfigManager. ArgumentCaptor<WifiConfiguration> addedConfig = @@ -282,8 +281,8 @@ public class PasspointNetworkEvaluatorTest { } /** - * Verify that when a network matches a home provider and another network matches a roaming - * provider are found, the network that matched to a home provider is preferred. + * Verify that when a network matches a roaming provider is found for different scanDetails, + * will nominate both as the candidates. * * @throws Exception */ @@ -304,67 +303,17 @@ public class PasspointNetworkEvaluatorTest { when(mPasspointManager.matchProvider(any(ScanResult.class))) .thenReturn(homeProvider).thenReturn(roamingProvider); when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) - .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); + .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)) + .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID + 1)); when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID)).thenReturn(TEST_CONFIG1); - assertNotNull(mEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener)); - verify(mOnConnectableListener).onConnectable(any(), any(), anyInt()); - - // Verify the content of the WifiConfiguration that was added to WifiConfigManager. - ArgumentCaptor<WifiConfiguration> addedConfig = - ArgumentCaptor.forClass(WifiConfiguration.class); - verify(mWifiConfigManager).addOrUpdateNetwork(addedConfig.capture(), anyInt()); - assertEquals(ScanResultUtil.createQuotedSSID(TEST_SSID1), addedConfig.getValue().SSID); - assertEquals(TEST_FQDN1, addedConfig.getValue().FQDN); - assertNotNull(addedConfig.getValue().enterpriseConfig); - assertEquals("", addedConfig.getValue().enterpriseConfig.getAnonymousIdentity()); - assertTrue(addedConfig.getValue().isHomeProviderNetwork); - verify(mWifiConfigManager).enableNetwork( - eq(TEST_NETWORK_ID), eq(false), anyInt(), any()); - verify(mWifiConfigManager).setNetworkCandidateScanResult( - eq(TEST_NETWORK_ID), any(ScanResult.class), anyInt()); - verify(mWifiConfigManager).updateScanDetailForNetwork( - eq(TEST_NETWORK_ID), any(ScanDetail.class)); - } - - /** - * Verify that when two networks both matches a home provider, with one of them being the - * active network, the active network is preferred. - * - * @throws Exception - */ - @Test - public void evaluateScansWithActiveNetworkMatchingHomeProvider() throws Exception { - List<ScanDetail> scanDetails = Arrays.asList(new ScanDetail[] { - generateScanDetail(TEST_SSID1, TEST_BSSID1), - generateScanDetail(TEST_SSID2, TEST_BSSID2)}); - - // Setup matching providers for both ScanDetail. - Pair<PasspointProvider, PasspointMatch> homeProvider = Pair.create( - TEST_PROVIDER1, PasspointMatch.HomeProvider); - - // Setup currently connected network - WifiConfiguration currentNetwork = new WifiConfiguration(); - currentNetwork.networkId = TEST_NETWORK_ID; - currentNetwork.SSID = ScanResultUtil.createQuotedSSID(TEST_SSID2); - String currentBssid = TEST_BSSID2; - - // Returning the same matching provider for both ScanDetail. - when(mPasspointManager.matchProvider(any(ScanResult.class))) - .thenReturn(homeProvider).thenReturn(homeProvider); - - WifiConfiguration config = mEvaluator.evaluateNetworks(scanDetails, currentNetwork, - currentBssid, true, false, mOnConnectableListener); - - verify(mOnConnectableListener).onConnectable(any(), any(), anyInt()); - - // Verify no new network is added to WifiConfigManager. - verify(mWifiConfigManager, never()).addOrUpdateNetwork( - any(WifiConfiguration.class), anyInt()); + when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID + 1)) + .thenReturn(TEST_CONFIG2); + mEvaluator.evaluateNetworks(scanDetails, null, null, false, + false, mOnConnectableListener); + verify(mOnConnectableListener, times(2)).onConnectable(any(), any()); - // Verify current active network is returned. - assertEquals(ScanResultUtil.createQuotedSSID(TEST_SSID2), config.SSID); - assertEquals(TEST_NETWORK_ID, config.networkId); + verify(mWifiConfigManager, times(2)) + .addOrUpdateNetwork(any(), anyInt()); } /** @@ -394,21 +343,24 @@ public class PasspointNetworkEvaluatorTest { .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID)).thenReturn(config); - WifiConfiguration result = mEvaluator.evaluateNetworks(scanDetails, null, null, false, + mEvaluator.evaluateNetworks(scanDetails, null, null, false, false, mOnConnectableListener); + verify(mOnConnectableListener).onConnectable(any(), + mWifiConfigurationArgumentCaptor.capture()); + - assertEquals("", result.enterpriseConfig.getAnonymousIdentity()); - assertTrue(TelephonyUtil.isSimEapMethod(result.enterpriseConfig.getEapMethod())); + assertEquals("", mWifiConfigurationArgumentCaptor.getValue() + .enterpriseConfig.getAnonymousIdentity()); + assertTrue(TelephonyUtil.isSimEapMethod( + mWifiConfigurationArgumentCaptor.getValue().enterpriseConfig.getEapMethod())); } /** * Verify that when the current active network is matched, the scan info associated with * the network is updated. - * - * @throws Exception */ @Test - public void evaluateScansMatchingActiveNetworkWithDifferentBSS() throws Exception { + public void evaluateScansMatchingActiveNetworkWithDifferentBSS() { List<ScanDetail> scanDetails = Arrays.asList(new ScanDetail[] { generateScanDetail(TEST_SSID1, TEST_BSSID2)}); // Setup matching provider. @@ -423,11 +375,14 @@ public class PasspointNetworkEvaluatorTest { // Match the current connected network to a home provider. when(mPasspointManager.matchProvider(any(ScanResult.class))).thenReturn(homeProvider); + when(mWifiConfigManager.addOrUpdateNetwork(any(WifiConfiguration.class), anyInt())) + .thenReturn(new NetworkUpdateResult(TEST_NETWORK_ID)); + when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID)).thenReturn(currentNetwork); - assertNotNull(mEvaluator.evaluateNetworks(scanDetails, currentNetwork, - currentBssid, true, false, mOnConnectableListener)); + mEvaluator.evaluateNetworks(scanDetails, currentNetwork, + currentBssid, true, false, mOnConnectableListener); - verify(mOnConnectableListener).onConnectable(any(), any(), anyInt()); + verify(mOnConnectableListener).onConnectable(any(), any()); // Verify network candidate information is updated. ArgumentCaptor<ScanResult> updatedCandidateScanResult = @@ -443,8 +398,8 @@ public class PasspointNetworkEvaluatorTest { } /** - * Verify that the current configuration for the passpoint network is disabled, it returns - * {@null} for the candidate. + * Verify that the current configuration for the passpoint network is disabled, it will not + * nominated that network. */ @Test public void evaluateNetworkWithDisabledWifiConfig() { @@ -473,16 +428,16 @@ public class PasspointNetworkEvaluatorTest { .thenReturn(null); when(mWifiConfigManager.getConfiguredNetwork(anyString())).thenReturn(disableConfig); - assertNull(mEvaluator.evaluateNetworks(scanDetails, null, null, false, - false, mOnConnectableListener)); + mEvaluator.evaluateNetworks(scanDetails, null, null, false, + false, mOnConnectableListener); verify(mWifiConfigManager, never()).addOrUpdateNetwork(any(WifiConfiguration.class), anyInt()); - verify(mOnConnectableListener, never()).onConnectable(any(), any(), anyInt()); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } /** * Verify that when a network matching a home provider is found, but the network was - * disconnected previously by user, it returns {@code null} for candidate. + * disconnected previously by user, it will not nominated that network. */ @Test public void evaluateScanResultWithHomeMatchButPreviouslyUserDisconnected() { @@ -500,8 +455,8 @@ public class PasspointNetworkEvaluatorTest { when(mWifiConfigManager.getConfiguredNetwork(TEST_NETWORK_ID)).thenReturn(TEST_CONFIG1); when(mWifiConfigManager.wasEphemeralNetworkDeleted("\"" + TEST_SSID1 + "\"")) .thenReturn(true); - assertEquals(null, mEvaluator.evaluateNetworks( - scanDetails, null, null, false, false, mOnConnectableListener)); - verify(mOnConnectableListener, never()).onConnectable(any(), any(), anyInt()); + mEvaluator.evaluateNetworks( + scanDetails, null, null, false, false, mOnConnectableListener); + verify(mOnConnectableListener, never()).onConnectable(any(), any()); } } |