Merge "Check multiple security types for Wi-Fi NetworkRequest" into tm-qpr-dev am: 47d87cb48d

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/20666966

Change-Id: I745c07401d1130ad320c476928f21d68c388f600
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
index 5fe156c..5639047 100644
--- a/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
+++ b/src/com/android/settings/wifi/NetworkRequestDialogFragment.java
@@ -16,7 +16,7 @@
 
 package com.android.settings.wifi;
 
-import static com.android.settings.wifi.WifiUtils.getWifiEntrySecurity;
+import static com.android.wifitrackerlib.Utils.getSecurityTypesFromScanResult;
 
 import static java.util.stream.Collectors.toList;
 
@@ -77,8 +77,12 @@
     private static final int MAX_NUMBER_LIST_ITEM = 5;
     private boolean mShowLimitedItem = true;
 
+    private static class MatchWifi {
+        String mSsid;
+        List<Integer> mSecurityTypes;
+    }
+    private List<MatchWifi> mMatchWifis = new ArrayList<>();
     @VisibleForTesting List<WifiEntry> mFilteredWifiEntries = new ArrayList<>();
-    @VisibleForTesting List<ScanResult> mMatchedScanResults = new ArrayList<>();
     private WifiEntryAdapter mDialogAdapter;
     private NetworkRequestUserSelectionCallback mUserSelectionCallback;
 
