Fixed wifi button flicker when enabled

The wifi switches would previously "flicker" when enabled as they would
be checked by the user, then unchecked when the wifi state went into
"enabling" and then rechecked when the wifi state got to "enabled." If
the switches are left on while in the enabling state, this behavior no
longer occurs.

Bug: 143476726
Test: manual
Change-Id: Ica177fb6984287ef02b88830bd6863a4dda2fb05
diff --git a/src/com/android/car/settings/wifi/WifiEntryPreferenceController.java b/src/com/android/car/settings/wifi/WifiEntryPreferenceController.java
index 0981d6f..bac0d04 100644
--- a/src/com/android/car/settings/wifi/WifiEntryPreferenceController.java
+++ b/src/com/android/car/settings/wifi/WifiEntryPreferenceController.java
@@ -18,6 +18,7 @@
 
 import android.car.drivingstate.CarUxRestrictions;
 import android.content.Context;
+import android.net.wifi.WifiManager;
 
 import com.android.car.settings.common.FragmentController;
 import com.android.car.settings.common.MasterSwitchPreference;
@@ -77,7 +78,8 @@
 
     @Override
     public void onWifiStateChanged(int state) {
-        getPreference().setSwitchChecked(mCarWifiManager.isWifiEnabled());
+        getPreference().setSwitchChecked(state == WifiManager.WIFI_STATE_ENABLED
+                || state == WifiManager.WIFI_STATE_ENABLING);
     }
 
     @Override
