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.settingslib.net;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.net.NetworkTemplate;
27 import android.os.ParcelUuid;
28 import android.os.RemoteException;
29 import android.telephony.SubscriptionInfo;
30 import android.telephony.SubscriptionManager;
31 import android.telephony.TelephonyManager;
32 
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.RobolectricTestRunner;
40 import org.robolectric.RuntimeEnvironment;
41 
42 import java.util.List;
43 
44 @RunWith(RobolectricTestRunner.class)
45 public class DataUsageUtilsTest {
46 
47     private static final int SUB_ID = 1;
48     private static final int SUB_ID_2 = 2;
49     private static final String SUBSCRIBER_ID = "Test Subscriber";
50     private static final String SUBSCRIBER_ID_2 = "Test Subscriber 2";
51 
52     @Mock
53     private TelephonyManager mTelephonyManager;
54     @Mock
55     private SubscriptionManager mSubscriptionManager;
56     @Mock
57     private SubscriptionInfo mInfo1;
58     @Mock
59     private SubscriptionInfo mInfo2;
60     @Mock
61     private ParcelUuid mParcelUuid;
62     private Context mContext;
63     private List<SubscriptionInfo> mInfos;
64 
65     @Before
setUp()66     public void setUp() throws RemoteException {
67         MockitoAnnotations.initMocks(this);
68 
69         mContext = spy(RuntimeEnvironment.application);
70         when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
71         when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
72         when(mTelephonyManager.getSubscriberId(SUB_ID)).thenReturn(SUBSCRIBER_ID);
73         when(mTelephonyManager.getSubscriberId(SUB_ID_2)).thenReturn(SUBSCRIBER_ID_2);
74         when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
75         when(mSubscriptionManager.isActiveSubscriptionId(anyInt())).thenReturn(true);
76     }
77 
78     @Test
79     @Ignore
getMobileTemplate_infoNull_returnMobileAll()80     public void getMobileTemplate_infoNull_returnMobileAll() {
81         when(mSubscriptionManager.isActiveSubscriptionId(SUB_ID)).thenReturn(false);
82 
83         final NetworkTemplate networkTemplate = DataUsageUtils.getMobileTemplate(mContext, SUB_ID);
84         assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID)).isTrue();
85         assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID_2)).isFalse();
86     }
87 
88     @Test
89     @Ignore
getMobileTemplate_groupUuidNull_returnMobileAll()90     public void getMobileTemplate_groupUuidNull_returnMobileAll() {
91         when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
92         when(mInfo1.getGroupUuid()).thenReturn(null);
93         when(mTelephonyManager.getMergedImsisFromGroup())
94                 .thenReturn(new String[] {SUBSCRIBER_ID});
95 
96         final NetworkTemplate networkTemplate = DataUsageUtils.getMobileTemplate(mContext, SUB_ID);
97         assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID)).isTrue();
98         assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID_2)).isFalse();
99     }
100 
101     @Test
102     @Ignore
getMobileTemplate_groupUuidExist_returnMobileMerged()103     public void getMobileTemplate_groupUuidExist_returnMobileMerged() {
104         when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
105         when(mInfo1.getGroupUuid()).thenReturn(mParcelUuid);
106         when(mTelephonyManager.getMergedImsisFromGroup())
107                 .thenReturn(new String[] {SUBSCRIBER_ID, SUBSCRIBER_ID_2});
108 
109         final NetworkTemplate networkTemplate = DataUsageUtils.getMobileTemplate(mContext, SUB_ID);
110         assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID)).isTrue();
111         assertThat(networkTemplate.matchesSubscriberId(SUBSCRIBER_ID_2)).isTrue();
112     }
113 }
114