@@ -237,7 +241,7 @@
     /** Called when the state of Wifi has changed. */
     @Override
     public void onWifiStateChanged() {
-        if (mMatchedScanResults.size() == 0) {
+        if (mMatchWifis.size() == 0) {
             return;
         }
         updateWifiEntries();
@@ -249,7 +253,7 @@
      */
     @Override
     public void onWifiEntriesChanged() {
-        if (mMatchedScanResults.size() == 0) {
+        if (mMatchWifis.size() == 0) {
             return;
         }
         updateWifiEntries();
@@ -275,16 +279,24 @@
         wifiEntries.addAll(mWifiPickerTracker.getWifiEntries());
 
         mFilteredWifiEntries.clear();
-        mFilteredWifiEntries.addAll(wifiEntries.stream().filter(entry -> {
-            for (ScanResult matchedScanResult : mMatchedScanResults) {
-                if (TextUtils.equals(entry.getSsid(), matchedScanResult.SSID)
-                        && entry.getSecurity() == getWifiEntrySecurity(matchedScanResult)) {
+        mFilteredWifiEntries.addAll(wifiEntries.stream()
+                .filter(entry -> isMatchedWifiEntry(entry))
+                .limit(mShowLimitedItem ? MAX_NUMBER_LIST_ITEM : Long.MAX_VALUE)
+                .collect(toList()));
+    }
+
+    private boolean isMatchedWifiEntry(WifiEntry entry) {
+        for (MatchWifi wifi : mMatchWifis) {
+            if (!TextUtils.equals(entry.getSsid(), wifi.mSsid)) {
+                continue;
+            }
+            for (Integer security : wifi.mSecurityTypes) {
+                if (entry.getSecurityTypes().contains(security)) {
                     return true;
                 }
             }
-            return false;
-        }).limit(mShowLimitedItem ? MAX_NUMBER_LIST_ITEM : Long.MAX_VALUE)
-                .collect(toList()));
+        }
+        return false;
     }
 
     private class WifiEntryAdapter extends ArrayAdapter<WifiEntry> {
@@ -350,7 +362,14 @@
 
     @Override
     public void onMatch(List<ScanResult> scanResults) {
-        mMatchedScanResults = scanResults;
+        mMatchWifis.clear();
+        for (ScanResult scanResult : scanResults) {
+            MatchWifi matchWifi = new MatchWifi();
+            matchWifi.mSsid = scanResult.SSID;
+            matchWifi.mSecurityTypes = getSecurityTypesFromScanResult(scanResult);
+            mMatchWifis.add(matchWifi);
+        }
+
         updateWifiEntries();
         updateUi();
     }
diff --git a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
index 66d399c..e5b13c4 100644
--- a/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/NetworkRequestDialogFragmentTest.java
@@ -16,6 +16,9 @@
 
 package com.android.settings.wifi;
 
+import static com.android.wifitrackerlib.WifiEntry.SECURITY_PSK;
+import static com.android.wifitrackerlib.WifiEntry.SECURITY_SAE;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.mockito.ArgumentMatchers.any;
@@ -46,16 +49,20 @@
 import com.android.wifitrackerlib.WifiPickerTracker;
 
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.InOrder;
-import org.mockito.MockitoAnnotations;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
 import org.robolectric.Robolectric;
 import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 @RunWith(RobolectricTestRunner.class)
@@ -63,15 +70,34 @@
 public class NetworkRequestDialogFragmentTest {
 
     private static final String KEY_SSID = "key_ssid";
-    private static final String KEY_SECURITY = "key_security";
+    private static final String TEST_CAPABILITIES_OPEN = "[ESS]";
+    private static final String TEST_CAPABILITIES_WPA2_PSK = "[WPA2-PSK-CCMP][ESS]";
+    private static final String TEST_CAPABILITIES_WPA3_SAE = "[RSN-PSK+SAE-CCMP][ESS]";
     private static final String TEST_APP_NAME = "TestAppName";
 
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    WifiPickerTracker mWifiPickerTracker;
+    @Mock
+    WifiEntry mWifiEntry;
+
     private FragmentActivity mActivity;
     private NetworkRequestDialogFragment networkRequestDialogFragment;
 
+    ScanResult mScanResult = new ScanResult();
+
     @Before
     public void setUp() {
-        MockitoAnnotations.initMocks(this);
+        when(mWifiEntry.getSsid()).thenReturn(KEY_SSID);
+        when(mWifiEntry.getSecurityTypes()).thenReturn(Arrays.asList(SECURITY_PSK, SECURITY_SAE));
+        when(mWifiEntry.getSecurity()).thenReturn(SECURITY_PSK);
+        when(mWifiPickerTracker.getConnectedWifiEntry()).thenReturn(null);
+        when(mWifiPickerTracker.getWifiEntries()).thenReturn(Arrays.asList(mWifiEntry));
+
+        mScanResult.SSID = KEY_SSID;
+        mScanResult.capabilities = TEST_CAPABILITIES_OPEN;
+
         FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
         when(fakeFeatureFactory.wifiTrackerLibProvider.createWifiPickerTracker(
                 any(), any(), any(), any(), any(), anyLong(), anyLong(), any()))
@@ -81,7 +107,7 @@
                 new Intent().putExtra(NetworkRequestDialogFragment.EXTRA_APP_NAME,
                         TEST_APP_NAME)).setup().get();
         networkRequestDialogFragment = spy(NetworkRequestDialogFragment.newInstance());
-        networkRequestDialogFragment.mWifiPickerTracker = mock(WifiPickerTracker.class);
+        networkRequestDialogFragment.mWifiPickerTracker = mWifiPickerTracker;
     }
 
     @Test
@@ -147,12 +173,10 @@
     @Test
     public void onWifiStateChanged_nonEmptyMatchedScanResults_shouldUpdateWifiEntries() {
         final InOrder inOrder = inOrder(networkRequestDialogFragment);
+        mScanResult.capabilities = TEST_CAPABILITIES_OPEN;
+        networkRequestDialogFragment.onMatch(Arrays.asList(mScanResult));
 
-        final List<ScanResult> scanResults = new ArrayList<>();
-        networkRequestDialogFragment.mMatchedScanResults = scanResults;
-        ScanResult scanResult = mock(ScanResult.class);
-        networkRequestDialogFragment.mMatchedScanResults.add(scanResult);
-        networkRequestDialogFragment.onMatch(scanResults);
+        networkRequestDialogFragment.onWifiStateChanged();
 
         inOrder.verify(networkRequestDialogFragment).updateWifiEntries();
         inOrder.verify(networkRequestDialogFragment).updateUi();
@@ -161,12 +185,10 @@
     @Test
     public void onWifiEntriesChanged_nonEmptyMatchedScanResults_shouldUpdateWifiEntries() {
         final InOrder inOrder = inOrder(networkRequestDialogFragment);
+        mScanResult.capabilities = TEST_CAPABILITIES_OPEN;
+        networkRequestDialogFragment.onMatch(Arrays.asList(mScanResult));
 
-        final List<ScanResult> scanResults = new ArrayList<>();
-        networkRequestDialogFragment.mMatchedScanResults = scanResults;
-        ScanResult scanResult = mock(ScanResult.class);
-        networkRequestDialogFragment.mMatchedScanResults.add(scanResult);
-        networkRequestDialogFragment.onMatch(scanResults);
+        networkRequestDialogFragment.onWifiEntriesChanged();
 
         inOrder.verify(networkRequestDialogFragment).updateWifiEntries();
         inOrder.verify(networkRequestDialogFragment).updateUi();
@@ -280,4 +302,34 @@
         // Check
         verify(selectionCallback, times(1)).reject();
     }
+
+    @Test
+    public void updateWifiEntries_noMatchSecurityWifi_filteredWifiIsEmpty() {
+        mScanResult.capabilities = TEST_CAPABILITIES_OPEN;
+        networkRequestDialogFragment.onMatch(Arrays.asList(mScanResult));
+
+        networkRequestDialogFragment.updateWifiEntries();
+
+        assertThat(networkRequestDialogFragment.mFilteredWifiEntries.size()).isEqualTo(0);
+    }
+
+    @Test
+    public void updateWifiEntries_matchWpa2Wifi_filteredWifiNotEmpty() {
+        mScanResult.capabilities = TEST_CAPABILITIES_WPA2_PSK;
+        networkRequestDialogFragment.onMatch(Arrays.asList(mScanResult));
+
+        networkRequestDialogFragment.updateWifiEntries();
+
+        assertThat(networkRequestDialogFragment.mFilteredWifiEntries.size()).isNotEqualTo(0);
+    }
+
+    @Test
+    public void updateWifiEntries_matchWpa3Wifi_filteredWifiNotEmpty() {
+        mScanResult.capabilities = TEST_CAPABILITIES_WPA3_SAE;
+        networkRequestDialogFragment.onMatch(Arrays.asList(mScanResult));
+
+        networkRequestDialogFragment.updateWifiEntries();
+
+        assertThat(networkRequestDialogFragment.mFilteredWifiEntries.size()).isNotEqualTo(0);
+    }
 }