Display "Unavailable" when received default MAC

Display "Unavailable" instead of "02:00:00:00:00:00" when the current
MAC address is "02:00:00:00:00:00" which indicates that we couldn't get
the actual device MAC address.

Bug: 110043449
Test: unittest (make RunSettingsRoboTests
ROBOTEST_FILTER=WifiInfoPreferenceControllerTest)
Change-Id: Iac9f81d144fd4c93ac12adaa80e1a55b19a6e186
diff --git a/src/com/android/settings/wifi/WifiInfoPreferenceController.java b/src/com/android/settings/wifi/WifiInfoPreferenceController.java
index 445d6a4..d15c508 100644
--- a/src/com/android/settings/wifi/WifiInfoPreferenceController.java
+++ b/src/com/android/settings/wifi/WifiInfoPreferenceController.java
@@ -102,11 +102,12 @@
                     Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 0);
             final String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
 
-            if (TextUtils.isEmpty(macAddress)) {
-                mWifiMacAddressPref.setSummary(R.string.status_unavailable);
-            } else if (macRandomizationMode == 1
+            if (macRandomizationMode == 1
                     && WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
                 mWifiMacAddressPref.setSummary(R.string.wifi_status_mac_randomized);
+            } else if (TextUtils.isEmpty(macAddress)
+                    || WifiInfo.DEFAULT_MAC_ADDRESS.equals(macAddress)) {
+                mWifiMacAddressPref.setSummary(R.string.status_unavailable);
             } else {
                 mWifiMacAddressPref.setSummary(macAddress);
             }
diff --git a/tests/robotests/src/com/android/settings/wifi/WifiInfoPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/WifiInfoPreferenceControllerTest.java
index 3a8ced5..d2e8cb8 100644
--- a/tests/robotests/src/com/android/settings/wifi/WifiInfoPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/WifiInfoPreferenceControllerTest.java
@@ -79,6 +79,7 @@
                 .thenReturn(mMacPreference)
                 .thenReturn(mIpPreference);
         when(mWifiManager.getConnectionInfo()).thenReturn(mWifiInfo);
+        when(mWifiManager.getCurrentNetwork()).thenReturn(null);
         mController = new WifiInfoPreferenceController(mContext, mLifecycle, mWifiManager);
     }
 
@@ -103,7 +104,6 @@
 
     @Test
     public void onResume_shouldUpdateWifiInfo() {
-        when(mWifiManager.getCurrentNetwork()).thenReturn(null);
         when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
 
         mController.displayPreference(mScreen);
@@ -114,22 +114,98 @@
     }
 
     @Test
-    public void testUpdateMacAddress() {
-        when(mWifiManager.getCurrentNetwork()).thenReturn(null);
+    public void updateWifiInfo_nullWifiInfoWithMacRandomizationOff_setMacUnavailable() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 0);
+        mController.displayPreference(mScreen);
+        when(mWifiManager.getConnectionInfo()).thenReturn(null);
+
+        mController.updateWifiInfo();
+
+        verify(mMacPreference).setSummary(R.string.status_unavailable);
+    }
+
+    @Test
+    public void updateWifiInfo_nullMacWithMacRandomizationOff_setMacUnavailable() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 0);
+        mController.displayPreference(mScreen);
+        when(mWifiInfo.getMacAddress()).thenReturn(null);
+
+        mController.updateWifiInfo();
+
+        verify(mMacPreference).setSummary(R.string.status_unavailable);
+    }
+
+    @Test
+    public void updateWifiInfo_defaultMacWithMacRandomizationOff_setMacUnavailable() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 0);
+        mController.displayPreference(mScreen);
+        when(mWifiInfo.getMacAddress()).thenReturn(WifiInfo.DEFAULT_MAC_ADDRESS);
+
+        mController.updateWifiInfo();
+
+        verify(mMacPreference).setSummary(R.string.status_unavailable);
+    }
+
+    @Test
+    public void updateWifiInfo_validMacWithMacRandomizationOff_setValidMac() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 0);
+        mController.displayPreference(mScreen);
+        when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
+
+        mController.updateWifiInfo();
+
+        verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
+    }
+
+    @Test
+    public void updateWifiInfo_nullWifiInfoWithMacRandomizationOn_setMacUnavailable() {
         Settings.Global.putInt(mContext.getContentResolver(),
                 Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 1);
         mController.displayPreference(mScreen);
+        when(mWifiManager.getConnectionInfo()).thenReturn(null);
 
-        when(mWifiInfo.getMacAddress()).thenReturn(null);
         mController.updateWifiInfo();
+
         verify(mMacPreference).setSummary(R.string.status_unavailable);
+    }
 
+    @Test
+    public void updateWifiInfo_nullMacWithMacRandomizationOn_setMacUnavailable() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 1);
+        mController.displayPreference(mScreen);
+        when(mWifiInfo.getMacAddress()).thenReturn(null);
+
+        mController.updateWifiInfo();
+
+        verify(mMacPreference).setSummary(R.string.status_unavailable);
+    }
+
+    @Test
+    public void updateWifiInfo_defaultMacWithMacRandomizationOn_setMacRandomized() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 1);
+        mController.displayPreference(mScreen);
         when(mWifiInfo.getMacAddress()).thenReturn(WifiInfo.DEFAULT_MAC_ADDRESS);
-        mController.updateWifiInfo();
-        verify(mMacPreference).setSummary(R.string.wifi_status_mac_randomized);
 
-        when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
         mController.updateWifiInfo();
+
+        verify(mMacPreference).setSummary(R.string.wifi_status_mac_randomized);
+    }
+
+    @Test
+    public void updateWifiInfo_validMacWithMacRandomizationOn_setValidMac() {
+        Settings.Global.putInt(mContext.getContentResolver(),
+                Settings.Global.WIFI_CONNECTED_MAC_RANDOMIZATION_ENABLED, 1);
+        mController.displayPreference(mScreen);
+        when(mWifiInfo.getMacAddress()).thenReturn(TEST_MAC_ADDRESS);
+
+        mController.updateWifiInfo();
+
         verify(mMacPreference).setSummary(TEST_MAC_ADDRESS);
     }
 }