1 /*
2  * Copyright (C) 2019 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.sim;
18 
19 import static com.android.settings.sim.SimDialogActivity.PREFERRED_PICK;
20 import static com.android.settings.sim.SimDialogActivity.PREFERRED_SIM;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.Mockito.doNothing;
26 import static org.mockito.Mockito.doReturn;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31 
32 import android.content.DialogInterface;
33 
34 import androidx.appcompat.app.AlertDialog;
35 
36 import com.android.settings.network.SubscriptionUtil;
37 import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
38 
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.annotation.Config;
44 
45 import java.util.Arrays;
46 
47 @RunWith(RobolectricTestRunner.class)
48 @Config(shadows = ShadowAlertDialogCompat.class)
49 public class PreferredSimDialogFragmentTest extends
50         SimDialogFragmentTestBase<PreferredSimDialogFragment> {
51 
52     @Override
setUp()53     public void setUp() {
54         super.setUp();
55         setDialogType(PREFERRED_PICK);
56         mFragment = spy(PreferredSimDialogFragment.newInstance());
57         doReturn(mSubscriptionManager).when(mFragment).getSubscriptionManager();
58     }
59 
60     @Ignore
61     @Test
onCreateDialog_noSims_dismissed()62     public void onCreateDialog_noSims_dismissed() {
63         when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(anyInt()))
64                 .thenReturn(null);
65         mIntent.putExtra(PREFERRED_SIM, 0);
66         startDialog();
67         verify(mFragment).dismiss();
68     }
69 
70     @Ignore
71     @Test
onCreateDialog_oneSimWrongSlotArgument_dismissed()72     public void onCreateDialog_oneSimWrongSlotArgument_dismissed() {
73         when(mSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(1)).thenReturn(null);
74         mIntent.putExtra(PREFERRED_SIM, 1);
75         startDialog();
76         verify(mFragment).dismiss();
77     }
78 
79     @Ignore
80     @Test
onCreateDialog_twoSimsSelectFirst_correctMessage()81     public void onCreateDialog_twoSimsSelectFirst_correctMessage() {
82         SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSim1, mSim2));
83         SubscriptionUtil.setActiveSubscriptionsForTesting(Arrays.asList(mSim1, mSim2));
84         mIntent.putExtra(PREFERRED_SIM, 0);
85 
86         final AlertDialog alertDialog = startDialog();
87         final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(alertDialog);
88         final String message = (String) shadowDialog.getMessage();
89         assertThat(message).contains(SIM1_NAME);
90         assertThat(message).doesNotContain(SIM2_NAME);
91     }
92 
93     @Ignore
94     @Test
onCreateDialog_twoSimsSelectSecond_correctMessage()95     public void onCreateDialog_twoSimsSelectSecond_correctMessage() {
96         SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSim1, mSim2));
97         SubscriptionUtil.setActiveSubscriptionsForTesting(Arrays.asList(mSim1, mSim2));
98         mIntent.putExtra(PREFERRED_SIM, 1);
99 
100         final AlertDialog alertDialog = startDialog();
101         final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(alertDialog);
102         final String message = (String) shadowDialog.getMessage();
103         assertThat(message).contains(SIM2_NAME);
104         assertThat(message).doesNotContain(SIM1_NAME);
105     }
106 
107     @Ignore
108     @Test
onClick_yesClicked_callsOnSubscriptionSelected()109     public void onClick_yesClicked_callsOnSubscriptionSelected() {
110         SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSim1, mSim2));
111         mIntent.putExtra(PREFERRED_SIM, 0);
112 
113         final AlertDialog alertDialog = startDialog();
114 
115         final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity());
116         doReturn(activity).when(mFragment).getActivity();
117         doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt());
118 
119         mFragment.onClick(alertDialog, DialogInterface.BUTTON_POSITIVE);
120         verify(activity).onSubscriptionSelected(PREFERRED_PICK, SIM1_ID);
121     }
122 
123     @Ignore
124     @Test
onClick_noClicked_doesNotCallOnSubscriptionSelected()125     public void onClick_noClicked_doesNotCallOnSubscriptionSelected() {
126         SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSim1, mSim2));
127         mIntent.putExtra(PREFERRED_SIM, 0);
128 
129         final AlertDialog alertDialog = startDialog();
130 
131         final SimDialogActivity activity = (SimDialogActivity) spy(mFragment.getActivity());
132         doReturn(activity).when(mFragment).getActivity();
133         doNothing().when(activity).onSubscriptionSelected(anyInt(), anyInt());
134 
135         mFragment.onClick(alertDialog, DialogInterface.BUTTON_NEGATIVE);
136         verify(activity, never()).onSubscriptionSelected(anyInt(), anyInt());
137     }
138 }
139