Merge "Fix PhoneNumberPreferenceControllerTest" into main
diff --git a/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java b/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
index 4723b0e..118ff71 100644
--- a/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
+++ b/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragment.java
@@ -262,16 +262,9 @@
                 continue;
             }
             canFindNetwork = true;
-            final int security =
-                    WifiDppUtils.getSecurityTypeFromWifiConfiguration(wifiConfiguration);
-            if (security == wifiEntry.getSecurity()) {
-                return REACHABLE_WIFI_NETWORK;
-            }
-
-            // Default security type of PSK/SAE transition mode WifiEntry is SECURITY_PSK and
-            // there is no way to know if a WifiEntry is of transition mode. Give it a chance.
-            if (security == WifiEntry.SECURITY_SAE
-                    && wifiEntry.getSecurity() == WifiEntry.SECURITY_PSK) {
+            int security = WifiDppUtils.getSecurityTypeFromWifiConfiguration(wifiConfiguration);
+            if (isSecurityMatched(security, wifiEntry.getSecurity())) {
+                Log.d(TAG, "WiFi DPP detects connection security for a matching WiFi network.");
                 return REACHABLE_WIFI_NETWORK;
             }
         }
@@ -284,6 +277,24 @@
     }
 
     @VisibleForTesting
+    boolean isSecurityMatched(int qrSecurity, int entrySecurity) {
+        if (qrSecurity == entrySecurity) {
+            return true;
+        }
+        // Default security type of PSK/SAE transition mode WifiEntry is SECURITY_PSK and
+        // there is no way to know if a WifiEntry is of transition mode. Give it a chance.
+        if (qrSecurity == WifiEntry.SECURITY_SAE && entrySecurity == WifiEntry.SECURITY_PSK) {
+            return true;
+        }
+        // If configured is no password, the Wi-Fi framework will attempt OPEN and OWE security.
+        return isNoPasswordSecurity(qrSecurity) && isNoPasswordSecurity(entrySecurity);
+    }
+
+    private boolean isNoPasswordSecurity(int security) {
+        return security == WifiEntry.SECURITY_NONE || security == WifiEntry.SECURITY_OWE;
+    }
+
+    @VisibleForTesting
     boolean canConnectWifi(String ssid) {
         final List<WifiEntry> wifiEntries = mWifiPickerTracker.getWifiEntries();
         for (WifiEntry wifiEntry : wifiEntries) {
diff --git a/tests/robotests/src/com/android/settings/display/DisplayScreenTest.kt b/tests/robotests/src/com/android/settings/display/DisplayScreenTest.kt
index d869b84..6a7c238 100644
--- a/tests/robotests/src/com/android/settings/display/DisplayScreenTest.kt
+++ b/tests/robotests/src/com/android/settings/display/DisplayScreenTest.kt
@@ -23,6 +23,7 @@
 import com.android.settings.testutils.FakeFeatureFactory
 import com.android.settingslib.preference.CatalystScreenTestCase
 import com.google.common.truth.Truth.assertThat
+import org.junit.Ignore
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.mockito.ArgumentMatchers.anyInt
@@ -65,6 +66,7 @@
         assertThat(preferenceScreenCreator.isAvailable(contextWrapper)).isFalse()
     }
 
+    @Ignore("robolectric.createActivityContexts cause other test failure")
     override fun migration() {
         // avoid UnsupportedOperationException when getDisplay from context
         System.setProperty("robolectric.createActivityContexts", "true")
diff --git a/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java b/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java
index ff1125d..9f5fcf8 100644
--- a/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/dpp/WifiDppQrCodeScannerFragmentTest.java
@@ -16,6 +16,11 @@
 
 package com.android.settings.wifi.dpp;
 
+import static com.android.wifitrackerlib.WifiEntry.SECURITY_NONE;
+import static com.android.wifitrackerlib.WifiEntry.SECURITY_OWE;
+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.anyInt;
@@ -148,4 +153,24 @@
         verify(mActivity).setResult(eq(Activity.RESULT_OK), any());
         verify(mActivity).finish();
     }
+
+    @Test
+    public void isSecurityMatched_securityNotMatch_returnFalse() {
+        assertThat(mFragment.isSecurityMatched(SECURITY_NONE, SECURITY_PSK)).isFalse();
+    }
+
+    @Test
+    public void isSecurityMatched_securityMatch_returnTrue() {
+        assertThat(mFragment.isSecurityMatched(SECURITY_PSK, SECURITY_PSK)).isTrue();
+    }
+
+    @Test
+    public void isSecurityMatched_tryPskSaeTransition_returnTrue() {
+        assertThat(mFragment.isSecurityMatched(SECURITY_SAE, SECURITY_PSK)).isTrue();
+    }
+
+    @Test
+    public void isSecurityMatched_noPasswordSecurity_returnTrue() {
+        assertThat(mFragment.isSecurityMatched(SECURITY_NONE, SECURITY_OWE)).isTrue();
+    }
 }