Merge "[MS83] Add Cts for NetworkStatsCollection/History builders"
This commit is contained in:
@@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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 android.net.netstats
|
||||||
|
|
||||||
|
import android.net.NetworkIdentitySet
|
||||||
|
import android.net.NetworkStatsCollection
|
||||||
|
import android.net.NetworkStatsHistory
|
||||||
|
import androidx.test.filters.SmallTest
|
||||||
|
import com.android.testutils.ConnectivityModuleTest
|
||||||
|
import com.android.testutils.DevSdkIgnoreRule
|
||||||
|
import com.android.testutils.SC_V2
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.junit.runners.JUnit4
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.fail
|
||||||
|
|
||||||
|
@ConnectivityModuleTest
|
||||||
|
@RunWith(JUnit4::class)
|
||||||
|
@SmallTest
|
||||||
|
class NetworkStatsCollectionTest {
|
||||||
|
@Rule
|
||||||
|
@JvmField
|
||||||
|
val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = SC_V2)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testBuilder() {
|
||||||
|
val ident = NetworkIdentitySet()
|
||||||
|
val key1 = NetworkStatsCollection.Key(ident, /* uid */ 0, /* set */ 0, /* tag */ 0)
|
||||||
|
val key2 = NetworkStatsCollection.Key(ident, /* uid */ 1, /* set */ 0, /* tag */ 0)
|
||||||
|
val bucketDuration = 10L
|
||||||
|
val entry1 = NetworkStatsHistory.Entry(10, 10, 40, 4, 50, 5, 60)
|
||||||
|
val entry2 = NetworkStatsHistory.Entry(30, 10, 3, 41, 7, 1, 0)
|
||||||
|
val history1 = NetworkStatsHistory.Builder(10, 5)
|
||||||
|
.addEntry(entry1)
|
||||||
|
.addEntry(entry2)
|
||||||
|
.build()
|
||||||
|
val history2 = NetworkStatsHistory(10, 5)
|
||||||
|
val actualCollection = NetworkStatsCollection.Builder(bucketDuration)
|
||||||
|
.addEntry(key1, history1)
|
||||||
|
.addEntry(key2, history2)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
// The builder will omit any entry with empty history. Thus, only history1
|
||||||
|
// is expected in the result collection.
|
||||||
|
val actualEntries = actualCollection.entries
|
||||||
|
assertEquals(1, actualEntries.size)
|
||||||
|
val actualHistory = actualEntries[key1] ?: fail("There should be an entry for $key1")
|
||||||
|
assertEquals(history1.entries, actualHistory.entries)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2022 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 android.net.netstats
|
||||||
|
|
||||||
|
import android.net.NetworkStatsHistory
|
||||||
|
import android.text.format.DateUtils
|
||||||
|
import androidx.test.filters.SmallTest
|
||||||
|
import com.android.testutils.ConnectivityModuleTest
|
||||||
|
import com.android.testutils.DevSdkIgnoreRule
|
||||||
|
import com.android.testutils.SC_V2
|
||||||
|
import org.junit.Rule
|
||||||
|
import org.junit.Test
|
||||||
|
import org.junit.runner.RunWith
|
||||||
|
import org.junit.runners.JUnit4
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
@ConnectivityModuleTest
|
||||||
|
@RunWith(JUnit4::class)
|
||||||
|
@SmallTest
|
||||||
|
class NetworkStatsHistoryTest {
|
||||||
|
@Rule
|
||||||
|
@JvmField
|
||||||
|
val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = SC_V2)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testBuilder() {
|
||||||
|
val entry1 = NetworkStatsHistory.Entry(10, 30, 40, 4, 50, 5, 60)
|
||||||
|
val entry2 = NetworkStatsHistory.Entry(30, 15, 3, 41, 7, 1, 0)
|
||||||
|
val entry3 = NetworkStatsHistory.Entry(7, 301, 11, 14, 31, 2, 80)
|
||||||
|
val statsEmpty = NetworkStatsHistory
|
||||||
|
.Builder(DateUtils.HOUR_IN_MILLIS, /* initialCapacity */ 10).build()
|
||||||
|
assertEquals(0, statsEmpty.entries.size)
|
||||||
|
assertEquals(DateUtils.HOUR_IN_MILLIS, statsEmpty.bucketDuration)
|
||||||
|
val statsSingle = NetworkStatsHistory
|
||||||
|
.Builder(DateUtils.HOUR_IN_MILLIS, /* initialCapacity */ 8)
|
||||||
|
.addEntry(entry1)
|
||||||
|
.build()
|
||||||
|
statsSingle.assertEntriesEqual(entry1)
|
||||||
|
assertEquals(DateUtils.HOUR_IN_MILLIS, statsSingle.bucketDuration)
|
||||||
|
val statsMultiple = NetworkStatsHistory
|
||||||
|
.Builder(DateUtils.SECOND_IN_MILLIS, /* initialCapacity */ 0)
|
||||||
|
.addEntry(entry1).addEntry(entry2).addEntry(entry3)
|
||||||
|
.build()
|
||||||
|
assertEquals(DateUtils.SECOND_IN_MILLIS, statsMultiple.bucketDuration)
|
||||||
|
statsMultiple.assertEntriesEqual(entry1, entry2, entry3)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun NetworkStatsHistory.assertEntriesEqual(vararg entries: NetworkStatsHistory.Entry) {
|
||||||
|
assertEquals(entries.size, this.entries.size)
|
||||||
|
entries.forEachIndexed { i, element ->
|
||||||
|
assertEquals(element, this.entries[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,13 +38,11 @@ import static org.junit.Assert.assertNotNull;
|
|||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
import android.net.NetworkStatsCollection.Key;
|
|
||||||
import android.os.Process;
|
import android.os.Process;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
import android.telephony.SubscriptionPlan;
|
import android.telephony.SubscriptionPlan;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.text.format.DateUtils;
|
import android.text.format.DateUtils;
|
||||||
import android.util.ArrayMap;
|
|
||||||
import android.util.RecurrenceRule;
|
import android.util.RecurrenceRule;
|
||||||
|
|
||||||
import androidx.test.InstrumentationRegistry;
|
import androidx.test.InstrumentationRegistry;
|
||||||
@@ -75,7 +73,6 @@ import java.time.ZoneId;
|
|||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link NetworkStatsCollection}.
|
* Tests for {@link NetworkStatsCollection}.
|
||||||
@@ -534,52 +531,6 @@ public class NetworkStatsCollectionTest {
|
|||||||
assertThrows(ArithmeticException.class, () -> multiplySafeByRational(30, 3, 0));
|
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
|
* Copy a {@link Resources#openRawResource(int)} into {@link File} for
|
||||||
* testing purposes.
|
* testing purposes.
|
||||||
|
|||||||
@@ -56,7 +56,6 @@ import java.io.ByteArrayInputStream;
|
|||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@RunWith(DevSdkIgnoreRunner.class)
|
@RunWith(DevSdkIgnoreRunner.class)
|
||||||
@@ -533,40 +532,6 @@ public class NetworkStatsHistoryTest {
|
|||||||
assertEquals(512L + 4096L, stats.getTotalBytes());
|
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(
|
private static void assertIndexBeforeAfter(
|
||||||
NetworkStatsHistory stats, int before, int after, long time) {
|
NetworkStatsHistory stats, int before, int after, long time) {
|
||||||
assertEquals("unexpected before", before, stats.getIndexBefore(time));
|
assertEquals("unexpected before", before, stats.getIndexBefore(time));
|
||||||
|
|||||||
Reference in New Issue
Block a user