Merge "[MS83] Add Cts for NetworkStatsCollection/History builders"

This commit is contained in:
Treehugger Robot
2022-05-02 02:26:55 +00:00
committed by Gerrit Code Review
4 changed files with 134 additions and 84 deletions

View File

@@ -38,13 +38,11 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import android.content.res.Resources;
import android.net.NetworkStatsCollection.Key;
import android.os.Process;
import android.os.UserHandle;
import android.telephony.SubscriptionPlan;
import android.telephony.TelephonyManager;
import android.text.format.DateUtils;
import android.util.ArrayMap;
import android.util.RecurrenceRule;
import androidx.test.InstrumentationRegistry;
@@ -75,7 +73,6 @@ import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* Tests for {@link NetworkStatsCollection}.
@@ -534,52 +531,6 @@ public class NetworkStatsCollectionTest {
assertThrows(ArithmeticException.class, () -> multiplySafeByRational(30, 3, 0));
}
@Test
public void testBuilder() {
final Map<Key, NetworkStatsHistory> expectedEntries = new ArrayMap<>();
final NetworkStats.Entry entry = new NetworkStats.Entry();
final NetworkIdentitySet ident = new NetworkIdentitySet();
final Key key1 = new Key(ident, 0, 0, 0);
final Key key2 = new Key(ident, 1, 0, 0);
final long bucketDuration = 10;
final NetworkStatsHistory.Entry entry1 = new NetworkStatsHistory.Entry(10, 10, 40,
4, 50, 5, 60);
final NetworkStatsHistory.Entry entry2 = new NetworkStatsHistory.Entry(30, 10, 3,
41, 7, 1, 0);
NetworkStatsHistory history1 = new NetworkStatsHistory.Builder(10, 5)
.addEntry(entry1)
.addEntry(entry2)
.build();
NetworkStatsHistory history2 = new NetworkStatsHistory(10, 5);
NetworkStatsCollection actualCollection = new NetworkStatsCollection.Builder(bucketDuration)
.addEntry(key1, history1)
.addEntry(key2, history2)
.build();
// The builder will omit any entry with empty history. Thus, history2
// is not expected in the result collection.
expectedEntries.put(key1, history1);
final Map<Key, NetworkStatsHistory> actualEntries = actualCollection.getEntries();
assertEquals(expectedEntries.size(), actualEntries.size());
for (Key expectedKey : expectedEntries.keySet()) {
final NetworkStatsHistory expectedHistory = expectedEntries.get(expectedKey);
final NetworkStatsHistory actualHistory = actualEntries.get(expectedKey);
assertNotNull(actualHistory);
assertEquals(expectedHistory.getEntries(), actualHistory.getEntries());
actualEntries.remove(expectedKey);
}
assertEquals(0, actualEntries.size());
}
/**
* Copy a {@link Resources#openRawResource(int)} into {@link File} for
* testing purposes.

View File

@@ -56,7 +56,6 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.List;
import java.util.Random;
@RunWith(DevSdkIgnoreRunner.class)
@@ -533,40 +532,6 @@ public class NetworkStatsHistoryTest {
assertEquals(512L + 4096L, stats.getTotalBytes());
}
@Test
public void testBuilder() {
final NetworkStatsHistory.Entry entry1 = new NetworkStatsHistory.Entry(10, 30, 40,
4, 50, 5, 60);
final NetworkStatsHistory.Entry entry2 = new NetworkStatsHistory.Entry(30, 15, 3,
41, 7, 1, 0);
final NetworkStatsHistory.Entry entry3 = new NetworkStatsHistory.Entry(7, 301, 11,
14, 31, 2, 80);
final NetworkStatsHistory statsEmpty = new NetworkStatsHistory
.Builder(HOUR_IN_MILLIS, 10).build();
assertEquals(0, statsEmpty.getEntries().size());
assertEquals(HOUR_IN_MILLIS, statsEmpty.getBucketDuration());
NetworkStatsHistory statsSingle = new NetworkStatsHistory
.Builder(HOUR_IN_MILLIS, 8)
.addEntry(entry1)
.build();
assertEquals(1, statsSingle.getEntries().size());
assertEquals(HOUR_IN_MILLIS, statsSingle.getBucketDuration());
assertEquals(entry1, statsSingle.getEntries().get(0));
NetworkStatsHistory statsMultiple = new NetworkStatsHistory
.Builder(SECOND_IN_MILLIS, 0)
.addEntry(entry1).addEntry(entry2).addEntry(entry3)
.build();
final List<NetworkStatsHistory.Entry> entries = statsMultiple.getEntries();
assertEquals(3, entries.size());
assertEquals(SECOND_IN_MILLIS, statsMultiple.getBucketDuration());
assertEquals(entry1, entries.get(0));
assertEquals(entry2, entries.get(1));
assertEquals(entry3, entries.get(2));
}
private static void assertIndexBeforeAfter(
NetworkStatsHistory stats, int before, int after, long time) {
assertEquals("unexpected before", before, stats.getIndexBefore(time));