1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settingslib.net;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.usage.NetworkStats;
import android.app.usage.NetworkStatsManager;
import android.content.Context;
import android.net.NetworkTemplate;
import android.os.RemoteException;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.shadows.ShadowSubscriptionManager;
import java.util.Set;
@RunWith(RobolectricTestRunner.class)
public class DataUsageControllerTest {
private static final String SUB_ID = "Test Subscriber";
private static final String SUB_ID_2 = "Test Subscriber 2";
@Mock
private TelephonyManager mTelephonyManager;
@Mock
private SubscriptionManager mSubscriptionManager;
@Mock
private NetworkStatsManager mNetworkStatsManager;
@Mock
private Context mContext;
private NetworkTemplate mNetworkTemplate;
private NetworkTemplate mNetworkTemplate2;
private NetworkTemplate mWifiNetworkTemplate;
private DataUsageController mController;
private final int mDefaultSubscriptionId = 1234;
@Before
public void setUp() throws RemoteException {
MockitoAnnotations.initMocks(this);
when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
.thenReturn(mSubscriptionManager);
when(mContext.getSystemService(NetworkStatsManager.class)).thenReturn(mNetworkStatsManager);
mController = new DataUsageController(mContext);
ShadowSubscriptionManager.setDefaultDataSubscriptionId(mDefaultSubscriptionId);
doReturn(SUB_ID).when(mTelephonyManager).getSubscriberId();
mNetworkTemplate = new NetworkTemplate.Builder(NetworkTemplate.MATCH_CARRIER)
.setMeteredness(android.net.NetworkStats.METERED_YES)
.setSubscriberIds(Set.of(SUB_ID)).build();
mNetworkTemplate2 = new NetworkTemplate.Builder(NetworkTemplate.MATCH_CARRIER)
.setMeteredness(android.net.NetworkStats.METERED_YES)
.setSubscriberIds(Set.of(SUB_ID_2)).build();
mWifiNetworkTemplate = new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build();
}
@Test
public void getHistoricalUsageLevel_shouldQuerySummaryForDevice() throws Exception {
mController.getHistoricalUsageLevel(mWifiNetworkTemplate);
verify(mNetworkStatsManager).querySummaryForDevice(eq(mWifiNetworkTemplate),
eq(0L) /* startTime */, anyLong() /* endTime */);
}
@Test
public void getHistoricalUsageLevel_noUsageData_shouldReturn0() throws Exception {
when(mNetworkStatsManager.querySummaryForDevice(eq(mWifiNetworkTemplate),
eq(0L) /* startTime */, anyLong() /* endTime */))
.thenReturn(mock(NetworkStats.Bucket.class));
assertThat(mController.getHistoricalUsageLevel(mWifiNetworkTemplate))
.isEqualTo(0L);
}
@Test
public void getHistoricalUsageLevel_hasUsageData_shouldReturnTotalUsage() throws Exception {
final long receivedBytes = 743823454L;
final long transmittedBytes = 16574289L;
final NetworkStats.Bucket bucket = mock(NetworkStats.Bucket.class);
when(bucket.getRxBytes()).thenReturn(receivedBytes);
when(bucket.getTxBytes()).thenReturn(transmittedBytes);
when(mNetworkStatsManager.querySummaryForDevice(eq(mWifiNetworkTemplate),
eq(0L) /* startTime */, anyLong() /* endTime */)).thenReturn(bucket);
assertThat(mController.getHistoricalUsageLevel(mWifiNetworkTemplate))
.isEqualTo(receivedBytes + transmittedBytes);
}
@Test
public void getDataUsageInfo_hasUsageData_shouldReturnCorrectUsageForExplicitSubId()
throws Exception {
// First setup a stats bucket for the default subscription / subscriber ID.
final long defaultSubRx = 1234567L;
final long defaultSubTx = 123456L;
final NetworkStats.Bucket defaultSubscriberBucket = mock(NetworkStats.Bucket.class);
when(defaultSubscriberBucket.getRxBytes()).thenReturn(defaultSubRx);
when(defaultSubscriberBucket.getTxBytes()).thenReturn(defaultSubTx);
when(mNetworkStatsManager.querySummaryForDevice(eq(mNetworkTemplate), eq(0L)/* startTime */,
anyLong() /* endTime */)).thenReturn(defaultSubscriberBucket);
// Now setup a stats bucket for a different, non-default subscription / subscriber ID.
final long nonDefaultSubRx = 7654321L;
final long nonDefaultSubTx = 654321L;
final NetworkStats.Bucket nonDefaultSubscriberBucket = mock(NetworkStats.Bucket.class);
when(nonDefaultSubscriberBucket.getRxBytes()).thenReturn(nonDefaultSubRx);
when(nonDefaultSubscriberBucket.getTxBytes()).thenReturn(nonDefaultSubTx);
final int explicitSubscriptionId = 55;
when(mNetworkStatsManager.querySummaryForDevice(eq(mNetworkTemplate2),
eq(0L)/* startTime */, anyLong() /* endTime */)).thenReturn(
nonDefaultSubscriberBucket);
doReturn(SUB_ID_2).when(mTelephonyManager).getSubscriberId();
// Now verify that when we're asking for stats on the non-default subscription, we get
// the data back for that subscription and *not* the default one.
mController.setSubscriptionId(explicitSubscriptionId);
assertThat(mController.getHistoricalUsageLevel(mNetworkTemplate2)).isEqualTo(
nonDefaultSubRx + nonDefaultSubTx);
}
@Test
public void getTelephonyManager_shouldCreateWithExplicitSubId() {
int explicitSubId = 1;
TelephonyManager tmForSub1 = new TelephonyManager(mContext, explicitSubId);
when(mTelephonyManager.createForSubscriptionId(eq(explicitSubId))).thenReturn(tmForSub1);
// Set a specific subId.
mController.setSubscriptionId(explicitSubId);
assertThat(mController.getTelephonyManager()).isEqualTo(tmForSub1);
verify(mTelephonyManager).createForSubscriptionId(eq(explicitSubId));
}
@Test
public void getTelephonyManager_noExplicitSubId_shouldCreateWithDefaultDataSubId()
throws Exception {
TelephonyManager tmForDefaultSub = new TelephonyManager(mContext, mDefaultSubscriptionId);
when(mTelephonyManager.createForSubscriptionId(mDefaultSubscriptionId))
.thenReturn(tmForDefaultSub);
// No subId is set. It should use default data sub.
assertThat(mController.getTelephonyManager()).isEqualTo(tmForDefaultSub);
verify(mTelephonyManager).createForSubscriptionId(mDefaultSubscriptionId);
}
@Test
public void getTelephonyManager_noExplicitSubIdOrDefaultSub_shouldCreateWithActiveSub()
throws Exception {
int activeSubId = 2;
ShadowSubscriptionManager.setDefaultDataSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
when(mSubscriptionManager.getActiveSubscriptionIdList())
.thenReturn(new int[] {activeSubId});
TelephonyManager tmForActiveSub = new TelephonyManager(mContext, activeSubId);
when(mTelephonyManager.createForSubscriptionId(activeSubId))
.thenReturn(tmForActiveSub);
// No subId is set, default data subId is also not set. So should use the only active subId.
assertThat(mController.getTelephonyManager()).isEqualTo(tmForActiveSub);
verify(mTelephonyManager).createForSubscriptionId(activeSubId);
}
}
|