Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 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 | |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 21 | import static org.mockito.Matchers.anyInt; |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 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; |
Lee Chou | a0e76e4 | 2020-01-14 17:54:02 +0800 | [diff] [blame] | 34 | import org.junit.Ignore; |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 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 | |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
| 44 | @RunWith(RobolectricTestRunner.class) |
| 45 | public class DataUsageUtilsTest { |
| 46 | |
| 47 | private static final int SUB_ID = 1; |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 48 | private static final int SUB_ID_2 = 2; |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 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 |
| 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 Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 73 | 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 Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
Lee Chou | a0e76e4 | 2020-01-14 17:54:02 +0800 | [diff] [blame] | 79 | @Ignore |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 80 | public void getMobileTemplate_infoNull_returnMobileAll() { |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 81 | when(mSubscriptionManager.isActiveSubId(SUB_ID)).thenReturn(false); |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 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 |
Lee Chou | a0e76e4 | 2020-01-14 17:54:02 +0800 | [diff] [blame] | 89 | @Ignore |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 90 | public void getMobileTemplate_groupUuidNull_returnMobileAll() { |
| 91 | when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1); |
| 92 | when(mInfo1.getGroupUuid()).thenReturn(null); |
zoey chen | e582f5b | 2019-12-03 21:02:27 +0800 | [diff] [blame] | 93 | when(mTelephonyManager.getMergedImsisFromGroup()) |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 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 |
Lee Chou | a0e76e4 | 2020-01-14 17:54:02 +0800 | [diff] [blame] | 102 | @Ignore |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 103 | public void getMobileTemplate_groupUuidExist_returnMobileMerged() { |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 104 | when(mSubscriptionManager.getActiveSubscriptionInfo(SUB_ID)).thenReturn(mInfo1); |
| 105 | when(mInfo1.getGroupUuid()).thenReturn(mParcelUuid); |
zoey chen | e582f5b | 2019-12-03 21:02:27 +0800 | [diff] [blame] | 106 | when(mTelephonyManager.getMergedImsisFromGroup()) |
Malcolm Chen | ac56a42 | 2019-07-01 16:32:21 -0700 | [diff] [blame] | 107 | .thenReturn(new String[] {SUBSCRIBER_ID, SUBSCRIBER_ID_2}); |
Lei Yu | 2a31c40 | 2019-05-28 10:08:58 -0700 | [diff] [blame] | 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 | } |