Revert "Use addEntries when creating NetworkStats"
This reverts commit 668560d85a.
Reason for revert: Devices failing to boot, see b/359795863
Change-Id: I13dc629e2eb626bff1aed6ff262699c3f577ab9c
This commit is contained in:
parent
668560d85a
commit
c006797ed4
4 changed files with 3 additions and 95 deletions
|
|
@ -296,9 +296,6 @@ java_library {
|
|||
"framework-connectivity-t.stubs.module_lib",
|
||||
"framework-location.stubs.module_lib",
|
||||
],
|
||||
static_libs: [
|
||||
"modules-utils-build",
|
||||
],
|
||||
jarjar_rules: "jarjar-rules-shared.txt",
|
||||
visibility: [
|
||||
"//cts/tests/tests/net",
|
||||
|
|
|
|||
|
|
@ -19,9 +19,6 @@ package com.android.net.module.util;
|
|||
import android.app.usage.NetworkStats;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.modules.utils.build.SdkLevel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Various utilities used for NetworkStats related code.
|
||||
|
|
@ -108,21 +105,12 @@ public class NetworkStatsUtils {
|
|||
*/
|
||||
public static android.net.NetworkStats fromPublicNetworkStats(
|
||||
NetworkStats publiceNetworkStats) {
|
||||
final ArrayList<android.net.NetworkStats.Entry> entries = new ArrayList<>();
|
||||
android.net.NetworkStats stats = new android.net.NetworkStats(0L, 0);
|
||||
while (publiceNetworkStats.hasNextBucket()) {
|
||||
NetworkStats.Bucket bucket = new NetworkStats.Bucket();
|
||||
publiceNetworkStats.getNextBucket(bucket);
|
||||
entries.add(fromBucket(bucket));
|
||||
}
|
||||
android.net.NetworkStats stats = new android.net.NetworkStats(0L, 0);
|
||||
// The new API is only supported on devices running the mainline version of `NetworkStats`.
|
||||
// It should always be used when available for memory efficiency.
|
||||
if (SdkLevel.isAtLeastT()) {
|
||||
stats = stats.addEntries(entries);
|
||||
} else {
|
||||
for (android.net.NetworkStats.Entry entry : entries) {
|
||||
stats = stats.addEntry(entry);
|
||||
}
|
||||
final android.net.NetworkStats.Entry entry = fromBucket(bucket);
|
||||
stats = stats.addEntry(entry);
|
||||
}
|
||||
return stats;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,27 +17,15 @@
|
|||
package com.android.net.module.util
|
||||
|
||||
import android.net.NetworkStats
|
||||
import android.net.NetworkStats.DEFAULT_NETWORK_NO
|
||||
import android.net.NetworkStats.DEFAULT_NETWORK_YES
|
||||
import android.net.NetworkStats.Entry
|
||||
import android.net.NetworkStats.METERED_NO
|
||||
import android.net.NetworkStats.ROAMING_NO
|
||||
import android.net.NetworkStats.ROAMING_YES
|
||||
import android.net.NetworkStats.SET_DEFAULT
|
||||
import android.net.NetworkStats.TAG_NONE
|
||||
import androidx.test.filters.SmallTest
|
||||
import androidx.test.runner.AndroidJUnit4
|
||||
import com.android.testutils.assertEntryEquals
|
||||
import com.android.testutils.assertNetworkStatsEquals
|
||||
import com.android.testutils.makePublicStatsFromAndroidNetStats
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mockito.doReturn
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.Mockito.`when`
|
||||
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
@SmallTest
|
||||
|
|
@ -105,37 +93,6 @@ class NetworkStatsUtilsTest {
|
|||
assertEntryEquals(expectedEntry, entry)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPublicStatsToAndroidNetStats() {
|
||||
val uid1 = 10001
|
||||
val uid2 = 10002
|
||||
val testIface = "wlan0"
|
||||
val testAndroidNetStats = NetworkStats(0L, 3)
|
||||
.addEntry(Entry(testIface, uid1, SET_DEFAULT, TAG_NONE,
|
||||
METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, 20, 3, 57, 40, 3))
|
||||
.addEntry(Entry(
|
||||
testIface, uid2, SET_DEFAULT, TAG_NONE,
|
||||
METERED_NO, ROAMING_YES, DEFAULT_NETWORK_NO, 2, 7, 2, 5, 7))
|
||||
.addEntry(Entry(testIface, uid2, SET_DEFAULT, TAG_NONE,
|
||||
METERED_NO, ROAMING_YES, DEFAULT_NETWORK_NO, 4, 5, 3, 1, 8))
|
||||
val publicStats: android.app.usage.NetworkStats =
|
||||
makePublicStatsFromAndroidNetStats(testAndroidNetStats)
|
||||
val androidNetStats: NetworkStats =
|
||||
NetworkStatsUtils.fromPublicNetworkStats(publicStats)
|
||||
|
||||
// 1. The public `NetworkStats` class does not include interface information.
|
||||
// Interface details must be removed and items with duplicated
|
||||
// keys need to be merged before making any comparisons.
|
||||
// 2. The public `NetworkStats` class lacks an operations field.
|
||||
// Thus, the information will not be preserved during the conversion.
|
||||
val expectedStats = NetworkStats(0L, 2)
|
||||
.addEntry(Entry(null, uid1, SET_DEFAULT, TAG_NONE,
|
||||
METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, 20, 3, 57, 40, 0))
|
||||
.addEntry(Entry(null, uid2, SET_DEFAULT, TAG_NONE,
|
||||
METERED_NO, ROAMING_YES, DEFAULT_NETWORK_NO, 6, 12, 5, 6, 0))
|
||||
assertNetworkStatsEquals(expectedStats, androidNetStats)
|
||||
}
|
||||
|
||||
private fun makeMockBucket(
|
||||
uid: Int,
|
||||
tag: Int,
|
||||
|
|
|
|||
|
|
@ -16,12 +16,7 @@
|
|||
|
||||
package com.android.testutils
|
||||
|
||||
import android.app.usage.NetworkStatsManager
|
||||
import android.content.Context
|
||||
import android.net.INetworkStatsService
|
||||
import android.net.INetworkStatsSession
|
||||
import android.net.NetworkStats
|
||||
import android.net.NetworkTemplate
|
||||
import android.text.TextUtils
|
||||
import com.android.modules.utils.build.SdkLevel
|
||||
import kotlin.test.assertTrue
|
||||
|
|
@ -117,32 +112,3 @@ fun assertNetworkStatsEquals(
|
|||
fun assertParcelingIsLossless(stats: NetworkStats) {
|
||||
assertParcelingIsLossless(stats) { a, b -> orderInsensitiveEquals(a, b) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a {@link android.app.usage.NetworkStats} instance from
|
||||
* a {@link android.net.NetworkStats} instance.
|
||||
*/
|
||||
// It's not possible to directly create a mocked `NetworkStats` instance
|
||||
// because of limitations with `NetworkStats#getNextBucket`.
|
||||
// As a workaround for testing, create a mock by controlling the return values
|
||||
// from the mocked service that provides the `NetworkStats` data.
|
||||
// Notes:
|
||||
// 1. The order of records in the final `NetworkStats` object might change or
|
||||
// some records might be merged if there are items with duplicate keys.
|
||||
// 2. The interface and operations fields will be empty since there is
|
||||
// no such field in the {@link android.app.usage.NetworkStats}.
|
||||
fun makePublicStatsFromAndroidNetStats(androidNetStats: NetworkStats):
|
||||
android.app.usage.NetworkStats {
|
||||
val mockService = Mockito.mock(INetworkStatsService::class.java)
|
||||
val manager = NetworkStatsManager(Mockito.mock(Context::class.java), mockService)
|
||||
val mockStatsSession = Mockito.mock(INetworkStatsSession::class.java)
|
||||
|
||||
Mockito.doReturn(mockStatsSession).`when`(mockService)
|
||||
.openSessionForUsageStats(anyInt(), any())
|
||||
Mockito.doReturn(androidNetStats).`when`(mockStatsSession).getSummaryForAllUid(
|
||||
any(NetworkTemplate::class.java), anyLong(), anyLong(), anyBoolean())
|
||||
return manager.querySummary(
|
||||
Mockito.mock(NetworkTemplate::class.java),
|
||||
Long.MIN_VALUE, Long.MAX_VALUE
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue