1package com.android.systemui.statusbar.policy;
2
3import static org.mockito.Mockito.mock;
4
5import java.util.ArrayList;
6import java.util.List;
7
8import org.mockito.Mockito;
9
10import android.content.Intent;
11import android.net.ConnectivityManager;
12import android.telephony.ServiceState;
13import android.telephony.SignalStrength;
14import android.telephony.SubscriptionInfo;
15import android.telephony.TelephonyManager;
16
17import com.android.internal.telephony.TelephonyIntents;
18import com.android.systemui.R;
19import com.android.systemui.statusbar.policy.NetworkControllerImpl.MobileSignalController;
20
21public class NetworkControllerSignalTest extends NetworkControllerBaseTest {
22
23    public void testNoIconWithoutMobile() {
24        // Turn off mobile network support.
25        Mockito.when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false);
26        // Create a new NetworkController as this is currently handled in constructor.
27        mNetworkController = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm,
28                mConfig, mock(AccessPointControllerImpl.class),
29                mock(MobileDataControllerImpl.class));
30        setupNetworkController();
31
32        verifyLastMobileDataIndicators(false, 0, 0);
33    }
34
35    public void testNoSimsIconPresent() {
36        // No Subscriptions.
37        mNetworkController.mMobileSignalControllers.clear();
38        mNetworkController.updateNoSims();
39
40        verifyHasNoSims(true);
41    }
42
43    public void testNoSimlessIconWithoutMobile() {
44        // Turn off mobile network support.
45        Mockito.when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false);
46        // Create a new NetworkController as this is currently handled in constructor.
47        mNetworkController = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm,
48                mConfig, mock(AccessPointControllerImpl.class),
49                mock(MobileDataControllerImpl.class));
50        setupNetworkController();
51
52        // No Subscriptions.
53        mNetworkController.mMobileSignalControllers.clear();
54        mNetworkController.updateNoSims();
55
56        verifyHasNoSims(false);
57    }
58
59    public void testSignalStrength() {
60        for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
61                testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
62            setupDefaultSignal();
63            setLevel(testStrength);
64
65            verifyLastMobileDataIndicators(true,
66                    TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH[1][testStrength],
67                    DEFAULT_ICON);
68
69            // Verify low inet number indexing.
70            setConnectivity(0, ConnectivityManager.TYPE_MOBILE, true);
71            verifyLastMobileDataIndicators(true,
72                    TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH[0][testStrength], 0);
73        }
74    }
75
76    public void testCdmaSignalStrength() {
77        for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
78                testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
79            setupDefaultSignal();
80            setCdma();
81            setLevel(testStrength);
82
83            verifyLastMobileDataIndicators(true,
84                    TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH[1][testStrength],
85                    TelephonyIcons.DATA_1X[1][0 /* No direction */]);
86        }
87    }
88
89    public void testSignalRoaming() {
90        for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
91                testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
92            setupDefaultSignal();
93            setGsmRoaming(true);
94            setLevel(testStrength);
95
96            verifyLastMobileDataIndicators(true,
97                    TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH_ROAMING[1][testStrength],
98                    TelephonyIcons.ROAMING_ICON);
99        }
100    }
101
102    public void testCdmaSignalRoaming() {
103        for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
104                testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
105            setupDefaultSignal();
106            setCdma();
107            setCdmaRoaming(true);
108            setLevel(testStrength);
109
110            verifyLastMobileDataIndicators(true,
111                    TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH_ROAMING[1][testStrength],
112                    TelephonyIcons.ROAMING_ICON);
113        }
114    }
115
116    public void testQsSignalStrength() {
117        for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
118                testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
119            setupDefaultSignal();
120            setLevel(testStrength);
121
122            verifyLastQsMobileDataIndicators(true,
123                    TelephonyIcons.QS_TELEPHONY_SIGNAL_STRENGTH[1][testStrength],
124                    DEFAULT_QS_ICON, false, false);
125        }
126    }
127
128    public void testCdmaQsSignalStrength() {
129        for (int testStrength = SignalStrength.SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
130                testStrength <= SignalStrength.SIGNAL_STRENGTH_GREAT; testStrength++) {
131            setupDefaultSignal();
132            setCdma();
133            setLevel(testStrength);
134
135            verifyLastQsMobileDataIndicators(true,
136                    TelephonyIcons.QS_TELEPHONY_SIGNAL_STRENGTH[1][testStrength],
137                    TelephonyIcons.QS_ICON_1X, false, false);
138        }
139    }
140
141    public void testNoRoamingWithoutSignal() {
142        setupDefaultSignal();
143        setCdma();
144        setCdmaRoaming(true);
145        setVoiceRegState(ServiceState.STATE_OUT_OF_SERVICE);
146        setDataRegState(ServiceState.STATE_OUT_OF_SERVICE);
147
148        // This exposes the bug in b/18034542, and should be switched to the commented out
149        // verification below (and pass), once the bug is fixed.
150        verifyLastMobileDataIndicators(true, R.drawable.stat_sys_signal_null,
151                TelephonyIcons.ROAMING_ICON);
152        //verifyLastMobileDataIndicators(true, R.drawable.stat_sys_signal_null, 0 /* No Icon */);
153    }
154
155    // Some tests of actual NetworkController code, just internals not display stuff
156    // TODO: Put this somewhere else, maybe in its own file.
157    public void testHasCorrectMobileControllers() {
158        int[] testSubscriptions = new int[] { 1, 5, 3 };
159        int notTestSubscription = 0;
160        MobileSignalController mobileSignalController = Mockito.mock(MobileSignalController.class);
161
162        mNetworkController.mMobileSignalControllers.clear();
163        List<SubscriptionInfo> subscriptions = new ArrayList<>();
164        for (int i = 0; i < testSubscriptions.length; i++) {
165            // Force the test controllers into NetworkController.
166            mNetworkController.mMobileSignalControllers.put(testSubscriptions[i],
167                    mobileSignalController);
168
169            // Generate a list of subscriptions we will tell the NetworkController to use.
170            SubscriptionInfo mockSubInfo = Mockito.mock(SubscriptionInfo.class);
171            Mockito.when(mockSubInfo.getSubscriptionId()).thenReturn(testSubscriptions[i]);
172            subscriptions.add(mockSubInfo);
173        }
174        assertTrue(mNetworkController.hasCorrectMobileControllers(subscriptions));
175
176        // Add a subscription that the NetworkController doesn't know about.
177        SubscriptionInfo mockSubInfo = Mockito.mock(SubscriptionInfo.class);
178        Mockito.when(mockSubInfo.getSubscriptionId()).thenReturn(notTestSubscription);
179        subscriptions.add(mockSubInfo);
180        assertFalse(mNetworkController.hasCorrectMobileControllers(subscriptions));
181    }
182
183    public void testSetCurrentSubscriptions() {
184        // We will not add one controller to make sure it gets created.
185        int indexToSkipController = 0;
186        // We will not add one subscription to make sure it's controller gets removed.
187        int indexToSkipSubscription = 1;
188
189        int[] testSubscriptions = new int[] { 1, 5, 3 };
190        MobileSignalController[] mobileSignalControllers = new MobileSignalController[] {
191                Mockito.mock(MobileSignalController.class),
192                Mockito.mock(MobileSignalController.class),
193                Mockito.mock(MobileSignalController.class),
194        };
195        mNetworkController.mMobileSignalControllers.clear();
196        List<SubscriptionInfo> subscriptions = new ArrayList<>();
197        for (int i = 0; i < testSubscriptions.length; i++) {
198            if (i != indexToSkipController) {
199                // Force the test controllers into NetworkController.
200                mNetworkController.mMobileSignalControllers.put(testSubscriptions[i],
201                        mobileSignalControllers[i]);
202            }
203
204            if (i != indexToSkipSubscription) {
205                // Generate a list of subscriptions we will tell the NetworkController to use.
206                SubscriptionInfo mockSubInfo = Mockito.mock(SubscriptionInfo.class);
207                Mockito.when(mockSubInfo.getSubscriptionId()).thenReturn(testSubscriptions[i]);
208                Mockito.when(mockSubInfo.getSimSlotIndex()).thenReturn(testSubscriptions[i]);
209                subscriptions.add(mockSubInfo);
210            }
211        }
212
213        // We can only test whether unregister gets called if it thinks its in a listening
214        // state.
215        mNetworkController.mListening = true;
216        mNetworkController.setCurrentSubscriptions(subscriptions);
217
218        for (int i = 0; i < testSubscriptions.length; i++) {
219            if (i == indexToSkipController) {
220                // Make sure a controller was created despite us not adding one.
221                assertTrue(mNetworkController.mMobileSignalControllers.containsKey(
222                        testSubscriptions[i]));
223            } else if (i == indexToSkipSubscription) {
224                // Make sure the controller that did exist was removed
225                assertFalse(mNetworkController.mMobileSignalControllers.containsKey(
226                        testSubscriptions[i]));
227            } else {
228                // If a MobileSignalController is around it needs to not be unregistered.
229                Mockito.verify(mobileSignalControllers[i], Mockito.never())
230                        .unregisterListener();
231            }
232        }
233    }
234
235    private void setCdma() {
236        setIsGsm(false);
237        updateDataConnectionState(TelephonyManager.DATA_CONNECTED,
238                TelephonyManager.NETWORK_TYPE_CDMA);
239        setCdmaRoaming(false);
240    }
241}
242