diff --git a/src/com/android/car/settings/wifi/WifiSettingsFragment.java b/src/com/android/car/settings/wifi/WifiSettingsFragment.java
index 73ed73f..048cfa4 100644
--- a/src/com/android/car/settings/wifi/WifiSettingsFragment.java
+++ b/src/com/android/car/settings/wifi/WifiSettingsFragment.java
@@ -135,7 +135,8 @@
 
     @Override
     public void onWifiStateChanged(int state) {
-        mWifiSwitch.setChecked(mCarWifiManager.isWifiEnabled());
+        mWifiSwitch.setChecked(state == WifiManager.WIFI_STATE_ENABLED
+                || state == WifiManager.WIFI_STATE_ENABLING);
         switch (state) {
             case WifiManager.WIFI_STATE_ENABLING:
                 mProgressBar.setVisible(true);
diff --git a/tests/robotests/src/com/android/car/settings/wifi/WifiEntryPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/wifi/WifiEntryPreferenceControllerTest.java
index 911d45a..baad408 100644
--- a/tests/robotests/src/com/android/car/settings/wifi/WifiEntryPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/wifi/WifiEntryPreferenceControllerTest.java
@@ -28,6 +28,7 @@
 import android.net.wifi.WifiManager;
 
 import androidx.lifecycle.Lifecycle;
+import androidx.test.core.app.ApplicationProvider;
 
 import com.android.car.settings.common.MasterSwitchPreference;
 import com.android.car.settings.common.PreferenceControllerTestHelper;
@@ -40,7 +41,6 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
 import org.robolectric.Shadows;
 import org.robolectric.annotation.Config;
 
@@ -60,7 +60,7 @@
     public void setUp() {
         MockitoAnnotations.initMocks(this);
         ShadowCarWifiManager.setInstance(mCarWifiManager);
-        mContext = RuntimeEnvironment.application;
+        mContext = ApplicationProvider.getApplicationContext();
         mMasterSwitchPreference = new MasterSwitchPreference(mContext);
         mControllerHelper = new PreferenceControllerTestHelper<>(mContext,
                 WifiEntryPreferenceController.class, mMasterSwitchPreference);
@@ -133,6 +133,18 @@
     }
 
     @Test
+    public void onWifiStateChanged_enabling_setsSwitchChecked() {
+        Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
+                PackageManager.FEATURE_WIFI, /* supported= */ true);
+        when(mCarWifiManager.isWifiEnabled()).thenReturn(false);
+        mControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
+
+        mController.onWifiStateChanged(WifiManager.WIFI_STATE_ENABLING);
+
+        assertThat(mMasterSwitchPreference.isSwitchChecked()).isTrue();
+    }
+
+    @Test
     public void getAvailabilityStatus_wifiAvailable_available() {
         Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
                 PackageManager.FEATURE_WIFI, /* supported= */ true);
diff --git a/tests/robotests/src/com/android/car/settings/wifi/WifiSettingsFragmentTest.java b/tests/robotests/src/com/android/car/settings/wifi/WifiSettingsFragmentTest.java
new file mode 100644
index 0000000..4b1a830
--- /dev/null
+++ b/tests/robotests/src/com/android/car/settings/wifi/WifiSettingsFragmentTest.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.wifi;
+
+import static com.android.car.ui.core.CarUi.requireToolbar;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.net.wifi.WifiManager;
+
+import androidx.test.core.app.ApplicationProvider;
+
+import com.android.car.settings.testutils.FragmentController;
+import com.android.car.settings.testutils.ShadowCarWifiManager;
+import com.android.car.settings.testutils.ShadowWifiManager;
+import com.android.car.ui.core.testsupport.CarUiInstallerRobolectric;
+import com.android.car.ui.toolbar.MenuItem;
+import com.android.car.ui.toolbar.ToolbarController;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.Shadows;
+import org.robolectric.annotation.Config;
+
+/** Unit test for {@link WifiSettingsFragment}. */
+@RunWith(RobolectricTestRunner.class)
+@Config(shadows = {ShadowCarWifiManager.class})
+public class WifiSettingsFragmentTest {
+
+    private Context mContext;
+    private WifiSettingsFragment mFragment;
+    private FragmentController<WifiSettingsFragment> mFragmentController;
+    @Mock
+    private CarWifiManager mCarWifiManager;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        ShadowCarWifiManager.setInstance(mCarWifiManager);
+        mContext = ApplicationProvider.getApplicationContext();
+        mFragment = new WifiSettingsFragment();
+        mFragmentController = FragmentController.of(mFragment);
+
+        // Needed to install Install CarUiLib BaseLayouts Toolbar for test activity
+        CarUiInstallerRobolectric.install();
+    }
+
+    @After
+    public void tearDown() {
+        ShadowWifiManager.reset();
+    }
+
+    @Test
+    public void onWifiStateChanged_disabled_setsSwitchUnchecked() {
+        Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
+                PackageManager.FEATURE_WIFI, /* supported= */ true);
+        when(mCarWifiManager.isWifiEnabled()).thenReturn(true);
+        mFragmentController.setup();
+
+        when(mCarWifiManager.isWifiEnabled()).thenReturn(false);
+        mFragment.onWifiStateChanged(WifiManager.WIFI_STATE_DISABLED);
+
+        assertThat(getWifiSwitch().isChecked()).isFalse();
+    }
+
+    @Test
+    public void onWifiStateChanged_enabled_setsSwitchChecked() {
+        Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
+                PackageManager.FEATURE_WIFI, /* supported= */ true);
+        when(mCarWifiManager.isWifiEnabled()).thenReturn(false);
+        mFragmentController.setup();
+
+        when(mCarWifiManager.isWifiEnabled()).thenReturn(true);
+        mFragment.onWifiStateChanged(WifiManager.WIFI_STATE_ENABLED);
+
+        assertThat(getWifiSwitch().isChecked()).isTrue();
+    }
+
+    @Test
+    public void onWifiStateChanged_enabling_setsSwitchChecked() {
+        Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
+                PackageManager.FEATURE_WIFI, /* supported= */ true);
+        when(mCarWifiManager.isWifiEnabled()).thenReturn(false);
+        mFragmentController.setup();
+
+        mFragment.onWifiStateChanged(WifiManager.WIFI_STATE_ENABLING);
+
+        assertThat(getWifiSwitch().isChecked()).isTrue();
+    }
+
+    private MenuItem getWifiSwitch() {
+        ToolbarController toolbar = requireToolbar(mFragment.requireActivity());
+        return toolbar.getMenuItems().get(0);
+    }
+}