From a4fe5c98714dbab424980fcee4820b88aa5d8a77 Mon Sep 17 00:00:00 2001 From: Salvador Martinez Date: Tue, 24 Apr 2018 16:03:34 -0700 Subject: [PATCH] Only start tether when not already enabled The toggle in the tether page currently tries to start tethering if it gets turned on. However, changing a wifi config can cause startTether to be called and also the toggle to switch leading to a double call to startTether. This CL makes it so that if we are already connected to tethering we don't try to start it again. Test: robotests Change-Id: I91085244c5d1fcc245a6b6480e638a32bc14f0ac Fixes: 78028548 Fixes: 78441216 Fixes: 74368363 --- .../tether/WifiTetherSwitchBarController.java | 6 ++--- .../WifiTetherSwitchBarControllerTest.java | 27 ++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java b/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java index bf203e61f05..f710613c471 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java +++ b/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java @@ -91,10 +91,10 @@ public class WifiTetherSwitchBarController implements SwitchWidgetController.OnS @Override public boolean onSwitchToggled(boolean isChecked) { - if (isChecked) { - startTether(); - } else { + if (!isChecked) { stopTether(); + } else if (!mWifiManager.isWifiApEnabled()) { + startTether(); } return true; } diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java index 943a66521fc..f5ac697fcf8 100644 --- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java @@ -18,7 +18,15 @@ package com.android.settings.wifi.tether; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import android.content.Context; @@ -68,7 +76,7 @@ public class WifiTetherSwitchBarControllerTest { } @Test - public void testConstructor_airplaneModeOn_switchBarDisabled() { + public void constructor_airplaneModeOn_switchBarDisabled() { Settings.Global.putInt(RuntimeEnvironment.application.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); @@ -79,7 +87,7 @@ public class WifiTetherSwitchBarControllerTest { } @Test - public void testStartTether_fail_resetSwitchBar() { + public void startTether_fail_resetSwitchBar() { when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(false); mController.startTether(); @@ -90,7 +98,7 @@ public class WifiTetherSwitchBarControllerTest { } @Test - public void testOnDataSaverChanged_setsEnabledCorrectly() { + public void onDataSaverChanged_setsEnabledCorrectly() { assertThat(mSwitchBar.isEnabled()).isTrue(); // try to turn data saver on @@ -102,6 +110,19 @@ public class WifiTetherSwitchBarControllerTest { when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(false); mController.onDataSaverChanged(false); assertThat(mSwitchBar.isEnabled()).isTrue(); + } + @Test + public void onSwitchToggled_onlyStartsTetherWhenNeeded() { + when(mWifiManager.isWifiApEnabled()).thenReturn(true); + mController.onSwitchToggled(true); + + verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any()); + + doReturn(false).when(mWifiManager).isWifiApEnabled(); + mController.onSwitchToggled(true); + + verify(mConnectivityManager, times(1)) + .startTethering(anyInt(), anyBoolean(), any(), any()); } }