blob: 7be9fee027a337002f6d0d54607643091e961b89 [file] [log] [blame]
Lei Yu2a31c402019-05-28 10:08:58 -07001/*
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
17package com.android.settingslib.net;
18
19import static com.google.common.truth.Truth.assertThat;
20
Malcolm Chenac56a422019-07-01 16:32:21 -070021import static org.mockito.Matchers.anyInt;
Lei Yu2a31c402019-05-28 10:08:58 -070022import static org.mockito.Mockito.spy;
23import static org.mockito.Mockito.when;
24
25import android.content.Context;
26import android.net.NetworkTemplate;
27import android.os.ParcelUuid;
28import android.os.RemoteException;
29import android.telephony.SubscriptionInfo;
30import android.telephony.SubscriptionManager;
31import android.telephony.TelephonyManager;
32
33import org.junit.Before;
Lee Choua0e76e42020-01-14 17:54:02 +080034import org.junit.Ignore;
Lei Yu2a31c402019-05-28 10:08:58 -070035import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39import org.robolectric.RobolectricTestRunner;
40import org.robolectric.RuntimeEnvironment;
41
Lei Yu2a31c402019-05-28 10:08:58 -070042import java.util.List;
43
44@RunWith(RobolectricTestRunner.class)
45public class DataUsageUtilsTest {
46
47 private static final int SUB_ID = 1;
Malcolm Chenac56a422019-07-01 16:32:21 -070048 private static final int SUB_ID_2 = 2;
Lei Yu2a31c402019-05-28 10:08:58 -070049 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
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);
Malcolm Chenac56a422019-07-01 16:32:21 -070073 when(mTelephonyManager.getSubscriberId(SUB_ID_2)).thenReturn(SUBSCRIBER_ID_2);
74 when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
75 when(mSubscriptionManager.isActiveSubId(anyInt())).thenReturn(true);
Lei Yu2a31c402019-05-28 10:08:58 -070076 }
77
78 @Test
Lee Choua0e76e42020-01-14 17:54:02 +080079 @Ignore
Lei Yu2a31c402019-05-28 10:08:58 -070080 public void getMobileTemplate_infoNull_returnMobileAll() {
Malcolm Chenac56a422019-07-01 16:32:21 -070081 when(mSubscriptionManager.isActiveSubId(SUB_ID)).thenReturn(false);
Lei Yu2a31c402019-05-28 10:08:58 -070082
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
Lee Choua0e76e42020-01-14 17:54:02 +080089 @Ignore
Malcolm Chenac56a422019-07-01 16:32:21 -070090 public void getMobileTemplate_groupUuidNull_returnMobileAll() {
91 when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
92 when(mInfo1.getGroupUuid()).thenReturn(null);
zoey chene582f5b2019-12-03 21:02:27 +080093 when(mTelephonyManager.getMergedImsisFromGroup())
Malcolm Chenac56a422019-07-01 16:32:21 -070094 .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
Lee Choua0e76e42020-01-14 17:54:02 +0800102 @Ignore
Malcolm Chenac56a422019-07-01 16:32:21 -0700103 public void getMobileTemplate_groupUuidExist_returnMobileMerged() {
Lei Yu2a31c402019-05-28 10:08:58 -0700104 when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1);
105 when(mInfo1.getGroupUuid()).thenReturn(mParcelUuid);
zoey chene582f5b2019-12-03 21:02:27 +0800106 when(mTelephonyManager.getMergedImsisFromGroup())
Malcolm Chenac56a422019-07-01 16:32:21 -0700107 .thenReturn(new String[] {SUBSCRIBER_ID, SUBSCRIBER_ID_2});
Lei Yu2a31c402019-05-28 10:08:58 -0700108
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}