Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e694c0af authored by Junyu Lai's avatar Junyu Lai Committed by Gerrit Code Review
Browse files

Merge "Fix NetworkStatsApiTest failed on Q device"

parents 9a20934a 821300b5
Loading
Loading
Loading
Loading
+62 −62
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.net.NetworkStats.TAG_NONE
import android.os.Build
import androidx.test.filters.SmallTest
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
import com.android.testutils.assertFieldCountEquals
import com.android.testutils.assertNetworkStatsEquals
import com.android.testutils.assertParcelingIsLossless
@@ -47,13 +46,32 @@ import kotlin.test.assertEquals
class NetworkStatsApiTest {
    @Rule
    @JvmField
    val ignoreRule = DevSdkIgnoreRule()
    val ignoreRule = DevSdkIgnoreRule(ignoreClassUpTo = Build.VERSION_CODES.Q)

    private val testStatsEmpty = NetworkStats(0L, 0)

    // Note that these variables need to be initialized outside of constructor, initialize
    // here with methods that don't exist in Q devices will result in crash.

    // stats1 and stats2 will have some entries with common keys, which are expected to
    // be merged if performing add on these 2 stats.
    private val testStats1 = NetworkStats(0L, 0)
    private lateinit var testStats1: NetworkStats
    private lateinit var testStats2: NetworkStats

    // This is a result of adding stats1 and stats2, while the merging of common key items is
    // subject to test later, this should not be initialized with for a loop to add stats1
    // and stats2 above.
    private lateinit var testStats3: NetworkStats

    companion object {
        private const val TEST_IFACE = "test0"
        private const val TEST_UID1 = 1001
        private const val TEST_UID2 = 1002
    }

    @Before
    fun setUp() {
        testStats1 = NetworkStats(0L, 0)
                // Entries which only appear in set1.
                .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_YES, 20, 3, 57, 40, 3))
@@ -72,8 +90,9 @@ class NetworkStatsApiTest {
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 40, 1, 0, 0, 8))
                .addEntry(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 3, 1, 6, 2, 0))
        assertEquals(8, testStats1.size())

    private val testStats2 = NetworkStats(0L, 0)
        testStats2 = NetworkStats(0L, 0)
                // Entries which are common for set1 and set2.
                .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, 0x80,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 3, 15, 2, 31, 1))
@@ -86,11 +105,9 @@ class NetworkStatsApiTest {
                // Entry which only appears in set2.
                .addEntry(Entry(IFACE_VT, TEST_UID2, SET_DEFAULT, TAG_NONE,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 2, 3, 7, 8, 0))
        assertEquals(5, testStats2.size())

    // This is a result of adding stats1 and stats2, while the merging of common key items is
    // subject to test later, this should not be initialized with for a loop to add stats1
    // and stats2 above.
    private val testStats3 = NetworkStats(0L, 9)
        testStats3 = NetworkStats(0L, 9)
                // Entries which are unique either in stats1 or stats2.
                .addEntry(Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, TAG_NONE,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 101, 2, 103, 4, 5))
@@ -111,22 +128,10 @@ class NetworkStatsApiTest {
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 51, 3, 3, 4, 15))
                .addEntry(Entry(IFACE_VT, TEST_UID1, SET_DEFAULT, TAG_NONE,
                        METERED_NO, ROAMING_NO, DEFAULT_NETWORK_NO, 7, 4, 8, 3, 0))

    companion object {
        private const val TEST_IFACE = "test0"
        private const val TEST_UID1 = 1001
        private const val TEST_UID2 = 1002
    }

    @Before
    fun setUp() {
        assertEquals(8, testStats1.size())
        assertEquals(5, testStats2.size())
        assertEquals(9, testStats3.size())
    }

    @Test
    @IgnoreUpTo(Build.VERSION_CODES.Q)
    fun testAddEntry() {
        val expectedEntriesInStats2 = arrayOf(
                Entry(TEST_IFACE, TEST_UID1, SET_DEFAULT, 0x80,
@@ -156,7 +161,6 @@ class NetworkStatsApiTest {
    }

    @Test
    @IgnoreUpTo(Build.VERSION_CODES.Q)
    fun testAdd() {
        var stats = NetworkStats(0L, 0)
        assertNetworkStatsEquals(testStatsEmpty, stats)
@@ -168,7 +172,6 @@ class NetworkStatsApiTest {
    }

    @Test
    @IgnoreUpTo(Build.VERSION_CODES.Q)
    fun testParcelUnparcel() {
        assertParcelingIsLossless(testStatsEmpty)
        assertParcelingIsLossless(testStats1)
@@ -177,7 +180,6 @@ class NetworkStatsApiTest {
    }

    @Test
    @IgnoreUpTo(Build.VERSION_CODES.Q)
    fun testDescribeContents() {
        assertEquals(0, testStatsEmpty.describeContents())
        assertEquals(0, testStats1.describeContents())
@@ -186,7 +188,6 @@ class NetworkStatsApiTest {
    }

    @Test
    @IgnoreUpTo(Build.VERSION_CODES.Q)
    fun testSubtract() {
        // STATS3 - STATS2 = STATS1
        assertNetworkStatsEquals(testStats1, testStats3.subtract(testStats2))
@@ -195,7 +196,6 @@ class NetworkStatsApiTest {
    }

    @Test
    @IgnoreUpTo(Build.VERSION_CODES.Q)
    fun testMethodsDontModifyReceiver() {
        listOf(testStatsEmpty, testStats1, testStats2, testStats3).forEach {
            val origStats = it.clone()