diff --git a/src/com/android/settings/ResetNetworkConfirm.java b/src/com/android/settings/ResetNetworkConfirm.java index 2044af5228d..270f22b6f5c 100644 --- a/src/com/android/settings/ResetNetworkConfirm.java +++ b/src/com/android/settings/ResetNetworkConfirm.java @@ -26,6 +26,7 @@ import android.net.ConnectivityManager; import android.net.NetworkPolicyManager; import android.net.Uri; import android.net.wifi.WifiManager; +import android.net.wifi.p2p.WifiP2pManager; import android.os.AsyncTask; import android.os.Bundle; import android.os.RecoverySystem; @@ -129,6 +130,8 @@ public class ResetNetworkConfirm extends InstrumentedFragment { wifiManager.factoryReset(); } + p2pFactoryReset(context); + TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { @@ -180,6 +183,20 @@ public class ResetNetworkConfirm extends InstrumentedFragment { } } + @VisibleForTesting + void p2pFactoryReset(Context context) { + WifiP2pManager wifiP2pManager = (WifiP2pManager) + context.getSystemService(Context.WIFI_P2P_SERVICE); + if (wifiP2pManager != null) { + WifiP2pManager.Channel channel = wifiP2pManager.initialize( + context.getApplicationContext(), context.getMainLooper(), + null /* listener */); + if (channel != null) { + wifiP2pManager.factoryReset(channel, null /* listener */); + } + } + } + /** * Restore APN settings to default. */ diff --git a/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java b/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java index a10a4a41b10..6286cb95e28 100644 --- a/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java +++ b/tests/robotests/src/com/android/settings/ResetNetworkConfirmTest.java @@ -24,6 +24,7 @@ import android.app.Activity; import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.shadow.ShadowRecoverySystem; +import com.android.settings.testutils.shadow.ShadowWifiP2pManager; import org.junit.After; import org.junit.Before; @@ -35,7 +36,7 @@ import org.robolectric.Robolectric; import org.robolectric.annotation.Config; @RunWith(SettingsRobolectricTestRunner.class) -@Config(shadows = {ShadowRecoverySystem.class}) +@Config(shadows = {ShadowRecoverySystem.class, ShadowWifiP2pManager.class}) public class ResetNetworkConfirmTest { private Activity mActivity; @@ -52,6 +53,7 @@ public class ResetNetworkConfirmTest { @After public void tearDown() { ShadowRecoverySystem.reset(); + ShadowWifiP2pManager.reset(); } @Test @@ -76,4 +78,16 @@ public class ResetNetworkConfirmTest { assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()) .isEqualTo(0); } + + /** + * Test for WifiP2pManager factoryReset method. + */ + @Test + public void testResetNetworkData_resetP2p() { + + mResetNetworkConfirm.p2pFactoryReset(mActivity); + + assertThat(ShadowWifiP2pManager.getFactoryResetCount()) + .isEqualTo(1); + } } diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowWifiP2pManager.java b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowWifiP2pManager.java new file mode 100644 index 00000000000..8c383470ab6 --- /dev/null +++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowWifiP2pManager.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2018 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.settings.testutils.shadow; + +import android.net.wifi.p2p.WifiP2pManager; + +import org.robolectric.annotation.Implementation; +import org.robolectric.annotation.Implements; +import org.robolectric.annotation.Resetter; + +/** + * Shadown class for WifiP2pManager. + */ +@Implements(value = WifiP2pManager.class) +public class ShadowWifiP2pManager extends org.robolectric.shadows.ShadowWifiP2pManager { + + private static int sFactoryResetCount; + + /** + * factoryReset mock method. + */ + @Implementation + public void factoryReset(WifiP2pManager.Channel c, WifiP2pManager.ActionListener listener) { + if (c != null) { + sFactoryResetCount++; + } else { + throw new IllegalArgumentException("channel must be non-null."); + } + } + + /** + * Reset variables in shadow class. + */ + @Resetter + public static void reset() { + sFactoryResetCount = 0; + } + + /** + * Return the count of factoryReset called. + * + * @return the count of factoryReset called. + */ + public static int getFactoryResetCount() { + return sFactoryResetCount; + } +}