Stop usb tethering when user cancel it

- Call stop tethering to clear entitlement result when user do not want
  using USB tethering on USB menu.
- Add test case

Bug: 154933817
Test: make -j42 RunSettingsRoboTests
Change-Id: Idaaba8df2052f45e710c7959251817e8947a544f
diff --git a/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java b/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
index 31bca12..0789303 100644
--- a/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
+++ b/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsController.java
@@ -89,6 +89,9 @@
     @Override
     protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
         if (!connected || dataRole != DATA_ROLE_DEVICE) {
+            if (mPreviousFunction == UsbManager.FUNCTION_RNDIS) {
+                mConnectivityManager.stopTethering(TETHERING_USB);
+            }
             mProfilesContainer.setEnabled(false);
         } else {
             // Functions are only available in device mode
@@ -129,6 +132,9 @@
                 mConnectivityManager.startTethering(TETHERING_USB, true /* showProvisioningUi */,
                         mOnStartTetheringCallback);
             } else {
+                if (mPreviousFunction == UsbManager.FUNCTION_RNDIS) {
+                    mConnectivityManager.stopTethering(TETHERING_USB);
+                }
                 mUsbBackend.setCurrentFunctions(function);
             }
         }
diff --git a/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
index 3a6eec0..f1c4728 100644
--- a/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/usb/UsbDetailsFunctionsControllerTest.java
@@ -270,4 +270,42 @@
 
         verify(mUsbBackend).setCurrentFunctions(UsbManager.FUNCTION_PTP);
     }
+
+    @Test
+    public void refresh_previousFunctionIsRndis_stopTethering() {
+        mDetailsFunctionsController.mPreviousFunction = UsbManager.FUNCTION_RNDIS;
+
+        mDetailsFunctionsController.refresh(false, 0, 0, 0);
+
+        verify(mConnectivityManager).stopTethering(TETHERING_USB);
+    }
+
+    @Test
+    public void refresh_previousFunctionIsNotRndis_doNothing() {
+        mDetailsFunctionsController.mPreviousFunction = UsbManager.FUNCTION_MIDI;
+
+        mDetailsFunctionsController.refresh(false, 0, 0, 0);
+
+        verify(mConnectivityManager, never()).stopTethering(TETHERING_USB);
+    }
+
+    @Test
+    public void onRadioButtonClicked_previousFunctionIsRndis_stopTethering() {
+        mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
+        doReturn(UsbManager.FUNCTION_RNDIS).when(mUsbBackend).getCurrentFunctions();
+
+        mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
+
+        verify(mConnectivityManager).stopTethering(TETHERING_USB);
+    }
+
+    @Test
+    public void onRadioButtonClicked_previousFunctionIsNotRndis_doNothing() {
+        mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
+        doReturn(UsbManager.FUNCTION_MIDI).when(mUsbBackend).getCurrentFunctions();
+
+        mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
+
+        verify(mConnectivityManager, never()).stopTethering(TETHERING_USB);
+    }
 }