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.Mockito.spy;
22 
23 import android.view.LayoutInflater;
24 import android.widget.TextView;
25 
26 import androidx.fragment.app.FragmentActivity;
27 
28 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
29 import com.android.settings.testutils.shadow.ShadowRecoverySystem;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Rule;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnit;
38 import org.mockito.junit.MockitoRule;
39 import org.robolectric.Robolectric;
40 import org.robolectric.RobolectricTestRunner;
41 import org.robolectric.android.util.concurrent.PausedExecutorService;
42 import org.robolectric.annotation.Config;
43 import org.robolectric.shadows.ShadowLooper;
44 import org.robolectric.shadows.ShadowPausedAsyncTask;
45 
46 @RunWith(RobolectricTestRunner.class)
47 @Config(shadows = {ShadowRecoverySystem.class, ShadowBluetoothAdapter.class})
48 public class ResetNetworkConfirmTest {
49     @Rule
50     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
51 
52     private static final String TEST_PACKAGE = "com.android.settings";
53 
54     private FragmentActivity mActivity;
55 
56     @Mock
57     private ResetNetworkConfirm mResetNetworkConfirm;
58     private PausedExecutorService mExecutorService;
59 
60     @Before
setUp()61     public void setUp() {
62         mExecutorService = new PausedExecutorService();
63         ShadowPausedAsyncTask.overrideExecutor(mExecutorService);
64         mResetNetworkConfirm = new ResetNetworkConfirm();
65         mActivity = spy(Robolectric.setupActivity(FragmentActivity.class));
66         mResetNetworkConfirm.mActivity = mActivity;
67     }
68 
69     @After
tearDown()70     public void tearDown() {
71         ShadowRecoverySystem.reset();
72     }
73 
74     @Test
testResetNetworkData_notResetEsim()75     public void testResetNetworkData_notResetEsim() {
76         mResetNetworkConfirm.mResetNetworkRequest =
77                 new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE);
78         mResetNetworkConfirm.mResetSubscriptionContract =
79                 new ResetSubscriptionContract(mActivity,
80                 mResetNetworkConfirm.mResetNetworkRequest) {
81             @Override
82             public void onSubscriptionInactive(int subscriptionId) {
83                 mActivity.onBackPressed();
84             }
85         };
86 
87         mResetNetworkConfirm.mFinalClickListener.onClick(null /* View */);
88         mExecutorService.runAll();
89         ShadowLooper.idleMainLooper();
90 
91         assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount()).isEqualTo(0);
92     }
93 
94     @Test
setSubtitle_eraseEsim()95     public void setSubtitle_eraseEsim() {
96         mResetNetworkConfirm.mResetNetworkRequest =
97                 new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE);
98         mResetNetworkConfirm.mResetNetworkRequest.setResetEsim(TEST_PACKAGE);
99 
100         mResetNetworkConfirm.mContentView =
101                 LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null);
102 
103         mResetNetworkConfirm.setSubtitle();
104 
105         assertThat(((TextView) mResetNetworkConfirm.mContentView
106                 .findViewById(R.id.reset_network_confirm)).getText())
107                 .isEqualTo(mActivity.getString(R.string.reset_network_final_desc_esim));
108     }
109 
110     @Test
setSubtitle_notEraseEsim()111     public void setSubtitle_notEraseEsim() {
112         mResetNetworkConfirm.mResetNetworkRequest =
113                 new ResetNetworkRequest(ResetNetworkRequest.RESET_NONE);
114 
115         mResetNetworkConfirm.mContentView =
116                 LayoutInflater.from(mActivity).inflate(R.layout.reset_network_confirm, null);
117 
118         mResetNetworkConfirm.setSubtitle();
119 
120         assertThat(((TextView) mResetNetworkConfirm.mContentView
121                 .findViewById(R.id.reset_network_confirm)).getText())
122                 .isEqualTo(mActivity.getString(R.string.reset_network_final_desc));
123     }
124 }
125