fix: 首次提交
This commit is contained in:
@@ -0,0 +1,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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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 org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.usage.NetworkStatsManager;
|
||||
import android.content.Context;
|
||||
import android.net.NetworkPolicy;
|
||||
import android.net.NetworkPolicyManager;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.os.RemoteException;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
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;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NetworkCycleChartDataLoaderTest {
|
||||
|
||||
@Mock
|
||||
private NetworkStatsManager mNetworkStatsManager;
|
||||
@Mock
|
||||
private NetworkPolicyManager mNetworkPolicyManager;
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private NetworkTemplate mNetworkTemplate;
|
||||
|
||||
private NetworkCycleChartDataLoader mLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getSystemService(Context.NETWORK_STATS_SERVICE))
|
||||
.thenReturn(mNetworkStatsManager);
|
||||
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE))
|
||||
.thenReturn(mNetworkPolicyManager);
|
||||
when(mNetworkPolicyManager.getNetworkPolicies()).thenReturn(new NetworkPolicy[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recordUsage_shouldQueryNetworkSummaryForDevice() throws RemoteException {
|
||||
final long end = System.currentTimeMillis();
|
||||
final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
|
||||
mLoader = NetworkCycleChartDataLoader.builder(mContext)
|
||||
.setNetworkTemplate(mNetworkTemplate)
|
||||
.build();
|
||||
|
||||
mLoader.recordUsage(start, end);
|
||||
|
||||
verify(mNetworkStatsManager).querySummaryForDevice(mNetworkTemplate, start, end);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* 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 android.app.usage.NetworkStats.Bucket.STATE_ALL;
|
||||
import static android.app.usage.NetworkStats.Bucket.STATE_FOREGROUND;
|
||||
import static android.net.NetworkStats.TAG_NONE;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.usage.NetworkStatsManager;
|
||||
import android.content.Context;
|
||||
import android.net.NetworkPolicy;
|
||||
import android.net.NetworkPolicyManager;
|
||||
import android.net.NetworkStats;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.text.format.DateUtils;
|
||||
|
||||
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 java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NetworkCycleDataForUidLoaderTest {
|
||||
private static final String SUB_ID = "Test Subscriber";
|
||||
|
||||
@Mock
|
||||
private NetworkStatsManager mNetworkStatsManager;
|
||||
@Mock
|
||||
private NetworkPolicyManager mNetworkPolicyManager;
|
||||
@Mock
|
||||
private Context mContext;
|
||||
private NetworkTemplate mNetworkTemplate;
|
||||
|
||||
private NetworkCycleDataForUidLoader mLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getSystemService(Context.NETWORK_STATS_SERVICE))
|
||||
.thenReturn(mNetworkStatsManager);
|
||||
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE))
|
||||
.thenReturn(mNetworkPolicyManager);
|
||||
when(mNetworkPolicyManager.getNetworkPolicies()).thenReturn(new NetworkPolicy[0]);
|
||||
mNetworkTemplate = new NetworkTemplate.Builder(NetworkTemplate.MATCH_CARRIER)
|
||||
.setMeteredness(NetworkStats.METERED_YES)
|
||||
.setSubscriberIds(Set.of(SUB_ID)).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recordUsage_shouldQueryNetworkDetailsForUidAndForegroundState() {
|
||||
final long end = System.currentTimeMillis();
|
||||
final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
|
||||
final int uid = 1;
|
||||
mLoader = spy(NetworkCycleDataForUidLoader.builder(mContext)
|
||||
.addUid(uid)
|
||||
.setNetworkTemplate(mNetworkTemplate)
|
||||
.build());
|
||||
doReturn(1024L).when(mLoader).getTotalUsage(any());
|
||||
|
||||
mLoader.recordUsage(start, end);
|
||||
|
||||
verify(mNetworkStatsManager).queryDetailsForUidTagState(
|
||||
mNetworkTemplate, start, end, uid, TAG_NONE, STATE_FOREGROUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recordUsage_retrieveDetailIsFalse_shouldNotQueryNetworkForegroundState() {
|
||||
final long end = System.currentTimeMillis();
|
||||
final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
|
||||
final int uid = 1;
|
||||
mLoader = spy(NetworkCycleDataForUidLoader.builder(mContext)
|
||||
.setRetrieveDetail(false).addUid(uid).build());
|
||||
doReturn(1024L).when(mLoader).getTotalUsage(any());
|
||||
|
||||
mLoader.recordUsage(start, end);
|
||||
verify(mNetworkStatsManager, never()).queryDetailsForUidTagState(
|
||||
mNetworkTemplate, start, end, uid, TAG_NONE, STATE_FOREGROUND);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recordUsage_multipleUids_shouldQueryNetworkDetailsForEachUid() {
|
||||
final long end = System.currentTimeMillis();
|
||||
final long start = end - (DateUtils.WEEK_IN_MILLIS * 4);
|
||||
mLoader = spy(NetworkCycleDataForUidLoader.builder(mContext)
|
||||
.addUid(1)
|
||||
.addUid(2)
|
||||
.addUid(3)
|
||||
.setNetworkTemplate(mNetworkTemplate)
|
||||
.build());
|
||||
doReturn(1024L).when(mLoader).getTotalUsage(any());
|
||||
|
||||
mLoader.recordUsage(start, end);
|
||||
|
||||
verify(mNetworkStatsManager).queryDetailsForUidTagState(mNetworkTemplate, start, end, 1,
|
||||
TAG_NONE, STATE_ALL);
|
||||
verify(mNetworkStatsManager).queryDetailsForUidTagState(mNetworkTemplate, start, end, 2,
|
||||
TAG_NONE, STATE_ALL);
|
||||
verify(mNetworkStatsManager).queryDetailsForUidTagState(mNetworkTemplate, start, end, 3,
|
||||
TAG_NONE, STATE_ALL);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
* 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 android.app.usage.NetworkStats.Bucket.UID_ALL;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
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.ConnectivityManager;
|
||||
import android.net.NetworkPolicy;
|
||||
import android.net.NetworkPolicyManager;
|
||||
import android.net.NetworkTemplate;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Range;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
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.util.ReflectionHelpers;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NetworkCycleDataLoaderTest {
|
||||
|
||||
@Mock
|
||||
private NetworkStatsManager mNetworkStatsManager;
|
||||
@Mock
|
||||
private NetworkPolicyManager mNetworkPolicyManager;
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private NetworkPolicy mPolicy;
|
||||
@Mock
|
||||
private Iterator<Range<ZonedDateTime>> mIterator;
|
||||
|
||||
private NetworkCycleDataTestLoader mLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
when(mContext.getSystemService(Context.NETWORK_STATS_SERVICE))
|
||||
.thenReturn(mNetworkStatsManager);
|
||||
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE))
|
||||
.thenReturn(mNetworkPolicyManager);
|
||||
when(mPolicy.cycleIterator()).thenReturn(mIterator);
|
||||
when(mNetworkPolicyManager.getNetworkPolicies()).thenReturn(new NetworkPolicy[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadInBackground_noNetworkPolicy_shouldLoad4WeeksData() {
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
doNothing().when(mLoader).loadFourWeeksData();
|
||||
|
||||
mLoader.loadInBackground();
|
||||
|
||||
verify(mLoader).loadFourWeeksData();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadInBackground_hasNetworkPolicy_shouldLoadPolicyData() {
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
ReflectionHelpers.setField(mLoader, "mPolicy", mPolicy);
|
||||
|
||||
mLoader.loadInBackground();
|
||||
|
||||
verify(mLoader).loadPolicyData();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadInBackground_hasCyclePeriod_shouldLoadDataForSpecificCycles() {
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
doNothing().when(mLoader).loadDataForSpecificCycles();
|
||||
final ArrayList<Long> cycles = new ArrayList<>();
|
||||
cycles.add(67890L);
|
||||
cycles.add(12345L);
|
||||
ReflectionHelpers.setField(mLoader, "mCycles", cycles);
|
||||
|
||||
mLoader.loadInBackground();
|
||||
|
||||
verify(mLoader).loadDataForSpecificCycles();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadPolicyData_shouldRecordUsageFromPolicyCycle() {
|
||||
final int networkType = ConnectivityManager.TYPE_MOBILE;
|
||||
final String subId = "TestSubscriber";
|
||||
final ZonedDateTime now = ZonedDateTime.now();
|
||||
final Range<ZonedDateTime> cycle = new Range<>(now, now);
|
||||
final long nowInMs = now.toInstant().toEpochMilli();
|
||||
// mock 1 cycle data.
|
||||
// hasNext() will be called internally in next(), hence setting it to return true twice.
|
||||
when(mIterator.hasNext()).thenReturn(true).thenReturn(true).thenReturn(false);
|
||||
when(mIterator.next()).thenReturn(cycle);
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
ReflectionHelpers.setField(mLoader, "mPolicy", mPolicy);
|
||||
|
||||
mLoader.loadPolicyData();
|
||||
|
||||
verify(mLoader).recordUsage(nowInMs, nowInMs);
|
||||
}
|
||||
|
||||
private NetworkStats.Bucket makeMockBucket(int uid, long rxBytes, long txBytes,
|
||||
long start, long end) {
|
||||
NetworkStats.Bucket ret = mock(NetworkStats.Bucket.class);
|
||||
when(ret.getUid()).thenReturn(uid);
|
||||
when(ret.getRxBytes()).thenReturn(rxBytes);
|
||||
when(ret.getTxBytes()).thenReturn(txBytes);
|
||||
when(ret.getStartTimeStamp()).thenReturn(start);
|
||||
when(ret.getEndTimeStamp()).thenReturn(end);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadFourWeeksData_shouldRecordUsageForLast4Weeks() {
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
final long now = System.currentTimeMillis();
|
||||
final long fourWeeksAgo = now - (DateUtils.WEEK_IN_MILLIS * 4);
|
||||
final long twoDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 2);
|
||||
mLoader.addBucket(makeMockBucket(UID_ALL, 123, 456, twoDaysAgo, now));
|
||||
|
||||
mLoader.loadFourWeeksData();
|
||||
|
||||
verify(mLoader).recordUsage(fourWeeksAgo, now);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void loadDataForSpecificCycles_shouldRecordUsageForSpecifiedTime() {
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
final long now = System.currentTimeMillis();
|
||||
final long tenDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 10);
|
||||
final long twentyDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 20);
|
||||
final long thirtyDaysAgo = now - (DateUtils.DAY_IN_MILLIS * 30);
|
||||
final ArrayList<Long> cycles = new ArrayList<>();
|
||||
cycles.add(now);
|
||||
cycles.add(tenDaysAgo);
|
||||
cycles.add(twentyDaysAgo);
|
||||
cycles.add(thirtyDaysAgo);
|
||||
ReflectionHelpers.setField(mLoader, "mCycles", cycles);
|
||||
|
||||
mLoader.loadDataForSpecificCycles();
|
||||
|
||||
verify(mLoader).recordUsage(tenDaysAgo, now);
|
||||
verify(mLoader).recordUsage(twentyDaysAgo, tenDaysAgo);
|
||||
verify(mLoader).recordUsage(thirtyDaysAgo, twentyDaysAgo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTimeRangeOf() {
|
||||
mLoader = spy(new NetworkCycleDataTestLoader(mContext));
|
||||
// If empty, new Range(MAX_VALUE, MIN_VALUE) will be constructed. Hence, the function
|
||||
// should throw.
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> mLoader.getTimeRangeOf(mock(NetworkStats.class)));
|
||||
|
||||
mLoader.addBucket(makeMockBucket(UID_ALL, 123, 456, 0, 10));
|
||||
// Feed the function with unused NetworkStats. The actual data injection is
|
||||
// done by addBucket.
|
||||
assertEquals(new Range(0L, 10L), mLoader.getTimeRangeOf(mock(NetworkStats.class)));
|
||||
|
||||
mLoader.addBucket(makeMockBucket(UID_ALL, 123, 456, 0, 10));
|
||||
mLoader.addBucket(makeMockBucket(UID_ALL, 123, 456, 30, 40));
|
||||
mLoader.addBucket(makeMockBucket(UID_ALL, 123, 456, 10, 25));
|
||||
assertEquals(new Range(0L, 40L), mLoader.getTimeRangeOf(mock(NetworkStats.class)));
|
||||
}
|
||||
|
||||
public class NetworkCycleDataTestLoader extends NetworkCycleDataLoader<List<NetworkCycleData>> {
|
||||
private final Queue<NetworkStats.Bucket> mMockedBuckets = new LinkedBlockingQueue<>();
|
||||
|
||||
private NetworkCycleDataTestLoader(Context context) {
|
||||
super(NetworkCycleDataLoader.builder(mContext)
|
||||
.setNetworkTemplate(mock(NetworkTemplate.class)));
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
void recordUsage(long start, long end) {
|
||||
}
|
||||
|
||||
@Override
|
||||
List<NetworkCycleData> getCycleUsage() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void addBucket(NetworkStats.Bucket bucket) {
|
||||
mMockedBuckets.add(bucket);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNextBucket(@NonNull NetworkStats unused) {
|
||||
return !mMockedBuckets.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NetworkStats.Bucket getNextBucket(@NonNull NetworkStats unused) {
|
||||
return mMockedBuckets.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user