• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.content.Context.TELEPHONY_SUBSCRIPTION_SERVICE;
4 import static android.os.Build.VERSION_CODES.N;
5 import static com.google.common.truth.Truth.assertThat;
6 import static org.robolectric.RuntimeEnvironment.application;
7 import static org.robolectric.Shadows.shadowOf;
8 
9 import android.telephony.SubscriptionInfo;
10 import android.telephony.SubscriptionManager;
11 import androidx.test.ext.junit.runners.AndroidJUnit4;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.robolectric.annotation.Config;
16 import org.robolectric.shadows.ShadowSubscriptionManager.SubscriptionInfoBuilder;
17 
18 /** Test for {@link ShadowSubscriptionManager}. */
19 @RunWith(AndroidJUnit4.class)
20 @Config(minSdk = N)
21 public class ShadowSubscriptionManagerTest {
22 
23   private SubscriptionManager subscriptionManager;
24   private ShadowSubscriptionManager shadowSubscriptionManager;
25 
26   @Before
setUp()27   public void setUp() throws Exception {
28     subscriptionManager =
29         (SubscriptionManager) application.getSystemService(TELEPHONY_SUBSCRIPTION_SERVICE);
30     shadowSubscriptionManager = shadowOf(subscriptionManager);
31   }
32 
33   @Test
shouldGiveDefaultSubscriptionId()34   public void shouldGiveDefaultSubscriptionId() {
35     int testId = 42;
36     ShadowSubscriptionManager.setDefaultSubscriptionId(testId);
37     assertThat(subscriptionManager.getDefaultSubscriptionId()).isEqualTo(testId);
38   }
39 
40   @Test
shouldGiveDefaultDataSubscriptionId()41   public void shouldGiveDefaultDataSubscriptionId() {
42     int testId = 42;
43     shadowSubscriptionManager.setDefaultDataSubscriptionId(testId);
44     assertThat(subscriptionManager.getDefaultDataSubscriptionId()).isEqualTo(testId);
45   }
46 
47   @Test
shouldGiveDefaultSmsSubscriptionId()48   public void shouldGiveDefaultSmsSubscriptionId() {
49     int testId = 42;
50     shadowSubscriptionManager.setDefaultSmsSubscriptionId(testId);
51     assertThat(subscriptionManager.getDefaultSmsSubscriptionId()).isEqualTo(testId);
52   }
53 
54   @Test
shouldGiveDefaultVoiceSubscriptionId()55   public void shouldGiveDefaultVoiceSubscriptionId() {
56     int testId = 42;
57     shadowSubscriptionManager.setDefaultVoiceSubscriptionId(testId);
58     assertThat(subscriptionManager.getDefaultVoiceSubscriptionId()).isEqualTo(testId);
59   }
60 
61   @Test
addOnSubscriptionsChangedListener_shouldAddListener()62   public void addOnSubscriptionsChangedListener_shouldAddListener() {
63     DummySubscriptionsChangedListener listener = new DummySubscriptionsChangedListener();
64     shadowSubscriptionManager.addOnSubscriptionsChangedListener(listener);
65 
66     shadowSubscriptionManager.setActiveSubscriptionInfos(
67         SubscriptionInfoBuilder.newBuilder().setId(123).buildSubscriptionInfo());
68 
69     assertThat(listener.subscriptionChanged).isTrue();
70   }
71 
72   @Test
removeOnSubscriptionsChangedListener_shouldRemoveListener()73   public void removeOnSubscriptionsChangedListener_shouldRemoveListener() {
74     DummySubscriptionsChangedListener listener = new DummySubscriptionsChangedListener();
75     DummySubscriptionsChangedListener listener2 = new DummySubscriptionsChangedListener();
76     shadowSubscriptionManager.addOnSubscriptionsChangedListener(listener);
77     shadowSubscriptionManager.addOnSubscriptionsChangedListener(listener2);
78 
79     shadowSubscriptionManager.removeOnSubscriptionsChangedListener(listener);
80     shadowSubscriptionManager.setActiveSubscriptionInfos(
81         SubscriptionInfoBuilder.newBuilder().setId(123).buildSubscriptionInfo());
82 
83     assertThat(listener.subscriptionChanged).isFalse();
84     assertThat(listener2.subscriptionChanged).isTrue();
85   }
86 
87   @Test
getActiveSubscriptionInfo_shouldReturnInfoWithSubId()88   public void getActiveSubscriptionInfo_shouldReturnInfoWithSubId() {
89     SubscriptionInfo expectedSubscriptionInfo =
90         SubscriptionInfoBuilder.newBuilder().setId(123).buildSubscriptionInfo();
91     shadowSubscriptionManager.setActiveSubscriptionInfos(expectedSubscriptionInfo);
92 
93     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfo(123))
94         .isSameAs(expectedSubscriptionInfo);
95   }
96 
97   @Test
getActiveSubscriptionInfoForSimSlotIndex_shouldReturnInfoWithSlotIndex()98   public void getActiveSubscriptionInfoForSimSlotIndex_shouldReturnInfoWithSlotIndex() {
99     SubscriptionInfo expectedSubscriptionInfo =
100         SubscriptionInfoBuilder.newBuilder().setSimSlotIndex(123).buildSubscriptionInfo();
101     shadowSubscriptionManager.setActiveSubscriptionInfos(expectedSubscriptionInfo);
102 
103     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfoForSimSlotIndex(123))
104         .isSameAs(expectedSubscriptionInfo);
105   }
106 
107   @Test
getActiveSubscriptionInfo_shouldReturnNullForNullList()108   public void getActiveSubscriptionInfo_shouldReturnNullForNullList() {
109     shadowSubscriptionManager.setActiveSubscriptionInfoList(null);
110     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfo(123)).isNull();
111   }
112 
113   @Test
getActiveSubscriptionInfo_shouldReturnNullForNullVarargsList()114   public void getActiveSubscriptionInfo_shouldReturnNullForNullVarargsList() {
115     shadowSubscriptionManager.setActiveSubscriptionInfos((SubscriptionInfo[]) null);
116     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfo(123)).isNull();
117   }
118 
119   @Test
getActiveSubscriptionInfo_shouldReturnNullForEmptyList()120   public void getActiveSubscriptionInfo_shouldReturnNullForEmptyList() {
121     shadowSubscriptionManager.setActiveSubscriptionInfos();
122     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfo(123)).isNull();
123   }
124 
125   @Test
isNetworkRoaming_shouldReturnTrueIfSet()126   public void isNetworkRoaming_shouldReturnTrueIfSet() {
127     shadowSubscriptionManager.setNetworkRoamingStatus(123, /*isNetworkRoaming=*/ true);
128     assertThat(shadowSubscriptionManager.isNetworkRoaming(123)).isTrue();
129   }
130 
131   /** Multi act-asserts are discouraged but here we are testing the set+unset. */
132   @Test
isNetworkRoaming_shouldReturnFalseIfUnset()133   public void isNetworkRoaming_shouldReturnFalseIfUnset() {
134     shadowSubscriptionManager.setNetworkRoamingStatus(123, /*isNetworkRoaming=*/ true);
135     assertThat(shadowSubscriptionManager.isNetworkRoaming(123)).isTrue();
136 
137     shadowSubscriptionManager.setNetworkRoamingStatus(123, /*isNetworkRoaming=*/ false);
138     assertThat(shadowSubscriptionManager.isNetworkRoaming(123)).isFalse();
139   }
140 
141   /** Multi act-asserts are discouraged but here we are testing the set+clear. */
142   @Test
isNetworkRoaming_shouldReturnFalseOnClear()143   public void isNetworkRoaming_shouldReturnFalseOnClear() {
144     shadowSubscriptionManager.setNetworkRoamingStatus(123, /*isNetworkRoaming=*/ true);
145     assertThat(shadowSubscriptionManager.isNetworkRoaming(123)).isTrue();
146 
147     shadowSubscriptionManager.clearNetworkRoamingStatus();
148     assertThat(shadowSubscriptionManager.isNetworkRoaming(123)).isFalse();
149   }
150 
151   @Test
getActiveSubscriptionInfoCount_shouldReturnZeroIfActiveSubscriptionInfoListNotSet()152   public void getActiveSubscriptionInfoCount_shouldReturnZeroIfActiveSubscriptionInfoListNotSet() {
153     shadowSubscriptionManager.setActiveSubscriptionInfoList(null);
154 
155     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfoCount()).isEqualTo(0);
156   }
157 
158   @Test
getActiveSubscriptionInfoCount_shouldReturnSizeOfActiveSubscriotionInfosList()159   public void getActiveSubscriptionInfoCount_shouldReturnSizeOfActiveSubscriotionInfosList() {
160     SubscriptionInfo expectedSubscriptionInfo =
161         SubscriptionInfoBuilder.newBuilder().setId(123).buildSubscriptionInfo();
162     shadowSubscriptionManager.setActiveSubscriptionInfos(expectedSubscriptionInfo);
163 
164     assertThat(shadowSubscriptionManager.getActiveSubscriptionInfoCount()).isEqualTo(1);
165   }
166 
167   private static class DummySubscriptionsChangedListener
168       extends SubscriptionManager.OnSubscriptionsChangedListener {
169     private boolean subscriptionChanged = false;
170 
171     @Override
onSubscriptionsChanged()172     public void onSubscriptionsChanged() {
173       subscriptionChanged = true;
174     }
175   }
176 }
177