1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.net.wifi.p2p.WifiP2pManager; 29 import android.os.Looper; 30 import android.view.LayoutInflater; 31 import android.widget.TextView; 32 33 import androidx.fragment.app.FragmentActivity; 34 35 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; 36 import com.android.settings.testutils.shadow.ShadowRecoverySystem; 37 38 import org.junit.After; 39 import org.junit.Before; 40 import org.junit.Ignore; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 45 import org.robolectric.Robolectric; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.annotation.Config; 48 49 @RunWith(RobolectricTestRunner.class) 50 @Config(shadows = {ShadowRecoverySystem.class, ShadowBluetoothAdapter.class}) 51 public class ResetNetworkConfirmTest { 52 53 private FragmentActivity mActivity; 54 55 @Mock 56 private WifiP2pManager mWifiP2pManager; 57 @Mock 58 private ResetNetworkConfirm mResetNetworkConfirm; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 when(mWifiP2pManager.initialize(any(Context.class), any(Looper.class), any())) 64 .thenReturn(mock(WifiP2pManager.Channel.class)); 65 66 mResetNetworkConfirm = new ResetNetworkConfirm(); 67 mActivity = spy(Robolectric.setupActivity(FragmentActivity.class)); 68 when(mActivity.getSystemService(Context.WIFI_P2P_SERVICE)).thenReturn(mWifiP2pManager); 69 mResetNetworkConfirm.mActivity = mActivity; 70 } 71 72 @After tearDown()73 public void tearDown() { 74 ShadowRecoverySystem.reset(); 75 } 76 77 @Test 78 @Ignore testResetNetworkData_resetEsim()79 public void testResetNetworkData_resetEsim() { 80 mResetNetworkConfirm.mEraseEsim = true; 81 82 mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */); 83 Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); 84 85 assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(1); 86 } 87 88 @Test 89 @Ignore testResetNetworkData_notResetEsim()90 public void testResetNetworkData_notResetEsim() { 91 mResetNetworkConfirm.mEraseEsim = false; 92 93 mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */); 94 Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); 95 96 assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(0); 97 } 98 99 /** 100 * Test for WifiP2pManager factoryReset method. 101 */ 102 @Test testResetNetworkData_resetP2p()103 public void testResetNetworkData_resetP2p() { 104 mResetNetworkConfirm.p2pFactoryReset(mActivity); 105 106 verify(mWifiP2pManager).factoryReset(any(WifiP2pManager.Channel.class), any()); 107 } 108 109 @Test setSubtitle_eraseEsim()110 public void setSubtitle_eraseEsim() { 111 mResetNetworkConfirm.mEraseEsim = true; 112 mResetNetworkConfirm.mContentView = 113 LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); 114 115 mResetNetworkConfirm.setSubtitle(); 116 117 assertThat(((TextView) mResetNetworkConfirm.mContentView 118 .findViewById(R.id.reset_network_confirm)).getText()) 119 .isEqualTo(mActivity.getString(R.string.reset_network_final_desc_esim)); 120 } 121 122 @Test setSubtitle_notEraseEsim()123 public void setSubtitle_notEraseEsim() { 124 mResetNetworkConfirm.mEraseEsim = false; 125 mResetNetworkConfirm.mContentView = 126 LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null); 127 128 mResetNetworkConfirm.setSubtitle(); 129 130 assertThat(((TextView) mResetNetworkConfirm.mContentView 131 .findViewById(R.id.reset_network_confirm)).getText()) 132 .isEqualTo(mActivity.getString(R.string.reset_network_final_desc)); 133 } 134 } 135